1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* s_bsd.c: Network functions. |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
* USA |
20 |
< |
* |
21 |
< |
* $Id$ |
20 |
> |
*/ |
21 |
> |
|
22 |
> |
/*! \file s_bsd.c |
23 |
> |
* \brief Network functions. |
24 |
> |
* \version $Id$ |
25 |
|
*/ |
26 |
|
|
27 |
|
#include "stdinc.h" |
28 |
< |
#ifndef _WIN32 |
28 |
> |
#ifdef HAVE_LIBCRYPTO |
29 |
> |
#include "rsa.h" |
30 |
> |
#endif |
31 |
|
#include <netinet/in_systm.h> |
32 |
|
#include <netinet/ip.h> |
33 |
|
#include <netinet/tcp.h> |
34 |
< |
#endif |
34 |
> |
#include "list.h" |
35 |
|
#include "fdlist.h" |
36 |
|
#include "s_bsd.h" |
37 |
|
#include "client.h" |
34 |
– |
#include "common.h" |
38 |
|
#include "dbuf.h" |
39 |
|
#include "event.h" |
40 |
|
#include "irc_string.h" |
38 |
– |
#include "irc_getnameinfo.h" |
39 |
– |
#include "irc_getaddrinfo.h" |
41 |
|
#include "ircd.h" |
41 |
– |
#include "list.h" |
42 |
|
#include "listener.h" |
43 |
|
#include "numeric.h" |
44 |
|
#include "packet.h" |
45 |
< |
#include "irc_res.h" |
46 |
< |
#include "inet_misc.h" |
45 |
> |
#include "res.h" |
46 |
|
#include "restart.h" |
47 |
< |
#include "s_auth.h" |
48 |
< |
#include "s_conf.h" |
49 |
< |
#include "s_log.h" |
50 |
< |
#include "s_serv.h" |
52 |
< |
#include "s_stats.h" |
47 |
> |
#include "auth.h" |
48 |
> |
#include "conf.h" |
49 |
> |
#include "log.h" |
50 |
> |
#include "server.h" |
51 |
|
#include "send.h" |
52 |
|
#include "memory.h" |
53 |
< |
#include "s_user.h" |
54 |
< |
#include "hook.h" |
53 |
> |
#include "user.h" |
54 |
> |
|
55 |
|
|
56 |
|
static const char *comm_err_str[] = { "Comm OK", "Error during bind()", |
57 |
|
"Error during DNS lookup", "connect timeout", "Error during connect()", |
58 |
|
"Comm Error" }; |
59 |
|
|
60 |
< |
struct Callback *setup_socket_cb = NULL; |
63 |
< |
|
64 |
< |
static void comm_connect_callback(fde_t *fd, int status); |
60 |
> |
static void comm_connect_callback(fde_t *, int); |
61 |
|
static PF comm_connect_timeout; |
62 |
< |
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
62 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *, size_t); |
63 |
|
static PF comm_connect_tryconnect; |
64 |
|
|
69 |
– |
extern void init_netio(void); |
70 |
– |
|
71 |
– |
/* check_can_use_v6() |
72 |
– |
* Check if the system can open AF_INET6 sockets |
73 |
– |
*/ |
74 |
– |
void |
75 |
– |
check_can_use_v6(void) |
76 |
– |
{ |
77 |
– |
#ifdef IPV6 |
78 |
– |
int v6; |
79 |
– |
|
80 |
– |
if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) |
81 |
– |
ServerInfo.can_use_v6 = 0; |
82 |
– |
else |
83 |
– |
{ |
84 |
– |
ServerInfo.can_use_v6 = 1; |
85 |
– |
#ifdef _WIN32 |
86 |
– |
closesocket(v6); |
87 |
– |
#else |
88 |
– |
close(v6); |
89 |
– |
#endif |
90 |
– |
} |
91 |
– |
#else |
92 |
– |
ServerInfo.can_use_v6 = 0; |
93 |
– |
#endif |
94 |
– |
} |
65 |
|
|
66 |
|
/* get_sockerr - get the error value from the socket or the current errno |
67 |
|
* |
72 |
|
int |
73 |
|
get_sockerr(int fd) |
74 |
|
{ |
105 |
– |
#ifndef _WIN32 |
75 |
|
int errtmp = errno; |
107 |
– |
#else |
108 |
– |
int errtmp = WSAGetLastError(); |
109 |
– |
#endif |
76 |
|
#ifdef SO_ERROR |
77 |
|
int err = 0; |
78 |
|
socklen_t len = sizeof(err); |
79 |
|
|
80 |
< |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
80 |
> |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
81 |
|
{ |
82 |
|
if (err) |
83 |
|
errtmp = err; |
88 |
|
} |
89 |
|
|
90 |
|
/* |
91 |
< |
* report_error - report an error from an errno. |
91 |
> |
* report_error - report an error from an errno. |
92 |
|
* Record error to log and also send a copy to all *LOCAL* opers online. |
93 |
|
* |
94 |
|
* text is a *format* string for outputing error. It must |
102 |
|
* Cannot use perror() within daemon. stderr is closed in |
103 |
|
* ircd and cannot be used. And, worse yet, it might have |
104 |
|
* been reassigned to a normal connection... |
105 |
< |
* |
105 |
> |
* |
106 |
|
* Actually stderr is still there IFF ircd was run with -s --Rodder |
107 |
|
*/ |
108 |
|
|
109 |
|
void |
110 |
< |
report_error(int level, const char* text, const char* who, int error) |
110 |
> |
report_error(int level, const char* text, const char* who, int error) |
111 |
|
{ |
112 |
|
who = (who) ? who : ""; |
113 |
|
|
114 |
< |
sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error)); |
115 |
< |
log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error)); |
116 |
< |
ilog(L_ERROR, text, who, strerror(error)); |
114 |
> |
sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE, |
115 |
> |
text, who, strerror(error)); |
116 |
> |
ilog(LOG_TYPE_IRCD, text, who, strerror(error)); |
117 |
|
} |
118 |
|
|
119 |
|
/* |
121 |
|
* |
122 |
|
* Set the socket non-blocking, and other wonderful bits. |
123 |
|
*/ |
124 |
< |
static void * |
125 |
< |
setup_socket(va_list args) |
124 |
> |
static void |
125 |
> |
setup_socket(int fd) |
126 |
|
{ |
161 |
– |
int fd = va_arg(args, int); |
127 |
|
int opt = 1; |
128 |
|
|
129 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
129 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
130 |
|
|
131 |
|
#ifdef IPTOS_LOWDELAY |
132 |
|
opt = IPTOS_LOWDELAY; |
133 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
133 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
134 |
|
#endif |
135 |
|
|
171 |
– |
#ifndef _WIN32 |
136 |
|
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(); |
137 |
|
} |
138 |
|
|
139 |
|
/* |
144 |
|
void |
145 |
|
close_connection(struct Client *client_p) |
146 |
|
{ |
147 |
< |
struct ConfItem *conf; |
148 |
< |
struct AccessItem *aconf; |
149 |
< |
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++; |
147 |
> |
dlink_node *ptr = NULL; |
148 |
> |
|
149 |
> |
assert(client_p); |
150 |
|
|
151 |
|
if (!IsDead(client_p)) |
152 |
|
{ |
155 |
|
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
156 |
|
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
157 |
|
*/ |
158 |
< |
ClearSendqBlocked(client_p); |
158 |
> |
DelFlag(client_p, FLAGS_BLOCKED); |
159 |
|
send_queued_write(client_p); |
160 |
|
} |
161 |
|
|
162 |
+ |
if (IsClient(client_p)) |
163 |
+ |
{ |
164 |
+ |
++ServerStats.is_cl; |
165 |
+ |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
166 |
+ |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
167 |
+ |
ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime; |
168 |
+ |
} |
169 |
+ |
else if (IsServer(client_p)) |
170 |
+ |
{ |
171 |
+ |
++ServerStats.is_sv; |
172 |
+ |
ServerStats.is_sbs += client_p->localClient->send.bytes; |
173 |
+ |
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
174 |
+ |
ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime; |
175 |
+ |
|
176 |
+ |
DLINK_FOREACH(ptr, server_items.head) |
177 |
+ |
{ |
178 |
+ |
struct MaskItem *conf = ptr->data; |
179 |
+ |
|
180 |
+ |
if (irccmp(conf->name, client_p->name)) |
181 |
+ |
continue; |
182 |
+ |
|
183 |
+ |
/* |
184 |
+ |
* Reset next-connect cycle of all connect{} blocks that match |
185 |
+ |
* this servername. |
186 |
+ |
*/ |
187 |
+ |
conf->until = CurrentTime + conf->class->con_freq; |
188 |
+ |
} |
189 |
+ |
} |
190 |
+ |
else |
191 |
+ |
++ServerStats.is_ni; |
192 |
+ |
|
193 |
|
#ifdef HAVE_LIBCRYPTO |
194 |
|
if (client_p->localClient->fd.ssl) |
195 |
< |
SSL_shutdown(client_p->localClient->fd.ssl); |
195 |
> |
{ |
196 |
> |
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
197 |
> |
|
198 |
> |
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
199 |
> |
SSL_shutdown(client_p->localClient->fd.ssl); |
200 |
> |
} |
201 |
|
#endif |
202 |
|
if (client_p->localClient->fd.flags.open) |
203 |
|
fd_close(&client_p->localClient->fd); |
204 |
|
|
266 |
– |
if (HasServlink(client_p)) |
267 |
– |
{ |
268 |
– |
if (client_p->localClient->ctrlfd.flags.open) |
269 |
– |
fd_close(&client_p->localClient->ctrlfd); |
270 |
– |
} |
271 |
– |
|
205 |
|
dbuf_clear(&client_p->localClient->buf_sendq); |
206 |
|
dbuf_clear(&client_p->localClient->buf_recvq); |
207 |
< |
|
208 |
< |
MyFree(client_p->localClient->passwd); |
209 |
< |
detach_conf(client_p, CONF_TYPE); |
210 |
< |
client_p->from = NULL; /* ...this should catch them! >:) --msa */ |
207 |
> |
|
208 |
> |
MyFree(client_p->localClient->password); |
209 |
> |
client_p->localClient->password = NULL; |
210 |
> |
|
211 |
> |
detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER); |
212 |
|
} |
213 |
|
|
214 |
|
#ifdef HAVE_LIBCRYPTO |
219 |
|
static void |
220 |
|
ssl_handshake(int fd, struct Client *client_p) |
221 |
|
{ |
222 |
< |
int ret = SSL_accept(client_p->localClient->fd.ssl); |
222 |
> |
X509 *cert = NULL; |
223 |
> |
int ret = 0; |
224 |
> |
|
225 |
> |
if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0) |
226 |
> |
{ |
227 |
> |
if ((CurrentTime - client_p->localClient->firsttime) > 30) |
228 |
> |
{ |
229 |
> |
exit_client(client_p, "Timeout during SSL handshake"); |
230 |
> |
return; |
231 |
> |
} |
232 |
|
|
290 |
– |
if (ret <= 0) |
233 |
|
switch (SSL_get_error(client_p->localClient->fd.ssl, ret)) |
234 |
|
{ |
235 |
|
case SSL_ERROR_WANT_WRITE: |
236 |
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE, |
237 |
< |
(PF *) ssl_handshake, client_p, 0); |
237 |
> |
(PF *)ssl_handshake, client_p, 30); |
238 |
|
return; |
239 |
|
|
240 |
|
case SSL_ERROR_WANT_READ: |
241 |
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, |
242 |
< |
(PF *) ssl_handshake, client_p, 0); |
242 |
> |
(PF *)ssl_handshake, client_p, 30); |
243 |
|
return; |
244 |
|
|
245 |
|
default: |
246 |
< |
exit_client(client_p, client_p, "Error during SSL handshake"); |
247 |
< |
return; |
246 |
> |
exit_client(client_p, "Error during SSL handshake"); |
247 |
> |
return; |
248 |
> |
} |
249 |
> |
} |
250 |
> |
|
251 |
> |
comm_settimeout(&client_p->localClient->fd, 0, NULL, NULL); |
252 |
> |
|
253 |
> |
if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl))) |
254 |
> |
{ |
255 |
> |
int res = SSL_get_verify_result(client_p->localClient->fd.ssl); |
256 |
> |
char buf[EVP_MAX_MD_SIZE * 2 + 1] = ""; |
257 |
> |
unsigned char md[EVP_MAX_MD_SIZE] = ""; |
258 |
> |
|
259 |
> |
if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || |
260 |
> |
res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || |
261 |
> |
res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) |
262 |
> |
{ |
263 |
> |
unsigned int n = 0; |
264 |
> |
|
265 |
> |
if (X509_digest(cert, ConfigServerInfo.message_digest_algorithm, md, &n)) |
266 |
> |
{ |
267 |
> |
binary_to_hex(md, buf, n); |
268 |
> |
client_p->certfp = xstrdup(buf); |
269 |
> |
} |
270 |
|
} |
271 |
+ |
else |
272 |
+ |
ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d", |
273 |
+ |
client_p->name, client_p->username, client_p->host, res); |
274 |
+ |
X509_free(cert); |
275 |
+ |
} |
276 |
|
|
277 |
< |
execute_callback(auth_cb, client_p); |
277 |
> |
start_auth(client_p); |
278 |
|
} |
279 |
|
#endif |
280 |
|
|
281 |
|
/* |
282 |
< |
* add_connection - creates a client which has just connected to us on |
282 |
> |
* add_connection - creates a client which has just connected to us on |
283 |
|
* the given fd. The sockhost field is initialized with the ip# of the host. |
284 |
|
* An unique id is calculated now, in case it is needed for auth. |
285 |
|
* The client is sent to the auth module for verification, and not put in |
286 |
|
* any client list yet. |
287 |
|
*/ |
288 |
|
void |
289 |
< |
add_connection(struct Listener* listener, int fd) |
289 |
> |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
290 |
|
{ |
291 |
< |
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 |
< |
*/ |
331 |
< |
|
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 |
< |
} |
291 |
> |
struct Client *client_p = make_client(NULL); |
292 |
|
|
293 |
< |
#ifdef IPV6 |
350 |
< |
remove_ipv6_mapping(&irn); |
351 |
< |
#else |
352 |
< |
irn.ss_len = len; |
353 |
< |
#endif |
354 |
< |
new_client = make_client(NULL); |
355 |
< |
fd_open(&new_client->localClient->fd, fd, 1, |
293 |
> |
fd_open(&client_p->localClient->fd, fd, 1, |
294 |
|
(listener->flags & LISTENER_SSL) ? |
295 |
< |
"Incoming SSL connection" : "Incoming connection"); |
358 |
< |
memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr)); |
295 |
> |
"Incoming SSL connection" : "Incoming connection"); |
296 |
|
|
297 |
< |
/* |
297 |
> |
/* |
298 |
|
* copy address to 'sockhost' as a string, copy it to host too |
299 |
|
* so we have something valid to put into error messages... |
300 |
|
*/ |
301 |
< |
new_client->localClient->port = ntohs(irn.ss_port); |
302 |
< |
memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr)); |
301 |
> |
memcpy(&client_p->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
302 |
> |
|
303 |
> |
getnameinfo((struct sockaddr *)&client_p->localClient->ip, |
304 |
> |
client_p->localClient->ip.ss_len, client_p->sockhost, |
305 |
> |
sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST); |
306 |
> |
client_p->localClient->aftype = client_p->localClient->ip.ss.ss_family; |
307 |
|
|
308 |
< |
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
309 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
310 |
< |
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
311 |
< |
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
312 |
< |
|
313 |
< |
*new_client->host = '\0'; |
314 |
< |
#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) |
379 |
< |
{ |
380 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
381 |
< |
strlcat(new_client->host, ".", HOSTLEN+1); |
382 |
< |
} else |
308 |
> |
#ifdef HAVE_LIBGEOIP |
309 |
> |
/* XXX IPV6 SUPPORT XXX */ |
310 |
> |
if (irn->ss.ss_family == AF_INET && geoip_ctx) |
311 |
> |
{ |
312 |
> |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)&client_p->localClient->ip; |
313 |
> |
client_p->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr)); |
314 |
> |
} |
315 |
|
#endif |
384 |
– |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
316 |
|
|
317 |
< |
new_client->localClient->listener = listener; |
318 |
< |
++listener->ref_count; |
317 |
> |
if (client_p->sockhost[0] == ':' && client_p->sockhost[1] == ':') |
318 |
> |
{ |
319 |
> |
strlcpy(client_p->host, "0", sizeof(client_p->host)); |
320 |
> |
strlcpy(client_p->host+1, client_p->sockhost, sizeof(client_p->host)-1); |
321 |
> |
memmove(client_p->sockhost+1, client_p->sockhost, sizeof(client_p->sockhost)-1); |
322 |
> |
client_p->sockhost[0] = '0'; |
323 |
> |
} |
324 |
> |
else |
325 |
> |
strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host)); |
326 |
|
|
327 |
< |
connect_id++; |
328 |
< |
new_client->connect_id = connect_id; |
327 |
> |
client_p->localClient->listener = listener; |
328 |
> |
++listener->ref_count; |
329 |
|
|
330 |
|
#ifdef HAVE_LIBCRYPTO |
331 |
< |
if ((listener->flags & LISTENER_SSL)) |
331 |
> |
if (listener->flags & LISTENER_SSL) |
332 |
|
{ |
333 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
333 |
> |
if ((client_p->localClient->fd.ssl = SSL_new(ConfigServerInfo.server_ctx)) == NULL) |
334 |
|
{ |
335 |
< |
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
335 |
> |
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
336 |
|
ERR_error_string(ERR_get_error(), NULL)); |
337 |
|
|
338 |
< |
SetDead(new_client); |
339 |
< |
exit_client(new_client, new_client, "SSL_new failed"); |
338 |
> |
SetDead(client_p); |
339 |
> |
exit_client(client_p, "SSL_new failed"); |
340 |
|
return; |
341 |
|
} |
342 |
|
|
343 |
< |
SSL_set_fd(new_client->localClient->fd.ssl, fd); |
344 |
< |
ssl_handshake(0, new_client); |
343 |
> |
AddFlag(client_p, FLAGS_SSL); |
344 |
> |
SSL_set_fd(client_p->localClient->fd.ssl, fd); |
345 |
> |
ssl_handshake(0, client_p); |
346 |
|
} |
347 |
|
else |
348 |
|
#endif |
349 |
< |
execute_callback(auth_cb, new_client); |
349 |
> |
start_auth(client_p); |
350 |
|
} |
351 |
|
|
352 |
|
/* |
401 |
|
* flush functions, and when comm_close() is implemented correctly |
402 |
|
* with close functions, we _actually_ don't call comm_close() here .. |
403 |
|
* -- originally Adrian's notes |
404 |
< |
* comm_close() is replaced with fd_close() in fdlist.c |
404 |
> |
* comm_close() is replaced with fd_close() in fdlist.c |
405 |
|
*/ |
406 |
|
void |
407 |
|
comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
421 |
|
* this will happen. |
422 |
|
*/ |
423 |
|
void |
424 |
< |
comm_checktimeouts(void *notused) |
424 |
> |
comm_checktimeouts(void *unused) |
425 |
|
{ |
426 |
|
int i; |
427 |
|
fde_t *F; |
476 |
|
void *data, int aftype, int timeout) |
477 |
|
{ |
478 |
|
struct addrinfo hints, *res; |
479 |
< |
char portname[PORTNAMELEN+1]; |
479 |
> |
char portname[PORTNAMELEN + 1]; |
480 |
|
|
481 |
|
assert(callback); |
482 |
|
fd->connect.callback = callback; |
493 |
|
* -- adrian |
494 |
|
*/ |
495 |
|
if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0)) |
496 |
< |
{ |
496 |
> |
{ |
497 |
|
/* Failure, call the callback with COMM_ERR_BIND */ |
498 |
|
comm_connect_callback(fd, COMM_ERR_BIND); |
499 |
|
/* ... and quit */ |
508 |
|
hints.ai_socktype = SOCK_STREAM; |
509 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
510 |
|
|
511 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
511 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
512 |
|
|
513 |
< |
if (irc_getaddrinfo(host, portname, &hints, &res)) |
513 |
> |
if (getaddrinfo(host, portname, &hints, &res)) |
514 |
|
{ |
515 |
|
/* Send the DNS request, for the next level */ |
516 |
< |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
517 |
< |
fd->dns_query->ptr = fd; |
518 |
< |
fd->dns_query->callback = comm_connect_dns_callback; |
519 |
< |
gethost_byname(host, fd->dns_query); |
516 |
> |
if (aftype == AF_INET6) |
517 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
518 |
> |
else |
519 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
520 |
|
} |
521 |
|
else |
522 |
|
{ |
526 |
|
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
527 |
|
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
528 |
|
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
529 |
< |
irc_freeaddrinfo(res); |
529 |
> |
freeaddrinfo(res); |
530 |
|
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
531 |
|
comm_connect_tryconnect(fd, NULL); |
532 |
|
} |
561 |
|
* called .. |
562 |
|
*/ |
563 |
|
static void |
564 |
< |
comm_connect_timeout(fde_t *fd, void *notused) |
564 |
> |
comm_connect_timeout(fde_t *fd, void *unused) |
565 |
|
{ |
566 |
|
/* error! */ |
567 |
|
comm_connect_callback(fd, COMM_ERR_TIMEOUT); |
574 |
|
* otherwise we initiate the connect() |
575 |
|
*/ |
576 |
|
static void |
577 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
577 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name, size_t namelength) |
578 |
|
{ |
579 |
|
fde_t *F = vptr; |
580 |
|
|
581 |
< |
if (reply == NULL) |
581 |
> |
if (!addr) |
582 |
|
{ |
644 |
– |
MyFree(F->dns_query); |
645 |
– |
F->dns_query = NULL; |
583 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
584 |
|
return; |
585 |
|
} |
590 |
|
/* Copy over the DNS reply info so we can use it in the connect() */ |
591 |
|
/* |
592 |
|
* Note we don't fudge the refcount here, because we aren't keeping |
593 |
< |
* the DNS record around, and the DNS cache is gone anyway.. |
593 |
> |
* the DNS record around, and the DNS cache is gone anyway.. |
594 |
|
* -- adrian |
595 |
|
*/ |
596 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
596 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
597 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
598 |
< |
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
599 |
< |
F->connect.hostaddr.ss_port; |
663 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
598 |
> |
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = F->connect.hostaddr.ss_port; |
599 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
600 |
|
|
601 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
666 |
– |
MyFree(F->dns_query); |
667 |
– |
F->dns_query = NULL; |
602 |
|
comm_connect_tryconnect(F, NULL); |
603 |
|
} |
604 |
|
|
605 |
< |
/* static void comm_connect_tryconnect(int fd, void *notused) |
605 |
> |
/* static void comm_connect_tryconnect(int fd, void *unused) |
606 |
|
* Input: The fd, the handler data(unused). |
607 |
|
* Output: None. |
608 |
|
* Side-effects: Try and connect with pending connect data for the FD. If |
611 |
|
* to select for a write event on this FD. |
612 |
|
*/ |
613 |
|
static void |
614 |
< |
comm_connect_tryconnect(fde_t *fd, void *notused) |
614 |
> |
comm_connect_tryconnect(fde_t *fd, void *unused) |
615 |
|
{ |
616 |
|
int retval; |
617 |
|
|
620 |
|
return; |
621 |
|
|
622 |
|
/* Try the connect() */ |
623 |
< |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
623 |
> |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
624 |
|
fd->connect.hostaddr.ss_len); |
625 |
|
|
626 |
|
/* Error? */ |
627 |
|
if (retval < 0) |
628 |
|
{ |
695 |
– |
#ifdef _WIN32 |
696 |
– |
errno = WSAGetLastError(); |
697 |
– |
#endif |
629 |
|
/* |
630 |
|
* If we get EISCONN, then we've already connect()ed the socket, |
631 |
|
* which is a good thing. |
684 |
|
*/ |
685 |
|
fd = socket(family, sock_type, proto); |
686 |
|
if (fd < 0) |
756 |
– |
{ |
757 |
– |
#ifdef _WIN32 |
758 |
– |
errno = WSAGetLastError(); |
759 |
– |
#endif |
687 |
|
return -1; /* errno will be passed through, yay.. */ |
761 |
– |
} |
688 |
|
|
689 |
< |
execute_callback(setup_socket_cb, fd); |
689 |
> |
setup_socket(fd); |
690 |
|
|
691 |
|
/* update things in our fd tracking */ |
692 |
|
fd_open(F, fd, 1, note); |
701 |
|
* fd_open (this function no longer does it). |
702 |
|
*/ |
703 |
|
int |
704 |
< |
comm_accept(struct Listener *lptr, struct irc_ssaddr *pn) |
704 |
> |
comm_accept(struct Listener *lptr, struct irc_ssaddr *addr) |
705 |
|
{ |
706 |
|
int newfd; |
707 |
|
socklen_t addrlen = sizeof(struct irc_ssaddr); |
712 |
|
return -1; |
713 |
|
} |
714 |
|
|
715 |
+ |
memset(addr, 0, sizeof(struct irc_ssaddr)); |
716 |
+ |
|
717 |
|
/* |
718 |
|
* Next, do the accept(). if we get an error, we should drop the |
719 |
|
* reserved fd limit, but we can deal with that when comm_open() |
720 |
|
* also does it. XXX -- adrian |
721 |
|
*/ |
722 |
< |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
722 |
> |
newfd = accept(lptr->fd.fd, (struct sockaddr *)addr, &addrlen); |
723 |
|
if (newfd < 0) |
796 |
– |
{ |
797 |
– |
#ifdef _WIN32 |
798 |
– |
errno = WSAGetLastError(); |
799 |
– |
#endif |
724 |
|
return -1; |
801 |
– |
} |
725 |
|
|
726 |
< |
#ifdef IPV6 |
804 |
< |
remove_ipv6_mapping(pn); |
805 |
< |
#else |
806 |
< |
pn->ss_len = addrlen; |
807 |
< |
#endif |
726 |
> |
remove_ipv6_mapping(addr); |
727 |
|
|
728 |
< |
execute_callback(setup_socket_cb, newfd); |
728 |
> |
setup_socket(newfd); |
729 |
|
|
730 |
|
/* .. and return */ |
731 |
|
return newfd; |
732 |
|
} |
733 |
|
|
734 |
< |
/* |
734 |
> |
/* |
735 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
736 |
< |
* 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 |
736 |
> |
* OSes with IPv6 mapping listening on both |
737 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
738 |
< |
* |
738 |
> |
* |
739 |
|
*/ |
822 |
– |
#ifdef IPV6 |
740 |
|
void |
741 |
|
remove_ipv6_mapping(struct irc_ssaddr *addr) |
742 |
|
{ |
743 |
|
if (addr->ss.ss_family == AF_INET6) |
744 |
|
{ |
745 |
< |
struct sockaddr_in6 *v6; |
829 |
< |
|
830 |
< |
v6 = (struct sockaddr_in6*)addr; |
831 |
< |
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) |
745 |
> |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
746 |
|
{ |
747 |
< |
char v4ip[HOSTIPLEN]; |
748 |
< |
struct sockaddr_in *v4 = (struct sockaddr_in*)addr; |
749 |
< |
inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN); |
750 |
< |
inet_pton(AF_INET, v4ip, &v4->sin_addr); |
747 |
> |
struct sockaddr_in6 v6; |
748 |
> |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
749 |
> |
|
750 |
> |
memcpy(&v6, addr, sizeof(v6)); |
751 |
> |
memset(v4, 0, sizeof(struct sockaddr_in)); |
752 |
> |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
753 |
> |
|
754 |
|
addr->ss.ss_family = AF_INET; |
755 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
756 |
|
} |
757 |
< |
else |
757 |
> |
else |
758 |
|
addr->ss_len = sizeof(struct sockaddr_in6); |
759 |
|
} |
760 |
|
else |
761 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
762 |
< |
} |
846 |
< |
#endif |
762 |
> |
} |