+
+int BindPorts()
+{
+ char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
+ sockaddr_in client,server;
+ int clientportcount = 0;
+ int BoundPortCount = 0;
+ for (int count = 0; count < Config->ConfValueEnum("bind",&Config->config_f); count++)
+ {
+ Config->ConfValue("bind","port",count,configToken,&Config->config_f);
+ Config->ConfValue("bind","address",count,Addr,&Config->config_f);
+ Config->ConfValue("bind","type",count,Type,&Config->config_f);
+ if ((!*Type) || (!strcmp(Type,"clients")))
+ {
+ // modules handle server bind types now
+ Config->ports[clientportcount] = atoi(configToken);
+
+ // If the client put bind "*", this is an unrealism.
+ // We don't actually support this as documented, but
+ // i got fed up of people trying it, so now it converts
+ // it to an empty string meaning the same 'bind to all'.
+ if (*Addr == '*')
+ *Addr = 0;
+
+ strlcpy(Config->addrs[clientportcount],Addr,256);
+ clientportcount++;
+ log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
+ }
+ }
+ int PortCount = clientportcount;
+
+ for (int count = 0; count < PortCount; count++)
+ {
+ if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
+ {
+ log(DEBUG,"InspIRCd: startup: bad fd %lu binding port [%s:%d]",(unsigned long)openSockfd[BoundPortCount],Config->addrs[count],(unsigned long)Config->ports[count]);
+ return(ERROR);
+ }
+ if (BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]) == ERROR)
+ {
+ log(DEFAULT,"InspIRCd: startup: failed to bind port [%s:%lu]: %s",Config->addrs[count],(unsigned long)Config->ports[count],strerror(errno));
+ }
+ else /* well we at least bound to one socket so we'll continue */
+ {
+ BoundPortCount++;
+ }
+ }
+
+ /* if we didn't bind to anything then abort */
+ if (!BoundPortCount)
+ {
+ log(DEFAULT,"InspIRCd: startup: no ports bound, bailing!");
+ printf("\nERROR: Was not able to bind any of %lu ports! Please check your configuration.\n\n", (unsigned long)PortCount);
+ return (ERROR);
+ }
+
+ return BoundPortCount;
+}
+