Exemple #1
0
ICELIB_Result
complete(void*        pUserData,
         unsigned int userval1,
         bool         controlling,
         bool         failed)
{
  (void) userval1;
  (void) controlling;
  (void) failed;
  struct prg_data* prg = pUserData;

  const ICE_CANDIDATE* lcand =
    ICELIB_getActiveCandidate(prg->mediaCnf->icelib,
                              0,
                              1);

  const ICE_REMOTE_CANDIDATES* rcand =
    ICELIB_getActiveRemoteCandidates(prg->mediaCnf->icelib,
                                     0);
  char laddr[SOCKADDR_MAX_STRLEN];
  char raddr[SOCKADDR_MAX_STRLEN];
  sockaddr_toString( (const struct sockaddr*)&lcand->connectionAddr,
                     laddr,
                     sizeof(laddr),
                     true );
  sockaddr_toString(
    (const struct sockaddr*)&rcand->remoteCandidate[0].connectionAddr,
    raddr,
    sizeof(raddr),
    true);
  printf("Media Path: %s  -> %s\n", laddr, raddr);
  printf("******** Complete ********* (TODO: Handle this)\n");
  return 0;
}
END_TEST



START_TEST (sockaddr_IPv6_toString)
{

    char ipaddr[SOCKADDR_MAX_STRLEN];

    fail_unless( strncmp(ipv6_1_str,
                         sockaddr_toString((const struct sockaddr *)sockaddr_IPv6_1,
                                           ipaddr,
                                           SOCKADDR_MAX_STRLEN,
                                           true),
                         SOCKADDR_MAX_STRLEN) == 0 ,
                 "sockaddr toString failed ('%s'", ipaddr);

    fail_unless( strncmp(ipv6_1_noport_str,
                         sockaddr_toString((const struct sockaddr *)sockaddr_IPv6_1,
                                           ipaddr,
                                           SOCKADDR_MAX_STRLEN,
                                           false),
                         SOCKADDR_MAX_STRLEN) == 0 ,
                 "sockaddr toString failed ('%s'", ipaddr);


}
Exemple #3
0
int createOffer(char *offer, struct turnData *tData){
    char addr[SOCKADDR_MAX_STRLEN];

    strcpy(offer, "INVITE\n");
    strcat(offer, "a=candidate UDP srflx ");    
    strcat(offer,
           sockaddr_toString((struct sockaddr *)&tData->rflx,
                             addr,
                             sizeof(addr),
                             true));
    strcat(offer, "\n");
    
    strcat(offer, "a=candidate UDP relay ");    
    strcat(offer,
           sockaddr_toString((struct sockaddr *)&tData->relay,
                             addr,
                             sizeof(addr),
                             true));
    strcat(offer, "\n");

    strcat(offer, "\0");
    

    return strlen(offer);

}
END_TEST

START_TEST (sockaddr_IPv4_toString)
{
    char ipaddr[SOCKADDR_MAX_STRLEN];
    char ipaddr_small[10];
    struct sockaddr_storage ss;

    fail_unless( strncmp(ipv4_1_str,
                         sockaddr_toString((const struct sockaddr *)sockaddr_IPv4_1,
                                           ipaddr,
                                           SOCKADDR_MAX_STRLEN,
                                           true),
                         INET_ADDRSTRLEN) == 0 ,
                 "sockaddr toString failed ('%s'", ipaddr);

    fail_unless( strncmp(ipv4_1_noport_str,
                         sockaddr_toString((const struct sockaddr *)sockaddr_IPv4_1,
                                           ipaddr,
                                           SOCKADDR_MAX_STRLEN,
                                           false),
                         SOCKADDR_MAX_STRLEN) == 0 ,
                 "sockaddr toString failed ('%s'", ipaddr);


    fail_unless( strncmp(ipv4_any_str,
                         sockaddr_toString((const struct sockaddr *)sockaddr_IPv4_any_2,
                                           ipaddr,
                                           SOCKADDR_MAX_STRLEN,
                                           true),
                         SOCKADDR_MAX_STRLEN ) == 0 ,
                 "sockaddr toString failed ('%s'", ipaddr);

    fail_if( strncmp(ipv4_any_str,
                     sockaddr_toString((const struct sockaddr *)sockaddr_IPv4_any_2,
                                       ipaddr_small,
                                       10,
                                       true),
                     SOCKADDR_MAX_STRLEN ) == 0 ,
             "sockaddr toString failed ('%s'", ipaddr);


    ss.ss_family = 12;

    fail_unless( NULL ==  sockaddr_toString((const struct sockaddr *)&ss,
                                            ipaddr,
                                            SOCKADDR_MAX_STRLEN,
                                            true) );


}
Exemple #5
0
void
turnCbFunc(void*               userCtx,
           TurnCallBackData_T* turnCbData)
{
  /* ctx points to whatever you initialized the library with. (Not used in this
   * simple example.) */
  (void)userCtx;
  if (turnCbData->turnResult == TurnResult_AllocOk)
  {
    char addr[SOCKADDR_MAX_STRLEN];

    printf( "Successfull Allocation: \n");

    printf( "   Active TURN server: '%s'\n",
            sockaddr_toString( (struct sockaddr*)&turnCbData->TurnResultData.
                               AllocResp.activeTurnServerAddr,
                               addr,
                               sizeof(addr),
                               true ) );

    printf( "   RFLX addr: '%s'\n",
            sockaddr_toString( (struct sockaddr*)&turnCbData->TurnResultData.
                               AllocResp.srflxAddr,
                               addr,
                               sizeof(addr),
                               true ) );

    printf( "   RELAY addr: '%s'\n",
            sockaddr_toString( (struct sockaddr*)&turnCbData->TurnResultData.
                               AllocResp.relAddrIPv4,
                               addr,
                               sizeof(addr),
                               true ) );


    printf( "   RELAY addr: '%s'\n",
            sockaddr_toString( (struct sockaddr*)&turnCbData->TurnResultData.
                               AllocResp.relAddrIPv6,
                               addr,
                               sizeof(addr),
                               true ) );

  }
  else if (turnCbData->turnResult == TurnResult_AllocUnauthorised)
  {
    printf("Unable to authorize. Wrong user/pass?\n");
  }
}
END_TEST


START_TEST (test_sockaddr_int_IPv6_init)
{

    char addr_str[256];
    char addr_str_small[4];
    uint8_t a[16] = {0x20, 0x1, 0x4, 0x70, 0xdc, 0x88, 0x0, 0x2, 0x2, 0x26, 0x18, 0xff, 0xfe, 0x92, 0x6d, 0x53};
    uint16_t port = 4567;
    struct sockaddr_storage a_addr;
    struct sockaddr_storage b_addr;

    sockaddr_initFromIPv6Int((struct sockaddr_in6 *)&a_addr, a, htons(port));

    fail_unless(sockaddr_initFromIPv6String((struct sockaddr_in6 *)&b_addr, "[2001:470:dc88:2:226:18ff:fe92:6d53]:4567"));

    sockaddr_toString( (struct sockaddr *)&a_addr,
                       addr_str,
                       256,
                       true);


    fail_unless( sockaddr_alike( (struct sockaddr *)&a_addr, (struct sockaddr *)&b_addr) );


    fail_if( sockaddr_initFromIPv6String((struct sockaddr_in6 *)&b_addr, "[2001:470:dc88:2:226:18ff:fe92:6d53"));

    fail_if( sockaddr_initFromIPv6String((struct sockaddr_in6 *)&b_addr, "[2001:470:dc88:2:226:18ff:fe92:6d53:456]"));


    fail_if( sockaddr_initFromIPv6String((struct sockaddr_in6 *)&b_addr, "2001:470:dc88:2:226:18ff:fe92:6d53:4567]:4567"));

}
END_TEST

START_TEST (test_sockaddr_int_IPv4_init)
{

    char addr_str[256];
    uint32_t a = 2653302794UL;
    uint16_t port = 4567;
    struct sockaddr_storage a_addr;
    struct sockaddr_storage b_addr;


    sockaddr_initFromIPv4Int((struct sockaddr_in *)&a_addr, htonl(a), htons(port));
    sockaddr_initFromIPv4String((struct sockaddr_in *)&b_addr, "158.38.48.10:4567");

    sockaddr_toString( (struct sockaddr *)&a_addr,
                       addr_str,
                       256,
                       true);

    fail_unless( sockaddr_alike( (struct sockaddr *)&a_addr, (struct sockaddr *)&b_addr) );

    fail_if(sockaddr_initFromIPv4String((struct sockaddr_in *)&b_addr, "158.38.48.10.45"));

    fail_if(sockaddr_initFromIPv4String((struct sockaddr_in *)&b_addr, "158.38.48.10.45.45.67.87:4556"));



}
static void
sendPacket(void*                  ctx,
           int                    sockfd,
           const uint8_t*         buf,
           int                    len,
           const struct sockaddr* addr,
           int                    proto,
           bool                   useRelay,
           uint8_t                ttl)
{
  (void) ctx;
  (void) sockfd;
  (void) len;
  (void) proto;
  (void) useRelay;
  char addr_str[SOCKADDR_MAX_STRLEN];
  /* find the transaction id  so we can use this in the simulated resp */
  memcpy(&LastTransId, &buf[8], STUN_MSG_ID_SIZE);
  memcpy(&LastTransId, &buf[8], STUN_MSG_ID_SIZE);

  sockaddr_copy( (struct sockaddr*)&LastAddress, addr );

  sockaddr_toString(addr, addr_str, SOCKADDR_MAX_STRLEN, true);

  LastTTL = ttl;
}
Exemple #9
0
static void
ICMPRespCallback(STUN_TRANSACTION_DATA* trans,
                 const struct sockaddr* srcAddr)
{
  STUN_CLIENT_DATA*  client = trans->client;
  char               ip_str [SOCKADDR_MAX_STRLEN];
  StunCallBackData_T res;

  memset( &res, 0, sizeof (StunCallBackData_T) );

  memcpy( &res.msgId, &trans->stunBindReq.transactionId, sizeof(StunMsgId) );

  res.stunResult = StunResult_ICMPResp;
  res.ICMPtype   = trans->ICMPtype;
  res.ttl        = trans->ttl;

  res.rtt         = getRTTvalue(trans);
  res.retransmits = trans->retransmits;
  sockaddr_copy( (struct sockaddr*)&res.srcAddr,
                 srcAddr );

  StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Info,
             "<STUNCLIENT:%02d> ICMPResp from src: %s",
             trans->inst,
             sockaddr_toString( (struct sockaddr*) &res.srcAddr, ip_str,
                                SOCKADDR_MAX_STRLEN,
                                true ) );


  if (trans->stunBindReq.stunCbFunc)
  {
    (trans->stunBindReq.stunCbFunc)(trans->stunBindReq.userCtx, &res);
  }
}
static int SendRawStun(int sockfd, 
                       uint8_t *buf, 
                       int len, 
                       struct sockaddr *addr,
                       bool useRelay)
{
    char addr_str[SOCKADDR_MAX_STRLEN];
    /* find the transaction id  so we can use this in the simulated resp */

    memcpy(&LastTransId, &buf[8], STUN_MSG_ID_SIZE); 

    sockaddr_copy((struct sockaddr *)&LastAddress, addr);
    
    sockaddr_toString(addr, addr_str, SOCKADDR_MAX_STRLEN, true);
                      
    //printf("Sendto: '%s'\n", addr_str);

}
Exemple #11
0
int sendRawUDP(int sockfd,
               const void *buf,
               size_t len,
               const struct sockaddr * p,
               socklen_t t){

    int numbytes;
    char addr[256];
    int rv;

    struct pollfd ufds[1];


    ufds[0].fd = sockfd;
    ufds[0].events = POLLOUT;

    rv = poll(ufds, 1, 3500);

    if (rv == -1) {
        perror("poll"); // error occurred in poll()
    } else if (rv == 0) {
        printf("Timeout occurred!  Not possible to send for 3.5 seconds.\n");
    } else {
        // check for events on s1:
        if (ufds[0].revents & POLLOUT) {
            numbytes = sendto(sockfd, buf, len, 0,
                              p , t);

            sockaddr_toString(p, addr, 256, true);

            if(numbytes == -1){
                int errsv = errno;
                printf("Error: %s \n", strerror(errsv));
                printf("sento( %i, buf, %zu, 0, %s, %i)\n", sockfd, len, addr, t);
            }
            //else {
            //    printf("Sending Raw (To: '%s'(%i), Bytes:%i/%i  )\n", addr, sockfd, numbytes, (int)len);
            //}
            return numbytes;
        }
    }

    return -1;
}
Exemple #12
0
static void
CommonRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans,
                          StunResult_T           stunResult,
                          const char*            errStr,
                          STUN_STATE             FailedState)
{
  STUN_CLIENT_DATA* client = trans->client;

  uint32_t max;

  if (trans->stunBindReq.stuntrace)
  {
    max = STUNTRACE_MAX_RETRANSMITS;
  }
  else
  {
    max = STUNCLIENT_MAX_RETRANSMITS;
  }

  if ( (trans->retransmits < max)
       && (stunTimeoutList[trans->retransmits] != 0) ) /* can be 0 terminated if
                                                        * using fewer
                                                        * retransmits
                                                        **/
  {
    char peer [SOCKADDR_MAX_STRLEN] = {0,};
    sockaddr_toString( (struct sockaddr*) &trans->stunBindReq.serverAddr, peer,
                       sizeof (peer), true );

    StunPrint(client->logUserData, client->Log_cb, StunInfoCategory_Trace,
              "<STUNCLIENT:%02d> Retrans %s Retry: %d to %s",
              trans->inst, errStr, trans->retransmits + 1, peer);
    RetransmitLastReq(trans, &trans->stunBindReq.serverAddr);
    StartNextRetransmitTimer(trans);
    trans->retransmits++;
    trans->stats.Retransmits++;
  }
  else
  {
    CallBack(trans, stunResult);
    SetNextState(trans, FailedState);
    trans->stats.Failures++;
  }
}
Exemple #13
0
static void
BindRespCallback(STUN_TRANSACTION_DATA* trans,
                 const struct sockaddr* srcAddr)
{
  STUN_CLIENT_DATA*  client = trans->client;
  char               ip_str [SOCKADDR_MAX_STRLEN];
  StunCallBackData_T res;

  memset( &res, 0, sizeof (StunCallBackData_T) );

  memcpy( &res.msgId, &trans->stunBindReq.transactionId, sizeof(StunMsgId) );

  res.stunResult = StunResult_BindOk;

  sockaddr_copy( (struct sockaddr*)&res.rflxAddr,
                 (struct sockaddr*)&trans->rflxAddr );

  sockaddr_copy( (struct sockaddr*)&res.srcAddr,
                 srcAddr );

  sockaddr_copy( (struct sockaddr*)&res.dstBaseAddr,
                 (struct sockaddr*)&trans->stunBindReq.baseAddr );

  /* So did we loose a packet, or got an answer to the first response?*/

  res.rtt = getRTTvalue(trans);
  res.ttl = trans->stunBindReq.ttl;

  StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Info,
             "<STUNCLIENT:%02d> BindResp from src: %s",
             trans->inst,
             sockaddr_toString( (struct sockaddr*) &res.srcAddr, ip_str,
                                SOCKADDR_MAX_STRLEN,
                                true ) );

  if (trans->stunBindReq.stunCbFunc)
  {
    (trans->stunBindReq.stunCbFunc)(trans->stunBindReq.userCtx, &res);
  }
}
Exemple #14
0
int createLocalUDPSocket(int ai_family,
                         const struct sockaddr *localIp,
                         struct sockaddr *hostaddr,
                         uint16_t port)
{
    int sockfd;

    int rv;
    int yes = 1;
    struct addrinfo hints, *ai, *p;
    char addr[SOCKADDR_MAX_STRLEN];
    char service[8];

    sockaddr_toString(localIp, addr, sizeof addr, false);

    //itoa(port, service, 10);

    snprintf(service, 8, "%d", port);
    //snprintf(service, 8, "%d", 3478);

        
    // get us a socket and bind it
    memset(&hints, 0, sizeof hints);
    hints.ai_family = ai_family;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_NUMERICHOST | AI_ADDRCONFIG;

    
    if ((rv = getaddrinfo(addr, service, &hints, &ai)) != 0) {
        fprintf(stderr, "selectserver: %s ('%s')\n", gai_strerror(rv), addr);
        exit(1);
    }

    for (p = ai; p != NULL; p = p->ai_next) {

        if (sockaddr_isAddrAny(p->ai_addr) ){
            //printf("Ignoring any\n");
            continue;

        }
        
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                             p->ai_protocol)) == -1) {
            perror("client: socket");
            continue;
        }

                
                    
        if (bind(sockfd, p->ai_addr, p->ai_addrlen) < 0) {
            printf("Bind failed\n");
            close(sockfd);
            continue;
        }
    
        if (localIp != NULL){
            struct sockaddr_storage ss;
            socklen_t len = sizeof(ss);

            if (getsockname(sockfd, (struct sockaddr *)&ss, &len) == -1){
                perror("getsockname");
            }
            else{
                if (ss.ss_family == AF_INET) {
                    ((struct sockaddr_in *)p->ai_addr)->sin_port = ((struct sockaddr_in *)&ss)->sin_port;
                }else{
                    ((struct sockaddr_in6 *)p->ai_addr)->sin6_port = ((struct sockaddr_in6 *)&ss)->sin6_port;
                }
            }
            
            
            sockaddr_copy(hostaddr, p->ai_addr);
            
            
            //printf("Bound to: '%s'\n",
            //       sockaddr_toString(localIp, addr, sizeof(addr), true));
            
        }
        
        break;
    }
    
    return sockfd;
}
Exemple #15
0
void
sendPacket(void*                  ctx,
           int                    sockHandle,
           const uint8_t*         buf,
           int                    bufLen,
           const struct sockaddr* dstAddr,
           int                    proto,
           bool                   useRelay,
           uint8_t                ttl)
{
  int32_t numbytes;
  /* char addrStr[SOCKADDR_MAX_STRLEN]; */
  uint32_t sock_ttl;
  uint32_t addr_len;
  (void) ctx;
  (void) proto; /* Todo: Sanity check? */
  (void) useRelay;

  if (dstAddr->sa_family == AF_INET)
  {
    addr_len = sizeof(struct sockaddr_in);
  }
  else
  {
    addr_len = sizeof(struct sockaddr_in6);
  }

  if (ttl > 0)
  {
    /*Special TTL, set it send packet and set it back*/
    int          old_ttl;
    unsigned int optlen;
    if (dstAddr->sa_family == AF_INET)
    {
      getsockopt(sockHandle, IPPROTO_IP, IP_TTL, &old_ttl, &optlen);
    }
    else
    {
      getsockopt(sockHandle, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &old_ttl,
                 &optlen);
    }

    sock_ttl = ttl;


    if (dstAddr->sa_family == AF_INET)
    {
      setsockopt( sockHandle, IPPROTO_IP, IP_TTL, &sock_ttl, sizeof(sock_ttl) );
    }
    else
    {
      setsockopt( sockHandle, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &sock_ttl,
                  sizeof(sock_ttl) );
    }

    if ( ( numbytes =
             sendto(sockHandle, buf, bufLen, 0, dstAddr, addr_len) ) == -1 )
    {
      perror("Stun sendto");
      exit(1);
    }
    if (dstAddr->sa_family == AF_INET)
    {
      setsockopt(sockHandle, IPPROTO_IP, IP_TTL, &old_ttl, optlen);
    }
    else
    {
      setsockopt(sockHandle, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &old_ttl, optlen);
    }


  }
  else
  {
    /*Nothing special, just send the packet*/
    if ( ( numbytes =
             sendto(sockHandle, buf, bufLen, 0, dstAddr, addr_len) ) == -1 )
    {
      perror("Stun sendto");
      char addr[SOCKADDR_MAX_STRLEN];
      sockaddr_toString(dstAddr,
                        addr,
                        sizeof(addr),
                        true);
      printf("Trying to send to: %s\n", addr);
      exit(1);
    }
  }
}