aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Ghigonis <laurent@p1sec.com>2012-11-29 14:38:01 +0100
committerLaurent Ghigonis <laurent@p1sec.com>2012-11-29 14:38:01 +0100
commit905038d0329af866a1f29ffaa0c765c931036a71 (patch)
treea51ff9952e4c4287e58a8a19d515ebe4ec032323
parentcorrectly handle signals (diff)
downloadglouglou-905038d0329af866a1f29ffaa0c765c931036a71.tar.xz
glouglou-905038d0329af866a1f29ffaa0c765c931036a71.zip
move around functions so they are in the right order.
no functionnal change
-rw-r--r--libglouglou/libglouglou.c226
1 files changed, 115 insertions, 111 deletions
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c
index 7ba1b95..a564b23 100644
--- a/libglouglou/libglouglou.c
+++ b/libglouglou/libglouglou.c
@@ -17,10 +17,10 @@
struct gg_user *user_add(struct gg_server *, struct sockaddr_in *);
struct gg_user *user_find(struct gg_server *, struct sockaddr_in *);
-struct gg_packet *pkt_decode(char **buf, int *buf_len);
-int pkt_getsize(struct gg_packet *);
void cb_srv_receive(evutil_socket_t, short, void *);
void cb_cli_receive(evutil_socket_t, short, void *);
+struct gg_packet *pkt_decode(char **buf, int *buf_len);
+int pkt_getsize(struct gg_packet *);
/*
* Server
@@ -144,115 +144,6 @@ user_add(struct gg_server *srv, struct sockaddr_in *remote)
return usr;
}
-struct gg_packet *
-pkt_decode(char **buf, int *buf_len)
-{
- struct gg_packet *pkt, *newpkt = NULL;
- int len;
- int packet_len;
-
- len = *buf_len;
-
- if (len < PACKET_HEADER_SIZE)
- goto incomplete;
- if (len > PACKET_BUFFER_SIZE)
- goto invalid;
- pkt = (struct gg_packet *)*buf;
- if (pkt->ver != PACKET_VERSION)
- goto invalid;
- 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;
- 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);
- break;
- case PACKET_DELCONN:
- packet_len = PACKET_NEWCONN_SIZE;
- if (len < packet_len)
- goto invalid;
- 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);
- break;
- case PACKET_NAME:
- packet_len = PACKET_NEWCONN_SIZE;
- if (len < packet_len)
- goto invalid;
- if (len < packet_len + pkt->name_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 = pkt->name_len;
- strncpy((char *)newpkt->name_fqdn, (char *)pkt->name_fqdn,
- pkt->name_len);
- break;
- goto invalid;
- }
-
- *buf = *buf + packet_len;
- *buf_len = len - packet_len;
- 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;
-}
-
-/* get the size of a packet before sending it on the wire
- * assumes that the packet is trusted (we can do strlen on pkt->name_fqdn...)
- */
-int
-pkt_getsize(struct gg_packet *pkt)
-{
- int size;
-
- switch(pkt->type) {
- case PACKET_NEWCONN:
- size = PACKET_NEWCONN_SIZE;
- break;
- case PACKET_DELCONN:
- size = PACKET_DELCONN_SIZE;
- break;
- case PACKET_DATA:
- size = PACKET_DATA_SIZE;
- break;
- case PACKET_NAME:
- size = PACKET_NAME_SIZE + strnlen((char *)pkt->name_fqdn, DNSNAME_MAX);
- break;
- default:
- size = 0;
- }
- return size;
-}
-
void cb_srv_receive(evutil_socket_t fd, short what, void *arg)
{
struct gg_server *srv;
@@ -423,6 +314,119 @@ void cb_cli_receive(evutil_socket_t fd, short what, void *arg)
}
/*
+ * Packets - private
+ */
+
+struct gg_packet *
+pkt_decode(char **buf, int *buf_len)
+{
+ struct gg_packet *pkt, *newpkt = NULL;
+ int len;
+ int packet_len;
+
+ len = *buf_len;
+
+ if (len < PACKET_HEADER_SIZE)
+ goto incomplete;
+ if (len > PACKET_BUFFER_SIZE)
+ goto invalid;
+ pkt = (struct gg_packet *)*buf;
+ if (pkt->ver != PACKET_VERSION)
+ goto invalid;
+ 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;
+ 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);
+ break;
+ case PACKET_DELCONN:
+ packet_len = PACKET_NEWCONN_SIZE;
+ if (len < packet_len)
+ goto invalid;
+ 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);
+ break;
+ case PACKET_NAME:
+ packet_len = PACKET_NEWCONN_SIZE;
+ if (len < packet_len)
+ goto invalid;
+ if (len < packet_len + pkt->name_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 = pkt->name_len;
+ strncpy((char *)newpkt->name_fqdn, (char *)pkt->name_fqdn,
+ pkt->name_len);
+ break;
+ goto invalid;
+ }
+
+ *buf = *buf + packet_len;
+ *buf_len = len - packet_len;
+ 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;
+}
+
+/* get the size of a packet before sending it on the wire
+ * assumes that the packet is trusted (we can do strlen on pkt->name_fqdn...)
+ */
+int
+pkt_getsize(struct gg_packet *pkt)
+{
+ int size;
+
+ switch(pkt->type) {
+ case PACKET_NEWCONN:
+ size = PACKET_NEWCONN_SIZE;
+ break;
+ case PACKET_DELCONN:
+ size = PACKET_DELCONN_SIZE;
+ break;
+ case PACKET_DATA:
+ size = PACKET_DATA_SIZE;
+ break;
+ case PACKET_NAME:
+ size = PACKET_NAME_SIZE + strnlen((char *)pkt->name_fqdn, DNSNAME_MAX);
+ break;
+ default:
+ size = 0;
+ }
+ return size;
+}
+
+/*
* Utils
*/