+ this->OnError(I_ERR_BIND);
+ log(DEBUG,"BindSocket() error %s",strerror(errno));
+ return;
+ }
+ else
+ {
+ this->state = I_LISTENING;
+ ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+ socket_ref[this->fd] = this;
+ log(DEBUG,"New socket now in I_LISTENING state");
+ return;
+ }
+ }
+ }
+ else
+ {
+ this->host = ahost;
+ this->port = aport;
+
+ if (!inet_aton(host.c_str(),&addy))
+ {
+ log(DEBUG,"Attempting to resolve %s",this->host.c_str());
+ /* Its not an ip, spawn the resolver */
+ this->dns.SetNS(std::string(Config->DNSServer));
+ this->dns.ForwardLookupWithFD(host,fd);
+ timeout_end = time(NULL) + maxtime;
+ timeout = false;
+ this->state = I_RESOLVING;
+ socket_ref[this->fd] = this;
+ }
+ else
+ {
+ log(DEBUG,"No need to resolve %s",this->host.c_str());
+ this->IP = host;
+ timeout_end = time(NULL) + maxtime;
+ this->DoConnect();
+ }
+ }
+}
+
+void InspSocket::SetQueues(int nfd)
+{
+ // attempt to increase socket sendq and recvq as high as its possible
+ int sendbuf = 32768;
+ int recvbuf = 32768;
+ setsockopt(nfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
+ setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
+}
+
+bool InspSocket::DoResolve()
+{
+ log(DEBUG,"In DoResolve(), trying to resolve IP");
+ if (this->dns.HasResult())
+ {
+ log(DEBUG,"Socket has result");
+ std::string res_ip = dns.GetResultIP();
+ if (res_ip != "")
+ {
+ log(DEBUG,"Socket result set to %s",res_ip.c_str());
+ this->IP = res_ip;
+ socket_ref[this->fd] = NULL;
+ }
+ else
+ {
+ log(DEBUG,"Socket DNS failure");
+ this->Close();
+ this->state = I_ERROR;
+ this->OnError(I_ERR_RESOLVE);
+ return false;
+ }
+ return this->DoConnect();
+ }
+ log(DEBUG,"No result for socket yet!");
+ return true;
+}
+
+bool InspSocket::DoConnect()
+{
+ log(DEBUG,"In DoConnect()");
+ if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ {
+ log(DEBUG,"Cant socket()");
+ this->state = I_ERROR;
+ this->OnError(I_ERR_SOCKET);
+ return false;
+ }
+
+ log(DEBUG,"Part 2 DoConnect() %s",this->IP.c_str());
+ inet_aton(this->IP.c_str(),&addy);
+ addr.sin_family = AF_INET;
+ addr.sin_addr = addy;
+ addr.sin_port = htons(this->port);
+
+ int flags;
+ flags = fcntl(this->fd, F_GETFL, 0);
+ fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
+
+ if (connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
+ {
+ if (errno != EINPROGRESS)
+ {
+ log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
+ this->OnError(I_ERR_CONNECT);
+ this->state = I_ERROR;
+ this->Close();
+ return false;
+ }