diff options
-rw-r--r-- | libglouglou/libglouglou.c | 39 | ||||
-rw-r--r-- | libglouglou/libglouglou.h | 4 | ||||
-rw-r--r-- | libglouglou/tests/sendrecv.c | 4 |
3 files changed, 41 insertions, 6 deletions
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c index a3eb271..7ba1b95 100644 --- a/libglouglou/libglouglou.c +++ b/libglouglou/libglouglou.c @@ -13,9 +13,12 @@ #include "libglouglou.h" +// XXX switch default + 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 *); @@ -76,10 +79,12 @@ err: } int -gg_server_send(struct gg_server *srv, struct gg_packet *pkt, int size, struct gg_user *usr) +gg_server_send(struct gg_server *srv, struct gg_packet *pkt, struct gg_user *usr) { struct gg_user *u; + int size; + size = pkt_getsize(pkt); if (usr) { if (sendto(srv->sock, pkt, size, 0, (struct sockaddr *)&usr->addr, sizeof(struct sockaddr_in)) == -1) { @@ -221,6 +226,33 @@ invalid: 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; @@ -321,8 +353,11 @@ err: } int -gg_client_send(struct gg_client *cli, struct gg_packet *pkt, int size) +gg_client_send(struct gg_client *cli, struct gg_packet *pkt) { + int size; + + size = pkt_getsize(pkt); if (sendto(cli->sock, pkt, size, 0, (struct sockaddr *)&cli->addr, sizeof(struct sockaddr_in)) == -1) { printf("gg_client_send failed: %s", strerror(errno)); diff --git a/libglouglou/libglouglou.h b/libglouglou/libglouglou.h index dd972d7..8f3ec7c 100644 --- a/libglouglou/libglouglou.h +++ b/libglouglou/libglouglou.h @@ -100,14 +100,14 @@ struct gg_server *gg_server_start(struct event_base *, char *, int, int (*handle_conn)(struct gg_server *, struct gg_user *), int (*handle_packet)(struct gg_server *, struct gg_user *, struct gg_packet *), void *); -int gg_server_send(struct gg_server *, struct gg_packet *, int, struct gg_user *); +int gg_server_send(struct gg_server *, struct gg_packet *, struct gg_user *); void gg_server_stop(struct gg_server *); struct gg_client *gg_client_connect(struct event_base *, char *, int, int (*handle_conn)(struct gg_client *), int (*handle_packet)(struct gg_client *, struct gg_packet *), void *); -int gg_client_send(struct gg_client *, struct gg_packet *, int); +int gg_client_send(struct gg_client *, struct gg_packet *); void gg_client_disconnect(struct gg_client *); void *xmalloc(size_t); diff --git a/libglouglou/tests/sendrecv.c b/libglouglou/tests/sendrecv.c index ea8fa0e..fbf4aed 100644 --- a/libglouglou/tests/sendrecv.c +++ b/libglouglou/tests/sendrecv.c @@ -46,10 +46,10 @@ main(void) pkt.ver = PACKET_VERSION; pkt.type = PACKET_NEWCONN; - gg_server_send(srv, &pkt, PACKET_NEWCONN_SIZE, NULL); + gg_server_send(srv, &pkt, NULL); event_base_loop(ev_base, EVLOOP_ONCE); - gg_client_send(cli, &pkt, PACKET_NEWCONN_SIZE); + gg_client_send(cli, &pkt); event_base_loop(ev_base, EVLOOP_ONCE); if (srv_recv_ok == 0) |