udp.cc

Go to the documentation of this file.
00001 #include <cassert>
00002 #include <cstdio>
00003 #include <cstring>
00004 #include <errno.h>
00005 #include <iostream>
00006 #include <cstdlib>
00007 #include <time.h>
00008 
00009 #ifdef WIN32
00010 
00011 #include <winsock2.h>
00012 #include <stdlib.h>
00013 #include <io.h>
00014 
00015 #else
00016 
00017 #include <arpa/inet.h>
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <fcntl.h>
00021 #include <netinet/in.h>
00022 #include <sys/socket.h>
00023 #include <sys/types.h>
00024 #include <netdb.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 
00028 #endif
00029 
00030 #include <string.h>
00031 
00032 #include "stun_udp.h"
00033 
00034 using namespace std;
00035 
00036 
00037 Socket
00038 openPort( unsigned short port, unsigned int interfaceIp, bool verbose )
00039 {
00040    Socket fd;
00041 
00042    fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
00043    if ( fd == STUN_INVALID_SOCKET )
00044    {
00045       int err = getErrno();
00046       cerr << "Could not create a UDP socket:" << err << endl;
00047       return STUN_INVALID_SOCKET;
00048    }
00049 
00050    struct sockaddr_in addr;
00051    memset((char*) &(addr),0, sizeof((addr)));
00052    addr.sin_family = AF_INET;
00053    addr.sin_addr.s_addr = htonl(INADDR_ANY);
00054    addr.sin_port = htons(port);
00055 
00056    if ( (interfaceIp != 0) &&
00057         ( interfaceIp != 0x100007f ) )
00058    {
00059       addr.sin_addr.s_addr = htonl(interfaceIp);
00060       if (verbose )
00061       {
00062          clog << "Binding to interface "
00063               << hex << "0x" << htonl(interfaceIp) << dec << endl;
00064       }
00065    }
00066 
00067    if ( bind( fd,(struct sockaddr*)&addr, sizeof(addr)) != 0 )
00068    {
00069       int e = getErrno();
00070 
00071       switch (e)
00072       {
00073          case 0:
00074          {
00075             cerr << "Could not bind socket" << endl;
00076             return STUN_INVALID_SOCKET;
00077          }
00078          case EADDRINUSE:
00079          {
00080             cerr << "Port " << port << " for receiving UDP is in use" << endl;
00081             return STUN_INVALID_SOCKET;
00082          }
00083          break;
00084          case EADDRNOTAVAIL:
00085          {
00086             if ( verbose )
00087             {
00088                cerr << "Cannot assign requested address" << endl;
00089             }
00090             return STUN_INVALID_SOCKET;
00091          }
00092          break;
00093          default:
00094          {
00095             cerr << "Could not bind UDP receive port"
00096                  << "Error=" << e << " " << strerror(e) << endl;
00097             return STUN_INVALID_SOCKET;
00098          }
00099          break;
00100       }
00101    }
00102    if ( verbose )
00103    {
00104       clog << "Opened port " << port << " with fd " << fd << endl;
00105    }
00106 
00107    assert( fd != STUN_INVALID_SOCKET  );
00108 
00109    return fd;
00110 }
00111 
00112 
00113 bool
00114 getMessage( Socket fd, char* buf, int* len,
00115             unsigned int* srcIp, unsigned short* srcPort,
00116             bool verbose)
00117 {
00118    assert( fd != STUN_INVALID_SOCKET );
00119 
00120    int originalSize = *len;
00121    assert( originalSize > 0 );
00122 
00123    struct sockaddr_in from;
00124    int fromLen = sizeof(from);
00125 
00126    *len = recvfrom(fd,
00127                    buf,
00128                    originalSize,
00129                    0,
00130                    (struct sockaddr *)&from,
00131                    (socklen_t*)&fromLen);
00132 
00133    if ( *len == STUN_SOCKET_ERROR )
00134    {
00135       int err = getErrno();
00136 
00137       switch (err)
00138       {
00139          case ENOTSOCK:
00140             cerr << "Error fd not a socket" <<   endl;
00141             break;
00142          case ECONNRESET:
00143             cerr << "Error connection reset - host not reachable" <<   endl;
00144             break;
00145 
00146          default:
00147             cerr << "Socket Error=" << err << endl;
00148       }
00149 
00150       return false;
00151    }
00152 
00153    if ( *len < 0 )
00154    {
00155       clog << "socket closed? negative len" << endl;
00156       return false;
00157    }
00158 
00159    if ( *len == 0 )
00160    {
00161       clog << "socket closed? zero len" << endl;
00162       return false;
00163    }
00164 
00165    *srcPort = ntohs(from.sin_port);
00166    *srcIp = ntohl(from.sin_addr.s_addr);
00167 
00168    if ( (*len)+1 >= originalSize )
00169    {
00170       if (verbose)
00171       {
00172          clog << "Received a message that was too large" << endl;
00173       }
00174       return false;
00175    }
00176    buf[*len]=0;
00177 
00178    return true;
00179 }
00180 
00181 
00182 bool
00183 sendMessage( Socket fd, char* buf, int l,
00184              unsigned int dstIp, unsigned short dstPort,
00185              bool verbose)
00186 {
00187    assert( fd != STUN_INVALID_SOCKET );
00188 
00189    int s;
00190    if ( dstPort == 0 )
00191    {
00192       // sending on a connected port
00193       assert( dstIp == 0 );
00194 
00195       s = send(fd,buf,l,0);
00196    }
00197    else
00198    {
00199       assert( dstIp != 0 );
00200       assert( dstPort != 0 );
00201 
00202       struct sockaddr_in to;
00203       int toLen = sizeof(to);
00204       memset(&to,0,toLen);
00205 
00206       to.sin_family = AF_INET;
00207       to.sin_port = htons(dstPort);
00208       to.sin_addr.s_addr = htonl(dstIp);
00209 
00210       s = sendto(fd, buf, l, 0,(sockaddr*)&to, toLen);
00211    }
00212 
00213    if ( s == STUN_SOCKET_ERROR )
00214    {
00215       int e = getErrno();
00216       switch (e)
00217       {
00218          case ECONNREFUSED:
00219          case EHOSTDOWN:
00220          case EHOSTUNREACH:
00221          {
00222             // quietly ignore this
00223          }
00224          break;
00225          case EAFNOSUPPORT:
00226          {
00227             cerr << "err EAFNOSUPPORT in send" << endl;
00228          }
00229          break;
00230          default:
00231          {
00232             cerr << "err " << e << " "  << strerror(e) << " in send" << endl;
00233          }
00234       }
00235       return false;
00236    }
00237 
00238    if ( s == 0 )
00239    {
00240       cerr << "no data sent in send" << endl;
00241       return false;
00242    }
00243 
00244    if ( s != l )
00245    {
00246       if (verbose)
00247       {
00248          cerr << "only " << s << " out of " << l << " bytes sent" << endl;
00249       }
00250       return false;
00251    }
00252 
00253    return true;
00254 }
00255 
00256 
00257 void
00258 initNetwork()
00259 {
00260 #ifdef WIN32
00261    WORD wVersionRequested = MAKEWORD( 2, 2 );
00262    WSADATA wsaData;
00263    int err;
00264 
00265    err = WSAStartup( wVersionRequested, &wsaData );
00266    if ( err != 0 )
00267    {
00268       // could not find a usable WinSock DLL
00269       cerr << "Could not load winsock" << endl;
00270       assert(0); // is this is failing, try a different version that 2.2, 1.0 or later will likely work
00271       exit(1);
00272    }
00273 
00274    /* Confirm that the WinSock DLL supports 2.2.*/
00275    /* Note that if the DLL supports versions greater    */
00276    /* than 2.2 in addition to 2.2, it will still return */
00277    /* 2.2 in wVersion since that is the version we      */
00278    /* requested.                                        */
00279 
00280    if ( LOBYTE( wsaData.wVersion ) != 2 ||
00281         HIBYTE( wsaData.wVersion ) != 2 )
00282    {
00283       /* Tell the user that we could not find a usable */
00284       /* WinSock DLL.                                  */
00285       WSACleanup( );
00286       cerr << "Bad winsock verion" << endl;
00287       assert(0); // is this is failing, try a different version that 2.2, 1.0 or later will likely work
00288       exit(1);
00289    }
00290 #endif
00291 }
00292 
00293 
00294 /* ====================================================================
00295  * The Vovida Software License, Version 1.0
00296  *
00297  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00298  *
00299  * Redistribution and use in source and binary forms, with or without
00300  * modification, are permitted provided that the following conditions
00301  * are met:
00302  *
00303  * 1. Redistributions of source code must retain the above copyright
00304  *    notice, this list of conditions and the following disclaimer.
00305  *
00306  * 2. Redistributions in binary form must reproduce the above copyright
00307  *    notice, this list of conditions and the following disclaimer in
00308  *    the documentation and/or other materials provided with the
00309  *    distribution.
00310  *
00311  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00312  *    and "Vovida Open Communication Application Library (VOCAL)" must
00313  *    not be used to endorse or promote products derived from this
00314  *    software without prior written permission. For written
00315  *    permission, please contact vocal@vovida.org.
00316  *
00317  * 4. Products derived from this software may not be called "VOCAL", nor
00318  *    may "VOCAL" appear in their name, without prior written
00319  *    permission of Vovida Networks, Inc.
00320  *
00321  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00322  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00323  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00324  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00325  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00326  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00327  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00328  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00329  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00330  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00331  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00332  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00333  * DAMAGE.
00334  *
00335  * ====================================================================
00336  *
00337  * This software consists of voluntary contributions made by Vovida
00338  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00339  * Inc.  For more information on Vovida Networks, Inc., please see
00340  * <http://www.vovida.org/>.
00341  *
00342  */
00343 
00344 // Local Variables:
00345 // mode:c++
00346 // c-file-style:"ellemtel"
00347 // c-file-offsets:((case-label . +))
00348 // indent-tabs-mode:nil
00349 // End:
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3