Revision 5f3adef4 ml/util/udpSocket.c
ml/util/udpSocket.c | ||
---|---|---|
32 | 32 |
* THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY. |
33 | 33 |
*/ |
34 | 34 |
|
35 |
#include <stdio.h> |
|
36 |
#include <stdlib.h> |
|
37 |
#include <unistd.h> |
|
38 |
#include <errno.h> |
|
39 |
#include <string.h> |
|
40 |
#include <sys/time.h> |
|
41 |
|
|
42 |
#include <event2/event.h> |
|
43 |
#ifndef WIN32 |
|
44 | 35 |
/* For sockaddr_in */ |
45 | 36 |
#include <netinet/in.h> |
46 | 37 |
/* For socket functions */ |
47 | 38 |
#include <sys/socket.h> |
48 | 39 |
/* For fcntl */ |
49 | 40 |
#include <fcntl.h> |
50 |
#include <sys/uio.h> |
|
51 |
#include <arpa/inet.h> |
|
52 |
#include <resolv.h> |
|
53 |
#include <sys/socket.h> |
|
54 |
#include <netdb.h> |
|
55 | 41 |
|
42 |
#include <event2/event.h> |
|
43 |
|
|
44 |
#include <stdio.h> |
|
45 |
#include <stdlib.h> |
|
46 |
#include <unistd.h> |
|
47 |
|
|
48 |
#include <sys/socket.h> |
|
56 | 49 |
#ifdef __linux__ |
57 | 50 |
#include <linux/types.h> |
58 | 51 |
#include <linux/errqueue.h> |
... | ... | |
62 | 55 |
#define MSG_ERRQUEUE 0 |
63 | 56 |
#endif |
64 | 57 |
|
65 |
#else |
|
66 |
#include <winsock2.h> |
|
67 |
#endif |
|
68 | 58 |
|
59 |
#include <errno.h> |
|
60 |
#include <string.h> |
|
61 |
#include <netdb.h> |
|
62 |
#include <netinet/in.h> |
|
69 | 63 |
|
64 |
#include <resolv.h> |
|
65 |
#include <sys/time.h> |
|
66 |
#include <sys/uio.h> |
|
67 |
#include <arpa/inet.h> |
|
70 | 68 |
|
69 |
#include <unistd.h> |
|
70 |
#include <stdlib.h> |
|
71 | 71 |
|
72 | 72 |
#include "udpSocket.h" |
73 |
|
|
74 | 73 |
#define LOG_MODULE "[ml] " |
75 | 74 |
#include "ml_log.h" |
76 | 75 |
|
77 |
#include "rateLimiter.h" |
|
78 |
#include "queueManagement.h" |
|
79 |
|
|
80 | 76 |
/* debug varible: set to 1 if you want debug output */ |
81 | 77 |
int verbose = 0; |
82 | 78 |
|
... | ... | |
86 | 82 |
struct sockaddr_in udpsrc, udpdst; |
87 | 83 |
|
88 | 84 |
int returnStatus = 0; |
89 |
debug("X.CreateSock %s %d\n",ipaddr, port); |
|
90 |
|
|
91 |
bzero((char *)&udpsrc, sizeof udpsrc); |
|
92 |
bzero((char *)&udpdst, sizeof udpsrc); |
|
93 | 85 |
|
94 | 86 |
//int udpSocket = 0; |
95 | 87 |
/*libevent2*/ |
... | ... | |
163 | 155 |
|
164 | 156 |
#endif |
165 | 157 |
|
166 |
debug("X.CreateSock\n"); |
|
167 | 158 |
return udpSocket; |
168 | 159 |
|
169 | 160 |
} |
170 | 161 |
|
171 |
#ifndef WIN32 |
|
172 | 162 |
/* Information: read the standard TTL from a socket */ |
173 | 163 |
int getTTL(const int udpSocket,uint8_t *ttl){ |
174 |
#ifdef MAC_OS |
|
175 |
return 0; |
|
176 |
#else |
|
177 |
|
|
178 | 164 |
unsigned int value; |
179 | 165 |
unsigned int size = sizeof(value); |
180 | 166 |
|
... | ... | |
187 | 173 |
if(verbose == 1) debug("TTL is %i\n",value); |
188 | 174 |
|
189 | 175 |
return 1; |
190 |
#endif |
|
191 | 176 |
} |
192 | 177 |
|
193 |
int sendPacketFinal(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr)
|
|
178 |
int sendPacket(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr) |
|
194 | 179 |
{ |
195 | 180 |
int error, ret; |
196 | 181 |
struct msghdr msgh; |
182 |
|
|
183 |
if(outputRateControl(len) != OK) return THROTTLE; |
|
197 | 184 |
|
198 | 185 |
msgh.msg_name = socketaddr; |
199 | 186 |
msgh.msg_namelen = sizeof(struct sockaddr_in); |
... | ... | |
475 | 462 |
#endif |
476 | 463 |
return NULL; |
477 | 464 |
} |
478 |
#else // WINDOWS |
|
479 |
|
|
480 |
int sendPacket(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr) |
|
481 |
{ |
|
482 |
char stack_buffer[1600]; |
|
483 |
char *buf = stack_buffer; |
|
484 |
int i; |
|
485 |
int total_len = 0; |
|
486 |
int ret; |
|
487 |
for(i = 0; i < len; i++) total_len += iov[i].iov_len; |
|
488 |
|
|
489 |
if(outputRateControl(total_len) != OK) return THROTTLE; |
|
490 |
|
|
491 |
if(total_len > sizeof(stack_buffer)) { |
|
492 |
warn("sendPacket total_length %d requires dynamically allocated buffer", total_len); |
|
493 |
buf = malloc(total_len); |
|
494 |
} |
|
495 |
total_len = 0; |
|
496 |
for(i = 0; i < len; i++) { |
|
497 |
memcpy(buf+total_len, iov[i].iov_base, iov[i].iov_len); |
|
498 |
total_len += iov[i].iov_len; |
|
499 |
} |
|
500 |
ret = sendto(udpSocket, buf, total_len, 0, (struct sockaddr *)socketaddr, sizeof(*socketaddr)); |
|
501 |
debug("Sent %d bytes (%d) to %d\n",ret, WSAGetLastError(), udpSocket); |
|
502 |
if(buf != stack_buffer) free(buf); |
|
503 |
return ret == total_len ? OK:FAILURE; |
|
504 |
} |
|
505 | 465 |
|
506 |
void recvPacket(const int udpSocket,char *buffer,int *recvSize,struct sockaddr_in *udpdst,icmp_error_cb icmpcb_value,int *ttl) |
|
507 |
{ |
|
508 |
fprintf(stderr,"X.RECV\n"); |
|
509 |
int salen = sizeof(struct sockaddr_in); |
|
510 |
int ret; |
|
511 |
ret = recvfrom(udpSocket, buffer, *recvSize, 0, (struct sockaddr *)udpdst, &salen); |
|
512 |
if(ret > 0) *recvSize = ret; |
|
513 |
*ttl=10; |
|
514 |
} |
|
515 |
|
|
516 |
int getTTL(const int udpSocket,uint8_t *ttl){ |
|
517 |
return 64; |
|
518 |
} |
|
519 |
|
|
520 |
const char *mlAutodetectIPAddress() { |
|
521 |
return NULL; |
|
522 |
} |
|
523 |
|
|
524 |
|
|
525 |
const char *inet_ntop(int af, const void *src, |
|
526 |
char *dst, size_t size) { |
|
527 |
char *c = inet_ntoa(*(struct in_addr *)src); |
|
528 |
if(strlen(c) >= size) return NULL; |
|
529 |
return strcpy(dst, c); |
|
530 |
} |
|
531 |
int inet_pton(int af, const char * src, void *dst) { |
|
532 |
unsigned long l = inet_addr(src); |
|
533 |
if(l == INADDR_NONE) return 0; |
|
534 |
*(unsigned long *)dst = l; |
|
535 |
return 1; |
|
536 |
} |
|
537 |
#endif |
Also available in: Unified diff