aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/libglouglou.c
diff options
context:
space:
mode:
authorLaurent Ghigonis <laurent@p1sec.com>2012-11-29 03:30:21 +0100
committerLaurent Ghigonis <laurent@p1sec.com>2012-11-29 03:30:21 +0100
commit8f56c9734ca0073c00ed3b4fda354966e2a2849d (patch)
tree327be09b02c6ce89f044a17b32cc2993bf04eeec /libglouglou/libglouglou.c
parentfix make install (diff)
downloadglouglou-8f56c9734ca0073c00ed3b4fda354966e2a2849d.tar.xz
glouglou-8f56c9734ca0073c00ed3b4fda354966e2a2849d.zip
implement client connection
fix includes
Diffstat (limited to 'libglouglou/libglouglou.c')
-rw-r--r--libglouglou/libglouglou.c77
1 files changed, 64 insertions, 13 deletions
diff --git a/libglouglou/libglouglou.c b/libglouglou/libglouglou.c
index 5c590ba..7b3db8f 100644
--- a/libglouglou/libglouglou.c
+++ b/libglouglou/libglouglou.c
@@ -5,6 +5,7 @@
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
+#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <netinet/in.h>
@@ -13,7 +14,8 @@
#include "libglouglou.h"
/* Server - private */
-void cb_receive(evutil_socket_t fd, short what, void *arg);
+void cb_srv_receive(evutil_socket_t fd, short what, void *arg);
+void cb_cli_receive(evutil_socket_t fd, short what, void *arg);
/*
* Server
@@ -27,30 +29,34 @@ gg_server_start(struct event_base *ev_base, char *ip, int port,
int (*handle_conn)(struct gg_server *s, int client_id, struct sockaddr_in *addr),
int (*handle_packet)(struct gg_server *s, struct gg_packet *p))
{
- struct sockaddr_in sock_addr;
struct gg_server *srv;
+ struct sockaddr_in sock_addr;
struct event *ev;
int s;
int sock_on = 1;
event_init(); /* XXX needed ? */
srv = xcalloc(1, sizeof(struct gg_server));
+ srv->ip = ip;
+ srv->port = port;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
goto err;
srv->sock = s;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
&sock_on, sizeof(sock_on));
- memset(&sock_addr, 0, sizeof(sock_addr));
+ fd_nonblock(s);
+
+ bzero(&sock_addr, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
- sock_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* XXX use ip */
+ sock_addr.sin_addr.s_addr = inet_addr(ip);
sock_addr.sin_port = htons(port);
+
if (bind(s, (struct sockaddr *)&sock_addr,
sizeof(sock_addr)) != 0)
goto err;
- fd_nonblock(s);
- ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_receive, srv);
+ ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_srv_receive, srv);
event_add(ev, NULL);
srv->ev = ev;
@@ -85,7 +91,7 @@ gg_server_stop(struct gg_server *srv)
void cb_receive(evutil_socket_t fd, short what, void *arg)
{
- printf("Incoming data !\n");
+ printf("srv: Incoming data !\n");
}
/*
@@ -96,15 +102,47 @@ void cb_receive(evutil_socket_t fd, short what, void *arg)
* connect to a server
*/
struct gg_client *
-gg_client_connect(char *ip, int port,
+gg_client_connect(struct event_base *ev_base, char *ip, int port,
int (*handle_conn)(struct gg_client *c, int status),
int (*handle_packet)(struct gg_client *c, struct gg_packet *p))
{
- struct gg_client *c;
+ struct gg_client *cli;
+ struct sockaddr_in sock_addr;
+ struct event *ev;
+ int s;
+ int sock_on = 1;
- c = xmalloc(sizeof(struct gg_client));
+ event_init(); /* XXX needed ? */
+ cli = xmalloc(sizeof(struct gg_client));
+ cli->ip = ip;
+ cli->port = port;
+ s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0)
+ goto err;
+ cli->sock = s;
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
+ &sock_on, sizeof(sock_on));
+ fd_nonblock(s);
+
+ bzero(&sock_addr, sizeof(sock_addr));
+ sock_addr.sin_family = AF_INET;
+ sock_addr.sin_addr.s_addr=inet_addr(ip);
+ sock_addr.sin_port=htons(port);
+
+ ev = event_new(ev_base, s, EV_READ|EV_PERSIST, cb_cli_receive, cli);
+ event_add(ev, NULL);
+ cli->ev = ev;
- return c;
+ sendto(s, "", strlen(""), 0,
+ (struct sockaddr *)&sock_addr, sizeof(struct sockaddr));
+
+ return cli;
+
+err:
+ printf("libglouglou: gg_client_connect: Error ! %s",
+ strerror(errno));
+ gg_client_disconnect(cli);
+ return NULL;
}
int
@@ -114,9 +152,22 @@ gg_client_send(struct gg_client *c, struct gg_packet *p)
}
void
-gg_client_disconnect(struct gg_client *c)
+gg_client_disconnect(struct gg_client *cli)
+{
+ if (cli->ev)
+ event_del(cli->ev);
+ if (cli->sock)
+ close(cli->sock);
+ free(cli);
+}
+
+/*
+ * Client - private
+ */
+
+void cb_cli_receive(evutil_socket_t fd, short what, void *arg)
{
- free(c);
+ printf("cli: Incoming data !\n");
}
/*