23 |
|
*/ |
24 |
|
|
25 |
|
#include "stdinc.h" |
26 |
– |
#ifndef _WIN32 |
26 |
|
#include <netinet/in_systm.h> |
27 |
|
#include <netinet/ip.h> |
28 |
|
#include <netinet/tcp.h> |
29 |
< |
#endif |
29 |
> |
#include "list.h" |
30 |
|
#include "fdlist.h" |
31 |
|
#include "s_bsd.h" |
32 |
|
#include "client.h" |
34 |
– |
#include "common.h" |
33 |
|
#include "dbuf.h" |
34 |
|
#include "event.h" |
35 |
|
#include "irc_string.h" |
38 |
– |
#include "irc_getnameinfo.h" |
39 |
– |
#include "irc_getaddrinfo.h" |
36 |
|
#include "ircd.h" |
41 |
– |
#include "list.h" |
37 |
|
#include "listener.h" |
38 |
|
#include "numeric.h" |
39 |
|
#include "packet.h" |
40 |
|
#include "irc_res.h" |
46 |
– |
#include "inet_misc.h" |
41 |
|
#include "restart.h" |
42 |
|
#include "s_auth.h" |
43 |
< |
#include "s_conf.h" |
44 |
< |
#include "s_log.h" |
43 |
> |
#include "conf.h" |
44 |
> |
#include "log.h" |
45 |
|
#include "s_serv.h" |
52 |
– |
#include "s_stats.h" |
46 |
|
#include "send.h" |
47 |
|
#include "memory.h" |
48 |
|
#include "s_user.h" |
52 |
|
"Error during DNS lookup", "connect timeout", "Error during connect()", |
53 |
|
"Comm Error" }; |
54 |
|
|
55 |
< |
struct Callback *setup_socket_cb = NULL; |
63 |
< |
|
64 |
< |
static void comm_connect_callback(fde_t *fd, int status); |
55 |
> |
static void comm_connect_callback(fde_t *, int); |
56 |
|
static PF comm_connect_timeout; |
57 |
< |
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
57 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
58 |
|
static PF comm_connect_tryconnect; |
59 |
|
|
69 |
– |
extern void init_netio(void); |
60 |
|
|
61 |
|
/* check_can_use_v6() |
62 |
|
* Check if the system can open AF_INET6 sockets |
72 |
|
else |
73 |
|
{ |
74 |
|
ServerInfo.can_use_v6 = 1; |
85 |
– |
#ifdef _WIN32 |
86 |
– |
closesocket(v6); |
87 |
– |
#else |
75 |
|
close(v6); |
89 |
– |
#endif |
76 |
|
} |
77 |
|
#else |
78 |
|
ServerInfo.can_use_v6 = 0; |
88 |
|
int |
89 |
|
get_sockerr(int fd) |
90 |
|
{ |
105 |
– |
#ifndef _WIN32 |
91 |
|
int errtmp = errno; |
107 |
– |
#else |
108 |
– |
int errtmp = WSAGetLastError(); |
109 |
– |
#endif |
92 |
|
#ifdef SO_ERROR |
93 |
|
int err = 0; |
94 |
|
socklen_t len = sizeof(err); |
95 |
|
|
96 |
< |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
96 |
> |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
97 |
|
{ |
98 |
|
if (err) |
99 |
|
errtmp = err; |
127 |
|
{ |
128 |
|
who = (who) ? who : ""; |
129 |
|
|
130 |
< |
sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error)); |
131 |
< |
log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error)); |
132 |
< |
ilog(L_ERROR, text, who, strerror(error)); |
130 |
> |
sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE, |
131 |
> |
text, who, strerror(error)); |
132 |
> |
ilog(LOG_TYPE_IRCD, text, who, strerror(error)); |
133 |
|
} |
134 |
|
|
135 |
|
/* |
137 |
|
* |
138 |
|
* Set the socket non-blocking, and other wonderful bits. |
139 |
|
*/ |
140 |
< |
static void * |
141 |
< |
setup_socket(va_list args) |
140 |
> |
static void |
141 |
> |
setup_socket(int fd) |
142 |
|
{ |
161 |
– |
int fd = va_arg(args, int); |
143 |
|
int opt = 1; |
144 |
|
|
145 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
145 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
146 |
|
|
147 |
|
#ifdef IPTOS_LOWDELAY |
148 |
|
opt = IPTOS_LOWDELAY; |
149 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
149 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
150 |
|
#endif |
151 |
|
|
171 |
– |
#ifndef _WIN32 |
152 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
173 |
– |
#endif |
174 |
– |
|
175 |
– |
return NULL; |
176 |
– |
} |
177 |
– |
|
178 |
– |
/* |
179 |
– |
* init_comm() |
180 |
– |
* |
181 |
– |
* Initializes comm subsystem. |
182 |
– |
*/ |
183 |
– |
void |
184 |
– |
init_comm(void) |
185 |
– |
{ |
186 |
– |
setup_socket_cb = register_callback("setup_socket", setup_socket); |
187 |
– |
init_netio(); |
153 |
|
} |
154 |
|
|
155 |
|
/* |
160 |
|
void |
161 |
|
close_connection(struct Client *client_p) |
162 |
|
{ |
163 |
< |
struct ConfItem *conf; |
164 |
< |
struct AccessItem *aconf; |
165 |
< |
struct ClassItem *aclass; |
201 |
< |
|
202 |
< |
assert(NULL != client_p); |
203 |
< |
|
204 |
< |
if (IsServer(client_p)) |
205 |
< |
{ |
206 |
< |
ServerStats->is_sv++; |
207 |
< |
ServerStats->is_sbs += client_p->localClient->send.bytes; |
208 |
< |
ServerStats->is_sbr += client_p->localClient->recv.bytes; |
209 |
< |
ServerStats->is_sti += CurrentTime - client_p->firsttime; |
210 |
< |
|
211 |
< |
/* XXX Does this even make any sense at all anymore? |
212 |
< |
* scheduling a 'quick' reconnect could cause a pile of |
213 |
< |
* nick collides under TSora protocol... -db |
214 |
< |
*/ |
215 |
< |
/* |
216 |
< |
* If the connection has been up for a long amount of time, schedule |
217 |
< |
* a 'quick' reconnect, else reset the next-connect cycle. |
218 |
< |
*/ |
219 |
< |
if ((conf = find_conf_exact(SERVER_TYPE, |
220 |
< |
client_p->name, client_p->username, |
221 |
< |
client_p->host))) |
222 |
< |
{ |
223 |
< |
/* |
224 |
< |
* Reschedule a faster reconnect, if this was a automatically |
225 |
< |
* connected configuration entry. (Note that if we have had |
226 |
< |
* a rehash in between, the status has been changed to |
227 |
< |
* CONF_ILLEGAL). But only do this if it was a "good" link. |
228 |
< |
*/ |
229 |
< |
aconf = (struct AccessItem *)map_to_conf(conf); |
230 |
< |
aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr); |
231 |
< |
aconf->hold = time(NULL); |
232 |
< |
aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ? |
233 |
< |
HANGONRETRYDELAY : ConFreq(aclass); |
234 |
< |
if (nextconnect > aconf->hold) |
235 |
< |
nextconnect = aconf->hold; |
236 |
< |
} |
237 |
< |
} |
238 |
< |
else if (IsClient(client_p)) |
239 |
< |
{ |
240 |
< |
ServerStats->is_cl++; |
241 |
< |
ServerStats->is_cbs += client_p->localClient->send.bytes; |
242 |
< |
ServerStats->is_cbr += client_p->localClient->recv.bytes; |
243 |
< |
ServerStats->is_cti += CurrentTime - client_p->firsttime; |
244 |
< |
} |
245 |
< |
else |
246 |
< |
ServerStats->is_ni++; |
163 |
> |
dlink_node *ptr = NULL; |
164 |
> |
|
165 |
> |
assert(client_p); |
166 |
|
|
167 |
|
if (!IsDead(client_p)) |
168 |
|
{ |
175 |
|
send_queued_write(client_p); |
176 |
|
} |
177 |
|
|
178 |
+ |
if (IsClient(client_p)) |
179 |
+ |
{ |
180 |
+ |
++ServerStats.is_cl; |
181 |
+ |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
182 |
+ |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
183 |
+ |
ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime; |
184 |
+ |
} |
185 |
+ |
else if (IsServer(client_p)) |
186 |
+ |
{ |
187 |
+ |
++ServerStats.is_sv; |
188 |
+ |
ServerStats.is_sbs += client_p->localClient->send.bytes; |
189 |
+ |
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
190 |
+ |
ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime; |
191 |
+ |
|
192 |
+ |
DLINK_FOREACH(ptr, server_items.head) |
193 |
+ |
{ |
194 |
+ |
struct MaskItem *conf = ptr->data; |
195 |
+ |
|
196 |
+ |
if (irccmp(conf->name, client_p->name)) |
197 |
+ |
continue; |
198 |
+ |
|
199 |
+ |
/* |
200 |
+ |
* Reset next-connect cycle of all connect{} blocks that match |
201 |
+ |
* this servername. |
202 |
+ |
*/ |
203 |
+ |
conf->until = CurrentTime + conf->class->con_freq; |
204 |
+ |
} |
205 |
+ |
} |
206 |
+ |
else |
207 |
+ |
++ServerStats.is_ni; |
208 |
+ |
|
209 |
|
#ifdef HAVE_LIBCRYPTO |
210 |
|
if (client_p->localClient->fd.ssl) |
211 |
< |
SSL_shutdown(client_p->localClient->fd.ssl); |
211 |
> |
{ |
212 |
> |
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
213 |
> |
|
214 |
> |
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
215 |
> |
SSL_shutdown(client_p->localClient->fd.ssl); |
216 |
> |
} |
217 |
|
#endif |
218 |
|
if (client_p->localClient->fd.flags.open) |
219 |
|
fd_close(&client_p->localClient->fd); |
220 |
|
|
266 |
– |
if (HasServlink(client_p)) |
267 |
– |
{ |
268 |
– |
if (client_p->localClient->ctrlfd.flags.open) |
269 |
– |
fd_close(&client_p->localClient->ctrlfd); |
270 |
– |
} |
271 |
– |
|
221 |
|
dbuf_clear(&client_p->localClient->buf_sendq); |
222 |
|
dbuf_clear(&client_p->localClient->buf_recvq); |
223 |
|
|
224 |
|
MyFree(client_p->localClient->passwd); |
225 |
< |
detach_conf(client_p, CONF_TYPE); |
225 |
> |
detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER); |
226 |
|
client_p->from = NULL; /* ...this should catch them! >:) --msa */ |
227 |
|
} |
228 |
|
|
234 |
|
static void |
235 |
|
ssl_handshake(int fd, struct Client *client_p) |
236 |
|
{ |
237 |
< |
int ret = SSL_accept(client_p->localClient->fd.ssl); |
237 |
> |
X509 *cert = NULL; |
238 |
> |
int ret = 0; |
239 |
|
|
240 |
< |
if (ret <= 0) |
240 |
> |
if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0) |
241 |
> |
{ |
242 |
|
switch (SSL_get_error(client_p->localClient->fd.ssl, ret)) |
243 |
|
{ |
244 |
|
case SSL_ERROR_WANT_WRITE: |
255 |
|
exit_client(client_p, client_p, "Error during SSL handshake"); |
256 |
|
return; |
257 |
|
} |
258 |
+ |
} |
259 |
+ |
|
260 |
+ |
if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl))) |
261 |
+ |
{ |
262 |
+ |
int res = SSL_get_verify_result(client_p->localClient->fd.ssl); |
263 |
+ |
char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' }; |
264 |
+ |
unsigned char md[EVP_MAX_MD_SIZE] = { '\0' }; |
265 |
+ |
|
266 |
+ |
if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || |
267 |
+ |
res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || |
268 |
+ |
res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) |
269 |
+ |
{ |
270 |
+ |
unsigned int i = 0, n = 0; |
271 |
|
|
272 |
< |
execute_callback(auth_cb, client_p); |
272 |
> |
if (X509_digest(cert, EVP_sha256(), md, &n)) |
273 |
> |
{ |
274 |
> |
for (; i < n; ++i) |
275 |
> |
snprintf(buf + 2 * i, 3, "%02X", md[i]); |
276 |
> |
client_p->certfp = xstrdup(buf); |
277 |
> |
} |
278 |
> |
} |
279 |
> |
else |
280 |
> |
ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d", |
281 |
> |
client_p->name, client_p->username, client_p->host, res); |
282 |
> |
X509_free(cert); |
283 |
> |
} |
284 |
> |
|
285 |
> |
start_auth(client_p); |
286 |
|
} |
287 |
|
#endif |
288 |
|
|
294 |
|
* any client list yet. |
295 |
|
*/ |
296 |
|
void |
297 |
< |
add_connection(struct Listener* listener, int fd) |
297 |
> |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
298 |
|
{ |
299 |
< |
struct Client *new_client; |
323 |
< |
socklen_t len = sizeof(struct irc_ssaddr); |
324 |
< |
struct irc_ssaddr irn; |
325 |
< |
assert(NULL != listener); |
326 |
< |
|
327 |
< |
/* |
328 |
< |
* get the client socket name from the socket |
329 |
< |
* the client has already been checked out in accept_connection |
330 |
< |
*/ |
299 |
> |
struct Client *new_client = make_client(NULL); |
300 |
|
|
332 |
– |
memset(&irn, 0, sizeof(irn)); |
333 |
– |
if (getpeername(fd, (struct sockaddr *)&irn, (socklen_t *)&len)) |
334 |
– |
{ |
335 |
– |
#ifdef _WIN32 |
336 |
– |
errno = WSAGetLastError(); |
337 |
– |
#endif |
338 |
– |
report_error(L_ALL, "Failed in adding new connection %s :%s", |
339 |
– |
get_listener_name(listener), errno); |
340 |
– |
ServerStats->is_ref++; |
341 |
– |
#ifdef _WIN32 |
342 |
– |
closesocket(fd); |
343 |
– |
#else |
344 |
– |
close(fd); |
345 |
– |
#endif |
346 |
– |
return; |
347 |
– |
} |
348 |
– |
|
349 |
– |
#ifdef IPV6 |
350 |
– |
remove_ipv6_mapping(&irn); |
351 |
– |
#else |
352 |
– |
irn.ss_len = len; |
353 |
– |
#endif |
354 |
– |
new_client = make_client(NULL); |
301 |
|
fd_open(&new_client->localClient->fd, fd, 1, |
302 |
|
(listener->flags & LISTENER_SSL) ? |
303 |
|
"Incoming SSL connection" : "Incoming connection"); |
358 |
– |
memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr)); |
304 |
|
|
305 |
< |
/* |
305 |
> |
/* |
306 |
|
* copy address to 'sockhost' as a string, copy it to host too |
307 |
|
* so we have something valid to put into error messages... |
308 |
|
*/ |
309 |
< |
new_client->localClient->port = ntohs(irn.ss_port); |
365 |
< |
memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr)); |
309 |
> |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
310 |
|
|
311 |
< |
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
312 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
313 |
< |
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
311 |
> |
getnameinfo((struct sockaddr *)&new_client->localClient->ip, |
312 |
> |
new_client->localClient->ip.ss_len, new_client->sockhost, |
313 |
> |
sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST); |
314 |
|
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
315 |
|
|
316 |
< |
*new_client->host = '\0'; |
317 |
< |
#ifdef IPV6 |
318 |
< |
if (*new_client->sockhost == ':') |
375 |
< |
strlcat(new_client->host, "0", HOSTLEN+1); |
376 |
< |
|
377 |
< |
if (new_client->localClient->aftype == AF_INET6 && |
378 |
< |
ConfigFileEntry.dot_in_ip6_addr == 1) |
316 |
> |
#ifdef HAVE_LIBGEOIP |
317 |
> |
/* XXX IPV6 SUPPORT XXX */ |
318 |
> |
if (irn->ss.ss_family == AF_INET && geoip_ctx) |
319 |
|
{ |
320 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
321 |
< |
strlcat(new_client->host, ".", HOSTLEN+1); |
322 |
< |
} else |
320 |
> |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)&new_client->localClient->ip; |
321 |
> |
new_client->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr)); |
322 |
> |
} |
323 |
|
#endif |
324 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
324 |
> |
|
325 |
> |
if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':') |
326 |
> |
{ |
327 |
> |
strlcpy(new_client->host, "0", sizeof(new_client->host)); |
328 |
> |
strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1); |
329 |
> |
memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1); |
330 |
> |
new_client->sockhost[0] = '0'; |
331 |
> |
} |
332 |
> |
else |
333 |
> |
strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
334 |
|
|
335 |
|
new_client->localClient->listener = listener; |
336 |
|
++listener->ref_count; |
337 |
|
|
389 |
– |
connect_id++; |
390 |
– |
new_client->connect_id = connect_id; |
391 |
– |
|
338 |
|
#ifdef HAVE_LIBCRYPTO |
339 |
< |
if ((listener->flags & LISTENER_SSL)) |
339 |
> |
if (listener->flags & LISTENER_SSL) |
340 |
|
{ |
341 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
341 |
> |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
342 |
|
{ |
343 |
< |
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
343 |
> |
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
344 |
|
ERR_error_string(ERR_get_error(), NULL)); |
345 |
|
|
346 |
|
SetDead(new_client); |
348 |
|
return; |
349 |
|
} |
350 |
|
|
351 |
+ |
AddFlag(new_client, FLAGS_SSL); |
352 |
|
SSL_set_fd(new_client->localClient->fd.ssl, fd); |
353 |
|
ssl_handshake(0, new_client); |
354 |
|
} |
355 |
|
else |
356 |
|
#endif |
357 |
< |
execute_callback(auth_cb, new_client); |
357 |
> |
start_auth(new_client); |
358 |
|
} |
359 |
|
|
360 |
|
/* |
484 |
|
void *data, int aftype, int timeout) |
485 |
|
{ |
486 |
|
struct addrinfo hints, *res; |
487 |
< |
char portname[PORTNAMELEN+1]; |
487 |
> |
char portname[PORTNAMELEN + 1]; |
488 |
|
|
489 |
|
assert(callback); |
490 |
|
fd->connect.callback = callback; |
516 |
|
hints.ai_socktype = SOCK_STREAM; |
517 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
518 |
|
|
519 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
519 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
520 |
|
|
521 |
< |
if (irc_getaddrinfo(host, portname, &hints, &res)) |
521 |
> |
if (getaddrinfo(host, portname, &hints, &res)) |
522 |
|
{ |
523 |
|
/* Send the DNS request, for the next level */ |
524 |
< |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
525 |
< |
fd->dns_query->ptr = fd; |
526 |
< |
fd->dns_query->callback = comm_connect_dns_callback; |
527 |
< |
gethost_byname(host, fd->dns_query); |
524 |
> |
if (aftype == AF_INET6) |
525 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
526 |
> |
else |
527 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
528 |
|
} |
529 |
|
else |
530 |
|
{ |
534 |
|
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
535 |
|
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
536 |
|
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
537 |
< |
irc_freeaddrinfo(res); |
537 |
> |
freeaddrinfo(res); |
538 |
|
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
539 |
|
comm_connect_tryconnect(fd, NULL); |
540 |
|
} |
582 |
|
* otherwise we initiate the connect() |
583 |
|
*/ |
584 |
|
static void |
585 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
585 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
586 |
|
{ |
587 |
|
fde_t *F = vptr; |
588 |
|
|
589 |
< |
if (reply == NULL) |
589 |
> |
if (name == NULL) |
590 |
|
{ |
644 |
– |
MyFree(F->dns_query); |
645 |
– |
F->dns_query = NULL; |
591 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
592 |
|
return; |
593 |
|
} |
601 |
|
* the DNS record around, and the DNS cache is gone anyway.. |
602 |
|
* -- adrian |
603 |
|
*/ |
604 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
604 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
605 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
606 |
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
607 |
|
F->connect.hostaddr.ss_port; |
608 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
608 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
609 |
|
|
610 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
666 |
– |
MyFree(F->dns_query); |
667 |
– |
F->dns_query = NULL; |
611 |
|
comm_connect_tryconnect(F, NULL); |
612 |
|
} |
613 |
|
|
635 |
|
/* Error? */ |
636 |
|
if (retval < 0) |
637 |
|
{ |
695 |
– |
#ifdef _WIN32 |
696 |
– |
errno = WSAGetLastError(); |
697 |
– |
#endif |
638 |
|
/* |
639 |
|
* If we get EISCONN, then we've already connect()ed the socket, |
640 |
|
* which is a good thing. |
693 |
|
*/ |
694 |
|
fd = socket(family, sock_type, proto); |
695 |
|
if (fd < 0) |
756 |
– |
{ |
757 |
– |
#ifdef _WIN32 |
758 |
– |
errno = WSAGetLastError(); |
759 |
– |
#endif |
696 |
|
return -1; /* errno will be passed through, yay.. */ |
761 |
– |
} |
697 |
|
|
698 |
< |
execute_callback(setup_socket_cb, fd); |
698 |
> |
setup_socket(fd); |
699 |
|
|
700 |
|
/* update things in our fd tracking */ |
701 |
|
fd_open(F, fd, 1, note); |
726 |
|
* reserved fd limit, but we can deal with that when comm_open() |
727 |
|
* also does it. XXX -- adrian |
728 |
|
*/ |
729 |
< |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
729 |
> |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen); |
730 |
|
if (newfd < 0) |
796 |
– |
{ |
797 |
– |
#ifdef _WIN32 |
798 |
– |
errno = WSAGetLastError(); |
799 |
– |
#endif |
731 |
|
return -1; |
801 |
– |
} |
732 |
|
|
733 |
|
#ifdef IPV6 |
734 |
|
remove_ipv6_mapping(pn); |
736 |
|
pn->ss_len = addrlen; |
737 |
|
#endif |
738 |
|
|
739 |
< |
execute_callback(setup_socket_cb, newfd); |
739 |
> |
setup_socket(newfd); |
740 |
|
|
741 |
|
/* .. and return */ |
742 |
|
return newfd; |
744 |
|
|
745 |
|
/* |
746 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
747 |
< |
* This function should really inspect the struct itself rather than relying |
818 |
< |
* on inet_pton and inet_ntop. OSes with IPv6 mapping listening on both |
747 |
> |
* OSes with IPv6 mapping listening on both |
748 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
749 |
|
* |
750 |
|
*/ |
754 |
|
{ |
755 |
|
if (addr->ss.ss_family == AF_INET6) |
756 |
|
{ |
757 |
< |
struct sockaddr_in6 *v6; |
829 |
< |
|
830 |
< |
v6 = (struct sockaddr_in6*)addr; |
831 |
< |
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) |
757 |
> |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
758 |
|
{ |
759 |
< |
char v4ip[HOSTIPLEN]; |
760 |
< |
struct sockaddr_in *v4 = (struct sockaddr_in*)addr; |
761 |
< |
inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN); |
762 |
< |
inet_pton(AF_INET, v4ip, &v4->sin_addr); |
759 |
> |
struct sockaddr_in6 v6; |
760 |
> |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
761 |
> |
|
762 |
> |
memcpy(&v6, addr, sizeof(v6)); |
763 |
> |
memset(v4, 0, sizeof(struct sockaddr_in)); |
764 |
> |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
765 |
> |
|
766 |
|
addr->ss.ss_family = AF_INET; |
767 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
768 |
|
} |