Revision 4c2e6802
util/udpSocket.c | ||
---|---|---|
48 | 48 |
#include <sys/socket.h> |
49 | 49 |
#include <linux/types.h> |
50 | 50 |
#include <linux/errqueue.h> |
51 |
#ifdef __linux__ |
|
52 |
#include <linux/if.h> |
|
53 |
#include <ifaddrs.h> |
|
54 |
#endif |
|
51 | 55 |
|
52 | 56 |
#include <errno.h> |
53 | 57 |
#include <string.h> |
... | ... | |
89 | 93 |
//This is debug code that checks if the socket was created |
90 | 94 |
if (udpSocket == -1) |
91 | 95 |
{ |
92 |
fprintf(stderr, "Could not create a socket!\n");
|
|
96 |
fatal("Could not create an UDP socket!");
|
|
93 | 97 |
exit(1); |
94 | 98 |
} |
95 |
else |
|
96 |
{ |
|
97 |
if(verbose == 1){ |
|
98 |
printf("Socket created.\n" ); |
|
99 |
} |
|
100 |
} |
|
101 | 99 |
|
102 | 100 |
/* sets the standard server address and port */ |
103 | 101 |
udpsrc.sin_family = AF_INET; |
... | ... | |
122 | 120 |
returnStatus = bind(udpSocket,(struct sockaddr *)&udpsrc,sizeof(udpsrc)); |
123 | 121 |
|
124 | 122 |
/* This is debug code */ |
125 |
if (returnStatus == 0) { |
|
126 |
if(verbose == 1){ |
|
127 |
fprintf(stderr, "SocketID bind completed!\n"); |
|
128 |
} |
|
129 |
} |
|
130 |
else{ |
|
131 |
fprintf(stderr, "Could not bind socketID to address!\n"); |
|
132 |
close(udpSocket); |
|
133 |
exit(1); |
|
123 |
if (returnStatus) { |
|
124 |
fatal("Could not bind socketID to address!"); |
|
134 | 125 |
} |
135 | 126 |
#ifdef IP_MTU_DISCOVER |
136 | 127 |
|
... | ... | |
142 | 133 |
* This sets the DON'T FRAGMENT bit on the IP HEADER |
143 | 134 |
*/ |
144 | 135 |
if(setsockopt(udpSocket,IPPROTO_IP,IP_MTU_DISCOVER,&pmtuopt ,size) == -1){ |
145 |
|
|
146 |
printf("setsockopt: set IP_DONTFRAG did not work. ERRNO %d \n ",errno); |
|
136 |
error("setsockopt: set IP_DONTFRAG did not work. ERRNO %d",errno); |
|
147 | 137 |
|
148 | 138 |
} |
149 | 139 |
|
150 | 140 |
/* This option writes the IP_TTL field into ancillary data */ |
151 | 141 |
if(setsockopt(udpSocket,IPPROTO_IP, IP_RECVTTL, &yes,size) < 0) |
152 | 142 |
{ |
153 |
printf("setsockopt: cannot set RECV_TTL. ERRNO %d \n",errno);
|
|
143 |
error("setsockopt: cannot set RECV_TTL. ERRNO %d",errno);
|
|
154 | 144 |
} |
155 | 145 |
|
156 | 146 |
/* This option writes received internal and external(icmp) error messages into ancillary data */ |
157 | 147 |
|
158 |
if(setsockopt(udpSocket,IPPROTO_IP, IP_RECVERR, &yes,size) < 0) |
|
159 |
{ |
|
160 |
printf("setsockopt: cannot set RECV_ERROR. ERRNO %d \n",errno); |
|
148 |
if(setsockopt(udpSocket,IPPROTO_IP, IP_RECVERR, &yes,size) < 0) { |
|
149 |
error("setsockopt: cannot set RECV_ERROR. ERRNO %d",errno); |
|
161 | 150 |
} |
162 | 151 |
|
163 | 152 |
#endif |
... | ... | |
172 | 161 |
unsigned int size = sizeof(value); |
173 | 162 |
|
174 | 163 |
if(getsockopt(udpSocket,SOL_IP,IP_TTL,&value,&size) == -1){ |
175 |
printf("get TTL did not work \n ");
|
|
164 |
error("get TTL did not work");
|
|
176 | 165 |
return 0; |
177 | 166 |
} |
178 | 167 |
|
179 | 168 |
*ttl = value; |
180 |
if(verbose == 1) |
|
181 |
printf("TTL is %i \n",value); |
|
169 |
if(verbose == 1) debug("TTL is %i",value); |
|
182 | 170 |
|
183 | 171 |
return 1; |
184 | 172 |
} |
... | ... | |
224 | 212 |
int handleSocketError(const int udpSocket,const int iofunc,char *buf,int *bufsize,struct sockaddr_in *addr,icmp_error_cb icmpcb_value,int *ttl){ |
225 | 213 |
|
226 | 214 |
|
227 |
if(verbose == 1) |
|
228 |
printf("handle Socket error is called \n"); |
|
215 |
if(verbose == 1) debug("handle Socket error is called"); |
|
229 | 216 |
|
230 | 217 |
/* variables */ |
231 | 218 |
struct msghdr msgh; |
... | ... | |
409 | 396 |
return 0; |
410 | 397 |
|
411 | 398 |
} |
399 |
|
|
400 |
const char *autodetect_ipaddress() { |
|
401 |
#ifndef __linux__ |
|
402 |
return NULL; |
|
403 |
#endif |
|
404 |
|
|
405 |
static char addr[128] = ""; |
|
406 |
|
|
407 |
FILE *r = fopen("/proc/net/route", "r"); |
|
408 |
if (!r) return NULL; |
|
409 |
|
|
410 |
char iface[IFNAMSIZ] = ""; |
|
411 |
char line[128] = "x"; |
|
412 |
while (1) { |
|
413 |
char dst[32]; |
|
414 |
char ifc[IFNAMSIZ]; |
|
415 |
|
|
416 |
fgets(line, 127, r); |
|
417 |
if (feof(r)) break; |
|
418 |
if ((sscanf(line, "%s\t%s", iface, dst) == 2) && !strcpy(dst, "00000000")) { |
|
419 |
strcpy(iface, ifc); |
|
420 |
break; |
|
421 |
} |
|
422 |
} |
|
423 |
if (iface[0] == 0) return NULL; |
|
424 |
|
|
425 |
struct ifaddrs *ifAddrStruct=NULL; |
|
426 |
if (getifaddrs(&ifAddrStruct)) return NULL; |
|
427 |
|
|
428 |
while (ifAddrStruct) { |
|
429 |
if (ifAddrStruct->ifa_addr && ifAddrStruct->ifa_addr->sa_family == AF_INET && |
|
430 |
ifAddrStruct->ifa_name && !strcmp(ifAddrStruct->ifa_name, iface)) { |
|
431 |
void *tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; |
|
432 |
return inet_ntop(AF_INET, tmpAddrPtr, addr, 127); |
|
433 |
} |
|
434 |
ifAddrStruct=ifAddrStruct->ifa_next; |
|
435 |
} |
|
436 |
|
|
437 |
return NULL; |
|
438 |
} |
|
439 |
|
Also available in: Unified diff