diff options
author | Laurent Ghigonis <laurent@p1sec.com> | 2012-11-29 23:14:59 +0100 |
---|---|---|
committer | Laurent Ghigonis <laurent@p1sec.com> | 2012-11-29 23:14:59 +0100 |
commit | 72b085ad21a36aa87121e40b97b283e040db2702 (patch) | |
tree | 2c3b8f1dbb0c4668976c5ee285fb53450d8b5191 /libglouglou/libglouglou.c | |
parent | add security mention when starting server or creating a client : (diff) | |
download | glouglou-72b085ad21a36aa87121e40b97b283e040db2702.tar.xz glouglou-72b085ad21a36aa87121e40b97b283e040db2702.zip |
make gg_packet static when returned by pkt_decode and handle_packet callback,
and mention it in function comments
Diffstat (limited to '')
-rw-r--r-- | libglouglou/libglouglou.c | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c index ce23e1a..da18ae6 100644 --- a/libglouglou/libglouglou.c +++ b/libglouglou/libglouglou.c @@ -33,7 +33,9 @@ int pkt_getsize(struct gg_packet *); /* * start a server - * for security, do not set handle_packet if you don't need it. + * For security reasons, do not set handle_packet if you don't need it. + * Also note that the packet passed by handle_packet is static, so you should + * not modify it or free it. */ struct gg_server * gg_server_start(struct event_base *ev_base, char *ip, int port, @@ -226,7 +228,9 @@ user_send(struct gg_user *usr, void *data, int size) /* * connect to a server - * for security, do not set handle_packet if you don't need it. + * For security reasons, do not set handle_packet if you don't need it. + * Also note that the packet passed by handle_packet is static, so you should + * not modify it or free it. */ struct gg_client * gg_client_connect(struct event_base *ev_base, char *ip, int port, @@ -400,7 +404,8 @@ void cb_cli_timer(evutil_socket_t fd, short what, void *arg) struct gg_packet * pkt_decode(char **buf, int *buf_len) { - struct gg_packet *pkt, *newpkt = NULL; + static struct gg_packet newpkt; + struct gg_packet *pkt; int len; int packet_len; @@ -416,32 +421,31 @@ pkt_decode(char **buf, int *buf_len) if (pkt->type < PACKET_TYPE_MIN || pkt->type > PACKET_TYPE_MAX) goto invalid; - newpkt = xmalloc(sizeof(struct gg_packet)); - newpkt->ver = pkt->ver; - newpkt->type = pkt->type; + newpkt.ver = pkt->ver; + newpkt.type = pkt->type; switch(pkt->type) { case PACKET_NEWCONN: packet_len = PACKET_NEWCONN_SIZE; if (len < packet_len) goto invalid; - newpkt->newconn_id = pkt->newconn_id; - newpkt->newconn_src = ntohl(pkt->newconn_src); - newpkt->newconn_dst = ntohl(pkt->newconn_dst); - newpkt->newconn_proto = pkt->newconn_proto; - newpkt->newconn_size = ntohs(pkt->newconn_size); + newpkt.newconn_id = pkt->newconn_id; + newpkt.newconn_src = ntohl(pkt->newconn_src); + newpkt.newconn_dst = ntohl(pkt->newconn_dst); + newpkt.newconn_proto = pkt->newconn_proto; + newpkt.newconn_size = ntohs(pkt->newconn_size); break; case PACKET_DELCONN: packet_len = PACKET_NEWCONN_SIZE; if (len < packet_len) goto invalid; - newpkt->delconn_id = pkt->delconn_id; + newpkt.delconn_id = pkt->delconn_id; break; case PACKET_DATA: packet_len = PACKET_NEWCONN_SIZE; if (len < packet_len) goto invalid; - newpkt->data_connid = pkt->data_connid; - newpkt->data_size = ntohs(pkt->data_size); + newpkt.data_connid = pkt->data_connid; + newpkt.data_size = ntohs(pkt->data_size); break; case PACKET_NAME: packet_len = PACKET_NEWCONN_SIZE; @@ -451,19 +455,19 @@ pkt_decode(char **buf, int *buf_len) goto invalid; if (strnlen((char *)pkt->name_fqdn, len) != pkt->name_len) goto invalid; - newpkt->name_addr = ntohl(pkt->name_addr); - newpkt->name_len = ntohs(pkt->name_len); - strncpy((char *)newpkt->name_fqdn, (char *)pkt->name_fqdn, + newpkt.name_addr = ntohl(pkt->name_addr); + newpkt.name_len = ntohs(pkt->name_len); + strncpy((char *)newpkt.name_fqdn, (char *)pkt->name_fqdn, pkt->name_len); break; case PACKET_PROC_FORK: packet_len = PACKET_PROC_FORK_SIZE; if (len < packet_len) goto invalid; - newpkt->proc_pid = ntohl(pkt->proc_pid); - newpkt->proc_fork_ppid = ntohl(pkt->proc_fork_ppid); - newpkt->proc_fork_cpid = ntohl(pkt->proc_fork_cpid); - newpkt->proc_fork_tgid = ntohl(pkt->proc_fork_tgid); + newpkt.proc_pid = ntohl(pkt->proc_pid); + newpkt.proc_fork_ppid = ntohl(pkt->proc_fork_ppid); + newpkt.proc_fork_cpid = ntohl(pkt->proc_fork_cpid); + newpkt.proc_fork_tgid = ntohl(pkt->proc_fork_tgid); break; case PACKET_PROC_EXEC: packet_len = PACKET_PROC_EXEC_SIZE; @@ -473,9 +477,9 @@ pkt_decode(char **buf, int *buf_len) goto invalid; if (strnlen((char *)pkt->proc_exec_cmd, len) != pkt->proc_exec_cmdlen) goto invalid; - newpkt->proc_pid = ntohl(pkt->proc_pid); - newpkt->proc_exec_cmdlen = ntohs(pkt->proc_exec_cmdlen); - strncpy((char *)newpkt->proc_exec_cmd, (char *)pkt->proc_exec_cmd, + newpkt.proc_pid = ntohl(pkt->proc_pid); + newpkt.proc_exec_cmdlen = ntohs(pkt->proc_exec_cmdlen); + strncpy((char *)newpkt.proc_exec_cmd, (char *)pkt->proc_exec_cmd, pkt->proc_exec_cmdlen); break; default: @@ -484,19 +488,15 @@ pkt_decode(char **buf, int *buf_len) *buf = *buf + packet_len; *buf_len = len - packet_len; - return newpkt; + return &newpkt; incomplete: printf("pkt_decode: incomplete packet\n"); - if (newpkt) - free(newpkt); *buf_len = len; return NULL; invalid: printf("pkt_decode: invalid packet\n"); - if (newpkt) - free(newpkt); *buf = NULL; *buf_len = 0; return NULL; |