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" |
46 |
|
#include "send.h" |
47 |
|
#include "memory.h" |
54 |
|
|
55 |
|
struct Callback *setup_socket_cb = NULL; |
56 |
|
|
57 |
< |
static void comm_connect_callback(fde_t *fd, int status); |
57 |
> |
static void comm_connect_callback(fde_t *, int); |
58 |
|
static PF comm_connect_timeout; |
59 |
< |
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
59 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
60 |
|
static PF comm_connect_tryconnect; |
61 |
|
|
68 |
– |
extern void init_netio(void); |
62 |
|
|
63 |
|
/* check_can_use_v6() |
64 |
|
* Check if the system can open AF_INET6 sockets |
74 |
|
else |
75 |
|
{ |
76 |
|
ServerInfo.can_use_v6 = 1; |
84 |
– |
#ifdef _WIN32 |
85 |
– |
closesocket(v6); |
86 |
– |
#else |
77 |
|
close(v6); |
88 |
– |
#endif |
78 |
|
} |
79 |
|
#else |
80 |
|
ServerInfo.can_use_v6 = 0; |
90 |
|
int |
91 |
|
get_sockerr(int fd) |
92 |
|
{ |
104 |
– |
#ifndef _WIN32 |
93 |
|
int errtmp = errno; |
106 |
– |
#else |
107 |
– |
int errtmp = WSAGetLastError(); |
108 |
– |
#endif |
94 |
|
#ifdef SO_ERROR |
95 |
|
int err = 0; |
96 |
|
socklen_t len = sizeof(err); |
97 |
|
|
98 |
< |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
98 |
> |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
99 |
|
{ |
100 |
|
if (err) |
101 |
|
errtmp = err; |
129 |
|
{ |
130 |
|
who = (who) ? who : ""; |
131 |
|
|
132 |
< |
sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error)); |
133 |
< |
log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error)); |
134 |
< |
ilog(L_ERROR, text, who, strerror(error)); |
132 |
> |
sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE, |
133 |
> |
text, who, strerror(error)); |
134 |
> |
ilog(LOG_TYPE_IRCD, text, who, strerror(error)); |
135 |
|
} |
136 |
|
|
137 |
|
/* |
145 |
|
int fd = va_arg(args, int); |
146 |
|
int opt = 1; |
147 |
|
|
148 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
148 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
149 |
|
|
150 |
|
#ifdef IPTOS_LOWDELAY |
151 |
|
opt = IPTOS_LOWDELAY; |
152 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
152 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
153 |
|
#endif |
154 |
|
|
170 |
– |
#ifndef _WIN32 |
155 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
172 |
– |
#endif |
156 |
|
|
157 |
|
return NULL; |
158 |
|
} |
177 |
|
void |
178 |
|
close_connection(struct Client *client_p) |
179 |
|
{ |
197 |
– |
struct ConfItem *conf; |
180 |
|
struct AccessItem *aconf; |
181 |
|
struct ClassItem *aclass; |
182 |
+ |
dlink_node *ptr = NULL; |
183 |
|
|
184 |
< |
assert(NULL != client_p); |
184 |
> |
assert(client_p); |
185 |
|
|
186 |
|
if (!IsDead(client_p)) |
187 |
|
{ |
194 |
|
send_queued_write(client_p); |
195 |
|
} |
196 |
|
|
197 |
< |
if (IsServer(client_p)) |
197 |
> |
if (IsClient(client_p)) |
198 |
> |
{ |
199 |
> |
++ServerStats.is_cl; |
200 |
> |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
201 |
> |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
202 |
> |
ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime; |
203 |
> |
} |
204 |
> |
else 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; |
209 |
> |
ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime; |
210 |
|
|
211 |
< |
/* XXX Does this even make any sense at all anymore? |
222 |
< |
* scheduling a 'quick' reconnect could cause a pile of |
223 |
< |
* nick collides under TSora protocol... -db |
224 |
< |
*/ |
225 |
< |
/* |
226 |
< |
* If the connection has been up for a long amount of time, schedule |
227 |
< |
* a 'quick' reconnect, else reset the next-connect cycle. |
228 |
< |
*/ |
229 |
< |
if ((conf = find_conf_exact(SERVER_TYPE, |
230 |
< |
client_p->name, client_p->username, |
231 |
< |
client_p->host))) |
211 |
> |
DLINK_FOREACH(ptr, server_items.head) |
212 |
|
{ |
213 |
+ |
struct ConfItem *conf = ptr->data; |
214 |
+ |
|
215 |
+ |
if (irccmp(conf->name, client_p->name)) |
216 |
+ |
continue; |
217 |
+ |
|
218 |
|
/* |
219 |
< |
* Reschedule a faster reconnect, if this was a automatically |
220 |
< |
* connected configuration entry. (Note that if we have had |
236 |
< |
* a rehash in between, the status has been changed to |
237 |
< |
* CONF_ILLEGAL). But only do this if it was a "good" link. |
219 |
> |
* Reset next-connect cycle of all connect{} blocks that match |
220 |
> |
* this servername. |
221 |
|
*/ |
222 |
< |
aconf = (struct AccessItem *)map_to_conf(conf); |
223 |
< |
aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr); |
224 |
< |
aconf->hold = time(NULL); |
242 |
< |
aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ? |
243 |
< |
HANGONRETRYDELAY : ConFreq(aclass); |
244 |
< |
if (nextconnect > aconf->hold) |
245 |
< |
nextconnect = aconf->hold; |
222 |
> |
aconf = map_to_conf(conf); |
223 |
> |
aclass = map_to_conf(aconf->class_ptr); |
224 |
> |
aconf->hold = CurrentTime + aclass->con_freq; |
225 |
|
} |
226 |
|
} |
248 |
– |
else if (IsClient(client_p)) |
249 |
– |
{ |
250 |
– |
++ServerStats.is_cl; |
251 |
– |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
252 |
– |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
253 |
– |
ServerStats.is_cti += CurrentTime - client_p->firsttime; |
254 |
– |
} |
227 |
|
else |
228 |
|
++ServerStats.is_ni; |
229 |
|
|
239 |
|
if (client_p->localClient->fd.flags.open) |
240 |
|
fd_close(&client_p->localClient->fd); |
241 |
|
|
270 |
– |
if (HasServlink(client_p)) |
271 |
– |
{ |
272 |
– |
if (client_p->localClient->ctrlfd.flags.open) |
273 |
– |
fd_close(&client_p->localClient->ctrlfd); |
274 |
– |
} |
275 |
– |
|
242 |
|
dbuf_clear(&client_p->localClient->buf_sendq); |
243 |
|
dbuf_clear(&client_p->localClient->buf_recvq); |
244 |
|
|
289 |
|
void |
290 |
|
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
291 |
|
{ |
292 |
< |
struct Client *new_client; |
327 |
< |
|
328 |
< |
assert(NULL != listener); |
329 |
< |
|
330 |
< |
new_client = make_client(NULL); |
292 |
> |
struct Client *new_client = make_client(NULL); |
293 |
|
|
294 |
|
fd_open(&new_client->localClient->fd, fd, 1, |
295 |
|
(listener->flags & LISTENER_SSL) ? |
296 |
|
"Incoming SSL connection" : "Incoming connection"); |
297 |
|
|
298 |
< |
/* |
298 |
> |
/* |
299 |
|
* copy address to 'sockhost' as a string, copy it to host too |
300 |
|
* so we have something valid to put into error messages... |
301 |
|
*/ |
302 |
|
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
303 |
|
|
304 |
< |
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
305 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
306 |
< |
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
304 |
> |
getnameinfo((struct sockaddr *)&new_client->localClient->ip, |
305 |
> |
new_client->localClient->ip.ss_len, new_client->sockhost, |
306 |
> |
sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST); |
307 |
|
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
346 |
– |
#ifdef IPV6 |
347 |
– |
if (new_client->sockhost[0] == ':') |
348 |
– |
strlcat(new_client->host, "0", HOSTLEN+1); |
308 |
|
|
309 |
< |
if (new_client->localClient->aftype == AF_INET6 && |
351 |
< |
ConfigFileEntry.dot_in_ip6_addr == 1) |
309 |
> |
if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':') |
310 |
|
{ |
311 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
312 |
< |
strlcat(new_client->host, ".", HOSTLEN+1); |
311 |
> |
strlcpy(new_client->host, "0", sizeof(new_client->host)); |
312 |
> |
strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1); |
313 |
> |
memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1); |
314 |
> |
new_client->sockhost[0] = '0'; |
315 |
|
} |
316 |
|
else |
317 |
< |
#endif |
358 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
317 |
> |
strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
318 |
|
|
319 |
|
new_client->localClient->listener = listener; |
320 |
|
++listener->ref_count; |
322 |
|
#ifdef HAVE_LIBCRYPTO |
323 |
|
if (listener->flags & LISTENER_SSL) |
324 |
|
{ |
325 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
325 |
> |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
326 |
|
{ |
327 |
< |
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
327 |
> |
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
328 |
|
ERR_error_string(ERR_get_error(), NULL)); |
329 |
|
|
330 |
|
SetDead(new_client); |
467 |
|
void *data, int aftype, int timeout) |
468 |
|
{ |
469 |
|
struct addrinfo hints, *res; |
470 |
< |
char portname[PORTNAMELEN+1]; |
470 |
> |
char portname[PORTNAMELEN + 1]; |
471 |
|
|
472 |
|
assert(callback); |
473 |
|
fd->connect.callback = callback; |
499 |
|
hints.ai_socktype = SOCK_STREAM; |
500 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
501 |
|
|
502 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
502 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
503 |
|
|
504 |
< |
if (irc_getaddrinfo(host, portname, &hints, &res)) |
504 |
> |
if (getaddrinfo(host, portname, &hints, &res)) |
505 |
|
{ |
506 |
|
/* Send the DNS request, for the next level */ |
548 |
– |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
549 |
– |
fd->dns_query->ptr = fd; |
550 |
– |
fd->dns_query->callback = comm_connect_dns_callback; |
507 |
|
if (aftype == AF_INET6) |
508 |
< |
gethost_byname_type(host, fd->dns_query, T_AAAA); |
508 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
509 |
|
else |
510 |
< |
gethost_byname_type(host, fd->dns_query, T_A); |
510 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
511 |
|
} |
512 |
|
else |
513 |
|
{ |
517 |
|
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
518 |
|
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
519 |
|
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
520 |
< |
irc_freeaddrinfo(res); |
520 |
> |
freeaddrinfo(res); |
521 |
|
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
522 |
|
comm_connect_tryconnect(fd, NULL); |
523 |
|
} |
565 |
|
* otherwise we initiate the connect() |
566 |
|
*/ |
567 |
|
static void |
568 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
568 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
569 |
|
{ |
570 |
|
fde_t *F = vptr; |
571 |
|
|
572 |
< |
if (reply == NULL) |
572 |
> |
if (name == NULL) |
573 |
|
{ |
618 |
– |
MyFree(F->dns_query); |
619 |
– |
F->dns_query = NULL; |
574 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
575 |
|
return; |
576 |
|
} |
584 |
|
* the DNS record around, and the DNS cache is gone anyway.. |
585 |
|
* -- adrian |
586 |
|
*/ |
587 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
587 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
588 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
589 |
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
590 |
|
F->connect.hostaddr.ss_port; |
591 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
591 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
592 |
|
|
593 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
640 |
– |
MyFree(F->dns_query); |
641 |
– |
F->dns_query = NULL; |
594 |
|
comm_connect_tryconnect(F, NULL); |
595 |
|
} |
596 |
|
|
618 |
|
/* Error? */ |
619 |
|
if (retval < 0) |
620 |
|
{ |
669 |
– |
#ifdef _WIN32 |
670 |
– |
errno = WSAGetLastError(); |
671 |
– |
#endif |
621 |
|
/* |
622 |
|
* If we get EISCONN, then we've already connect()ed the socket, |
623 |
|
* which is a good thing. |
676 |
|
*/ |
677 |
|
fd = socket(family, sock_type, proto); |
678 |
|
if (fd < 0) |
730 |
– |
{ |
731 |
– |
#ifdef _WIN32 |
732 |
– |
errno = WSAGetLastError(); |
733 |
– |
#endif |
679 |
|
return -1; /* errno will be passed through, yay.. */ |
735 |
– |
} |
680 |
|
|
681 |
|
execute_callback(setup_socket_cb, fd); |
682 |
|
|
709 |
|
* reserved fd limit, but we can deal with that when comm_open() |
710 |
|
* also does it. XXX -- adrian |
711 |
|
*/ |
712 |
< |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
712 |
> |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen); |
713 |
|
if (newfd < 0) |
770 |
– |
{ |
771 |
– |
#ifdef _WIN32 |
772 |
– |
errno = WSAGetLastError(); |
773 |
– |
#endif |
714 |
|
return -1; |
775 |
– |
} |
715 |
|
|
716 |
|
#ifdef IPV6 |
717 |
|
remove_ipv6_mapping(pn); |
727 |
|
|
728 |
|
/* |
729 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
730 |
< |
* This function should really inspect the struct itself rather than relying |
792 |
< |
* on inet_pton and inet_ntop. OSes with IPv6 mapping listening on both |
730 |
> |
* OSes with IPv6 mapping listening on both |
731 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
732 |
|
* |
733 |
|
*/ |
737 |
|
{ |
738 |
|
if (addr->ss.ss_family == AF_INET6) |
739 |
|
{ |
740 |
< |
struct sockaddr_in6 *v6; |
803 |
< |
|
804 |
< |
v6 = (struct sockaddr_in6*)addr; |
805 |
< |
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) |
740 |
> |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
741 |
|
{ |
742 |
< |
char v4ip[HOSTIPLEN]; |
743 |
< |
struct sockaddr_in *v4 = (struct sockaddr_in*)addr; |
744 |
< |
inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN); |
745 |
< |
inet_pton(AF_INET, v4ip, &v4->sin_addr); |
742 |
> |
struct sockaddr_in6 v6; |
743 |
> |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
744 |
> |
|
745 |
> |
memcpy(&v6, addr, sizeof(v6)); |
746 |
> |
memset(v4, 0, sizeof(struct sockaddr_in)); |
747 |
> |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
748 |
> |
|
749 |
|
addr->ss.ss_family = AF_INET; |
750 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
751 |
|
} |