+/* Most irc servers require you to specify the ip you want to bind to.
+ * If you dont specify an IP, they rather dumbly bind to the first IP
+ * of the box (e.g. INADDR_ANY). In InspIRCd, we scan thought the IP
+ * addresses we've bound server ports to, and we try and bind our outbound
+ * connections to the first usable non-loopback and non-any IP we find.
+ * This is easier to configure when you have a lot of links and a lot
+ * of servers to configure.
+ */
+bool InspSocket::BindAddr()
+{
+ insp_inaddr n;
+ ConfigReader Conf;
+
+ log(DEBUG,"In InspSocket::BindAddr()");
+ for (int j =0; j < Conf.Enumerate("bind"); j++)
+ {
+ std::string Type = Conf.ReadValue("bind","type",j);
+ std::string IP = Conf.ReadValue("bind","address",j);
+ if (Type == "servers")
+ {
+ if ((IP != "*") && (IP != "127.0.0.1") && (IP != ""))
+ {
+ insp_sockaddr s;
+ char resolved_addr[MAXBUF];
+
+ if (!inet_aton(IP.c_str(),&n))
+ {
+ /* If they gave a hostname, bind to the IP it resolves to */
+ log(DEBUG,"Resolving host %s",IP.c_str());
+ if (CleanAndResolve(resolved_addr, IP.c_str(), true))
+ {
+ log(DEBUG,"Resolved host %s to %s",IP.c_str(),resolved_addr);
+ IP = resolved_addr;
+ }
+ }
+
+ if (inet_aton(IP.c_str(),&n))
+ {
+ log(DEBUG,"Found an IP to bind to: %s",IP.c_str());
+ s.sin_addr = n;
+ s.sin_family = AF_INET;
+ if (bind(this->fd,(struct sockaddr*)&s,sizeof(s)) < 0)
+ {
+ log(DEBUG,"Cant bind()");
+ this->state = I_ERROR;
+ this->OnError(I_ERR_BIND);
+ this->fd = -1;
+ return false;
+ }
+ log(DEBUG,"bind() reports outbound fd bound to ip %s",IP.c_str());
+ return true;
+ }
+ else
+ {
+ log(DEBUG,"Address '%s' was not an IP address",IP.c_str());
+ }
+ }
+ }
+ }
+ log(DEBUG,"Found no suitable IPs to bind, binding INADDR_ANY");
+ return true;
+}
+