-
-InspSocket* socket_ref[MAX_DESCRIPTORS];
-
-InspSocket::InspSocket()
-{
- this->state = I_DISCONNECTED;
- this->fd = -1;
-}
-
-InspSocket::InspSocket(int newfd, char* ip)
-{
- this->fd = newfd;
- this->state = I_CONNECTED;
- this->IP = ip;
- ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
- socket_ref[this->fd] = this;
-}
-
-InspSocket::InspSocket(const std::string &ahost, int aport, bool listening, unsigned long maxtime)
- : fd(-1), host(ahost)
-{
- this->outbuffer.clear();
- if (listening) {
- if ((this->fd = OpenTCPSocket()) == ERROR)
- {
- this->fd = -1;
- this->state = I_ERROR;
- this->OnError(I_ERR_SOCKET);
- log(DEBUG,"OpenTCPSocket() error");
- return;
- }
- else
- {
- if (BindSocket(this->fd,this->client,this->server,aport,(char*)ahost.c_str()) == ERROR)
- {
- this->Close();
- this->fd = -1;
- this->state = I_ERROR;
- 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
+extern int openSockfd[MAX_DESCRIPTORS];
+
+/** This will bind a socket to a port. It works for UDP/TCP.
+ * If a hostname is given to bind to, the function will first
+ * attempt to resolve the hostname, then bind to the IP the
+ * hostname resolves to. This is a blocking lookup blocking for
+ * a maximum of one second before it times out, using the DNS
+ * server specified in the configuration file.
+ */
+bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
+{
+ memset(&server,0,sizeof(server));
+ insp_inaddr addy;
+ bool resolved = false;
+ char resolved_addr[128];
+
+ if (*addr == '*')
+ *addr = 0;
+
+ if (*addr && !inet_aton(addr,&addy))