Пример #1
0
int Socket::Connect(const std::string& addr, const std::string& port)
{
    int rc;
    char str[INET6_ADDRSTRLEN];
    bool success = false;

    struct addrinfo *AddrInfo, *AI;

    AddrInfo = toAddrinfo( addr, port, false );

    for ( AI = AddrInfo; AI != NULL; AI = AI->ai_next )
    {
        struct sockaddr_in6 connectAddr;

        toIpv6( AI, &connectAddr );

        if ( ( IN6_IS_ADDR_LINKLOCAL((struct in6_addr *) &connectAddr.sin6_addr) ) &&
             ( connectAddr.sin6_scope_id == 0) )
        {
            log(LOG_WARN) << "IPv6 link local addresses should specify a scope ID!";
        }

        inet_ntop( AF_INET6, &connectAddr.sin6_addr, str, sizeof(str));
        log(LOG_NOTICE) << "attempting connect to \"" << addr << "\" -> ip " << str << " port " << ntohs(connectAddr.sin6_port);

        rc = connect( socket_->fd, (struct sockaddr*) &connectAddr, sizeof(connectAddr) );

        if (rc < 0 && errno != EINPROGRESS)
        {
            log(LOG_EMERG) << "connect attempt failed with error " << strerror(errno);
        }
        else
        {
            success = true;
            break;
        }
    }

    if ( AddrInfo )
        freeaddrinfo( AddrInfo );

    return success ? WaitForConnect() : -1;
}
Пример #2
0
int Socket::BindToAddr(const std::string& addr, const std::string& port)
{
    char str[INET6_ADDRSTRLEN];
    bool success = false;

    struct addrinfo *AddrInfo, *AI;

    AddrInfo = toAddrinfo( addr, port, true );

    for ( AI = AddrInfo; AI != NULL; AI = AI->ai_next )
    {
        struct sockaddr_in6 bindAddr;

        toIpv6( AI, &bindAddr );

        if ( ( IN6_IS_ADDR_LINKLOCAL((struct in6_addr *) &bindAddr.sin6_addr) ) &&
             ( bindAddr.sin6_scope_id == 0) )
        {
            log(LOG_WARN) << "IPv6 link local addresses should specify a scope ID!";
        }

        inet_ntop( AF_INET6, &bindAddr.sin6_addr, str, sizeof(str));
        log(LOG_NOTICE) << "attempting bind to \"" << addr << "\" -> ip " << str << " port " << ntohs(bindAddr.sin6_port);

        if ( bind( socket_->fd, (struct sockaddr*) &bindAddr, sizeof(bindAddr) ) < 0 )
        {
            log(LOG_EMERG) << "bind attempt failed with error " << strerror(errno);
        }
        else
        {
            success = true;
            break;
        }
    }

    if ( AddrInfo )
        freeaddrinfo( AddrInfo );

    return success ? 0 : -1;
}
Пример #3
0
char *ICMPv6::restHeader(){
    char *pcat = new char[450];
    char p1[75],p2[75],p3[75],p4[75],p5[75],p6[75];
    switch(tipo){
    case DESTINATION_UNREACHABLE6:
        sprintf(p1,"MTU: %d",sizeof(payload));
        sprintf(pcat,"%s\n",p1);
        return pcat;
    case ECHO_REQUEST6:
    case ECHO_REPLY6:
        unsigned int identifier,sequenceNumber;
        memcpy(&identifier,&(payload[0]),4);
        memcpy(&sequenceNumber,&(payload[4]),4);
        sprintf(p1,"Identifier: %d",identifier);
        sprintf(p2,"Sequence Number: %d",sequenceNumber);
        sprintf(p3,"Carga: %d",sizeof(payload) - 8);
        sprintf(pcat,"%s%s%s\n",p1,p2,p3);
        return pcat;
    case ROUTER_SOLICITATION:
        unsigned char curHopLimit,MOReserved;
        unsigned int routerLifetime[2];
        unsigned char reachableTime[4];
        unsigned char retransTimer[4];
        sprintf(p1,"Cur Hop Limit: %d\n",(int) curHopLimit);
        if(MOReserved & 128){
            sprintf(p1,"M (bit) activado\n");
        }else{
           sprintf(p1,"M (bit) desactivado\n");
        }
        if(MOReserved & 64){
            sprintf(p2,"O (bit) activado\n");
        }else{
           sprintf(p2,"O (bit) desactivado\n");
        }
        sprintf(p3,"Router life time: %d\n",routerLifetime);
        sprintf(p4,"Reachable time: %d\n",reachableTime);
        sprintf(p5,"Retransfer timer: %d\n",retransTimer);
        sprintf(pcat,"%s%s%s%s%s\n",p1,p2,p3,p4,p5);
        return pcat;
    case ROUTER_ADVERTISEMENT:
    case NEIGHBOR_SOLICITATION:
        unsigned char targetSolicitacion[16];
        memcpy(targetSolicitacion,&(payload[8]),16);
        toIpv6(targetSolicitacion);
        payload = &(payload[24]);
        sprintf(p1,"Carga util: %d\n",strlen(payload));
        sprintf(pcat,"%s",p1);
        return pcat;
    case NEIGHBOR_ADVERTISEMENT:
        //unsigned char targetSolicitacion[16];
        unsigned char rso;
        rso = (unsigned char) payload[0];
        //unsigned char rso = (unsigned char) payload[0];
        if(rso & 128){
            sprintf(p1,"Bit R: activadp\n");
        }else{
            sprintf(p1,"Bit R: desactivado\n");
        }
        if(rso & 64){
            sprintf(p2,"Bit S: activadp\n");
        }else{
            sprintf(p2,"Bit S: desactivado\n");
        }
        if(rso & 32){
            sprintf(p3,"Bit O: activadp\n");
        }else{
            sprintf(p3,"Bit O: desactivado\n");
        }
        memcpy(targetSolicitacion,&(payload[8]),16);
        payload = &(payload[24]);
        sprintf(p5,"Target solicitation: ");
        toIpv6(targetSolicitacion);
        sprintf(p6,"Carga util: %d\n",sizeof(payload));
        sprintf(pcat,"%s%s%s%s%s%s\n",p1,p2,p3,p4,p5,p6);
        return pcat;
    case REDIRECT6:
        unsigned char targetAddress[16];
        unsigned char destinationAdress[16];
        memcpy(targetAddress,&(payload[8]),16);
        memcpy(destinationAdress,&(payload[24]),16);
        toIpv6(targetSolicitacion);
        toIpv6(destinationAdress);
        payload = &(payload[40]);
        return pcat;
    default:
        sprintf(p1,"No tiene rest header\n");
        sprintf(pcat,"%s",p1);
        return pcat;
    }
}