diff options
author | Laurent Ghigonis <laurent@p1sec.com> | 2012-11-29 04:26:01 +0100 |
---|---|---|
committer | Laurent Ghigonis <laurent@p1sec.com> | 2012-11-29 04:26:01 +0100 |
commit | eb2cd11aac7d9ee2756f1851063a4ba463701cdb (patch) | |
tree | 6701b40a46b6d1e8ceba1dbceedc07ead96a30ae | |
parent | fix errors count in unit tests run (diff) | |
download | glouglou-eb2cd11aac7d9ee2756f1851063a4ba463701cdb.tar.xz glouglou-eb2cd11aac7d9ee2756f1851063a4ba463701cdb.zip |
implement callbacks when server receives a connect, and test it in unit test
-rw-r--r-- | libglouglou/libglouglou.c | 9 | ||||
-rw-r--r-- | libglouglou/libglouglou.h | 33 | ||||
-rw-r--r-- | libglouglou/tests/connect.c | 22 |
3 files changed, 46 insertions, 18 deletions
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c index bffb2e4..27144e4 100644 --- a/libglouglou/libglouglou.c +++ b/libglouglou/libglouglou.c @@ -39,6 +39,8 @@ gg_server_start(struct event_base *ev_base, char *ip, int port, srv = xcalloc(1, sizeof(struct gg_server)); srv->ip = ip; srv->port = port; + srv->handle_conn = handle_conn; + srv->handle_packet = handle_packet; s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) goto err; @@ -91,7 +93,12 @@ gg_server_stop(struct gg_server *srv) void cb_srv_receive(evutil_socket_t fd, short what, void *arg) { + struct gg_server *srv; + + srv = arg; printf("srv: Incoming data !\n"); + if (srv->handle_conn) + srv->handle_conn(srv, 0, NULL); // XXX IN PROGRESS } /* @@ -116,6 +123,8 @@ gg_client_connect(struct event_base *ev_base, char *ip, int port, cli = xmalloc(sizeof(struct gg_client)); cli->ip = ip; cli->port = port; + cli->handle_conn = handle_conn; + cli->handle_packet = handle_packet; s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) goto err; diff --git a/libglouglou/libglouglou.h b/libglouglou/libglouglou.h index 74712d7..2a7e1c7 100644 --- a/libglouglou/libglouglou.h +++ b/libglouglou/libglouglou.h @@ -3,20 +3,6 @@ #define PACKET_VERSION 1 #define DNSNAME_MAX 20 -struct gg_server { - const char *ip; - int port; - struct event *ev; - int sock; -}; - -struct gg_client { - const char *ip; - int port; - struct event *ev; - int sock; -}; - struct gg_packet { u_int8_t ver; u_int8_t type; @@ -64,6 +50,25 @@ struct gg_packet { #define name_fqdn pdat.name.fqdn }; +struct gg_server { + const char *ip; + int port; + struct event *ev; + int sock; + int (*handle_conn)(struct gg_server *, int, struct sockaddr_in *); + int (*handle_packet)(struct gg_server *, struct gg_packet *); +}; + +struct gg_client { + const char *ip; + int port; + struct event *ev; + int sock; + int (*handle_conn)(struct gg_client *, int); + int (*handle_packet)(struct gg_client *, struct gg_packet *); +}; + + struct gg_server *gg_server_start(struct event_base *, char *, int, int (*handle_conn)(struct gg_server *, int, struct sockaddr_in *), int (*handle_packet)(struct gg_server *, struct gg_packet *)); diff --git a/libglouglou/tests/connect.c b/libglouglou/tests/connect.c index 6c33e61..361daf8 100644 --- a/libglouglou/tests/connect.c +++ b/libglouglou/tests/connect.c @@ -2,22 +2,36 @@ #include "../libglouglou.h" +int connect_ok = 0; + +int +handle_conn(struct gg_server *s, int client_id, struct sockaddr_in *addr) +{ + connect_ok = 1; + return 0; +} + int main(void) { struct event_base *ev_base; ev_base = event_base_new(); - if (!gg_server_start(ev_base, "127.0.0.1", 12345, NULL, NULL)) { - printf("gg_server_start returned NULL\n"); + if (!gg_server_start(ev_base, "127.0.0.1", 12345, handle_conn, NULL)) { + printf("error: gg_server_start returned NULL\n"); return 1; } if (!gg_client_connect(ev_base, "127.0.0.1", 12345, NULL, NULL)) { - printf("gg_client_connect returned NULL\n"); + printf("error: gg_client_connect returned NULL\n"); return 1; } - event_dispatch(); + event_base_loop(ev_base, EVLOOP_ONCE); + + if (connect_ok == 0) { + printf("error: connect_ok == 0\n"); + return 1; + } return 0; } |