- Instance->Log(DEBUG,"BindAddr(%s)", ip.c_str());
- ConfigReader Conf(this->Instance);
- bool bindfail = false;
- socklen_t size = sizeof(sockaddr_in);
-#ifdef IPV6
- bool v6 = false;
- /* Are we looking for a binding to fit an ipv6 host? */
- if ((!*this->host) || strchr(this->host, ':'))
- v6 = true;
-#endif
- int j = 0;
- while (j < Conf.Enumerate("bind") || (!ip.empty()))
- {
- std::string IP = ip.empty() ? Conf.ReadValue("bind","address",j) : ip;
- if (!ip.empty() || Conf.ReadValue("bind","type",j) == "servers")
- {
- if (!ip.empty() || ((IP != "*") && (IP != "127.0.0.1") && (IP != "") && (IP != "::1")))
- {
- sockaddr* s = new sockaddr[2];
-#ifdef IPV6
- if (v6)
- {
- in6_addr n;
- if (inet_pton(AF_INET6, IP.c_str(), &n) > 0)
- {
- memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(n));
- ((sockaddr_in6*)s)->sin6_family = AF_INET6;
- size = sizeof(sockaddr_in6);
- }
- else
- bindfail = true;
- }
- else
- {
- in_addr n;
- if (inet_aton(IP.c_str(), &n) > 0)
- {
- ((sockaddr_in*)s)->sin_addr = n;
- ((sockaddr_in*)s)->sin_family = AF_INET;
- }
- else
- bindfail = true;
- }
-#else
- in_addr n;
- if (insp_aton(IP.c_str(), &n) > 0)
- {
- ((sockaddr_in*)s)->sin_addr = n;
- ((sockaddr_in*)s)->sin_family = AF_INET;
- }
- else
- bindfail = true;
-#endif
- if (bindfail)
- {
- delete[] s;
- return false;
- }
-
- if (bind(this->fd, s, size) < 0)
- {
- this->state = I_ERROR;
- this->OnError(I_ERR_BIND);
- this->fd = -1;
- delete[] s;
- return false;
- }