- ConfigReader Conf(this->Instance);
- socklen_t size = sizeof(sockaddr_in);
-#ifdef IPV6
- bool v6 = false;
- /* Are we looking for a binding to fit an ipv6 host? */
- if ((ip.empty()) || (ip.find(':') != std::string::npos))
- v6 = true;
-#endif
- int j = 0;
- while (j < Conf.Enumerate("bind") || (!ip.empty()))
- {
- std::string sIP = ip.empty() ? Conf.ReadValue("bind","address",j) : ip;
- if (!ip.empty() || Conf.ReadValue("bind","type",j) == "servers")
- {
- if (!ip.empty() || ((sIP != "*") && (sIP != "127.0.0.1") && (!sIP.empty()) && (sIP != "::1")))
- {
- /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */
- sockaddr* s = new sockaddr[2];
-#ifdef IPV6
- if (v6)
- {
- in6_addr n;
- if (inet_pton(AF_INET6, sIP.c_str(), &n) > 0)
- {
- memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(sockaddr_in6));
- ((sockaddr_in6*)s)->sin6_port = 0;
- ((sockaddr_in6*)s)->sin6_family = AF_INET6;
- size = sizeof(sockaddr_in6);
- }
- else
- {
- delete[] s;
- j++;
- continue;
- }
- }
- else
-#endif
- {
- in_addr n;
- if (inet_aton(sIP.c_str(), &n) > 0)
- {
- ((sockaddr_in*)s)->sin_addr = n;
- ((sockaddr_in*)s)->sin_port = 0;
- ((sockaddr_in*)s)->sin_family = AF_INET;
- }
- else
- {
- delete[] s;
- j++;
- continue;
- }
- }
-
- if (Instance->SE->Bind(this->fd, s, size) < 0)
- {
- this->state = I_ERROR;
- this->OnError(I_ERR_BIND);
- this->fd = -1;
- delete[] s;
- return false;
- }