00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include <stdlib.h>
00071 #include <string.h>
00072 #include <stdio.h>
00073 #if !defined(WIN32) && !defined(_WIN32_WCE)
00074 #include <sys/ioctl.h>
00075 #include <sys/socket.h>
00076 #include <sys/time.h>
00077 #include <sys/types.h>
00078 #include <arpa/inet.h>
00079 #include <fcntl.h>
00080 #include <netdb.h>
00081 #include <netinet/in.h>
00082 #include <arpa/nameser.h>
00083 #include <resolv.h>
00084 #include <net/if.h>
00085 #endif
00086 #include <assert.h>
00087
00088 #include <time.h>
00089
00090 #if defined(WIN32) || defined(_WIN32_WCE)
00091
00092 #include <winsock2.h>
00093 #include <stdlib.h>
00094
00095
00096 #else
00097
00098 #include <arpa/inet.h>
00099 #include <stdlib.h>
00100 #include <unistd.h>
00101 #include <fcntl.h>
00102 #include <netinet/in.h>
00103 #include <sys/socket.h>
00104 #include <sys/types.h>
00105 #include <netdb.h>
00106 #include <string.h>
00107 #include <unistd.h>
00108
00109 #endif
00110
00111 #include <string.h>
00112
00113 #include "ortp/stun_udp.h"
00114
00115 #if !defined(WIN32) && !defined(_WIN32_WCE)
00116 int getErrno() { return errno; }
00117 #else
00118 int getErrno() { return WSAGetLastError(); }
00119 #endif
00120
00121 Socket
00122 openPort( unsigned short port, unsigned int interfaceIp, bool_t verbose )
00123 {
00124 struct sockaddr_in addr;
00125 Socket fd;
00126
00127 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
00128 if ( fd == INVALID_SOCKET )
00129 {
00130 printf("Could not create a UDP socket:\n");
00131 return INVALID_SOCKET;
00132 }
00133
00134 memset((char*) &(addr),0, sizeof((addr)));
00135 addr.sin_family = AF_INET;
00136 addr.sin_addr.s_addr = htonl(INADDR_ANY);
00137 addr.sin_port = htons(port);
00138
00139 if ( (interfaceIp != 0) &&
00140 ( interfaceIp != 0x100007f ) )
00141 {
00142 addr.sin_addr.s_addr = htonl(interfaceIp);
00143 if (verbose )
00144 {
00145 printf("Binding to interface 0x%lu\n",(unsigned long) htonl(interfaceIp));
00146 }
00147 }
00148
00149 if ( bind( fd,(struct sockaddr*)&addr, sizeof(addr)) != 0 )
00150 {
00151 int e = getErrno();
00152
00153 switch (e)
00154 {
00155 case 0:
00156 {
00157 printf("Could not bind socket\n");;
00158 return INVALID_SOCKET;
00159 }
00160 case EADDRINUSE:
00161 {
00162 printf("Port %i for receiving UDP is in use\n", port);
00163 return INVALID_SOCKET;
00164 }
00165 break;
00166 case EADDRNOTAVAIL:
00167 {
00168 if ( verbose )
00169 {
00170 printf("Cannot assign requested address\n");
00171 }
00172 return INVALID_SOCKET;
00173 }
00174 break;
00175 default:
00176 {
00177 #if !defined(_WIN32_WCE)
00178 printf("Could not bind UDP receive port Error=%i %s\n",
00179 e, strerror(e));
00180 #else
00181 printf("Could not bind UDP receive port Error=%i\n",
00182 e);
00183 #endif
00184 return INVALID_SOCKET;
00185 }
00186 break;
00187 }
00188 }
00189 if ( verbose )
00190 {
00191 printf("Opened port %i with fd %i\n", port, fd);
00192 }
00193
00194
00195
00196 return fd;
00197 }
00198
00199
00200 bool_t
00201 getMessage( Socket fd, char* buf, int* len,
00202 unsigned int* srcIp, unsigned short* srcPort,
00203 bool_t verbose)
00204 {
00205
00206
00207 int originalSize = *len;
00208 struct sockaddr_in from;
00209 int fromLen = sizeof(from);
00210
00211
00212 int err;
00213 struct timeval tv;
00214 fd_set fdSet;
00215 #if defined(WIN32) || defined(_WIN32_WCE)
00216 unsigned int fdSetSize;
00217 #else
00218 int fdSetSize;
00219 #endif
00220
00221 assert( originalSize > 0 );
00222
00223 tv.tv_sec=1;
00224 tv.tv_usec=0;
00225 FD_ZERO(&fdSet); fdSetSize=0;
00226 FD_SET(fd,&fdSet); fdSetSize = fd+1;
00227
00228 err = select(fdSetSize, &fdSet, NULL, NULL, &tv);
00229 if ( err == SOCKET_ERROR )
00230 {
00231 int e = getErrno();
00232 switch (e)
00233 {
00234 case ENOTSOCK:
00235 printf("Error fd not a socket\n");
00236 break;
00237 case ECONNRESET:
00238 printf("Error connection reset - host not reachable\n");
00239 break;
00240
00241 default:
00242 printf("Socket Error=%i\n", e);
00243 }
00244 return FALSE;
00245 }
00246
00247 if (err==0)
00248 {
00249 printf("Connection timeout with stun server!\n");
00250 *len = 0;
00251 return FALSE;
00252 }
00253
00254 if (FD_ISSET (fd, &fdSet))
00255 {
00256 *len = recvfrom(fd,
00257 buf,
00258 originalSize,
00259 0,
00260 (struct sockaddr *)&from,
00261 (socklen_t*)&fromLen);
00262
00263 if ( *len == SOCKET_ERROR )
00264 {
00265 int e = getErrno();
00266
00267 switch (e)
00268 {
00269 case ENOTSOCK:
00270 printf("Error fd not a socket\n");
00271 break;
00272 case ECONNRESET:
00273 printf("Error connection reset - host not reachable\n");
00274 break;
00275
00276 default:
00277 printf("Socket Error=%i\n", e);
00278 }
00279
00280 return FALSE;
00281 }
00282
00283 if ( *len < 0 )
00284 {
00285 printf("socket closed? negative len\n");
00286 return FALSE;
00287 }
00288
00289 if ( *len == 0 )
00290 {
00291 printf("socket closed? zero len\n");
00292 return FALSE;
00293 }
00294
00295 *srcPort = ntohs(from.sin_port);
00296 *srcIp = ntohl(from.sin_addr.s_addr);
00297
00298 if ( (*len)+1 >= originalSize )
00299 {
00300 if (verbose)
00301 {
00302 printf("Received a message that was too large\n");
00303 }
00304 return FALSE;
00305 }
00306 buf[*len]=0;
00307
00308 return TRUE;
00309 }
00310 return FALSE;
00311 }
00312
00313
00314 bool_t
00315 sendMessage( Socket fd, char* buf, int l,
00316 unsigned int dstIp, unsigned short dstPort,
00317 bool_t verbose)
00318 {
00319 int s;
00320 assert( fd != INVALID_SOCKET );
00321
00322 if ( dstPort == 0 )
00323 {
00324
00325 assert( dstIp == 0 );
00326
00327 s = send(fd,buf,l,0);
00328 }
00329 else
00330 {
00331 struct sockaddr_in to;
00332 int toLen = sizeof(to);
00333 assert( dstIp != 0 );
00334 assert( dstPort != 0 );
00335
00336
00337 memset(&to,0,toLen);
00338
00339 to.sin_family = AF_INET;
00340 to.sin_port = htons(dstPort);
00341 to.sin_addr.s_addr = htonl(dstIp);
00342
00343 s = sendto(fd, buf, l, 0,(struct sockaddr*)&to, toLen);
00344 }
00345
00346 if ( s == SOCKET_ERROR )
00347 {
00348 int e = getErrno();
00349 switch (e)
00350 {
00351 case ECONNREFUSED:
00352 case EHOSTDOWN:
00353 case EHOSTUNREACH:
00354 {
00355
00356 }
00357 break;
00358 case EAFNOSUPPORT:
00359 {
00360 printf("err EAFNOSUPPORT in send\n");
00361 }
00362 break;
00363 default:
00364 {
00365 #if !defined(_WIN32_WCE)
00366 printf("err %i %s in send\n", e, strerror(e));
00367 #else
00368 printf("err %i in send\n", e);
00369 #endif
00370 }
00371 }
00372 return FALSE;
00373 }
00374
00375 if ( s == 0 )
00376 {
00377 printf("no data sent in send\n");
00378 return FALSE;
00379 }
00380
00381 if ( s != l )
00382 {
00383 if (verbose)
00384 {
00385 printf("only %i out of %i bytes sent\n", s, l);
00386 }
00387 return FALSE;
00388 }
00389
00390 return TRUE;
00391 }
00392
00393
00394 void
00395 initNetwork()
00396 {
00397 #if defined(WIN32) || defined(_WIN32_WCE)
00398 WORD wVersionRequested = MAKEWORD( 2, 2 );
00399 WSADATA wsaData;
00400 int err;
00401
00402 err = WSAStartup( wVersionRequested, &wsaData );
00403 if ( err != 0 )
00404 {
00405
00406 printf("Could not load winsock\n");
00407 assert(0);
00408 exit(1);
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 if ( LOBYTE( wsaData.wVersion ) != 2 ||
00418 HIBYTE( wsaData.wVersion ) != 2 )
00419 {
00420
00421
00422 WSACleanup( );
00423 printf("Bad winsock verion\n");
00424 assert(0);
00425 exit(1);
00426 }
00427 #endif
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487