| @@ -1,36 +1,36 @@ | | | @@ -1,36 +1,36 @@ |
1 | $NetBSD: patch-aj,v 1.1 2002/01/10 16:43:29 skrll Exp $ | | 1 | $NetBSD: patch-aj,v 1.2 2011/12/05 22:46:12 joerg Exp $ |
2 | | | 2 | |
3 | --- uquake/net_udp.c.orig Mon Jan 10 16:59:39 2000 | | 3 | --- uquake/net_udp.c.orig 2000-01-10 16:59:39.000000000 +0000 |
4 | +++ uquake/net_udp.c | | 4 | +++ uquake/net_udp.c |
5 | @@ -32,12 +32,17 @@ | | 5 | @@ -32,12 +32,17 @@ Foundation, Inc., 59 Temple Place - Suit |
6 | #include <sys/param.h> | | 6 | #include <sys/param.h> |
7 | #include <sys/ioctl.h> | | 7 | #include <sys/ioctl.h> |
8 | #include <errno.h> | | 8 | #include <errno.h> |
9 | +#include <net/if.h> | | 9 | +#include <net/if.h> |
10 | | | 10 | |
11 | #ifdef __sun__ | | 11 | #ifdef __sun__ |
12 | #include <sys/filio.h> | | 12 | #include <sys/filio.h> |
13 | #undef model_t | | 13 | #undef model_t |
14 | #endif | | 14 | #endif |
15 | | | 15 | |
16 | +#ifndef INADDR_LOOPBACK | | 16 | +#ifndef INADDR_LOOPBACK |
17 | +#define INADDR_LOOPBACK (u_int32_t)0x7f000001 | | 17 | +#define INADDR_LOOPBACK (u_int32_t)0x7f000001 |
18 | +#endif | | 18 | +#endif |
19 | + | | 19 | + |
20 | #if defined(sgi) && defined(sa_family) | | 20 | #if defined(sgi) && defined(sa_family) |
21 | /* Get rid of problematic SGI #def */ | | 21 | /* Get rid of problematic SGI #def */ |
22 | #undef sa_family | | 22 | #undef sa_family |
23 | @@ -62,6 +67,91 @@ | | 23 | @@ -62,6 +67,91 @@ static unsigned long myAddr; |
24 | #include "net_udp.h" | | 24 | #include "net_udp.h" |
25 | | | 25 | |
26 | //============================================================================= | | 26 | //============================================================================= |
27 | +/* get any of my non-loopback addr. */ | | 27 | +/* get any of my non-loopback addr. */ |
28 | +static int | | 28 | +static int |
29 | +grab_myaddr(family, sa) | | 29 | +grab_myaddr(family, sa) |
30 | + int family; | | 30 | + int family; |
31 | + struct sockaddr *sa; | | 31 | + struct sockaddr *sa; |
32 | +{ | | 32 | +{ |
33 | + int s; | | 33 | + int s; |
34 | + unsigned int maxif; | | 34 | + unsigned int maxif; |
35 | + struct ifreq *iflist; | | 35 | + struct ifreq *iflist; |
36 | + struct ifconf ifconf; | | 36 | + struct ifconf ifconf; |
| @@ -102,82 +102,82 @@ $NetBSD: patch-aj,v 1.1 2002/01/10 16:43 | | | @@ -102,82 +102,82 @@ $NetBSD: patch-aj,v 1.1 2002/01/10 16:43 |
102 | + else { | | 102 | + else { |
103 | + printf("non-printable local address, family %d\n", | | 103 | + printf("non-printable local address, family %d\n", |
104 | + family); | | 104 | + family); |
105 | + } | | 105 | + } |
106 | + } | | 106 | + } |
107 | + break; | | 107 | + break; |
108 | + } | | 108 | + } |
109 | + | | 109 | + |
110 | + free(iflist); | | 110 | + free(iflist); |
111 | +} | | 111 | +} |
112 | | | 112 | |
113 | int UDP_Init (void) | | 113 | int UDP_Init (void) |
114 | { | | 114 | { |
115 | @@ -74,9 +164,18 @@ | | 115 | @@ -74,9 +164,18 @@ int UDP_Init (void) |
116 | return -1; | | 116 | return -1; |
117 | | | 117 | |
118 | // determine my name & address | | 118 | // determine my name & address |
119 | + memset(&myAddr, 0, sizeof(myAddr)); | | 119 | + memset(&myAddr, 0, sizeof(myAddr)); |
120 | +#if 0 | | 120 | +#if 0 |
121 | gethostname(buff, MAXHOSTNAMELEN); | | 121 | gethostname(buff, MAXHOSTNAMELEN); |
122 | local = gethostbyname(buff); | | 122 | local = gethostbyname(buff); |
123 | myAddr = *(int *)local->h_addr_list[0]; | | 123 | myAddr = *(int *)local->h_addr_list[0]; |
124 | +#else | | 124 | +#else |
125 | + { | | 125 | + { |
126 | + struct sockaddr_in sin; | | 126 | + struct sockaddr_in sin; |
127 | + if (grab_myaddr(AF_INET, (struct sockaddr *)&sin) == 0) | | 127 | + if (grab_myaddr(AF_INET, (struct sockaddr *)&sin) == 0) |
128 | + memcpy(&myAddr, &sin.sin_addr, sizeof(myAddr)); | | 128 | + memcpy(&myAddr, &sin.sin_addr, sizeof(myAddr)); |
129 | + } | | 129 | + } |
130 | +#endif | | 130 | +#endif |
131 | | | 131 | |
132 | // if the quake hostname isn't set, set it to the machine name | | 132 | // if the quake hostname isn't set, set it to the machine name |
133 | if (Q_strcmp(hostname.string, "UNNAMED") == 0) | | 133 | if (Q_strcmp(hostname.string, "UNNAMED") == 0) |
134 | @@ -137,25 +236,45 @@ | | 134 | @@ -137,25 +236,45 @@ void UDP_Listen (qboolean state) |
135 | | | 135 | |
136 | int UDP_OpenSocket (int port) | | 136 | int UDP_OpenSocket (int port) |
137 | { | | 137 | { |
138 | + struct addrinfo hints, *res = NULL; | | 138 | + struct addrinfo hints, *res = NULL; |
139 | + int error; | | 139 | + int error; |
140 | + char pbuf[NI_MAXSERV]; | | 140 | + char pbuf[NI_MAXSERV]; |
141 | int newsocket; | | 141 | int newsocket; |
142 | - struct sockaddr_in address; | | 142 | - struct sockaddr_in address; |
143 | qboolean _true = true; | | 143 | qboolean _true = true; |
144 | + const int false = 0; | | 144 | + const int wrong = 0; |
145 | | | 145 | |
146 | - if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) | | 146 | - if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) |
147 | + snprintf(pbuf, sizeof(pbuf), "%d", port); | | 147 | + snprintf(pbuf, sizeof(pbuf), "%d", port); |
148 | + memset(&hints, 0, sizeof(hints)); | | 148 | + memset(&hints, 0, sizeof(hints)); |
149 | + hints.ai_family = PF_INET6; | | 149 | + hints.ai_family = PF_INET6; |
150 | + hints.ai_socktype = SOCK_DGRAM; | | 150 | + hints.ai_socktype = SOCK_DGRAM; |
151 | + hints.ai_flags = AI_PASSIVE; | | 151 | + hints.ai_flags = AI_PASSIVE; |
152 | + error = getaddrinfo(NULL, pbuf, &hints, &res); | | 152 | + error = getaddrinfo(NULL, pbuf, &hints, &res); |
153 | + if (error) | | 153 | + if (error) |
154 | return -1; | | 154 | return -1; |
155 | | | 155 | |
156 | - if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1) | | 156 | - if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1) |
157 | + if ((newsocket = socket (res->ai_family, res->ai_socktype, | | 157 | + if ((newsocket = socket (res->ai_family, res->ai_socktype, |
158 | + res->ai_protocol)) < 0) | | 158 | + res->ai_protocol)) < 0) |
159 | + return -1; | | 159 | + return -1; |
160 | + | | 160 | + |
161 | + if (ioctl (newsocket, FIONBIO, (char *)&_true) < 0) | | 161 | + if (ioctl (newsocket, FIONBIO, (char *)&_true) < 0) |
162 | goto ErrorReturn; | | 162 | goto ErrorReturn; |
163 | | | 163 | |
164 | - address.sin_family = AF_INET; | | 164 | - address.sin_family = AF_INET; |
165 | - address.sin_addr.s_addr = INADDR_ANY; | | 165 | - address.sin_addr.s_addr = INADDR_ANY; |
166 | - address.sin_port = htons(port); | | 166 | - address.sin_port = htons(port); |
167 | - if( bind (newsocket, (void *)&address, sizeof(address)) == -1) | | 167 | - if( bind (newsocket, (void *)&address, sizeof(address)) == -1) |
168 | +#ifdef IPV6_BINDV6ONLY | | 168 | +#ifdef IPV6_BINDV6ONLY |
169 | + if (setsockopt(newsocket, IPPROTO_IPV6, IPV6_BINDV6ONLY, &false, | | 169 | + if (setsockopt(newsocket, IPPROTO_IPV6, IPV6_BINDV6ONLY, &wrong, |
170 | + sizeof(false)) < 0) { | | 170 | + sizeof(wrong)) < 0) { |
171 | + /* I don't care */ | | 171 | + /* I don't care */ |
172 | + } | | 172 | + } |
173 | +#endif | | 173 | +#endif |
174 | + | | 174 | + |
175 | + if (bind (newsocket, res->ai_addr, res->ai_addrlen) < 0) | | 175 | + if (bind (newsocket, res->ai_addr, res->ai_addrlen) < 0) |
176 | goto ErrorReturn; | | 176 | goto ErrorReturn; |
177 | | | 177 | |
178 | + freeaddrinfo(res); | | 178 | + freeaddrinfo(res); |
179 | return newsocket; | | 179 | return newsocket; |
180 | | | 180 | |
181 | ErrorReturn: | | 181 | ErrorReturn: |
182 | + if (res) | | 182 | + if (res) |
183 | + freeaddrinfo(res); | | 183 | + freeaddrinfo(res); |