aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/libglouglou.c
diff options
context:
space:
mode:
Diffstat (limited to 'libglouglou/libglouglou.c')
-rw-r--r--libglouglou/libglouglou.c39
1 files changed, 37 insertions, 2 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));