//params: s=whole ip:port string, ip=output for ip number only, port=output for port number integer int parseIpAddress(const char *s, char *ip, unsigned short *port) { const char *ip_end, *port_start; //ip_end and port_start are pointers to memory area of s, not offsets or indexes to s if (validateIpAddress(s, &ip_end, &port_start) == -1) return -1; int IPAddrLen=ip_end - s; if(IPAddrLen<7 || IPAddrLen>15 )//check length before writing to *ip return -1; memcpy(ip, s, IPAddrLen); ip[IPAddrLen] = '\0'; if (port_start) { *port = 0; while (*port_start) { *port = *port * 10 + (*port_start - '0'); ++port_start; } } return 0; }
void Machine::setMachineIP(QString ip){ if(!validateIpAddress(ip)){ return; } else{ machineIP = ip; } }
void TcpClient::doConnect(QString host, quint16 port) { //MainWindow *ui = new MainWindow; //MainWindow *ui = new MainWindow(); socket = new QTcpSocket(this); //connect(socket, SIGNAL(connected()), ui, SLOT(connectedToServer())); connect(socket, SIGNAL(connected()), this, SLOT(connected())); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64))); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(displayState(QAbstractSocket::SocketState))); if(validateIpAddress(host)) { qDebug() << "Connecting to " + host + ":" + QString::number(port); // this is not blocking call socket->connectToHost(host, port); // we need to wait... if(!socket->waitForConnected(5000)) { qDebug() << "Error: " << socket->errorString(); } } else { QMessageBox::warning(NULL, "Error", "This IP address is invalid."); qDebug() << "Invalid ip address: " + host; } }
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; }