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-2017 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 |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU General Public License |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
< |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
18 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
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" |
39 |
|
#include "listener.h" |
40 |
|
#include "numeric.h" |
41 |
|
#include "packet.h" |
42 |
< |
#include "irc_res.h" |
42 |
> |
#include "res.h" |
43 |
|
#include "restart.h" |
44 |
< |
#include "s_auth.h" |
44 |
> |
#include "auth.h" |
45 |
|
#include "conf.h" |
46 |
|
#include "log.h" |
47 |
< |
#include "s_serv.h" |
47 |
> |
#include "server.h" |
48 |
|
#include "send.h" |
49 |
|
#include "memory.h" |
50 |
< |
#include "s_user.h" |
49 |
< |
#include "hook.h" |
50 |
> |
#include "user.h" |
51 |
|
|
51 |
– |
static const char *comm_err_str[] = { "Comm OK", "Error during bind()", |
52 |
– |
"Error during DNS lookup", "connect timeout", "Error during connect()", |
53 |
– |
"Comm Error" }; |
52 |
|
|
53 |
< |
struct Callback *setup_socket_cb = NULL; |
53 |
> |
static const char *const comm_err_str[] = |
54 |
> |
{ |
55 |
> |
[COMM_OK] = "Comm OK", |
56 |
> |
[COMM_ERR_BIND] = "Error during bind()", |
57 |
> |
[COMM_ERR_DNS] = "Error during DNS lookup", |
58 |
> |
[COMM_ERR_TIMEOUT] = "connect timeout", |
59 |
> |
[COMM_ERR_CONNECT] = "Error during connect()", |
60 |
> |
[COMM_ERROR] = "Comm Error" |
61 |
> |
}; |
62 |
|
|
63 |
|
static void comm_connect_callback(fde_t *, int); |
64 |
< |
static PF comm_connect_timeout; |
65 |
< |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
66 |
< |
static PF comm_connect_tryconnect; |
61 |
< |
|
64 |
> |
static void comm_connect_timeout(fde_t *, void *); |
65 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *, size_t); |
66 |
> |
static void comm_connect_tryconnect(fde_t *, void *); |
67 |
|
|
63 |
– |
/* check_can_use_v6() |
64 |
– |
* Check if the system can open AF_INET6 sockets |
65 |
– |
*/ |
66 |
– |
void |
67 |
– |
check_can_use_v6(void) |
68 |
– |
{ |
69 |
– |
#ifdef IPV6 |
70 |
– |
int v6; |
71 |
– |
|
72 |
– |
if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) |
73 |
– |
ServerInfo.can_use_v6 = 0; |
74 |
– |
else |
75 |
– |
{ |
76 |
– |
ServerInfo.can_use_v6 = 1; |
77 |
– |
close(v6); |
78 |
– |
} |
79 |
– |
#else |
80 |
– |
ServerInfo.can_use_v6 = 0; |
81 |
– |
#endif |
82 |
– |
} |
68 |
|
|
69 |
|
/* get_sockerr - get the error value from the socket or the current errno |
70 |
|
* |
91 |
|
} |
92 |
|
|
93 |
|
/* |
94 |
< |
* report_error - report an error from an errno. |
94 |
> |
* report_error - report an error from an errno. |
95 |
|
* Record error to log and also send a copy to all *LOCAL* opers online. |
96 |
|
* |
97 |
|
* text is a *format* string for outputing error. It must |
105 |
|
* Cannot use perror() within daemon. stderr is closed in |
106 |
|
* ircd and cannot be used. And, worse yet, it might have |
107 |
|
* been reassigned to a normal connection... |
108 |
< |
* |
108 |
> |
* |
109 |
|
* Actually stderr is still there IFF ircd was run with -s --Rodder |
110 |
|
*/ |
111 |
|
|
112 |
|
void |
113 |
< |
report_error(int level, const char* text, const char* who, int error) |
113 |
> |
report_error(int level, const char* text, const char* who, int error) |
114 |
|
{ |
115 |
|
who = (who) ? who : ""; |
116 |
|
|
117 |
< |
sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error)); |
117 |
> |
sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE, |
118 |
> |
text, who, strerror(error)); |
119 |
|
ilog(LOG_TYPE_IRCD, text, who, strerror(error)); |
120 |
|
} |
121 |
|
|
124 |
|
* |
125 |
|
* Set the socket non-blocking, and other wonderful bits. |
126 |
|
*/ |
127 |
< |
static void * |
128 |
< |
setup_socket(va_list args) |
127 |
> |
static void |
128 |
> |
setup_socket(int fd) |
129 |
|
{ |
144 |
– |
int fd = va_arg(args, int); |
130 |
|
int opt = 1; |
131 |
|
|
132 |
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
137 |
|
#endif |
138 |
|
|
139 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
155 |
– |
|
156 |
– |
return NULL; |
157 |
– |
} |
158 |
– |
|
159 |
– |
/* |
160 |
– |
* init_comm() |
161 |
– |
* |
162 |
– |
* Initializes comm subsystem. |
163 |
– |
*/ |
164 |
– |
void |
165 |
– |
init_comm(void) |
166 |
– |
{ |
167 |
– |
setup_socket_cb = register_callback("setup_socket", setup_socket); |
168 |
– |
init_netio(); |
140 |
|
} |
141 |
|
|
142 |
|
/* |
147 |
|
void |
148 |
|
close_connection(struct Client *client_p) |
149 |
|
{ |
150 |
< |
struct ConfItem *conf; |
180 |
< |
struct AccessItem *aconf; |
181 |
< |
struct ClassItem *aclass; |
182 |
< |
|
183 |
< |
assert(NULL != client_p); |
150 |
> |
assert(client_p); |
151 |
|
|
152 |
|
if (!IsDead(client_p)) |
153 |
|
{ |
156 |
|
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
157 |
|
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
158 |
|
*/ |
159 |
< |
ClearSendqBlocked(client_p); |
159 |
> |
DelFlag(client_p, FLAGS_BLOCKED); |
160 |
|
send_queued_write(client_p); |
161 |
|
} |
162 |
|
|
163 |
|
if (IsClient(client_p)) |
164 |
|
{ |
165 |
|
++ServerStats.is_cl; |
166 |
< |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
167 |
< |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
168 |
< |
ServerStats.is_cti += CurrentTime - client_p->localClient->firsttime; |
166 |
> |
ServerStats.is_cbs += client_p->connection->send.bytes; |
167 |
> |
ServerStats.is_cbr += client_p->connection->recv.bytes; |
168 |
> |
ServerStats.is_cti += CurrentTime - client_p->connection->firsttime; |
169 |
|
} |
170 |
|
else if (IsServer(client_p)) |
171 |
|
{ |
172 |
+ |
dlink_node *node = NULL; |
173 |
+ |
|
174 |
|
++ServerStats.is_sv; |
175 |
< |
ServerStats.is_sbs += client_p->localClient->send.bytes; |
176 |
< |
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
177 |
< |
ServerStats.is_sti += CurrentTime - client_p->localClient->firsttime; |
178 |
< |
|
179 |
< |
/* XXX Does this even make any sense at all anymore? |
211 |
< |
* scheduling a 'quick' reconnect could cause a pile of |
212 |
< |
* nick collides under TSora protocol... -db |
213 |
< |
*/ |
214 |
< |
/* |
215 |
< |
* If the connection has been up for a long amount of time, schedule |
216 |
< |
* a 'quick' reconnect, else reset the next-connect cycle. |
217 |
< |
*/ |
218 |
< |
if ((conf = find_conf_exact(SERVER_TYPE, client_p->name, |
219 |
< |
client_p->username, client_p->host))) |
175 |
> |
ServerStats.is_sbs += client_p->connection->send.bytes; |
176 |
> |
ServerStats.is_sbr += client_p->connection->recv.bytes; |
177 |
> |
ServerStats.is_sti += CurrentTime - client_p->connection->firsttime; |
178 |
> |
|
179 |
> |
DLINK_FOREACH(node, connect_items.head) |
180 |
|
{ |
181 |
+ |
struct MaskItem *conf = node->data; |
182 |
+ |
|
183 |
+ |
if (irccmp(conf->name, client_p->name)) |
184 |
+ |
continue; |
185 |
+ |
|
186 |
|
/* |
187 |
< |
* Reschedule a faster reconnect, if this was a automatically |
188 |
< |
* connected configuration entry. (Note that if we have had |
224 |
< |
* a rehash in between, the status has been changed to |
225 |
< |
* CONF_ILLEGAL). But only do this if it was a "good" link. |
187 |
> |
* Reset next-connect cycle of all connect{} blocks that match |
188 |
> |
* this servername. |
189 |
|
*/ |
190 |
< |
aconf = map_to_conf(conf); |
228 |
< |
aclass = map_to_conf(aconf->class_ptr); |
229 |
< |
aconf->hold = time(NULL); |
230 |
< |
aconf->hold += (aconf->hold - client_p->localClient->since > HANGONGOODLINK) ? |
231 |
< |
HANGONRETRYDELAY : aclass->con_freq; |
190 |
> |
conf->until = CurrentTime + conf->class->con_freq; |
191 |
|
} |
192 |
|
} |
193 |
|
else |
194 |
|
++ServerStats.is_ni; |
195 |
|
|
196 |
< |
#ifdef HAVE_LIBCRYPTO |
197 |
< |
if (client_p->localClient->fd.ssl) |
239 |
< |
{ |
240 |
< |
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
196 |
> |
if (tls_isusing(&client_p->connection->fd.ssl)) |
197 |
> |
tls_shutdown(&client_p->connection->fd.ssl); |
198 |
|
|
199 |
< |
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
200 |
< |
SSL_shutdown(client_p->localClient->fd.ssl); |
244 |
< |
} |
245 |
< |
#endif |
246 |
< |
if (client_p->localClient->fd.flags.open) |
247 |
< |
fd_close(&client_p->localClient->fd); |
199 |
> |
if (client_p->connection->fd.flags.open) |
200 |
> |
fd_close(&client_p->connection->fd); |
201 |
|
|
202 |
< |
dbuf_clear(&client_p->localClient->buf_sendq); |
203 |
< |
dbuf_clear(&client_p->localClient->buf_recvq); |
204 |
< |
|
205 |
< |
MyFree(client_p->localClient->passwd); |
206 |
< |
detach_conf(client_p, CONF_TYPE); |
207 |
< |
client_p->from = NULL; /* ...this should catch them! >:) --msa */ |
202 |
> |
dbuf_clear(&client_p->connection->buf_sendq); |
203 |
> |
dbuf_clear(&client_p->connection->buf_recvq); |
204 |
> |
|
205 |
> |
xfree(client_p->connection->password); |
206 |
> |
client_p->connection->password = NULL; |
207 |
> |
|
208 |
> |
detach_conf(client_p, CONF_CLIENT | CONF_OPER | CONF_SERVER); |
209 |
|
} |
210 |
|
|
257 |
– |
#ifdef HAVE_LIBCRYPTO |
211 |
|
/* |
212 |
|
* ssl_handshake - let OpenSSL initialize the protocol. Register for |
213 |
|
* read/write events if necessary. |
214 |
|
*/ |
215 |
|
static void |
216 |
< |
ssl_handshake(int fd, struct Client *client_p) |
216 |
> |
ssl_handshake(fde_t *fd, void *data) |
217 |
|
{ |
218 |
< |
int ret = SSL_accept(client_p->localClient->fd.ssl); |
218 |
> |
struct Client *client_p = data; |
219 |
|
|
220 |
< |
if (ret <= 0) |
221 |
< |
switch (SSL_get_error(client_p->localClient->fd.ssl, ret)) |
220 |
> |
tls_handshake_status_t ret = tls_handshake(&client_p->connection->fd.ssl, TLS_ROLE_SERVER, NULL); |
221 |
> |
if (ret != TLS_HANDSHAKE_DONE) |
222 |
> |
{ |
223 |
> |
if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT) |
224 |
|
{ |
225 |
< |
case SSL_ERROR_WANT_WRITE: |
226 |
< |
comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE, |
227 |
< |
(PF *) ssl_handshake, client_p, 0); |
273 |
< |
return; |
225 |
> |
exit_client(client_p, "Timeout during TLS handshake"); |
226 |
> |
return; |
227 |
> |
} |
228 |
|
|
229 |
< |
case SSL_ERROR_WANT_READ: |
230 |
< |
comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, |
231 |
< |
(PF *) ssl_handshake, client_p, 0); |
229 |
> |
switch (ret) |
230 |
> |
{ |
231 |
> |
case TLS_HANDSHAKE_WANT_WRITE: |
232 |
> |
comm_setselect(&client_p->connection->fd, COMM_SELECT_WRITE, |
233 |
> |
ssl_handshake, client_p, CONNECTTIMEOUT); |
234 |
> |
return; |
235 |
> |
case TLS_HANDSHAKE_WANT_READ: |
236 |
> |
comm_setselect(&client_p->connection->fd, COMM_SELECT_READ, |
237 |
> |
ssl_handshake, client_p, CONNECTTIMEOUT); |
238 |
|
return; |
279 |
– |
|
239 |
|
default: |
240 |
< |
exit_client(client_p, client_p, "Error during SSL handshake"); |
241 |
< |
return; |
240 |
> |
exit_client(client_p, "Error during TLS handshake"); |
241 |
> |
return; |
242 |
|
} |
243 |
+ |
} |
244 |
+ |
|
245 |
+ |
comm_settimeout(&client_p->connection->fd, 0, NULL, NULL); |
246 |
+ |
|
247 |
+ |
if (!tls_verify_cert(&client_p->connection->fd.ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp)) |
248 |
+ |
ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad TLS client certificate", |
249 |
+ |
client_p->name, client_p->username, client_p->host); |
250 |
|
|
251 |
< |
execute_callback(auth_cb, client_p); |
251 |
> |
start_auth(client_p); |
252 |
|
} |
287 |
– |
#endif |
253 |
|
|
254 |
|
/* |
255 |
< |
* add_connection - creates a client which has just connected to us on |
255 |
> |
* add_connection - creates a client which has just connected to us on |
256 |
|
* the given fd. The sockhost field is initialized with the ip# of the host. |
257 |
|
* An unique id is calculated now, in case it is needed for auth. |
258 |
|
* The client is sent to the auth module for verification, and not put in |
261 |
|
void |
262 |
|
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
263 |
|
{ |
264 |
< |
struct Client *new_client = make_client(NULL); |
264 |
> |
struct Client *client_p = make_client(NULL); |
265 |
|
|
266 |
< |
fd_open(&new_client->localClient->fd, fd, 1, |
266 |
> |
fd_open(&client_p->connection->fd, fd, 1, |
267 |
|
(listener->flags & LISTENER_SSL) ? |
268 |
< |
"Incoming SSL connection" : "Incoming connection"); |
268 |
> |
"Incoming SSL connection" : "Incoming connection"); |
269 |
|
|
270 |
|
/* |
271 |
|
* copy address to 'sockhost' as a string, copy it to host too |
272 |
|
* so we have something valid to put into error messages... |
273 |
|
*/ |
274 |
< |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
274 |
> |
memcpy(&client_p->connection->ip, irn, sizeof(struct irc_ssaddr)); |
275 |
> |
|
276 |
> |
getnameinfo((const struct sockaddr *)&client_p->connection->ip, |
277 |
> |
client_p->connection->ip.ss_len, client_p->sockhost, |
278 |
> |
sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST); |
279 |
> |
client_p->connection->aftype = client_p->connection->ip.ss.ss_family; |
280 |
|
|
281 |
< |
getnameinfo((struct sockaddr *)&new_client->localClient->ip, |
282 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
283 |
< |
sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST); |
284 |
< |
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
285 |
< |
|
286 |
< |
if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':') |
287 |
< |
{ |
288 |
< |
strlcpy(new_client->host, "0", sizeof(new_client->host)); |
289 |
< |
strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1); |
290 |
< |
memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1); |
291 |
< |
new_client->sockhost[0] = '0'; |
281 |
> |
#ifdef HAVE_LIBGEOIP |
282 |
> |
if (irn->ss.ss_family == AF_INET && GeoIPv4_ctx) |
283 |
> |
{ |
284 |
> |
GeoIPLookup gl; |
285 |
> |
const struct sockaddr_in *const v4 = (const struct sockaddr_in *)&client_p->connection->ip; |
286 |
> |
client_p->connection->country_id = GeoIP_id_by_ipnum_gl(GeoIPv4_ctx, (unsigned long)ntohl(v4->sin_addr.s_addr), &gl); |
287 |
> |
} |
288 |
> |
else if (irn->ss.ss_family == AF_INET6 && GeoIPv6_ctx) |
289 |
> |
{ |
290 |
> |
GeoIPLookup gl; |
291 |
> |
const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)&client_p->connection->ip; |
292 |
> |
client_p->connection->country_id = GeoIP_id_by_ipnum_v6_gl(GeoIPv6_ctx, v6->sin6_addr, &gl); |
293 |
> |
} |
294 |
> |
#endif |
295 |
> |
|
296 |
> |
if (client_p->sockhost[0] == ':' && client_p->sockhost[1] == ':') |
297 |
> |
{ |
298 |
> |
strlcpy(client_p->host, "0", sizeof(client_p->host)); |
299 |
> |
strlcpy(client_p->host + 1, client_p->sockhost, sizeof(client_p->host) - 1); |
300 |
> |
memmove(client_p->sockhost + 1, client_p->sockhost, sizeof(client_p->sockhost) - 1); |
301 |
> |
client_p->sockhost[0] = '0'; |
302 |
|
} |
303 |
|
else |
304 |
< |
strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
304 |
> |
strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host)); |
305 |
|
|
306 |
< |
new_client->localClient->listener = listener; |
306 |
> |
client_p->connection->listener = listener; |
307 |
|
++listener->ref_count; |
308 |
|
|
329 |
– |
#ifdef HAVE_LIBCRYPTO |
309 |
|
if (listener->flags & LISTENER_SSL) |
310 |
|
{ |
311 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
311 |
> |
if (!tls_new(&client_p->connection->fd.ssl, fd, TLS_ROLE_SERVER)) |
312 |
|
{ |
313 |
< |
ilog(LOG_TYPE_IRCD, "SSL_new() ERROR! -- %s", |
314 |
< |
ERR_error_string(ERR_get_error(), NULL)); |
336 |
< |
|
337 |
< |
SetDead(new_client); |
338 |
< |
exit_client(new_client, new_client, "SSL_new failed"); |
313 |
> |
SetDead(client_p); |
314 |
> |
exit_client(client_p, "TLS context initialization failed"); |
315 |
|
return; |
316 |
|
} |
317 |
|
|
318 |
< |
SSL_set_fd(new_client->localClient->fd.ssl, fd); |
319 |
< |
ssl_handshake(0, new_client); |
318 |
> |
AddFlag(client_p, FLAGS_SSL); |
319 |
> |
ssl_handshake(NULL, client_p); |
320 |
|
} |
321 |
|
else |
322 |
< |
#endif |
347 |
< |
execute_callback(auth_cb, new_client); |
322 |
> |
start_auth(client_p); |
323 |
|
} |
324 |
|
|
325 |
|
/* |
355 |
|
* Set the timeout for the fd |
356 |
|
*/ |
357 |
|
void |
358 |
< |
comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
358 |
> |
comm_settimeout(fde_t *fd, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata) |
359 |
|
{ |
360 |
|
assert(fd->flags.open); |
361 |
|
|
374 |
|
* flush functions, and when comm_close() is implemented correctly |
375 |
|
* with close functions, we _actually_ don't call comm_close() here .. |
376 |
|
* -- originally Adrian's notes |
377 |
< |
* comm_close() is replaced with fd_close() in fdlist.c |
377 |
> |
* comm_close() is replaced with fd_close() in fdlist.c |
378 |
|
*/ |
379 |
|
void |
380 |
< |
comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
380 |
> |
comm_setflush(fde_t *fd, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata) |
381 |
|
{ |
382 |
|
assert(fd->flags.open); |
383 |
|
|
394 |
|
* this will happen. |
395 |
|
*/ |
396 |
|
void |
397 |
< |
comm_checktimeouts(void *notused) |
397 |
> |
comm_checktimeouts(void *unused) |
398 |
|
{ |
424 |
– |
int i; |
399 |
|
fde_t *F; |
400 |
< |
PF *hdl; |
400 |
> |
void (*hdl)(fde_t *, void *); |
401 |
|
void *data; |
402 |
|
|
403 |
< |
for (i = 0; i < FD_HASH_SIZE; i++) |
403 |
> |
for (unsigned int i = 0; i < FD_HASH_SIZE; i++) |
404 |
> |
{ |
405 |
|
for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop) |
406 |
|
{ |
407 |
|
assert(F->flags.open); |
428 |
|
hdl(F, data); |
429 |
|
} |
430 |
|
} |
431 |
+ |
} |
432 |
|
} |
433 |
|
|
434 |
|
/* |
445 |
|
* may be called now, or it may be called later. |
446 |
|
*/ |
447 |
|
void |
448 |
< |
comm_connect_tcp(fde_t *fd, const char *host, unsigned short port, |
449 |
< |
struct sockaddr *clocal, int socklen, CNCB *callback, |
450 |
< |
void *data, int aftype, int timeout) |
448 |
> |
comm_connect_tcp(fde_t *fd, const char *host, unsigned short port, struct sockaddr *clocal, |
449 |
> |
int socklen, void (*callback)(fde_t *, int, void *), void *data, |
450 |
> |
int aftype, uintmax_t timeout) |
451 |
|
{ |
452 |
|
struct addrinfo hints, *res; |
453 |
|
char portname[PORTNAMELEN + 1]; |
466 |
|
* virtual host IP, for completeness. |
467 |
|
* -- adrian |
468 |
|
*/ |
469 |
< |
if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0)) |
470 |
< |
{ |
469 |
> |
if (clocal && bind(fd->fd, clocal, socklen) < 0) |
470 |
> |
{ |
471 |
|
/* Failure, call the callback with COMM_ERR_BIND */ |
472 |
|
comm_connect_callback(fd, COMM_ERR_BIND); |
473 |
< |
/* ... and quit */ |
498 |
< |
return; |
473 |
> |
return; /* ... and quit */ |
474 |
|
} |
475 |
|
|
501 |
– |
/* Next, if we have been given an IP, get the addr and skip the |
502 |
– |
* DNS check (and head direct to comm_connect_tryconnect(). |
503 |
– |
*/ |
476 |
|
memset(&hints, 0, sizeof(hints)); |
477 |
+ |
|
478 |
|
hints.ai_family = AF_UNSPEC; |
479 |
|
hints.ai_socktype = SOCK_STREAM; |
480 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
481 |
|
|
482 |
|
snprintf(portname, sizeof(portname), "%d", port); |
483 |
|
|
484 |
+ |
/* |
485 |
+ |
* Next, if we have been given an IP address, get the address and skip the |
486 |
+ |
* DNS check (and head direct to comm_connect_tryconnect()). |
487 |
+ |
*/ |
488 |
|
if (getaddrinfo(host, portname, &hints, &res)) |
489 |
|
{ |
490 |
|
/* Send the DNS request, for the next level */ |
497 |
|
{ |
498 |
|
/* We have a valid IP, so we just call tryconnect */ |
499 |
|
/* Make sure we actually set the timeout here .. */ |
500 |
< |
assert(res != NULL); |
500 |
> |
assert(res); |
501 |
> |
|
502 |
|
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
503 |
|
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
504 |
|
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
505 |
|
freeaddrinfo(res); |
506 |
< |
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
506 |
> |
|
507 |
> |
comm_settimeout(fd, timeout * 1000, comm_connect_timeout, NULL); |
508 |
|
comm_connect_tryconnect(fd, NULL); |
509 |
|
} |
510 |
|
} |
515 |
|
static void |
516 |
|
comm_connect_callback(fde_t *fd, int status) |
517 |
|
{ |
518 |
< |
CNCB *hdl; |
518 |
> |
void (*hdl)(fde_t *, int, void *); |
519 |
|
|
520 |
|
/* This check is gross..but probably necessary */ |
521 |
|
if (fd->connect.callback == NULL) |
538 |
|
* called .. |
539 |
|
*/ |
540 |
|
static void |
541 |
< |
comm_connect_timeout(fde_t *fd, void *notused) |
541 |
> |
comm_connect_timeout(fde_t *fd, void *unused) |
542 |
|
{ |
543 |
|
/* error! */ |
544 |
|
comm_connect_callback(fd, COMM_ERR_TIMEOUT); |
551 |
|
* otherwise we initiate the connect() |
552 |
|
*/ |
553 |
|
static void |
554 |
< |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
554 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name, size_t namelength) |
555 |
|
{ |
556 |
< |
fde_t *F = vptr; |
556 |
> |
fde_t *const F = vptr; |
557 |
|
|
558 |
< |
if (name == NULL) |
558 |
> |
if (!addr) |
559 |
|
{ |
560 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
561 |
|
return; |
567 |
|
/* Copy over the DNS reply info so we can use it in the connect() */ |
568 |
|
/* |
569 |
|
* Note we don't fudge the refcount here, because we aren't keeping |
570 |
< |
* the DNS record around, and the DNS cache is gone anyway.. |
570 |
> |
* the DNS record around, and the DNS cache is gone anyway.. |
571 |
|
* -- adrian |
572 |
|
*/ |
573 |
|
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
574 |
+ |
|
575 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
576 |
< |
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
597 |
< |
F->connect.hostaddr.ss_port; |
576 |
> |
((struct sockaddr_in *)&F->connect.hostaddr)->sin_port = F->connect.hostaddr.ss_port; |
577 |
|
F->connect.hostaddr.ss_len = addr->ss_len; |
578 |
|
|
579 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
580 |
|
comm_connect_tryconnect(F, NULL); |
581 |
|
} |
582 |
|
|
583 |
< |
/* static void comm_connect_tryconnect(int fd, void *notused) |
583 |
> |
/* static void comm_connect_tryconnect(int fd, void *unused) |
584 |
|
* Input: The fd, the handler data(unused). |
585 |
|
* Output: None. |
586 |
|
* Side-effects: Try and connect with pending connect data for the FD. If |
589 |
|
* to select for a write event on this FD. |
590 |
|
*/ |
591 |
|
static void |
592 |
< |
comm_connect_tryconnect(fde_t *fd, void *notused) |
592 |
> |
comm_connect_tryconnect(fde_t *fd, void *unused) |
593 |
|
{ |
615 |
– |
int retval; |
616 |
– |
|
594 |
|
/* This check is needed or re-entrant s_bsd_* like sigio break it. */ |
595 |
|
if (fd->connect.callback == NULL) |
596 |
|
return; |
597 |
|
|
598 |
|
/* Try the connect() */ |
599 |
< |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
623 |
< |
fd->connect.hostaddr.ss_len); |
599 |
> |
int retval = connect(fd->fd, (struct sockaddr *)&fd->connect.hostaddr, fd->connect.hostaddr.ss_len); |
600 |
|
|
601 |
|
/* Error? */ |
602 |
|
if (retval < 0) |
610 |
|
comm_connect_callback(fd, COMM_OK); |
611 |
|
else if (ignoreErrno(errno)) |
612 |
|
/* Ignore error? Reschedule */ |
613 |
< |
comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect, |
638 |
< |
NULL, 0); |
613 |
> |
comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect, NULL, 0); |
614 |
|
else |
615 |
|
/* Error? Fail with COMM_ERR_CONNECT */ |
616 |
|
comm_connect_callback(fd, COMM_ERR_CONNECT); |
642 |
|
int |
643 |
|
comm_open(fde_t *F, int family, int sock_type, int proto, const char *note) |
644 |
|
{ |
670 |
– |
int fd; |
671 |
– |
|
645 |
|
/* First, make sure we aren't going to run out of file descriptors */ |
646 |
|
if (number_fd >= hard_fdlimit) |
647 |
|
{ |
654 |
|
* limit if/when we get an error, but we can deal with that later. |
655 |
|
* XXX !!! -- adrian |
656 |
|
*/ |
657 |
< |
fd = socket(family, sock_type, proto); |
657 |
> |
int fd = socket(family, sock_type, proto); |
658 |
|
if (fd < 0) |
659 |
|
return -1; /* errno will be passed through, yay.. */ |
660 |
|
|
661 |
< |
execute_callback(setup_socket_cb, fd); |
661 |
> |
setup_socket(fd); |
662 |
|
|
663 |
|
/* update things in our fd tracking */ |
664 |
|
fd_open(F, fd, 1, note); |
673 |
|
* fd_open (this function no longer does it). |
674 |
|
*/ |
675 |
|
int |
676 |
< |
comm_accept(struct Listener *lptr, struct irc_ssaddr *pn) |
676 |
> |
comm_accept(struct Listener *lptr, struct irc_ssaddr *addr) |
677 |
|
{ |
705 |
– |
int newfd; |
678 |
|
socklen_t addrlen = sizeof(struct irc_ssaddr); |
679 |
|
|
680 |
|
if (number_fd >= hard_fdlimit) |
683 |
|
return -1; |
684 |
|
} |
685 |
|
|
686 |
+ |
memset(addr, 0, sizeof(struct irc_ssaddr)); |
687 |
+ |
|
688 |
|
/* |
689 |
|
* Next, do the accept(). if we get an error, we should drop the |
690 |
|
* reserved fd limit, but we can deal with that when comm_open() |
691 |
|
* also does it. XXX -- adrian |
692 |
|
*/ |
693 |
< |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen); |
694 |
< |
if (newfd < 0) |
693 |
> |
int fd = accept(lptr->fd.fd, (struct sockaddr *)addr, &addrlen); |
694 |
> |
if (fd < 0) |
695 |
|
return -1; |
696 |
|
|
697 |
< |
#ifdef IPV6 |
724 |
< |
remove_ipv6_mapping(pn); |
725 |
< |
#else |
726 |
< |
pn->ss_len = addrlen; |
727 |
< |
#endif |
697 |
> |
remove_ipv6_mapping(addr); |
698 |
|
|
699 |
< |
execute_callback(setup_socket_cb, newfd); |
699 |
> |
setup_socket(fd); |
700 |
|
|
701 |
|
/* .. and return */ |
702 |
< |
return newfd; |
702 |
> |
return fd; |
703 |
|
} |
704 |
|
|
705 |
< |
/* |
705 |
> |
/* |
706 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
707 |
|
* OSes with IPv6 mapping listening on both |
708 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
709 |
< |
* |
709 |
> |
* |
710 |
|
*/ |
741 |
– |
#ifdef IPV6 |
711 |
|
void |
712 |
|
remove_ipv6_mapping(struct irc_ssaddr *addr) |
713 |
|
{ |
715 |
|
{ |
716 |
|
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
717 |
|
{ |
718 |
< |
struct sockaddr_in6 v6; |
718 |
> |
struct sockaddr_in6 v6; |
719 |
|
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
720 |
|
|
721 |
|
memcpy(&v6, addr, sizeof(v6)); |
725 |
|
addr->ss.ss_family = AF_INET; |
726 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
727 |
|
} |
728 |
< |
else |
728 |
> |
else |
729 |
|
addr->ss_len = sizeof(struct sockaddr_in6); |
730 |
|
} |
731 |
|
else |
732 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
733 |
< |
} |
765 |
< |
#endif |
733 |
> |
} |