aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/libglouglou.c
diff options
context:
space:
mode:
authorLaurent Ghigonis <laurent@p1sec.com>2012-11-29 11:41:38 +0100
committerLaurent Ghigonis <laurent@p1sec.com>2012-11-29 11:41:38 +0100
commit89f78318058590a2ccda22ed447a9d134857ff14 (patch)
tree1c1bb040a7f55326c8d613e46999e981cc6dc177 /libglouglou/libglouglou.c
parentadd very basic glougloud (diff)
downloadglouglou-89f78318058590a2ccda22ed447a9d134857ff14.tar.xz
glouglou-89f78318058590a2ccda22ed447a9d134857ff14.zip
auto calculate the packet size in gg_*_send()
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));