-
- if (ipv6) {
- dht_socket6 = socket(PF_INET6, SOCK_DGRAM, 0);
- if (dht_socket6 < 0)
- perror("socket(IPv6)");
- }
-
- if (dht_socket < 0 && dht_socket6 < 0) {
- fprintf(stderr, "Eek!\n");
- exit(1);
- }
-
- if (dht_socket >= 0) {
- struct sockaddr_in sin;
- int rc;
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_port = htons(port);
- rc = bind(dht_socket, (struct sockaddr *)&sin, sizeof(sin));
- if (rc < 0) {
- perror("bind(IPv4)");
- exit(1);
- }
-
- rc = fcntl(dht_socket, F_GETFL, 0);
- if (rc < 0) {
- perror("F_GETFL");
- exit(1);
- }
-
- rc = fcntl(dht_socket, F_SETFL, (rc | O_NONBLOCK));
- if (rc < 0) {
- perror("F_SETFL");
- exit(1);
- }
- }
-
- if (dht_socket6 >= 0) {
- struct sockaddr_in6 sin6;
- int rc;
- int val = 1;
-
- rc = setsockopt(
- dht_socket6, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&val, sizeof(val));
- if (rc < 0) {
- perror("setsockopt(IPV6_V6ONLY)");
- exit(1);
- }
-
- /* BEP-32 mandates that we should bind this socket to one of our
- global IPv6 addresses. In this program, this only happens if
- the user used the -b flag. */
-
- memset(&sin6, 0, sizeof(sin6));
- sin6.sin6_family = AF_INET6;
- sin6.sin6_port = htons(port);
- rc = bind(dht_socket6, (struct sockaddr *)&sin6, sizeof(sin6));
- if (rc < 0) {
- perror("bind(IPv6)");
- exit(1);
- }
-
- rc = fcntl(dht_socket6, F_GETFL, 0);
- if (rc < 0) {
- perror("F_GETFL");
- exit(1);
- }
-
- rc = fcntl(dht_socket6, F_SETFL, (rc | O_NONBLOCK));
- if (rc < 0) {
- perror("F_SETFL");
- exit(1);
- }