- if ((this->fd = OpenTCPSocket(host)) == ERROR)
- {
- this->fd = -1;
- this->state = I_ERROR;
- this->OnError(I_ERR_SOCKET);
- return;
- }
- else
- {
- if (!SI->BindSocket(this->fd,aport,(char*)ipaddr.c_str()))
- {
- this->Close();
- this->fd = -1;
- this->state = I_ERROR;
- this->OnError(I_ERR_BIND);
- this->ClosePending = true;
- return;
- }
- else
- {
- this->state = I_LISTENING;
- if (this->fd > -1)
- {
- if (!this->Instance->SE->AddFd(this))
- {
- this->Close();
- this->state = I_ERROR;
- this->OnError(I_ERR_NOMOREFDS);
- }
- }
- return;
- }
- }
- }
- else
- {
- strlcpy(this->host,ipaddr.c_str(),MAXBUF);
- this->port = aport;
-
- bool ipvalid = true;
-#ifdef IPV6
- if (strchr(host,':'))
- {
- in6_addr n;
- if (inet_pton(AF_INET6, host, &n) < 1)
- ipvalid = false;
- }
- else
- {
- in_addr n;
- if (inet_aton(host,&n) < 1)
- ipvalid = false;
- }
-#else
- in_addr n;
- if (inet_aton(host,&n) < 1)
- ipvalid = false;
-#endif
- if (!ipvalid)
- {
- this->Instance->Log(DEBUG,"BUG: Hostname passed to InspSocket, rather than an IP address!");
- this->Close();
- this->fd = -1;
- this->state = I_ERROR;
- this->OnError(I_ERR_RESOLVE);
- return;
- }
- else
- {
- strlcpy(this->IP,host,MAXBUF);
- timeout_val = maxtime;
- if (!this->DoConnect())
- {
- this->Close();
- this->fd = -1;
- this->state = I_ERROR;
- this->OnError(I_ERR_CONNECT);
- return;
- }
- }