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" |
26 |
– |
#ifndef _WIN32 |
28 |
|
#include <netinet/in_systm.h> |
29 |
|
#include <netinet/ip.h> |
30 |
|
#include <netinet/tcp.h> |
31 |
< |
#endif |
31 |
> |
#include "list.h" |
32 |
|
#include "fdlist.h" |
33 |
|
#include "s_bsd.h" |
34 |
|
#include "client.h" |
34 |
– |
#include "common.h" |
35 |
|
#include "dbuf.h" |
36 |
|
#include "event.h" |
37 |
|
#include "irc_string.h" |
38 |
– |
#include "irc_getnameinfo.h" |
39 |
– |
#include "irc_getaddrinfo.h" |
38 |
|
#include "ircd.h" |
41 |
– |
#include "list.h" |
39 |
|
#include "listener.h" |
40 |
|
#include "numeric.h" |
41 |
|
#include "packet.h" |
42 |
< |
#include "irc_res.h" |
46 |
< |
#include "inet_misc.h" |
42 |
> |
#include "res.h" |
43 |
|
#include "restart.h" |
44 |
< |
#include "s_auth.h" |
45 |
< |
#include "s_conf.h" |
46 |
< |
#include "s_log.h" |
47 |
< |
#include "s_serv.h" |
44 |
> |
#include "auth.h" |
45 |
> |
#include "conf.h" |
46 |
> |
#include "log.h" |
47 |
> |
#include "server.h" |
48 |
|
#include "send.h" |
49 |
|
#include "memory.h" |
50 |
< |
#include "s_user.h" |
51 |
< |
#include "hook.h" |
50 |
> |
#include "user.h" |
51 |
> |
|
52 |
|
|
53 |
|
static const char *comm_err_str[] = { "Comm OK", "Error during bind()", |
54 |
|
"Error during DNS lookup", "connect timeout", "Error during connect()", |
55 |
|
"Comm Error" }; |
56 |
|
|
57 |
< |
struct Callback *setup_socket_cb = NULL; |
62 |
< |
|
63 |
< |
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; |
106 |
|
} |
107 |
|
|
108 |
|
/* |
109 |
< |
* report_error - report an error from an errno. |
109 |
> |
* report_error - report an error from an errno. |
110 |
|
* Record error to log and also send a copy to all *LOCAL* opers online. |
111 |
|
* |
112 |
|
* text is a *format* string for outputing error. It must |
120 |
|
* Cannot use perror() within daemon. stderr is closed in |
121 |
|
* ircd and cannot be used. And, worse yet, it might have |
122 |
|
* been reassigned to a normal connection... |
123 |
< |
* |
123 |
> |
* |
124 |
|
* Actually stderr is still there IFF ircd was run with -s --Rodder |
125 |
|
*/ |
126 |
|
|
127 |
|
void |
128 |
< |
report_error(int level, const char* text, const char* who, int error) |
128 |
> |
report_error(int level, const char* text, const char* who, int error) |
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 |
|
/* |
139 |
|
* |
140 |
|
* Set the socket non-blocking, and other wonderful bits. |
141 |
|
*/ |
142 |
< |
static void * |
143 |
< |
setup_socket(va_list args) |
142 |
> |
static void |
143 |
> |
setup_socket(int fd) |
144 |
|
{ |
160 |
– |
int fd = va_arg(args, int); |
145 |
|
int opt = 1; |
146 |
|
|
147 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
147 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
148 |
|
|
149 |
|
#ifdef IPTOS_LOWDELAY |
150 |
|
opt = IPTOS_LOWDELAY; |
151 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
151 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
152 |
|
#endif |
153 |
|
|
170 |
– |
#ifndef _WIN32 |
154 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
172 |
– |
#endif |
173 |
– |
|
174 |
– |
return NULL; |
175 |
– |
} |
176 |
– |
|
177 |
– |
/* |
178 |
– |
* init_comm() |
179 |
– |
* |
180 |
– |
* Initializes comm subsystem. |
181 |
– |
*/ |
182 |
– |
void |
183 |
– |
init_comm(void) |
184 |
– |
{ |
185 |
– |
setup_socket_cb = register_callback("setup_socket", setup_socket); |
186 |
– |
init_netio(); |
155 |
|
} |
156 |
|
|
157 |
|
/* |
162 |
|
void |
163 |
|
close_connection(struct Client *client_p) |
164 |
|
{ |
165 |
< |
struct ConfItem *conf; |
198 |
< |
struct AccessItem *aconf; |
199 |
< |
struct ClassItem *aclass; |
165 |
> |
dlink_node *ptr = NULL; |
166 |
|
|
167 |
< |
assert(NULL != client_p); |
167 |
> |
assert(client_p); |
168 |
|
|
169 |
|
if (!IsDead(client_p)) |
170 |
|
{ |
173 |
|
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
174 |
|
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
175 |
|
*/ |
176 |
< |
ClearSendqBlocked(client_p); |
176 |
> |
DelFlag(client_p, FLAGS_BLOCKED); |
177 |
|
send_queued_write(client_p); |
178 |
|
} |
179 |
|
|
180 |
< |
if (IsServer(client_p)) |
180 |
> |
if (IsClient(client_p)) |
181 |
> |
{ |
182 |
> |
++ServerStats.is_cl; |
183 |
> |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
184 |
> |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
185 |
> |
ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime; |
186 |
> |
} |
187 |
> |
else if (IsServer(client_p)) |
188 |
|
{ |
189 |
|
++ServerStats.is_sv; |
190 |
|
ServerStats.is_sbs += client_p->localClient->send.bytes; |
191 |
|
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
192 |
< |
ServerStats.is_sti += CurrentTime - client_p->firsttime; |
192 |
> |
ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime; |
193 |
|
|
194 |
< |
/* 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))) |
194 |
> |
DLINK_FOREACH(ptr, server_items.head) |
195 |
|
{ |
196 |
+ |
struct MaskItem *conf = ptr->data; |
197 |
+ |
|
198 |
+ |
if (irccmp(conf->name, client_p->name)) |
199 |
+ |
continue; |
200 |
+ |
|
201 |
|
/* |
202 |
< |
* Reschedule a faster reconnect, if this was a automatically |
203 |
< |
* 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. |
202 |
> |
* Reset next-connect cycle of all connect{} blocks that match |
203 |
> |
* this servername. |
204 |
|
*/ |
205 |
< |
aconf = (struct AccessItem *)map_to_conf(conf); |
240 |
< |
aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr); |
241 |
< |
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; |
205 |
> |
conf->until = CurrentTime + conf->class->con_freq; |
206 |
|
} |
207 |
|
} |
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 |
– |
} |
208 |
|
else |
209 |
|
++ServerStats.is_ni; |
210 |
|
|
220 |
|
if (client_p->localClient->fd.flags.open) |
221 |
|
fd_close(&client_p->localClient->fd); |
222 |
|
|
270 |
– |
if (HasServlink(client_p)) |
271 |
– |
{ |
272 |
– |
if (client_p->localClient->ctrlfd.flags.open) |
273 |
– |
fd_close(&client_p->localClient->ctrlfd); |
274 |
– |
} |
275 |
– |
|
223 |
|
dbuf_clear(&client_p->localClient->buf_sendq); |
224 |
|
dbuf_clear(&client_p->localClient->buf_recvq); |
225 |
< |
|
225 |
> |
|
226 |
|
MyFree(client_p->localClient->passwd); |
227 |
< |
detach_conf(client_p, CONF_TYPE); |
281 |
< |
client_p->from = NULL; /* ...this should catch them! >:) --msa */ |
227 |
> |
detach_conf(client_p, CONF_CLIENT|CONF_OPER|CONF_SERVER); |
228 |
|
} |
229 |
|
|
230 |
|
#ifdef HAVE_LIBCRYPTO |
235 |
|
static void |
236 |
|
ssl_handshake(int fd, struct Client *client_p) |
237 |
|
{ |
238 |
< |
int ret = SSL_accept(client_p->localClient->fd.ssl); |
238 |
> |
X509 *cert = NULL; |
239 |
> |
int ret = 0; |
240 |
> |
|
241 |
> |
if ((ret = SSL_accept(client_p->localClient->fd.ssl)) <= 0) |
242 |
> |
{ |
243 |
> |
if ((CurrentTime - client_p->localClient->firsttime) > 30) |
244 |
> |
{ |
245 |
> |
exit_client(client_p, "Timeout during SSL handshake"); |
246 |
> |
return; |
247 |
> |
} |
248 |
|
|
294 |
– |
if (ret <= 0) |
249 |
|
switch (SSL_get_error(client_p->localClient->fd.ssl, ret)) |
250 |
|
{ |
251 |
|
case SSL_ERROR_WANT_WRITE: |
252 |
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE, |
253 |
< |
(PF *) ssl_handshake, client_p, 0); |
253 |
> |
(PF *)ssl_handshake, client_p, 30); |
254 |
|
return; |
255 |
|
|
256 |
|
case SSL_ERROR_WANT_READ: |
257 |
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, |
258 |
< |
(PF *) ssl_handshake, client_p, 0); |
258 |
> |
(PF *)ssl_handshake, client_p, 30); |
259 |
|
return; |
260 |
|
|
261 |
|
default: |
262 |
< |
exit_client(client_p, client_p, "Error during SSL handshake"); |
263 |
< |
return; |
262 |
> |
exit_client(client_p, "Error during SSL handshake"); |
263 |
> |
return; |
264 |
|
} |
265 |
+ |
} |
266 |
+ |
|
267 |
+ |
comm_settimeout(&client_p->localClient->fd, 0, NULL, NULL); |
268 |
|
|
269 |
< |
execute_callback(auth_cb, client_p); |
269 |
> |
if ((cert = SSL_get_peer_certificate(client_p->localClient->fd.ssl))) |
270 |
> |
{ |
271 |
> |
int res = SSL_get_verify_result(client_p->localClient->fd.ssl); |
272 |
> |
char buf[EVP_MAX_MD_SIZE * 2 + 1] = { '\0' }; |
273 |
> |
unsigned char md[EVP_MAX_MD_SIZE] = { '\0' }; |
274 |
> |
|
275 |
> |
if (res == X509_V_OK || res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || |
276 |
> |
res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || |
277 |
> |
res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) |
278 |
> |
{ |
279 |
> |
unsigned int i = 0, n = 0; |
280 |
> |
|
281 |
> |
if (X509_digest(cert, EVP_sha256(), md, &n)) |
282 |
> |
{ |
283 |
> |
for (; i < n; ++i) |
284 |
> |
snprintf(buf + 2 * i, 3, "%02X", md[i]); |
285 |
> |
client_p->certfp = xstrdup(buf); |
286 |
> |
} |
287 |
> |
} |
288 |
> |
else |
289 |
> |
ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad SSL client certificate: %d", |
290 |
> |
client_p->name, client_p->username, client_p->host, res); |
291 |
> |
X509_free(cert); |
292 |
> |
} |
293 |
> |
|
294 |
> |
start_auth(client_p); |
295 |
|
} |
296 |
|
#endif |
297 |
|
|
298 |
|
/* |
299 |
< |
* add_connection - creates a client which has just connected to us on |
299 |
> |
* add_connection - creates a client which has just connected to us on |
300 |
|
* the given fd. The sockhost field is initialized with the ip# of the host. |
301 |
|
* An unique id is calculated now, in case it is needed for auth. |
302 |
|
* The client is sent to the auth module for verification, and not put in |
305 |
|
void |
306 |
|
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
307 |
|
{ |
308 |
< |
struct Client *new_client; |
327 |
< |
|
328 |
< |
assert(NULL != listener); |
308 |
> |
struct Client *client_p = make_client(NULL); |
309 |
|
|
310 |
< |
new_client = make_client(NULL); |
331 |
< |
|
332 |
< |
fd_open(&new_client->localClient->fd, fd, 1, |
310 |
> |
fd_open(&client_p->localClient->fd, fd, 1, |
311 |
|
(listener->flags & LISTENER_SSL) ? |
312 |
< |
"Incoming SSL connection" : "Incoming connection"); |
312 |
> |
"Incoming SSL connection" : "Incoming connection"); |
313 |
|
|
314 |
< |
/* |
314 |
> |
/* |
315 |
|
* copy address to 'sockhost' as a string, copy it to host too |
316 |
|
* so we have something valid to put into error messages... |
317 |
|
*/ |
318 |
< |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
318 |
> |
memcpy(&client_p->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
319 |
|
|
320 |
< |
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
321 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
322 |
< |
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
323 |
< |
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); |
320 |
> |
getnameinfo((struct sockaddr *)&client_p->localClient->ip, |
321 |
> |
client_p->localClient->ip.ss_len, client_p->sockhost, |
322 |
> |
sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST); |
323 |
> |
client_p->localClient->aftype = client_p->localClient->ip.ss.ss_family; |
324 |
|
|
325 |
< |
if (new_client->localClient->aftype == AF_INET6 && |
326 |
< |
ConfigFileEntry.dot_in_ip6_addr == 1) |
325 |
> |
#ifdef HAVE_LIBGEOIP |
326 |
> |
/* XXX IPV6 SUPPORT XXX */ |
327 |
> |
if (irn->ss.ss_family == AF_INET && geoip_ctx) |
328 |
|
{ |
329 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
330 |
< |
strlcat(new_client->host, ".", HOSTLEN+1); |
329 |
> |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)&client_p->localClient->ip; |
330 |
> |
client_p->localClient->country_id = GeoIP_id_by_ipnum(geoip_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr)); |
331 |
|
} |
356 |
– |
else |
332 |
|
#endif |
358 |
– |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
333 |
|
|
334 |
< |
new_client->connect_id = ++connect_id; |
335 |
< |
new_client->localClient->listener = listener; |
334 |
> |
if (client_p->sockhost[0] == ':' && client_p->sockhost[1] == ':') |
335 |
> |
{ |
336 |
> |
strlcpy(client_p->host, "0", sizeof(client_p->host)); |
337 |
> |
strlcpy(client_p->host+1, client_p->sockhost, sizeof(client_p->host)-1); |
338 |
> |
memmove(client_p->sockhost+1, client_p->sockhost, sizeof(client_p->sockhost)-1); |
339 |
> |
client_p->sockhost[0] = '0'; |
340 |
> |
} |
341 |
> |
else |
342 |
> |
strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host)); |
343 |
> |
|
344 |
> |
client_p->localClient->listener = listener; |
345 |
|
++listener->ref_count; |
346 |
|
|
347 |
|
#ifdef HAVE_LIBCRYPTO |
348 |
|
if (listener->flags & LISTENER_SSL) |
349 |
|
{ |
350 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
350 |
> |
if ((client_p->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
351 |
|
{ |
352 |
< |
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
352 |
> |
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
353 |
|
ERR_error_string(ERR_get_error(), NULL)); |
354 |
|
|
355 |
< |
SetDead(new_client); |
356 |
< |
exit_client(new_client, new_client, "SSL_new failed"); |
355 |
> |
SetDead(client_p); |
356 |
> |
exit_client(client_p, "SSL_new failed"); |
357 |
|
return; |
358 |
|
} |
359 |
|
|
360 |
< |
SSL_set_fd(new_client->localClient->fd.ssl, fd); |
361 |
< |
ssl_handshake(0, new_client); |
360 |
> |
AddFlag(client_p, FLAGS_SSL); |
361 |
> |
SSL_set_fd(client_p->localClient->fd.ssl, fd); |
362 |
> |
ssl_handshake(0, client_p); |
363 |
|
} |
364 |
|
else |
365 |
|
#endif |
366 |
< |
execute_callback(auth_cb, new_client); |
366 |
> |
start_auth(client_p); |
367 |
|
} |
368 |
|
|
369 |
|
/* |
418 |
|
* flush functions, and when comm_close() is implemented correctly |
419 |
|
* with close functions, we _actually_ don't call comm_close() here .. |
420 |
|
* -- originally Adrian's notes |
421 |
< |
* comm_close() is replaced with fd_close() in fdlist.c |
421 |
> |
* comm_close() is replaced with fd_close() in fdlist.c |
422 |
|
*/ |
423 |
|
void |
424 |
|
comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
493 |
|
void *data, int aftype, int timeout) |
494 |
|
{ |
495 |
|
struct addrinfo hints, *res; |
496 |
< |
char portname[PORTNAMELEN+1]; |
496 |
> |
char portname[PORTNAMELEN + 1]; |
497 |
|
|
498 |
|
assert(callback); |
499 |
|
fd->connect.callback = callback; |
510 |
|
* -- adrian |
511 |
|
*/ |
512 |
|
if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0)) |
513 |
< |
{ |
513 |
> |
{ |
514 |
|
/* Failure, call the callback with COMM_ERR_BIND */ |
515 |
|
comm_connect_callback(fd, COMM_ERR_BIND); |
516 |
|
/* ... and quit */ |
525 |
|
hints.ai_socktype = SOCK_STREAM; |
526 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
527 |
|
|
528 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
528 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
529 |
|
|
530 |
< |
if (irc_getaddrinfo(host, portname, &hints, &res)) |
530 |
> |
if (getaddrinfo(host, portname, &hints, &res)) |
531 |
|
{ |
532 |
|
/* Send the DNS request, for the next level */ |
549 |
– |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
550 |
– |
fd->dns_query->ptr = fd; |
551 |
– |
fd->dns_query->callback = comm_connect_dns_callback; |
533 |
|
if (aftype == AF_INET6) |
534 |
< |
gethost_byname_type(host, fd->dns_query, T_AAAA); |
534 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
535 |
|
else |
536 |
< |
gethost_byname_type(host, fd->dns_query, T_A); |
536 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
537 |
|
} |
538 |
|
else |
539 |
|
{ |
543 |
|
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
544 |
|
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
545 |
|
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
546 |
< |
irc_freeaddrinfo(res); |
546 |
> |
freeaddrinfo(res); |
547 |
|
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
548 |
|
comm_connect_tryconnect(fd, NULL); |
549 |
|
} |
591 |
|
* otherwise we initiate the connect() |
592 |
|
*/ |
593 |
|
static void |
594 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
594 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
595 |
|
{ |
596 |
|
fde_t *F = vptr; |
597 |
|
|
598 |
< |
if (reply == NULL) |
598 |
> |
if (name == NULL) |
599 |
|
{ |
619 |
– |
MyFree(F->dns_query); |
620 |
– |
F->dns_query = NULL; |
600 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
601 |
|
return; |
602 |
|
} |
607 |
|
/* Copy over the DNS reply info so we can use it in the connect() */ |
608 |
|
/* |
609 |
|
* Note we don't fudge the refcount here, because we aren't keeping |
610 |
< |
* the DNS record around, and the DNS cache is gone anyway.. |
610 |
> |
* the DNS record around, and the DNS cache is gone anyway.. |
611 |
|
* -- adrian |
612 |
|
*/ |
613 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
613 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
614 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
615 |
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
616 |
|
F->connect.hostaddr.ss_port; |
617 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
617 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
618 |
|
|
619 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
641 |
– |
MyFree(F->dns_query); |
642 |
– |
F->dns_query = NULL; |
620 |
|
comm_connect_tryconnect(F, NULL); |
621 |
|
} |
622 |
|
|
638 |
|
return; |
639 |
|
|
640 |
|
/* Try the connect() */ |
641 |
< |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
641 |
> |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
642 |
|
fd->connect.hostaddr.ss_len); |
643 |
|
|
644 |
|
/* Error? */ |
645 |
|
if (retval < 0) |
646 |
|
{ |
670 |
– |
#ifdef _WIN32 |
671 |
– |
errno = WSAGetLastError(); |
672 |
– |
#endif |
647 |
|
/* |
648 |
|
* If we get EISCONN, then we've already connect()ed the socket, |
649 |
|
* which is a good thing. |
702 |
|
*/ |
703 |
|
fd = socket(family, sock_type, proto); |
704 |
|
if (fd < 0) |
731 |
– |
{ |
732 |
– |
#ifdef _WIN32 |
733 |
– |
errno = WSAGetLastError(); |
734 |
– |
#endif |
705 |
|
return -1; /* errno will be passed through, yay.. */ |
736 |
– |
} |
706 |
|
|
707 |
< |
execute_callback(setup_socket_cb, fd); |
707 |
> |
setup_socket(fd); |
708 |
|
|
709 |
|
/* update things in our fd tracking */ |
710 |
|
fd_open(F, fd, 1, note); |
735 |
|
* reserved fd limit, but we can deal with that when comm_open() |
736 |
|
* also does it. XXX -- adrian |
737 |
|
*/ |
738 |
< |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
738 |
> |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen); |
739 |
|
if (newfd < 0) |
771 |
– |
{ |
772 |
– |
#ifdef _WIN32 |
773 |
– |
errno = WSAGetLastError(); |
774 |
– |
#endif |
740 |
|
return -1; |
776 |
– |
} |
741 |
|
|
742 |
|
#ifdef IPV6 |
743 |
|
remove_ipv6_mapping(pn); |
745 |
|
pn->ss_len = addrlen; |
746 |
|
#endif |
747 |
|
|
748 |
< |
execute_callback(setup_socket_cb, newfd); |
748 |
> |
setup_socket(newfd); |
749 |
|
|
750 |
|
/* .. and return */ |
751 |
|
return newfd; |
752 |
|
} |
753 |
|
|
754 |
< |
/* |
754 |
> |
/* |
755 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
756 |
< |
* This function should really inspect the struct itself rather than relying |
793 |
< |
* on inet_pton and inet_ntop. OSes with IPv6 mapping listening on both |
756 |
> |
* OSes with IPv6 mapping listening on both |
757 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
758 |
< |
* |
758 |
> |
* |
759 |
|
*/ |
760 |
|
#ifdef IPV6 |
761 |
|
void |
763 |
|
{ |
764 |
|
if (addr->ss.ss_family == AF_INET6) |
765 |
|
{ |
766 |
< |
struct sockaddr_in6 *v6; |
804 |
< |
|
805 |
< |
v6 = (struct sockaddr_in6*)addr; |
806 |
< |
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) |
766 |
> |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
767 |
|
{ |
768 |
< |
char v4ip[HOSTIPLEN]; |
769 |
< |
struct sockaddr_in *v4 = (struct sockaddr_in*)addr; |
770 |
< |
inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN); |
771 |
< |
inet_pton(AF_INET, v4ip, &v4->sin_addr); |
768 |
> |
struct sockaddr_in6 v6; |
769 |
> |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
770 |
> |
|
771 |
> |
memcpy(&v6, addr, sizeof(v6)); |
772 |
> |
memset(v4, 0, sizeof(struct sockaddr_in)); |
773 |
> |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
774 |
> |
|
775 |
|
addr->ss.ss_family = AF_INET; |
776 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
777 |
|
} |
778 |
< |
else |
778 |
> |
else |
779 |
|
addr->ss_len = sizeof(struct sockaddr_in6); |
780 |
|
} |
781 |
|
else |
782 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
783 |
< |
} |
783 |
> |
} |
784 |
|
#endif |