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 #include "ortp/ortp.h"
00115
00116 #if !defined(WIN32) && !defined(_WIN32_WCE)
00117 int getErrno() { return errno; }
00118 #else
00119 int getErrno() { return WSAGetLastError(); }
00120 #endif
00121
00122 Socket
00123 openPort( unsigned short port, unsigned int interfaceIp, bool_t verbose )
00124 {
00125 struct sockaddr_in addr;
00126 Socket fd;
00127
00128 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
00129 if ( fd == INVALID_SOCKET )
00130 {
00131 ortp_error("stun_udp: Could not create a UDP socket");
00132 return INVALID_SOCKET;
00133 }
00134
00135 memset((char*) &(addr),0, sizeof((addr)));
00136 addr.sin_family = AF_INET;
00137 addr.sin_addr.s_addr = htonl(INADDR_ANY);
00138 addr.sin_port = htons(port);
00139
00140 if ( (interfaceIp != 0) &&
00141 ( interfaceIp != 0x100007f ) )
00142 {
00143 addr.sin_addr.s_addr = htonl(interfaceIp);
00144 if (verbose )
00145 {
00146 ortp_message("Binding to interface 0x%lu\n",(unsigned long) htonl(interfaceIp));
00147 }
00148 }
00149
00150 if ( bind( fd,(struct sockaddr*)&addr, sizeof(addr)) != 0 )
00151 {
00152 int e = getErrno();
00153
00154 switch (e)
00155 {
00156 case 0:
00157 {
00158 ortp_error("stun_udp: Could not bind socket");;
00159 return INVALID_SOCKET;
00160 }
00161 case EADDRINUSE:
00162 {
00163 ortp_error("stun_udp: Port %i for receiving UDP is in use", port);
00164 return INVALID_SOCKET;
00165 }
00166 break;
00167 case EADDRNOTAVAIL:
00168 {
00169 if ( verbose )
00170 {
00171 ortp_error("stun_udp: Cannot assign requested address");
00172 }
00173 return INVALID_SOCKET;
00174 }
00175 break;
00176 default:
00177 {
00178 #if !defined(_WIN32_WCE)
00179 ortp_error("stun_udp: Could not bind UDP receive port Error=%i %s",
00180 e, strerror(e));
00181 #else
00182 ortp_error("stun_udp: Could not bind UDP receive port Error=%i",
00183 e);
00184 #endif
00185 return INVALID_SOCKET;
00186 }
00187 break;
00188 }
00189 }
00190 if ( verbose )
00191 {
00192 ortp_message("stun: opened port %i with fd %i\n", port, fd);
00193 }
00194
00195
00196
00197 return fd;
00198 }
00199
00200
00201 bool_t
00202 getMessage( Socket fd, char* buf, int* len,
00203 unsigned int* srcIp, unsigned short* srcPort,
00204 bool_t verbose)
00205 {
00206
00207
00208 int originalSize = *len;
00209 struct sockaddr_in from;
00210 int fromLen = sizeof(from);
00211
00212
00213 int err;
00214 struct timeval tv;
00215 fd_set fdSet;
00216 #if defined(WIN32) || defined(_WIN32_WCE)
00217 unsigned int fdSetSize;
00218 #else
00219 int fdSetSize;
00220 #endif
00221
00222 if (originalSize <= 0)
00223 {
00224 return FALSE;
00225 }
00226
00227 tv.tv_sec=1;
00228 tv.tv_usec=0;
00229 FD_ZERO(&fdSet); fdSetSize=0;
00230 FD_SET(fd,&fdSet); fdSetSize = fd+1;
00231
00232 err = select(fdSetSize, &fdSet, NULL, NULL, &tv);
00233 if ( err == SOCKET_ERROR )
00234 {
00235 int e = getErrno();
00236 switch (e)
00237 {
00238 case ENOTSOCK:
00239 ortp_error("stun_udp: Error fd not a socket");
00240 break;
00241 case ECONNRESET:
00242 ortp_error("stun_udp: Error connection reset - host not reachable");
00243 break;
00244
00245 default:
00246 ortp_error("stun_udp: Socket Error=%i", e);
00247 }
00248 return FALSE;
00249 }
00250
00251 if (err==0)
00252 {
00253 ortp_error("stun_udp: Connection timeout with stun server!");
00254 *len = 0;
00255 return FALSE;
00256 }
00257
00258 if (FD_ISSET (fd, &fdSet))
00259 {
00260 *len = recvfrom(fd,
00261 buf,
00262 originalSize,
00263 0,
00264 (struct sockaddr *)&from,
00265 (socklen_t*)&fromLen);
00266
00267 if ( *len == SOCKET_ERROR )
00268 {
00269 int e = getErrno();
00270
00271 switch (e)
00272 {
00273 case ENOTSOCK:
00274 ortp_error("stun_udp: Error fd not a socket");
00275 break;
00276 case ECONNRESET:
00277 ortp_error("stun_udp: Error connection reset - host not reachable");
00278 break;
00279
00280 default:
00281 ortp_error("stun_udp: Socket Error=%i", e);
00282 }
00283
00284 return FALSE;
00285 }
00286
00287 if ( *len < 0 )
00288 {
00289 ortp_error("stun_udp: socket closed? negative len");
00290 return FALSE;
00291 }
00292
00293 if ( *len == 0 )
00294 {
00295 ortp_error("stun_udp: socket closed? zero len");
00296 return FALSE;
00297 }
00298
00299 *srcPort = ntohs(from.sin_port);
00300 *srcIp = ntohl(from.sin_addr.s_addr);
00301
00302 if ( (*len)+1 >= originalSize )
00303 {
00304 if (verbose)
00305 {
00306 ortp_error("stun_udp: Received a message that was too large");
00307 }
00308 return FALSE;
00309 }
00310 buf[*len]=0;
00311
00312 return TRUE;
00313 }
00314 return FALSE;
00315 }
00316
00317
00318 bool_t
00319 sendMessage( Socket fd, char* buf, int l,
00320 unsigned int dstIp, unsigned short dstPort,
00321 bool_t verbose)
00322 {
00323 int s;
00324
00325 if (fd == INVALID_SOCKET)
00326 return FALSE;
00327
00328 if ( dstPort == 0 )
00329 {
00330
00331 s = send(fd,buf,l,0);
00332 }
00333 else
00334 {
00335 struct sockaddr_in to;
00336 int toLen = sizeof(to);
00337 if (dstIp == 0)
00338 {
00339 ortp_error("stun_udp: invalid IP provided (dstIP==0)");
00340 return FALSE;
00341 }
00342
00343 memset(&to,0,toLen);
00344
00345 to.sin_family = AF_INET;
00346 to.sin_port = htons(dstPort);
00347 to.sin_addr.s_addr = htonl(dstIp);
00348
00349 s = sendto(fd, buf, l, 0,(struct sockaddr*)&to, toLen);
00350 }
00351
00352 if ( s == SOCKET_ERROR )
00353 {
00354 int e = getErrno();
00355 switch (e)
00356 {
00357 case ECONNREFUSED:
00358 case EHOSTDOWN:
00359 case EHOSTUNREACH:
00360 {
00361
00362 }
00363 break;
00364 case EAFNOSUPPORT:
00365 {
00366 ortp_error("stun_udp: err EAFNOSUPPORT in send");
00367 }
00368 break;
00369 default:
00370 {
00371 #if !defined(_WIN32_WCE)
00372 ortp_error("stun_udp: err %i %s in send", e, strerror(e));
00373 #else
00374 ortp_error("stun_udp: err %i in send", e);
00375 #endif
00376 }
00377 }
00378 return FALSE;
00379 }
00380
00381 if ( s == 0 )
00382 {
00383 ortp_error("stun_udp: no data sent in send");
00384 return FALSE;
00385 }
00386
00387 if ( s != l )
00388 {
00389 if (verbose)
00390 {
00391 ortp_error("stun_udp: only %i out of %i bytes sent", s, l);
00392 }
00393 return FALSE;
00394 }
00395
00396 return TRUE;
00397 }
00398
00399
00400 void
00401 initNetwork()
00402 {
00403 #if defined(WIN32) || defined(_WIN32_WCE)
00404 WORD wVersionRequested = MAKEWORD( 2, 2 );
00405 WSADATA wsaData;
00406 int err;
00407
00408 err = WSAStartup( wVersionRequested, &wsaData );
00409 if ( err != 0 )
00410 {
00411
00412 ortp_error("stun_udp: Could not load winsock");
00413 }
00414
00415
00416
00417
00418
00419
00420
00421 if ( LOBYTE( wsaData.wVersion ) != 2 ||
00422 HIBYTE( wsaData.wVersion ) != 2 )
00423 {
00424
00425
00426 WSACleanup( );
00427 ortp_error("stun_udp: Wrong winsock (!= 2.2) version");
00428 }
00429 #endif
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
00488
00489