Commit 3a35cf50ce106490470580dcdfc1a85766efe1f5

Authored by Georg Hopp
1 parent da893bf6

IPv6 support

@@ -30,12 +30,12 @@ @@ -30,12 +30,12 @@
30 #include <netdb.h> 30 #include <netdb.h>
31 31
32 #include "trbase.h" 32 #include "trbase.h"
  33 +#include "tr/socket.h"
33 34
34 TR_CLASS(TR_RemoteData) { 35 TR_CLASS(TR_RemoteData) {
35 - unsigned char * data;  
36 - size_t ndata;  
37 - struct sockaddr_in addr; // for now... should be more generic  
38 - socklen_t addrlen; 36 + unsigned char * data;
  37 + size_t ndata;
  38 + TR_Socket socket;
39 }; 39 };
40 40
41 TR_INSTANCE_INIT(TR_RemoteData); 41 TR_INSTANCE_INIT(TR_RemoteData);
@@ -34,7 +34,7 @@ @@ -34,7 +34,7 @@
34 //#include <arpa/inet.h> // for in_port_t 34 //#include <arpa/inet.h> // for in_port_t
35 35
36 #include "trbase.h" 36 #include "trbase.h"
37 -#include "tr/interface/socket.h" 37 +//#include "tr/interface/socket.h"
38 #include "tr/logger.h" 38 #include "tr/logger.h"
39 39
40 typedef enum TR_e_socket_fin { 40 typedef enum TR_e_socket_fin {
@@ -44,18 +44,25 @@ typedef enum TR_e_socket_fin { @@ -44,18 +44,25 @@ typedef enum TR_e_socket_fin {
44 TR_FIN_RDWR = 3 44 TR_FIN_RDWR = 3
45 } TR_SocketFin; 45 } TR_SocketFin;
46 46
  47 +#define TR_MAX_HOST 256
  48 +#define TR_MAX_CNAME 512
  49 +
47 TR_CLASS(TR_Socket) { 50 TR_CLASS(TR_Socket) {
48 - TR_Logger log;  
49 - int flags;  
50 - int type;  
51 - char * host;  
52 - char * cname;  
53 - int port;  
54 - time_t ttl;  
55 - struct sockaddr_in addr; // for now... should be more generic.  
56 - socklen_t addrlen;  
57 - int handle;  
58 - TR_SocketFin fin_state; 51 + TR_Logger log;
  52 + int flags;
  53 + int type;
  54 + char host[TR_MAX_HOST];
  55 + char cname[TR_MAX_CNAME];
  56 + int port;
  57 + time_t ttl;
  58 + union {
  59 + struct sockaddr info;
  60 + struct sockaddr_in in;
  61 + struct sockaddr_in6 in6;
  62 + } addr;
  63 + socklen_t addrlen;
  64 + int handle;
  65 + TR_SocketFin fin_state;
59 }; 66 };
60 67
61 TR_INSTANCE_INIT(TR_Socket); 68 TR_INSTANCE_INIT(TR_Socket);
@@ -67,7 +74,14 @@ TR_INSTANCE_INIT(TR_Socket); @@ -67,7 +74,14 @@ TR_INSTANCE_INIT(TR_Socket);
67 #define TR_socketPort(socket) (((TR_Socket)(socket))->port) 74 #define TR_socketPort(socket) (((TR_Socket)(socket))->port)
68 #define TR_socketCname(socket) (((TR_Socket)(socket))->cname) 75 #define TR_socketCname(socket) (((TR_Socket)(socket))->cname)
69 #define TR_socketTtl(socket) (((TR_Socket)(socket))->ttl) 76 #define TR_socketTtl(socket) (((TR_Socket)(socket))->ttl)
70 -#define TR_socketAddr(socket) (((TR_Socket)(socket))->addr) 77 +
  78 +#define TR_socketAddr(socket) \
  79 + (((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
  80 + ? ((TR_Socket)(socket))->addr.in \
  81 + : ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
  82 + ? ((TR_Socket)(socket))->addr.in6 \
  83 + : NULL)
  84 +
71 #define TR_socketAddrlen(socket) (((TR_Socket)(socket))->addrlen) 85 #define TR_socketAddrlen(socket) (((TR_Socket)(socket))->addrlen)
72 #define TR_socketHandle(socket) (((TR_Socket)(socket))->handle) 86 #define TR_socketHandle(socket) (((TR_Socket)(socket))->handle)
73 #define TR_socketFinState(socket) (((TR_Socket)(socket))->fin_state) 87 #define TR_socketFinState(socket) (((TR_Socket)(socket))->fin_state)
@@ -75,8 +89,32 @@ TR_INSTANCE_INIT(TR_Socket); @@ -75,8 +89,32 @@ TR_INSTANCE_INIT(TR_Socket);
75 #define TR_socketFinWr(socket) ((TR_socketFinState((socket)) & 2) == 2) 89 #define TR_socketFinWr(socket) ((TR_socketFinState((socket)) & 2) == 2)
76 #define TR_socketFinRdWr(socket) ((TR_socketFinState((socket)) & 3) == 3) 90 #define TR_socketFinRdWr(socket) ((TR_socketFinState((socket)) & 3) == 3)
77 91
78 -#define TR_socketGetIp(socket) (TR_socketAddr(socket).sin_addr.s_addr)  
79 -#define TR_socketGetIpStr(socket) (inet_ntoa(TR_socketGetIp(socket))) 92 +#define TR_socketAddrPort(socket) \
  93 + (((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
  94 + ? ((TR_Socket)(socket))->addr.in.sin_port \
  95 + : ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
  96 + ? ((TR_Socket)(socket))->addr.in6.sin6_port \
  97 + : -1)
  98 +
  99 +#define TR_socketAddrIp(socket) \
  100 + (((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
  101 + ? ((TR_Socket)(socket))->addr.in.sin_addr.s_addr \
  102 + : ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
  103 + ? ((TR_Socket)(socket))->addr.in6.sin6_addr.s6_addr \
  104 + : NULL)
  105 +
  106 +#define TR_socketAddrIpStr(socket, buffer, nbuffer) \
  107 + (((TR_Socket)(socket))->addr.info.sa_family == AF_INET \
  108 + ? inet_ntop( \
  109 + ((TR_Socket)(socket))->addr.info.sa_family, \
  110 + &((TR_Socket)(socket))->addr.in.sin_addr.s_addr, \
  111 + buffer, nbuffer) \
  112 + : ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \
  113 + ? inet_ntop( \
  114 + ((TR_Socket)(socket))->addr.info.sa_family, \
  115 + ((TR_Socket)(socket))->addr.in6.sin6_addr.s6_addr, \
  116 + buffer, nbuffer) \
  117 + : NULL)
80 118
81 TR_CLASS(TR_TcpSocket) { 119 TR_CLASS(TR_TcpSocket) {
82 TR_EXTENDS(TR_Socket); 120 TR_EXTENDS(TR_Socket);
@@ -30,18 +30,17 @@ @@ -30,18 +30,17 @@
30 #include <sys/socket.h> 30 #include <sys/socket.h>
31 #include <netdb.h> 31 #include <netdb.h>
32 32
33 -#include "tr/remote_data.h"  
34 #include "trbase.h" 33 #include "trbase.h"
  34 +#include "tr/remote_data.h"
  35 +#include "tr/socket.h"
35 36
36 static 37 static
37 int 38 int
38 remoteDataCtor(void * _this, va_list * params) 39 remoteDataCtor(void * _this, va_list * params)
39 { 40 {
40 - TR_RemoteData this = _this;  
41 - struct sockaddr * addr_ptr = va_arg(*params, struct sockaddr *); 41 + TR_RemoteData this = _this;
42 42
43 - this->addrlen = va_arg(*params, socklen_t);  
44 - memcpy(&(this->addr), addr_ptr, this->addrlen); 43 + this->socket = va_arg(*params, TR_Socket);
45 44
46 this->data = NULL; 45 this->data = NULL;
47 this->ndata = 0; 46 this->ndata = 0;
@@ -35,7 +35,10 @@ TR_remoteDataSetData(TR_RemoteData this, unsigned char * data, size_t size) @@ -35,7 +35,10 @@ TR_remoteDataSetData(TR_RemoteData this, unsigned char * data, size_t size)
35 TR_MEM_FREE(this->data); 35 TR_MEM_FREE(this->data);
36 } 36 }
37 37
38 - this->data = TR_malloc(size); 38 + if (! this->data) {
  39 + this->data = TR_malloc(size);
  40 + }
  41 +
39 this->ndata = size; 42 this->ndata = size;
40 memcpy(this->data, data, size); 43 memcpy(this->data, data, size);
41 } 44 }
@@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
25 #include <unistd.h> 25 #include <unistd.h>
26 #include <stdio.h> 26 #include <stdio.h>
27 #include <fcntl.h> 27 #include <fcntl.h>
  28 +#include <string.h>
28 29
29 #include <sys/types.h> 30 #include <sys/types.h>
30 #include <sys/socket.h> 31 #include <sys/socket.h>
@@ -32,6 +33,7 @@ @@ -32,6 +33,7 @@
32 33
33 #include "tr/remote_data.h" 34 #include "tr/remote_data.h"
34 #include "tr/socket.h" 35 #include "tr/socket.h"
  36 +#include "tr/interface/socket.h"
35 #include "tr/logger.h" 37 #include "tr/logger.h"
36 #include "trbase.h" 38 #include "trbase.h"
37 39
@@ -39,16 +41,21 @@ static @@ -39,16 +41,21 @@ static
39 int 41 int
40 socketCtor(void * _this, va_list * params) 42 socketCtor(void * _this, va_list * params)
41 { 43 {
42 - TR_Socket this = _this; 44 + TR_Socket this = _this;
  45 + char * host;
43 46
44 - this->type = 0; 47 + this->type = AF_UNSPEC;
45 this->handle = -1; 48 this->handle = -1;
46 this->log = va_arg(*params, TR_Logger); 49 this->log = va_arg(*params, TR_Logger);
47 - this->host = TR_strdup(va_arg(*params, char*)); 50 + host = va_arg(*params, char*);
48 this->port = va_arg(*params, int); 51 this->port = va_arg(*params, int);
49 this->flags = va_arg(*params, int); 52 this->flags = va_arg(*params, int);
50 this->fin_state = TR_FIN_RDWR; 53 this->fin_state = TR_FIN_RDWR;
51 54
  55 + if (host) {
  56 + strncpy(this->host, host, TR_MAX_HOST);
  57 + }
  58 +
52 return 0; 59 return 0;
53 } 60 }
54 61
@@ -58,9 +65,6 @@ socketDtor(void * _this) @@ -58,9 +65,6 @@ socketDtor(void * _this)
58 { 65 {
59 TR_Socket this = _this; 66 TR_Socket this = _this;
60 67
61 - TR_MEM_FREE(this->host);  
62 - TR_MEM_FREE(this->cname);  
63 -  
64 if (STDERR_FILENO < this->handle) { 68 if (STDERR_FILENO < this->handle) {
65 TR_socketClose(this); 69 TR_socketClose(this);
66 } 70 }
@@ -73,7 +77,7 @@ socketBind(void * _this) @@ -73,7 +77,7 @@ socketBind(void * _this)
73 TR_Socket this = _this; 77 TR_Socket this = _this;
74 int state; 78 int state;
75 79
76 - state = bind(this->handle, (struct sockaddr *)&(this->addr), this->addrlen); 80 + state = bind(this->handle, &(this->addr.info), this->addrlen);
77 81
78 if (-1 == state) { 82 if (-1 == state) {
79 perror("bind"); 83 perror("bind");
@@ -46,7 +46,7 @@ TR_socketAccept(TR_TcpSocket this) @@ -46,7 +46,7 @@ TR_socketAccept(TR_TcpSocket this)
46 remote->addrlen = ((TR_Socket)this)->addrlen; 46 remote->addrlen = ((TR_Socket)this)->addrlen;
47 remote->handle = accept( 47 remote->handle = accept(
48 TR_socketHandle(this), 48 TR_socketHandle(this),
49 - (struct sockaddr *)&(remote->addr), 49 + &(remote->addr.info),
50 &(remote->addrlen)); 50 &(remote->addrlen));
51 51
52 //flags = fcntl(remote->handle, F_GETFL, 0); 52 //flags = fcntl(remote->handle, F_GETFL, 0);
@@ -56,8 +56,8 @@ TR_socketAccept(TR_TcpSocket this) @@ -56,8 +56,8 @@ TR_socketAccept(TR_TcpSocket this)
56 perror("accept"); 56 perror("accept");
57 TR_delete(remote); 57 TR_delete(remote);
58 } else { 58 } else {
59 - remote->host = TR_strdup(inet_ntoa(remote->addr.sin_addr));  
60 - remote->port = ntohs(remote->addr.sin_port); 59 + TR_socketAddrIpStr(remote, remote->host, TR_MAX_HOST);
  60 + remote->port = TR_socketAddrPort(remote);
61 remote->type = TR_socketType(this); 61 remote->type = TR_socketType(this);
62 remote->flags = TR_socketFlags(this); 62 remote->flags = TR_socketFlags(this);
63 } 63 }
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
27 #include <stdio.h> 27 #include <stdio.h>
28 #include <unistd.h> 28 #include <unistd.h>
29 #include <fcntl.h> 29 #include <fcntl.h>
  30 +#include <errno.h>
30 31
31 #include <sys/types.h> 32 #include <sys/types.h>
32 #include <sys/socket.h> 33 #include <sys/socket.h>
@@ -41,6 +42,7 @@ TR_socketInit(TR_Socket this, TR_socketAction_fptr action) @@ -41,6 +42,7 @@ TR_socketInit(TR_Socket this, TR_socketAction_fptr action)
41 struct addrinfo hint; 42 struct addrinfo hint;
42 struct addrinfo * info, * current_info; 43 struct addrinfo * info, * current_info;
43 char port_str[6]; 44 char port_str[6];
  45 + int code;
44 46
45 hint.ai_socktype = this->type; 47 hint.ai_socktype = this->type;
46 hint.ai_flags = this->flags; 48 hint.ai_flags = this->flags;
@@ -52,8 +54,11 @@ TR_socketInit(TR_Socket this, TR_socketAction_fptr action) @@ -52,8 +54,11 @@ TR_socketInit(TR_Socket this, TR_socketAction_fptr action)
52 hint.ai_next = NULL; 54 hint.ai_next = NULL;
53 55
54 sprintf(port_str, "%u", this->port); 56 sprintf(port_str, "%u", this->port);
55 - if (0 != getaddrinfo(this->host, port_str, &hint, &info)) { 57 + code = getaddrinfo(this->host, port_str, &hint, &info);
  58 + if (0 != code) {
56 // TODO error handling... 59 // TODO error handling...
  60 + puts(gai_strerror(code));
  61 +
57 return FALSE; 62 return FALSE;
58 } 63 }
59 64
@@ -88,7 +93,9 @@ TR_socketInit(TR_Socket this, TR_socketAction_fptr action) @@ -88,7 +93,9 @@ TR_socketInit(TR_Socket this, TR_socketAction_fptr action)
88 //int flags = fcntl(this->handle, F_GETFL, 0); 93 //int flags = fcntl(this->handle, F_GETFL, 0);
89 94
90 //fcntl(this->handle, F_SETFL, flags | O_NONBLOCK); 95 //fcntl(this->handle, F_SETFL, flags | O_NONBLOCK);
91 - this->cname = TR_strdup(current_info->ai_canonname); 96 + if (current_info->ai_canonname) {
  97 + strncpy(this->cname, current_info->ai_canonname, TR_MAX_CNAME);
  98 + }
92 this->fin_state = TR_FIN_NO; 99 this->fin_state = TR_FIN_NO;
93 100
94 //! Make the socket REUSE a TIME_WAIT socket 101 //! Make the socket REUSE a TIME_WAIT socket
@@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
30 #include <netdb.h> 30 #include <netdb.h>
31 31
32 #include "tr/socket.h" 32 #include "tr/socket.h"
  33 +#include "tr/interface/socket.h"
33 #include "tr/logger.h" 34 #include "tr/logger.h"
34 #include "trbase.h" 35 #include "trbase.h"
35 36
@@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
30 #include <netdb.h> 30 #include <netdb.h>
31 31
32 #include "tr/socket.h" 32 #include "tr/socket.h"
  33 +#include "tr/interface/socket.h"
33 #include "tr/logger.h" 34 #include "tr/logger.h"
34 #include "trbase.h" 35 #include "trbase.h"
35 36
@@ -85,8 +86,8 @@ udpSocketSend(TR_Socket this, TR_RemoteData data) @@ -85,8 +86,8 @@ udpSocketSend(TR_Socket this, TR_RemoteData data)
85 data->data, 86 data->data,
86 data->ndata, 87 data->ndata,
87 this->flags, 88 this->flags,
88 - (struct sockaddr *)&(data->addr),  
89 - data->addrlen); 89 + &(data->socket->addr.info),
  90 + data->socket->addrlen);
90 91
91 if (-1 == send) { 92 if (-1 == send) {
92 perror("sendto"); 93 perror("sendto");
Please register or login to post a comment