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" |
37 |
|
#include "irc_getnameinfo.h" |
38 |
|
#include "irc_getaddrinfo.h" |
39 |
|
#include "ircd.h" |
41 |
– |
#include "list.h" |
40 |
|
#include "listener.h" |
41 |
|
#include "numeric.h" |
42 |
|
#include "packet.h" |
47 |
|
#include "s_conf.h" |
48 |
|
#include "s_log.h" |
49 |
|
#include "s_serv.h" |
52 |
– |
#include "s_stats.h" |
50 |
|
#include "send.h" |
51 |
|
#include "memory.h" |
52 |
|
#include "s_user.h" |
58 |
|
|
59 |
|
struct Callback *setup_socket_cb = NULL; |
60 |
|
|
61 |
< |
static void comm_connect_callback(fde_t *fd, int status); |
61 |
> |
static void comm_connect_callback(fde_t *, int); |
62 |
|
static PF comm_connect_timeout; |
63 |
< |
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
63 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
64 |
|
static PF comm_connect_tryconnect; |
65 |
|
|
69 |
– |
extern void init_netio(void); |
66 |
|
|
67 |
|
/* check_can_use_v6() |
68 |
|
* Check if the system can open AF_INET6 sockets |
78 |
|
else |
79 |
|
{ |
80 |
|
ServerInfo.can_use_v6 = 1; |
85 |
– |
#ifdef _WIN32 |
86 |
– |
closesocket(v6); |
87 |
– |
#else |
81 |
|
close(v6); |
89 |
– |
#endif |
82 |
|
} |
83 |
|
#else |
84 |
|
ServerInfo.can_use_v6 = 0; |
94 |
|
int |
95 |
|
get_sockerr(int fd) |
96 |
|
{ |
105 |
– |
#ifndef _WIN32 |
97 |
|
int errtmp = errno; |
107 |
– |
#else |
108 |
– |
int errtmp = WSAGetLastError(); |
109 |
– |
#endif |
98 |
|
#ifdef SO_ERROR |
99 |
|
int err = 0; |
100 |
|
socklen_t len = sizeof(err); |
101 |
|
|
102 |
< |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
102 |
> |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
103 |
|
{ |
104 |
|
if (err) |
105 |
|
errtmp = err; |
149 |
|
int fd = va_arg(args, int); |
150 |
|
int opt = 1; |
151 |
|
|
152 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
152 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
153 |
|
|
154 |
|
#ifdef IPTOS_LOWDELAY |
155 |
|
opt = IPTOS_LOWDELAY; |
156 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
156 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
157 |
|
#endif |
158 |
|
|
171 |
– |
#ifndef _WIN32 |
159 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
173 |
– |
#endif |
160 |
|
|
161 |
|
return NULL; |
162 |
|
} |
187 |
|
|
188 |
|
assert(NULL != client_p); |
189 |
|
|
190 |
+ |
if (!IsDead(client_p)) |
191 |
+ |
{ |
192 |
+ |
/* attempt to flush any pending dbufs. Evil, but .. -- adrian */ |
193 |
+ |
/* there is still a chance that we might send data to this socket |
194 |
+ |
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
195 |
+ |
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
196 |
+ |
*/ |
197 |
+ |
ClearSendqBlocked(client_p); |
198 |
+ |
send_queued_write(client_p); |
199 |
+ |
} |
200 |
+ |
|
201 |
|
if (IsServer(client_p)) |
202 |
|
{ |
203 |
< |
ServerStats->is_sv++; |
204 |
< |
ServerStats->is_sbs += client_p->localClient->send.bytes; |
205 |
< |
ServerStats->is_sbr += client_p->localClient->recv.bytes; |
206 |
< |
ServerStats->is_sti += CurrentTime - client_p->firsttime; |
203 |
> |
++ServerStats.is_sv; |
204 |
> |
ServerStats.is_sbs += client_p->localClient->send.bytes; |
205 |
> |
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
206 |
> |
ServerStats.is_sti += CurrentTime - client_p->firsttime; |
207 |
|
|
208 |
|
/* XXX Does this even make any sense at all anymore? |
209 |
|
* scheduling a 'quick' reconnect could cause a pile of |
213 |
|
* If the connection has been up for a long amount of time, schedule |
214 |
|
* a 'quick' reconnect, else reset the next-connect cycle. |
215 |
|
*/ |
216 |
< |
if ((conf = find_conf_exact(SERVER_TYPE, |
217 |
< |
client_p->name, client_p->username, |
221 |
< |
client_p->host))) |
216 |
> |
if ((conf = find_conf_exact(SERVER_TYPE, client_p->name, |
217 |
> |
client_p->username, client_p->host))) |
218 |
|
{ |
219 |
|
/* |
220 |
|
* Reschedule a faster reconnect, if this was a automatically |
222 |
|
* a rehash in between, the status has been changed to |
223 |
|
* CONF_ILLEGAL). But only do this if it was a "good" link. |
224 |
|
*/ |
225 |
< |
aconf = (struct AccessItem *)map_to_conf(conf); |
226 |
< |
aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr); |
225 |
> |
aconf = map_to_conf(conf); |
226 |
> |
aclass = map_to_conf(aconf->class_ptr); |
227 |
|
aconf->hold = time(NULL); |
228 |
|
aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ? |
229 |
|
HANGONRETRYDELAY : ConFreq(aclass); |
234 |
– |
if (nextconnect > aconf->hold) |
235 |
– |
nextconnect = aconf->hold; |
230 |
|
} |
231 |
|
} |
232 |
|
else if (IsClient(client_p)) |
233 |
|
{ |
234 |
< |
ServerStats->is_cl++; |
235 |
< |
ServerStats->is_cbs += client_p->localClient->send.bytes; |
236 |
< |
ServerStats->is_cbr += client_p->localClient->recv.bytes; |
237 |
< |
ServerStats->is_cti += CurrentTime - client_p->firsttime; |
234 |
> |
++ServerStats.is_cl; |
235 |
> |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
236 |
> |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
237 |
> |
ServerStats.is_cti += CurrentTime - client_p->firsttime; |
238 |
|
} |
239 |
|
else |
240 |
< |
ServerStats->is_ni++; |
247 |
< |
|
248 |
< |
if (!IsDead(client_p)) |
249 |
< |
{ |
250 |
< |
/* attempt to flush any pending dbufs. Evil, but .. -- adrian */ |
251 |
< |
/* there is still a chance that we might send data to this socket |
252 |
< |
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
253 |
< |
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
254 |
< |
*/ |
255 |
< |
ClearSendqBlocked(client_p); |
256 |
< |
send_queued_write(client_p); |
257 |
< |
} |
240 |
> |
++ServerStats.is_ni; |
241 |
|
|
242 |
|
#ifdef HAVE_LIBCRYPTO |
243 |
|
if (client_p->localClient->fd.ssl) |
244 |
< |
SSL_shutdown(client_p->localClient->fd.ssl); |
244 |
> |
{ |
245 |
> |
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
246 |
> |
|
247 |
> |
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
248 |
> |
SSL_shutdown(client_p->localClient->fd.ssl); |
249 |
> |
} |
250 |
|
#endif |
251 |
|
if (client_p->localClient->fd.flags.open) |
252 |
|
fd_close(&client_p->localClient->fd); |
305 |
|
* any client list yet. |
306 |
|
*/ |
307 |
|
void |
308 |
< |
add_connection(struct Listener* listener, int fd) |
308 |
> |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
309 |
|
{ |
310 |
|
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 |
– |
*/ |
311 |
|
|
312 |
< |
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 |
< |
} |
312 |
> |
assert(NULL != listener); |
313 |
|
|
349 |
– |
#ifdef IPV6 |
350 |
– |
remove_ipv6_mapping(&irn); |
351 |
– |
#else |
352 |
– |
irn.ss_len = len; |
353 |
– |
#endif |
314 |
|
new_client = make_client(NULL); |
315 |
+ |
|
316 |
|
fd_open(&new_client->localClient->fd, fd, 1, |
317 |
|
(listener->flags & LISTENER_SSL) ? |
318 |
|
"Incoming SSL connection" : "Incoming connection"); |
358 |
– |
memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr)); |
319 |
|
|
320 |
|
/* |
321 |
|
* copy address to 'sockhost' as a string, copy it to host too |
322 |
|
* so we have something valid to put into error messages... |
323 |
|
*/ |
324 |
< |
new_client->localClient->port = ntohs(irn.ss_port); |
365 |
< |
memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr)); |
324 |
> |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
325 |
|
|
326 |
|
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
327 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
328 |
< |
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
327 |
> |
new_client->localClient->ip.ss_len, new_client->sockhost, |
328 |
> |
sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST); |
329 |
|
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
330 |
|
|
331 |
< |
*new_client->host = '\0'; |
373 |
< |
#ifdef IPV6 |
374 |
< |
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) |
331 |
> |
if (new_client->sockhost[0] == ':') |
332 |
|
{ |
333 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
334 |
< |
strlcat(new_client->host, ".", HOSTLEN+1); |
335 |
< |
} else |
336 |
< |
#endif |
337 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
333 |
> |
strlcpy(new_client->host, "0", sizeof(new_client->host)); |
334 |
> |
strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1); |
335 |
> |
} |
336 |
> |
else |
337 |
> |
strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
338 |
|
|
339 |
|
new_client->localClient->listener = listener; |
340 |
|
++listener->ref_count; |
341 |
|
|
389 |
– |
connect_id++; |
390 |
– |
new_client->connect_id = connect_id; |
391 |
– |
|
342 |
|
#ifdef HAVE_LIBCRYPTO |
343 |
< |
if ((listener->flags & LISTENER_SSL)) |
343 |
> |
if (listener->flags & LISTENER_SSL) |
344 |
|
{ |
345 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
345 |
> |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
346 |
|
{ |
347 |
|
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
348 |
|
ERR_error_string(ERR_get_error(), NULL)); |
487 |
|
void *data, int aftype, int timeout) |
488 |
|
{ |
489 |
|
struct addrinfo hints, *res; |
490 |
< |
char portname[PORTNAMELEN+1]; |
490 |
> |
char portname[PORTNAMELEN + 1]; |
491 |
|
|
492 |
|
assert(callback); |
493 |
|
fd->connect.callback = callback; |
519 |
|
hints.ai_socktype = SOCK_STREAM; |
520 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
521 |
|
|
522 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
522 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
523 |
|
|
524 |
|
if (irc_getaddrinfo(host, portname, &hints, &res)) |
525 |
|
{ |
526 |
|
/* Send the DNS request, for the next level */ |
527 |
< |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
528 |
< |
fd->dns_query->ptr = fd; |
529 |
< |
fd->dns_query->callback = comm_connect_dns_callback; |
530 |
< |
gethost_byname(host, fd->dns_query); |
527 |
> |
if (aftype == AF_INET6) |
528 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
529 |
> |
else |
530 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
531 |
|
} |
532 |
|
else |
533 |
|
{ |
585 |
|
* otherwise we initiate the connect() |
586 |
|
*/ |
587 |
|
static void |
588 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
588 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
589 |
|
{ |
590 |
|
fde_t *F = vptr; |
591 |
|
|
592 |
< |
if (reply == NULL) |
592 |
> |
if (name == NULL) |
593 |
|
{ |
644 |
– |
MyFree(F->dns_query); |
645 |
– |
F->dns_query = NULL; |
594 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
595 |
|
return; |
596 |
|
} |
604 |
|
* the DNS record around, and the DNS cache is gone anyway.. |
605 |
|
* -- adrian |
606 |
|
*/ |
607 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
607 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
608 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
609 |
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
610 |
|
F->connect.hostaddr.ss_port; |
611 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
611 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
612 |
|
|
613 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
666 |
– |
MyFree(F->dns_query); |
667 |
– |
F->dns_query = NULL; |
614 |
|
comm_connect_tryconnect(F, NULL); |
615 |
|
} |
616 |
|
|
638 |
|
/* Error? */ |
639 |
|
if (retval < 0) |
640 |
|
{ |
695 |
– |
#ifdef _WIN32 |
696 |
– |
errno = WSAGetLastError(); |
697 |
– |
#endif |
641 |
|
/* |
642 |
|
* If we get EISCONN, then we've already connect()ed the socket, |
643 |
|
* which is a good thing. |
696 |
|
*/ |
697 |
|
fd = socket(family, sock_type, proto); |
698 |
|
if (fd < 0) |
756 |
– |
{ |
757 |
– |
#ifdef _WIN32 |
758 |
– |
errno = WSAGetLastError(); |
759 |
– |
#endif |
699 |
|
return -1; /* errno will be passed through, yay.. */ |
761 |
– |
} |
700 |
|
|
701 |
|
execute_callback(setup_socket_cb, fd); |
702 |
|
|
729 |
|
* reserved fd limit, but we can deal with that when comm_open() |
730 |
|
* also does it. XXX -- adrian |
731 |
|
*/ |
732 |
< |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
732 |
> |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen); |
733 |
|
if (newfd < 0) |
796 |
– |
{ |
797 |
– |
#ifdef _WIN32 |
798 |
– |
errno = WSAGetLastError(); |
799 |
– |
#endif |
734 |
|
return -1; |
801 |
– |
} |
735 |
|
|
736 |
|
#ifdef IPV6 |
737 |
|
remove_ipv6_mapping(pn); |
747 |
|
|
748 |
|
/* |
749 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
750 |
< |
* 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 |
750 |
> |
* OSes with IPv6 mapping listening on both |
751 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
752 |
|
* |
753 |
|
*/ |
757 |
|
{ |
758 |
|
if (addr->ss.ss_family == AF_INET6) |
759 |
|
{ |
760 |
< |
struct sockaddr_in6 *v6; |
829 |
< |
|
830 |
< |
v6 = (struct sockaddr_in6*)addr; |
831 |
< |
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) |
760 |
> |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
761 |
|
{ |
762 |
< |
char v4ip[HOSTIPLEN]; |
763 |
< |
struct sockaddr_in *v4 = (struct sockaddr_in*)addr; |
764 |
< |
inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN); |
765 |
< |
inet_pton(AF_INET, v4ip, &v4->sin_addr); |
762 |
> |
struct sockaddr_in6 v6; |
763 |
> |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
764 |
> |
|
765 |
> |
memcpy(&v6, addr, sizeof(v6)); |
766 |
> |
memset(v4, 0, sizeof(struct sockaddr_in)); |
767 |
> |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
768 |
> |
|
769 |
|
addr->ss.ss_family = AF_INET; |
770 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
771 |
|
} |