Revision 6575ae37 util/udpSocket.c

View differences:

util/udpSocket.c
32 32
 *     THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
33 33
 */
34 34

  
35
/* For sockaddr_in */
36
#include <netinet/in.h>
37
/* For socket functions */
38
#include <sys/socket.h>
39
/* For fcntl */
40
#include <fcntl.h>
35
#include "../ml_all.h"
41 36

  
42
#include <event2/event.h>
43

  
44
#include <stdio.h>
45
#include <stdlib.h>
46
#include <unistd.h>
47

  
48
#include <sys/socket.h>
49 37
#ifdef __linux__
50 38
#include <linux/types.h>
51 39
#include <linux/errqueue.h>
52 40
#include <linux/if.h>
53 41
#include <ifaddrs.h>
54
#else 
42
#else
55 43
#define MSG_ERRQUEUE 0
56 44
#endif
57 45

  
58 46

  
59
#include <errno.h>
60
#include <string.h>
61
#include <netdb.h>
62
#include <netinet/in.h>
63

  
64
#include <resolv.h>
65
#include <sys/time.h>
66
#include <sys/uio.h>
67
#include <arpa/inet.h>
68

  
69
#include <unistd.h>
70
#include <stdlib.h>
71

  
72
#include "udpSocket.h"
73
#define LOG_MODULE "[ml] "
74
#include "ml_log.h"
75 47

  
76 48
/* debug varible: set to 1 if you want debug output  */
77 49
int verbose = 0;
......
82 54
  struct sockaddr_in udpsrc, udpdst;
83 55

  
84 56
  int returnStatus = 0;
57
  debug("X.CreateSock %s %d\n",ipaddr, port);
58

  
59
  bzero((char *)&udpsrc, sizeof udpsrc);
60
  bzero((char *)&udpdst, sizeof udpsrc);
85 61

  
86 62
  //int udpSocket = 0;
87 63
  /*libevent2*/
......
155 131

  
156 132
#endif
157 133

  
134
  debug("X.CreateSock\n");
158 135
  return udpSocket;
159 136

  
160 137
}
161 138

  
139
#ifndef WIN32
162 140
/* Information: read the standard TTL from a socket  */
163 141
int getTTL(const int udpSocket,uint8_t *ttl){
142
#ifdef MAC_OS
143
	return 0;
144
#else
145

  
164 146
	unsigned int value;
165 147
	unsigned int size = sizeof(value);
166 148

  
......
173 155
	if(verbose == 1) debug("TTL is %i\n",value);
174 156

  
175 157
	return 1;
158
#endif
176 159
}
177 160

  
178
int sendPacket(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr)
161
int sendPacketFinal(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr)
179 162
{
180 163
	int error, ret;
181 164
	struct msghdr msgh;
182
        
183
        if(outputRateControl(len) != OK) return THROTTLE;
184 165

  
185 166
	msgh.msg_name = socketaddr;
186 167
	msgh.msg_namelen = sizeof(struct sockaddr_in);
......
462 443
#endif
463 444
	return NULL;
464 445
}
446
#else  // WINDOWS
447

  
448
int sendPacketFinal(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr)
449
{
450
  char stack_buffer[1600];
451
  char *buf = stack_buffer;
452
  int i;
453
  int total_len = 0;
454
  int ret;
455
  for(i = 0; i < len; i++) total_len += iov[i].iov_len;
456

  
457
  if(outputRateControl(total_len) != OK) return THROTTLE;
458

  
459
  if(total_len > sizeof(stack_buffer)) {
460
     warn("sendPacket total_length %d requires dynamically allocated buffer", total_len);
461
     buf = malloc(total_len);
462
  }
463
  total_len = 0;
464
  for(i = 0; i < len; i++) {
465
     memcpy(buf+total_len, iov[i].iov_base, iov[i].iov_len);
466
     total_len += iov[i].iov_len;
467
  } 
468
  ret = sendto(udpSocket, buf, total_len, 0, (struct sockaddr *)socketaddr, sizeof(*socketaddr));
469
debug("Sent %d bytes (%d) to %d\n",ret, WSAGetLastError(), udpSocket);
470
  if(buf != stack_buffer) free(buf);
471
  return ret == total_len ? OK:FAILURE; 
472
}
465 473

  
474
void recvPacket(const int udpSocket,char *buffer,int *recvSize,struct sockaddr_in *udpdst,icmp_error_cb icmpcb_value,int *ttl)
475
{
476
  debug("recvPacket");
477
  int salen = sizeof(struct sockaddr_in);
478
  int ret;
479
  ret = recvfrom(udpSocket, buffer, *recvSize, 0, (struct sockaddr *)udpdst, &salen);
480
  *recvSize = ret;
481
  if(ret == SOCKET_ERROR) {
482
      int err = WSAGetLastError();
483
      if(err = WSAECONNRESET) {
484
           warn("RECVPACKET detected ICMP Unreachable  for %s:%d", inet_ntoa(udpdst->sin_addr), udpdst->sin_port);
485
      }
486
      else {
487
           warn("RECVPACKET unclassified error %d", err);
488
      }
489
      *recvSize = -1;
490
      return;
491
  }
492
  *ttl=10;
493
}
494

  
495
int getTTL(const int udpSocket,uint8_t *ttl){
496
  return 64;
497
}
498

  
499
const char *mlAutodetectIPAddress() {
500
  return NULL;
501
}
502

  
503

  
504
const char *inet_ntop(int af, const void *src,
505
       char *dst, size_t size) {
506
    char *c = inet_ntoa(*(struct in_addr *)src);
507
    if(strlen(c) >= size) return NULL;
508
    return strcpy(dst, c);
509
}
510
int inet_pton(int af, const char * src, void *dst) {
511
    unsigned long l = inet_addr(src);
512
    if(l == INADDR_NONE) return 0;
513
    *(unsigned long *)dst = l;
514
    return 1;
515
}
516
#endif

Also available in: Unified diff