An Overlay-based
Virtual Network Substrate
SpoVNet

Ignore:
Timestamp:
Dec 8, 2009, 5:28:53 PM (10 years ago)
Author:
mies
Message:

updated protlib to work with v4 only

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/utility/transport/tcpip/protlib/tp_over_tcp.cpp

    r5284 r7038  
    6161const unsigned int max_listen_queue_size= 10;
    6262
     63#define IPV6_ADDR_INT32_SMP 0x0000ffff
     64
    6365namespace protlib {
     66
     67void v6_to_v4(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)       {
     68        bzero(sin, sizeof(*sin));
     69        sin->sin_family = AF_INET;
     70        sin->sin_port = sin6->sin6_port;
     71        memcpy(&sin->sin_addr, &sin6->sin6_addr.s6_addr[12], sizeof(struct in_addr));
     72}
     73
     74/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
     75void v4_to_v6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) {
     76        bzero(sin6, sizeof(*sin6));
     77        sin6->sin6_family = AF_INET6;
     78        sin6->sin6_port = sin->sin_port;
     79        *(uint32_t *)&sin6->sin6_addr.s6_addr[0] = 0;
     80        *(uint32_t *)&sin6->sin6_addr.s6_addr[4] = 0;
     81        *(uint32_t *)&sin6->sin6_addr.s6_addr[8] = IPV6_ADDR_INT32_SMP;
     82        *(uint32_t *)&sin6->sin6_addr.s6_addr[12] = sin->sin_addr.s_addr;
     83}
    6484
    6585using namespace log;
     
    129149
    130150    // no connection found, create a new one
    131     new_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
     151    new_socket = socket( v4_mode ? AF_INET : AF_INET6, SOCK_STREAM, IPPROTO_TCP);
    132152    if (new_socket == -1)
    133153    {
     
    161181    }
    162182
    163     struct sockaddr_in6 dest_address;
    164     dest_address.sin6_flowinfo= 0;
    165     dest_address.sin6_scope_id= 0;
    166     addr.get_sockaddr(dest_address);
     183        struct sockaddr_in6 dest_address;
     184        dest_address.sin6_flowinfo= 0;
     185        dest_address.sin6_scope_id= 0;
     186        addr.get_sockaddr(dest_address);
     187
     188        // connect the socket to the destination address
     189        int connect_status = 0;
     190        if (v4_mode) {
     191                struct sockaddr_in dest_address_v4;
     192                v6_to_v4( &dest_address_v4, &dest_address );
     193                connect_status = connect(new_socket,
     194                                         reinterpret_cast<const struct sockaddr*>(&dest_address_v4),
     195                                         sizeof(dest_address));
     196        } else {
     197                connect_status = connect(new_socket,
     198                                         reinterpret_cast<const struct sockaddr*>(&dest_address),
     199                                         sizeof(dest_address));
     200        }
    167201     
    168     // connect the socket to the destination address
    169     int connect_status = connect(new_socket,
    170                                  reinterpret_cast<const struct sockaddr*>(&dest_address),
    171                                  sizeof(dest_address));
    172 
    173202    // connects to the listening_port of the peer's masterthread   
    174203    if (connect_status != 0)
     
    180209    }
    181210
    182    
     211
    183212    struct sockaddr_in6 own_address;
    184213    socklen_t own_address_len= sizeof(own_address);
    185     getsockname(new_socket, reinterpret_cast<struct sockaddr*>(&own_address), &own_address_len);
    186 
    187    
     214        getsockname(new_socket, reinterpret_cast<struct sockaddr*>(&own_address), &own_address_len);
    188215
    189216    Log(DEBUG_LOG,LOG_UNIMP, tpparam.name,">>--Connect-->> to " << addr.get_ip_str() << " port #" << addr.get_port()
    190217        << " from " << inet_ntop(AF_INET6,&own_address.sin6_addr,in6_addrstr,INET6_ADDRSTRLEN)
    191218        << " port #" << ntohs(own_address.sin6_port));
    192    
    193    
    194219
    195220   
     
    14221447{
    14231448  // create a new address-structure for the listening masterthread
    1424   struct sockaddr_in6 own_address;
    1425   own_address.sin6_family = AF_INET6;
    1426   own_address.sin6_flowinfo= 0;
    1427   own_address.sin6_port = htons(tpparam.port); // use port number in param structure
     1449  struct sockaddr_in6 own_address_v6;
     1450  own_address_v6.sin6_family = AF_INET6;
     1451  own_address_v6.sin6_flowinfo= 0;
     1452  own_address_v6.sin6_port = htons(tpparam.port); // use port number in param structure
    14281453  // accept incoming connections on all interfaces
    1429   own_address.sin6_addr = in6addr_any;
    1430   own_address.sin6_scope_id= 0;
     1454  own_address_v6.sin6_addr = in6addr_any;
     1455  own_address_v6.sin6_scope_id= 0;
     1456
     1457  // create a new address-structure for the listening masterthread
     1458  struct sockaddr_in own_address_v4;
     1459  own_address_v4.sin_family = AF_INET;
     1460  own_address_v4.sin_port = htons(tpparam.port); // use port number in param structure
     1461  // accept incoming connections on all interfaces
     1462  own_address_v4.sin_addr.s_addr = INADDR_ANY;
    14311463 
    14321464  // create a listening socket
    1433   int master_listener_socket= socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
     1465  int master_listener_socket= socket( AF_INET6, SOCK_STREAM, IPPROTO_TCP);
     1466  if (master_listener_socket!=-1) v4_mode = false;
     1467  if (master_listener_socket == -1) {
     1468          master_listener_socket= socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);
     1469          if (master_listener_socket!=-1) v4_mode = true;
     1470  }
    14341471  if (master_listener_socket == -1)
    14351472  {
     
    14641501 
    14651502  // bind the newly created socket to a specific address
    1466   int bind_status = bind(master_listener_socket,
    1467                          reinterpret_cast<struct sockaddr *>(&own_address),
    1468                          sizeof(own_address));
     1503  int bind_status = bind(master_listener_socket, v4_mode ?
     1504                         reinterpret_cast<struct sockaddr *>(&own_address_v4) :
     1505                         reinterpret_cast<struct sockaddr *>(&own_address_v6),
     1506                                         v4_mode ? sizeof(own_address_v4) : sizeof(own_address_v6));
    14691507  if (bind_status)
    14701508    {
    14711509      Log(ERROR_LOG,LOG_CRIT, tpparam.name, "Binding to "
    1472           << inet_ntop(AF_INET6, &own_address.sin6_addr, in6_addrstr, INET6_ADDRSTRLEN)
     1510          << (v4_mode ? inet_ntop(AF_INET, &own_address_v4.sin_addr, in_addrstr, INET_ADDRSTRLEN) :
     1511                          inet_ntop(AF_INET6, &own_address_v6.sin6_addr, in6_addrstr, INET6_ADDRSTRLEN))
    14731512          << " port " << tpparam.port << " failed, error: " << strerror(errno));
    14741513      return;
     
    15981637
    15991638        struct sockaddr_in6 own_address;
    1600         socklen_t own_address_len= sizeof(own_address);
    1601         getsockname(conn_socket, reinterpret_cast<struct sockaddr*>(&own_address), &own_address_len);
     1639        if (v4_mode) {
     1640                struct sockaddr_in own_address_v4;
     1641                socklen_t own_address_len_v4 = sizeof(own_address_v4);
     1642                getsockname(conn_socket, reinterpret_cast<struct sockaddr*>(&own_address_v4), &own_address_len_v4);
     1643                v4_to_v6(&own_address_v4, &own_address);
     1644        } else {
     1645                socklen_t own_address_len= sizeof(own_address);
     1646                getsockname(conn_socket, reinterpret_cast<struct sockaddr*>(&own_address), &own_address_len);
     1647        }
    16021648
    16031649        // AssocData will copy addr content into its own structure
Note: See TracChangeset for help on using the changeset viewer.