Example #1
0
int main(int argc,char **argv){
	int p;
	t a1;
	int array[4];
	int i=0;
	p = isValidIpAddress(argv[1]);
	printf(" Valid ? %d \n",p);
	parseIpAddress(argv[1],&a1.array);
	for(i=0;i<4;i++)
		printf("%d\n",a1.array[i]);
	return 0;

}
Example #2
0
bool LogType::validate()
{
    writeInfo ( "config FILE = %s", getConfigFile().c_str());

    // check if config file exists
    if( !fileExists(getConfigFile()) )
    {
        writeError ("Can't find config FILE = %s", getConfigFile().c_str());
        return false;
    }

    // check if config file can read
    if( !fileReadable(getConfigFile()) )
    {
        writeError ("Can't read from config FILE = %s", getConfigFile().c_str());
        return false;
    }

    // check if config file exists
    if( !iniParse(getConfigFile()) )
    {
        writeError ("Can't parse config FILE = %s", getConfigFile().c_str());
        return false;
    }

    bool result = parseStorageType()
                    && parseIpAddress()
                    && parseRemotePort()
                    && parseSourcePort()
                    && parseUdpBufferSize();

    if (result) {
        __udp_socket = new boost::asio::ip::udp::socket(
            __io_service,
            boost::asio::ip::udp::endpoint(
                boost::asio::ip::address::from_string(getIpAddress()),
                getSourcePort()));
        __udp_remote_endpoint = new boost::asio::ip::udp::endpoint(
            boost::asio::ip::udp::endpoint(
                boost::asio::ip::address::from_string(getIpAddress()),
                getRemotePort()));
    }

    return result;
}
smBusdevicePointer tcpipPortOpen(const char * devicename, smint32 baudrate_bps, smbool *success)
{
    int sockfd;
    struct sockaddr_in server;
    struct timeval tv;
    fd_set myset;
    int res, valopt;
    socklen_t lon;
    unsigned long arg;

    *success=smfalse;

    if (validateIpAddress(devicename, NULL, NULL) != 0)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: device name '%s' does not appear to be IP address, skipping TCP/IP open attempt (note: this is normal if opening a non-TCP/IP port)\n",devicename);
        return SMBUSDEVICE_RETURN_ON_OPEN_FAIL;
    }

    char ip_addr[16];
    unsigned short port = 4001;
    if (parseIpAddress(devicename, ip_addr, &port) < 0)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: IP address parse failed\n");
        return SMBUSDEVICE_RETURN_ON_OPEN_FAIL;
    }


    if(baudrate_bps!=SM_BAUDRATE)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: Non-default baudrate not supported by TCP/IP protocol\n");
        return SMBUSDEVICE_RETURN_ON_OPEN_FAIL;
    }

    smDebug(-1,SMDebugLow,"TCP/IP: Attempting to connect to %s:%d\n",ip_addr,port);

#if defined(_WIN32)
    initwsa();
#endif

    //Create socket
    sockfd = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
    if (sockfd == -1)
    {
        smDebug(-1,SMDebugLow,"TCP/IP: Socket open failed\n");
        return SMBUSDEVICE_RETURN_ON_OPEN_FAIL;
    }

    // Set OFF NAGLE algorithm to disable stack buffering of small packets
    int one = 1;
    setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&one, sizeof(one));

    server.sin_addr.s_addr = inet_addr(ip_addr);
    server.sin_family = AF_INET;
    server.sin_port = htons(port);

    // Set non-blocking when trying to establish the connection
#if !defined(_WIN32)
    arg = fcntl(sockfd, F_GETFL, NULL);
    arg |= O_NONBLOCK;
    fcntl(sockfd, F_SETFL, arg);
#else
    arg = 1;
    ioctlsocket(sockfd, FIONBIO, &arg);
#endif

    res = connect(sockfd, (struct sockaddr *)&server, sizeof(server));

    if (res < 0)
    {
        if (errno == EINPROGRESS)
        {
            tv.tv_sec = 5;
            tv.tv_usec = 0;
            FD_ZERO(&myset);
            FD_SET((unsigned int)sockfd, &myset);
            if (select(sockfd+1, NULL, &myset, NULL, &tv) > 0)
            {
                lon = sizeof(int);
                getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
                if (valopt)
                {
                    smDebug(-1,SMDebugLow,"TCP/IP: Setting socket properties failed\n");
                    return SMBUSDEVICE_RETURN_ON_OPEN_FAIL;
                }
            }
            else
            {
               smDebug(-1,SMDebugLow,"TCP/IP: Setting socket properties failed\n");
               return SMBUSDEVICE_RETURN_ON_OPEN_FAIL;
            }
        }
        else
        {
            smDebug(-1,SMDebugLow,"TCP/IP: Connecting socket failed\n");
            return SMBUSDEVICE_RETURN_ON_OPEN_FAIL;
        }
    }

    // Set to blocking mode again
#if !defined(_WIN32)
    arg = fcntl(sockfd, F_GETFL, NULL);
    arg &= (~O_NONBLOCK);
    fcntl(sockfd, F_SETFL, arg);
#else
    arg = 0;
    ioctlsocket(sockfd, FIONBIO, &arg);
#endif

    *success=smtrue;
    return (smBusdevicePointer)sockfd;
}