Module: sip-router Branch: master Commit: 3d673225829b6dc87281816a7a074417671f395e URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=3d673225...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Sun Mar 30 14:19:04 2014 +0200
evapi: store src ip and port in client connection struct
---
modules/evapi/evapi_dispatch.c | 31 ++++++++++++++++++++++++++++++- 1 files changed, 30 insertions(+), 1 deletions(-)
diff --git a/modules/evapi/evapi_dispatch.c b/modules/evapi/evapi_dispatch.c index 05dd6e4..59297e2 100644 --- a/modules/evapi/evapi_dispatch.c +++ b/modules/evapi/evapi_dispatch.c @@ -27,6 +27,8 @@
#include <sys/socket.h> #include <sys/types.h> +#include <netinet/in.h> +#include <arpa/inet.h> #include <fcntl.h>
#include <ev.h> @@ -37,9 +39,13 @@
static int _evapi_notify_sockets[2];
+#define EVAPI_IPADDR_SIZE 64 typedef struct _evapi_client { int connected; int sock; + unsigned short af; + unsigned short src_port; + char src_addr[EVAPI_IPADDR_SIZE]; } evapi_client_t;
#define EVAPI_MAX_CLIENTS 8 @@ -146,7 +152,7 @@ void evapi_recv_client(struct ev_loop *loop, struct ev_io *watcher, int revents) */ void evapi_accept_client(struct ev_loop *loop, struct ev_io *watcher, int revents) { - struct sockaddr_in caddr; + struct sockaddr caddr; socklen_t clen = sizeof(caddr); int csock; struct ev_io *evapi_client; @@ -172,8 +178,28 @@ void evapi_accept_client(struct ev_loop *loop, struct ev_io *watcher, int revent } for(i=0; i<EVAPI_MAX_CLIENTS; i++) { if(_evapi_clients[i].connected==0) { + if (caddr.sa_family == AF_INET) { + _evapi_clients[i].src_port = ntohs(((struct sockaddr_in*)&caddr)->sin_port); + if(inet_ntop(AF_INET, &((struct sockaddr_in*)&caddr)->sin_addr, + _evapi_clients[i].src_addr, + EVAPI_IPADDR_SIZE)==NULL) { + LM_ERR("cannot convert ipv4 address\n"); + close(csock); + return; + } + } else { + _evapi_clients[i].src_port = ntohs(((struct sockaddr_in6*)&caddr)->sin6_port); + if(inet_ntop(AF_INET6, &((struct sockaddr_in6*)&caddr)->sin6_addr, + _evapi_clients[i].src_addr, + EVAPI_IPADDR_SIZE)==NULL) { + LM_ERR("cannot convert ipv6 address\n"); + close(csock); + return; + } + } _evapi_clients[i].connected = 1; _evapi_clients[i].sock = csock; + _evapi_clients[i].af = caddr.sa_family; break; } } @@ -183,6 +209,9 @@ void evapi_accept_client(struct ev_loop *loop, struct ev_io *watcher, int revent return; }
+ LM_DBG("new connection - pos[%d] from: [%s:%d]\n", i, + _evapi_clients[i].src_addr, _evapi_clients[i].src_port); + /* start watcher to read messages from whatchers */ ev_io_init(evapi_client, evapi_recv_client, csock, EV_READ); ev_io_start(loop, evapi_client);