Ignore:
Timestamp:
Sep 25, 2006, 6:37:45 PM (15 years ago)
Author:
joshe
Message:

Merge nat-traversal branch to trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/net.c

    r791 r920  
    3636 * Returns a non-zero value if an error occurs.
    3737 **********************************************************************/
    38 int tr_netResolve( char * address, struct in_addr * addr )
     38int tr_netResolve( const char * address, struct in_addr * addr )
    3939{
    4040    addr->s_addr = inet_addr( address );
     
    5353struct tr_resolve_s
    5454{
    55     int            status;
     55    tr_tristate_t  status;
    5656    char           * address;
    5757    struct in_addr addr;
     
    9595 * Adds an address to the resolution queue.
    9696 **********************************************************************/
    97 tr_resolve_t * tr_netResolveInit( char * address )
     97tr_resolve_t * tr_netResolveInit( const char * address )
    9898{
    9999    tr_resolve_t * r;
    100100
    101101    r           = malloc( sizeof( tr_resolve_t ) );
    102     r->status   = TR_RESOLVE_WAIT;
     102    r->status   = TR_WAIT;
    103103    r->address  = strdup( address );
    104104    r->refcount = 2;
     
    126126 * Checks the current status of a resolution.
    127127 **********************************************************************/
    128 int tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
    129 {
    130     int ret;
     128tr_tristate_t tr_netResolvePulse( tr_resolve_t * r, struct in_addr * addr )
     129{
     130    tr_tristate_t ret;
    131131
    132132    tr_lockLock( &resolveLock );
    133133    ret = r->status;
    134     if( ret == TR_RESOLVE_OK )
     134    if( ret == TR_OK )
    135135    {
    136136        *addr = r->addr;
     
    202202        {
    203203            memcpy( &r->addr, host->h_addr, host->h_length );
    204             r->status = TR_RESOLVE_OK;
     204            r->status = TR_OK;
    205205        }
    206206        else
    207207        {
    208             r->status = TR_RESOLVE_ERROR;
     208            r->status = TR_ERROR;
    209209        }
    210210       
     
    252252}
    253253
    254 static int createSocket()
     254static int createSocket( int type )
    255255{
    256256    int s;
    257257
    258     s = socket( AF_INET, SOCK_STREAM, 0 );
     258    s = socket( AF_INET, type, 0 );
    259259    if( s < 0 )
    260260    {
     
    266266}
    267267
    268 int tr_netOpen( struct in_addr addr, in_port_t port )
     268int tr_netOpen( struct in_addr addr, in_port_t port, int type )
    269269{
    270270    int s;
    271271    struct sockaddr_in sock;
    272272
    273     s = createSocket();
     273    s = createSocket( type );
    274274    if( s < 0 )
    275275    {
     
    294294}
    295295
    296 int tr_netBind( int port )
     296#ifdef IP_ADD_MEMBERSHIP
     297int tr_netMcastOpen( int port, struct in_addr addr )
     298{
     299    int fd;
     300    struct ip_mreq req;
     301
     302    fd = tr_netBindUDP( port );
     303    if( 0 > fd )
     304    {
     305        return -1;
     306    }
     307
     308    memset( &req, 0, sizeof( req ) );
     309    req.imr_multiaddr.s_addr = addr.s_addr;
     310    req.imr_interface.s_addr = htonl( INADDR_ANY );
     311    if( setsockopt( fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof ( req ) ) )
     312    {
     313        tr_err( "Could not join multicast group (%s)", strerror( errno ) );
     314        tr_netClose( fd );
     315        return -1;
     316    }
     317
     318    return fd;
     319}
     320#else /* IP_ADD_MEMBERSHIP */
     321int tr_netMcastOpen( int port UNUSED, struct in_addr addr UNUSED )
     322{
     323    return -1;
     324}
     325#endif /* IP_ADD_MEMBERSHIP */
     326
     327int tr_netBind( int port, int type )
    297328{
    298329    int s;
    299330    struct sockaddr_in sock;
    300 #ifdef SO_REUSEADDR
     331#if defined( SO_REUSEADDR ) || defined( SO_REUSEPORT )
    301332    int optval;
    302333#endif
    303334
    304     s = createSocket();
     335    s = createSocket( type );
    305336    if( s < 0 )
    306337    {
     
    313344#endif
    314345
     346#ifdef SO_REUSEPORT
     347    if( SOCK_DGRAM == type )
     348    {
     349        optval = 1;
     350        setsockopt( s, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof( optval ) );
     351    }
     352#endif
     353
    315354    memset( &sock, 0, sizeof( sock ) );
    316355    sock.sin_family      = AF_INET;
     
    325364        return -1;
    326365    }
    327    
    328     tr_inf( "Binded port %d", port );
    329     listen( s, 5 );
    330366
    331367    return s;
     
    372408}
    373409
    374 int tr_netRecv( int s, uint8_t * buf, int size )
    375 {
    376     int ret;
    377 
    378     ret = recv( s, buf, size, 0 );
     410int tr_netRecvFrom( int s, uint8_t * buf, int size, struct sockaddr_in * addr )
     411{
     412    socklen_t len;
     413    int       ret;
     414
     415    len = ( NULL == addr ? 0 : sizeof( *addr ) );
     416    ret = recvfrom( s, buf, size, 0, ( struct sockaddr * ) addr, &len );
    379417    if( ret < 0 )
    380418    {
Note: See TracChangeset for help on using the changeset viewer.