| 1 |
adx |
30 |
/* |
| 2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
| 3 |
|
|
* s_bsd.c: Network functions. |
| 4 |
|
|
* |
| 5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
| 6 |
|
|
* |
| 7 |
|
|
* This program is free software; you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the GNU General Public License as published by |
| 9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
|
|
* (at your option) any later version. |
| 11 |
|
|
* |
| 12 |
|
|
* This program is distributed in the hope that it will be useful, |
| 13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
|
|
* GNU General Public License for more details. |
| 16 |
|
|
* |
| 17 |
|
|
* You should have received a copy of the GNU General Public License |
| 18 |
|
|
* along with this program; if not, write to the Free Software |
| 19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
|
|
* USA |
| 21 |
|
|
* |
| 22 |
knight |
31 |
* $Id$ |
| 23 |
adx |
30 |
*/ |
| 24 |
|
|
|
| 25 |
|
|
#include "stdinc.h" |
| 26 |
|
|
#ifndef _WIN32 |
| 27 |
|
|
#include <netinet/in_systm.h> |
| 28 |
|
|
#include <netinet/ip.h> |
| 29 |
|
|
#include <netinet/tcp.h> |
| 30 |
|
|
#endif |
| 31 |
|
|
#include "fdlist.h" |
| 32 |
|
|
#include "s_bsd.h" |
| 33 |
|
|
#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" |
| 40 |
|
|
#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" |
| 47 |
|
|
#include "restart.h" |
| 48 |
|
|
#include "s_auth.h" |
| 49 |
|
|
#include "s_conf.h" |
| 50 |
|
|
#include "s_log.h" |
| 51 |
|
|
#include "s_serv.h" |
| 52 |
|
|
#include "send.h" |
| 53 |
|
|
#include "memory.h" |
| 54 |
|
|
#include "s_user.h" |
| 55 |
|
|
#include "hook.h" |
| 56 |
|
|
|
| 57 |
|
|
static const char *comm_err_str[] = { "Comm OK", "Error during bind()", |
| 58 |
|
|
"Error during DNS lookup", "connect timeout", "Error during connect()", |
| 59 |
|
|
"Comm Error" }; |
| 60 |
|
|
|
| 61 |
|
|
struct Callback *setup_socket_cb = NULL; |
| 62 |
|
|
|
| 63 |
|
|
static void comm_connect_callback(fde_t *fd, int status); |
| 64 |
|
|
static PF comm_connect_timeout; |
| 65 |
|
|
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
| 66 |
|
|
static PF comm_connect_tryconnect; |
| 67 |
|
|
|
| 68 |
|
|
extern void init_netio(void); |
| 69 |
|
|
|
| 70 |
|
|
/* check_can_use_v6() |
| 71 |
|
|
* Check if the system can open AF_INET6 sockets |
| 72 |
|
|
*/ |
| 73 |
|
|
void |
| 74 |
|
|
check_can_use_v6(void) |
| 75 |
|
|
{ |
| 76 |
|
|
#ifdef IPV6 |
| 77 |
|
|
int v6; |
| 78 |
|
|
|
| 79 |
|
|
if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) |
| 80 |
|
|
ServerInfo.can_use_v6 = 0; |
| 81 |
|
|
else |
| 82 |
|
|
{ |
| 83 |
|
|
ServerInfo.can_use_v6 = 1; |
| 84 |
|
|
#ifdef _WIN32 |
| 85 |
|
|
closesocket(v6); |
| 86 |
|
|
#else |
| 87 |
|
|
close(v6); |
| 88 |
|
|
#endif |
| 89 |
|
|
} |
| 90 |
|
|
#else |
| 91 |
|
|
ServerInfo.can_use_v6 = 0; |
| 92 |
|
|
#endif |
| 93 |
|
|
} |
| 94 |
|
|
|
| 95 |
|
|
/* get_sockerr - get the error value from the socket or the current errno |
| 96 |
|
|
* |
| 97 |
|
|
* Get the *real* error from the socket (well try to anyway..). |
| 98 |
|
|
* This may only work when SO_DEBUG is enabled but its worth the |
| 99 |
|
|
* gamble anyway. |
| 100 |
|
|
*/ |
| 101 |
|
|
int |
| 102 |
|
|
get_sockerr(int fd) |
| 103 |
|
|
{ |
| 104 |
|
|
#ifndef _WIN32 |
| 105 |
|
|
int errtmp = errno; |
| 106 |
|
|
#else |
| 107 |
|
|
int errtmp = WSAGetLastError(); |
| 108 |
|
|
#endif |
| 109 |
|
|
#ifdef SO_ERROR |
| 110 |
|
|
int err = 0; |
| 111 |
|
|
socklen_t len = sizeof(err); |
| 112 |
|
|
|
| 113 |
|
|
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
| 114 |
|
|
{ |
| 115 |
|
|
if (err) |
| 116 |
|
|
errtmp = err; |
| 117 |
|
|
} |
| 118 |
|
|
errno = errtmp; |
| 119 |
|
|
#endif |
| 120 |
|
|
return errtmp; |
| 121 |
|
|
} |
| 122 |
|
|
|
| 123 |
|
|
/* |
| 124 |
|
|
* report_error - report an error from an errno. |
| 125 |
|
|
* Record error to log and also send a copy to all *LOCAL* opers online. |
| 126 |
|
|
* |
| 127 |
|
|
* text is a *format* string for outputing error. It must |
| 128 |
|
|
* contain only two '%s', the first will be replaced |
| 129 |
|
|
* by the sockhost from the client_p, and the latter will |
| 130 |
|
|
* be taken from sys_errlist[errno]. |
| 131 |
|
|
* |
| 132 |
|
|
* client_p if not NULL, is the *LOCAL* client associated with |
| 133 |
|
|
* the error. |
| 134 |
|
|
* |
| 135 |
|
|
* Cannot use perror() within daemon. stderr is closed in |
| 136 |
|
|
* ircd and cannot be used. And, worse yet, it might have |
| 137 |
|
|
* been reassigned to a normal connection... |
| 138 |
|
|
* |
| 139 |
|
|
* Actually stderr is still there IFF ircd was run with -s --Rodder |
| 140 |
|
|
*/ |
| 141 |
|
|
|
| 142 |
|
|
void |
| 143 |
|
|
report_error(int level, const char* text, const char* who, int error) |
| 144 |
|
|
{ |
| 145 |
|
|
who = (who) ? who : ""; |
| 146 |
|
|
|
| 147 |
|
|
sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error)); |
| 148 |
|
|
log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error)); |
| 149 |
|
|
ilog(L_ERROR, text, who, strerror(error)); |
| 150 |
|
|
} |
| 151 |
|
|
|
| 152 |
|
|
/* |
| 153 |
|
|
* setup_socket() |
| 154 |
|
|
* |
| 155 |
|
|
* Set the socket non-blocking, and other wonderful bits. |
| 156 |
|
|
*/ |
| 157 |
|
|
static void * |
| 158 |
|
|
setup_socket(va_list args) |
| 159 |
|
|
{ |
| 160 |
|
|
int fd = va_arg(args, int); |
| 161 |
|
|
int opt = 1; |
| 162 |
|
|
|
| 163 |
|
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
| 164 |
|
|
|
| 165 |
|
|
#ifdef IPTOS_LOWDELAY |
| 166 |
|
|
opt = IPTOS_LOWDELAY; |
| 167 |
|
|
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
| 168 |
|
|
#endif |
| 169 |
|
|
|
| 170 |
|
|
#ifndef _WIN32 |
| 171 |
|
|
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(); |
| 187 |
|
|
} |
| 188 |
|
|
|
| 189 |
|
|
/* |
| 190 |
|
|
* close_connection |
| 191 |
|
|
* Close the physical connection. This function must make |
| 192 |
|
|
* MyConnect(client_p) == FALSE, and set client_p->from == NULL. |
| 193 |
|
|
*/ |
| 194 |
|
|
void |
| 195 |
|
|
close_connection(struct Client *client_p) |
| 196 |
|
|
{ |
| 197 |
|
|
struct ConfItem *conf; |
| 198 |
|
|
struct AccessItem *aconf; |
| 199 |
|
|
struct ClassItem *aclass; |
| 200 |
|
|
|
| 201 |
|
|
assert(NULL != client_p); |
| 202 |
|
|
|
| 203 |
michael |
683 |
if (!IsDead(client_p)) |
| 204 |
|
|
{ |
| 205 |
|
|
/* attempt to flush any pending dbufs. Evil, but .. -- adrian */ |
| 206 |
|
|
/* there is still a chance that we might send data to this socket |
| 207 |
|
|
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
| 208 |
|
|
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
| 209 |
|
|
*/ |
| 210 |
|
|
ClearSendqBlocked(client_p); |
| 211 |
|
|
send_queued_write(client_p); |
| 212 |
|
|
} |
| 213 |
|
|
|
| 214 |
adx |
30 |
if (IsServer(client_p)) |
| 215 |
|
|
{ |
| 216 |
michael |
896 |
++ServerStats.is_sv; |
| 217 |
|
|
ServerStats.is_sbs += client_p->localClient->send.bytes; |
| 218 |
|
|
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
| 219 |
|
|
ServerStats.is_sti += CurrentTime - client_p->firsttime; |
| 220 |
adx |
30 |
|
| 221 |
|
|
/* 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))) |
| 232 |
|
|
{ |
| 233 |
|
|
/* |
| 234 |
|
|
* Reschedule a faster reconnect, if this was a automatically |
| 235 |
|
|
* 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. |
| 238 |
|
|
*/ |
| 239 |
|
|
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; |
| 246 |
|
|
} |
| 247 |
|
|
} |
| 248 |
|
|
else if (IsClient(client_p)) |
| 249 |
|
|
{ |
| 250 |
michael |
896 |
++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 |
adx |
30 |
} |
| 255 |
|
|
else |
| 256 |
michael |
896 |
++ServerStats.is_ni; |
| 257 |
adx |
30 |
|
| 258 |
|
|
#ifdef HAVE_LIBCRYPTO |
| 259 |
|
|
if (client_p->localClient->fd.ssl) |
| 260 |
michael |
451 |
{ |
| 261 |
|
|
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
| 262 |
|
|
|
| 263 |
|
|
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
| 264 |
|
|
SSL_shutdown(client_p->localClient->fd.ssl); |
| 265 |
|
|
} |
| 266 |
adx |
30 |
#endif |
| 267 |
|
|
if (client_p->localClient->fd.flags.open) |
| 268 |
|
|
fd_close(&client_p->localClient->fd); |
| 269 |
|
|
|
| 270 |
|
|
if (HasServlink(client_p)) |
| 271 |
|
|
{ |
| 272 |
|
|
if (client_p->localClient->ctrlfd.flags.open) |
| 273 |
|
|
fd_close(&client_p->localClient->ctrlfd); |
| 274 |
|
|
} |
| 275 |
|
|
|
| 276 |
|
|
dbuf_clear(&client_p->localClient->buf_sendq); |
| 277 |
|
|
dbuf_clear(&client_p->localClient->buf_recvq); |
| 278 |
|
|
|
| 279 |
|
|
MyFree(client_p->localClient->passwd); |
| 280 |
|
|
detach_conf(client_p, CONF_TYPE); |
| 281 |
|
|
client_p->from = NULL; /* ...this should catch them! >:) --msa */ |
| 282 |
|
|
} |
| 283 |
|
|
|
| 284 |
|
|
#ifdef HAVE_LIBCRYPTO |
| 285 |
|
|
/* |
| 286 |
|
|
* ssl_handshake - let OpenSSL initialize the protocol. Register for |
| 287 |
|
|
* read/write events if necessary. |
| 288 |
|
|
*/ |
| 289 |
|
|
static void |
| 290 |
|
|
ssl_handshake(int fd, struct Client *client_p) |
| 291 |
|
|
{ |
| 292 |
|
|
int ret = SSL_accept(client_p->localClient->fd.ssl); |
| 293 |
|
|
|
| 294 |
|
|
if (ret <= 0) |
| 295 |
|
|
switch (SSL_get_error(client_p->localClient->fd.ssl, ret)) |
| 296 |
|
|
{ |
| 297 |
|
|
case SSL_ERROR_WANT_WRITE: |
| 298 |
|
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE, |
| 299 |
|
|
(PF *) ssl_handshake, client_p, 0); |
| 300 |
|
|
return; |
| 301 |
|
|
|
| 302 |
|
|
case SSL_ERROR_WANT_READ: |
| 303 |
|
|
comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, |
| 304 |
|
|
(PF *) ssl_handshake, client_p, 0); |
| 305 |
|
|
return; |
| 306 |
|
|
|
| 307 |
|
|
default: |
| 308 |
|
|
exit_client(client_p, client_p, "Error during SSL handshake"); |
| 309 |
|
|
return; |
| 310 |
|
|
} |
| 311 |
|
|
|
| 312 |
|
|
execute_callback(auth_cb, client_p); |
| 313 |
|
|
} |
| 314 |
|
|
#endif |
| 315 |
|
|
|
| 316 |
|
|
/* |
| 317 |
|
|
* add_connection - creates a client which has just connected to us on |
| 318 |
|
|
* the given fd. The sockhost field is initialized with the ip# of the host. |
| 319 |
|
|
* An unique id is calculated now, in case it is needed for auth. |
| 320 |
|
|
* The client is sent to the auth module for verification, and not put in |
| 321 |
|
|
* any client list yet. |
| 322 |
|
|
*/ |
| 323 |
|
|
void |
| 324 |
michael |
549 |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
| 325 |
adx |
30 |
{ |
| 326 |
|
|
struct Client *new_client; |
| 327 |
michael |
549 |
|
| 328 |
adx |
30 |
assert(NULL != listener); |
| 329 |
|
|
|
| 330 |
michael |
549 |
new_client = make_client(NULL); |
| 331 |
adx |
30 |
|
| 332 |
|
|
fd_open(&new_client->localClient->fd, fd, 1, |
| 333 |
|
|
(listener->flags & LISTENER_SSL) ? |
| 334 |
|
|
"Incoming SSL connection" : "Incoming connection"); |
| 335 |
|
|
|
| 336 |
|
|
/* |
| 337 |
|
|
* copy address to 'sockhost' as a string, copy it to host too |
| 338 |
|
|
* so we have something valid to put into error messages... |
| 339 |
|
|
*/ |
| 340 |
michael |
549 |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
| 341 |
adx |
30 |
|
| 342 |
|
|
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
| 343 |
|
|
new_client->localClient->ip.ss_len, new_client->sockhost, |
| 344 |
|
|
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
| 345 |
|
|
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
| 346 |
|
|
#ifdef IPV6 |
| 347 |
michael |
549 |
if (new_client->sockhost[0] == ':') |
| 348 |
adx |
30 |
strlcat(new_client->host, "0", HOSTLEN+1); |
| 349 |
|
|
|
| 350 |
|
|
if (new_client->localClient->aftype == AF_INET6 && |
| 351 |
|
|
ConfigFileEntry.dot_in_ip6_addr == 1) |
| 352 |
|
|
{ |
| 353 |
|
|
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
| 354 |
|
|
strlcat(new_client->host, ".", HOSTLEN+1); |
| 355 |
michael |
549 |
} |
| 356 |
|
|
else |
| 357 |
adx |
30 |
#endif |
| 358 |
|
|
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
| 359 |
|
|
|
| 360 |
michael |
549 |
new_client->connect_id = ++connect_id; |
| 361 |
adx |
30 |
new_client->localClient->listener = listener; |
| 362 |
|
|
++listener->ref_count; |
| 363 |
|
|
|
| 364 |
|
|
#ifdef HAVE_LIBCRYPTO |
| 365 |
michael |
549 |
if (listener->flags & LISTENER_SSL) |
| 366 |
adx |
30 |
{ |
| 367 |
|
|
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
| 368 |
|
|
{ |
| 369 |
|
|
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
| 370 |
|
|
ERR_error_string(ERR_get_error(), NULL)); |
| 371 |
|
|
|
| 372 |
|
|
SetDead(new_client); |
| 373 |
|
|
exit_client(new_client, new_client, "SSL_new failed"); |
| 374 |
|
|
return; |
| 375 |
|
|
} |
| 376 |
|
|
|
| 377 |
|
|
SSL_set_fd(new_client->localClient->fd.ssl, fd); |
| 378 |
|
|
ssl_handshake(0, new_client); |
| 379 |
|
|
} |
| 380 |
|
|
else |
| 381 |
|
|
#endif |
| 382 |
|
|
execute_callback(auth_cb, new_client); |
| 383 |
|
|
} |
| 384 |
|
|
|
| 385 |
|
|
/* |
| 386 |
|
|
* stolen from squid - its a neat (but overused! :) routine which we |
| 387 |
|
|
* can use to see whether we can ignore this errno or not. It is |
| 388 |
|
|
* generally useful for non-blocking network IO related errnos. |
| 389 |
|
|
* -- adrian |
| 390 |
|
|
*/ |
| 391 |
|
|
int |
| 392 |
|
|
ignoreErrno(int ierrno) |
| 393 |
|
|
{ |
| 394 |
|
|
switch (ierrno) |
| 395 |
|
|
{ |
| 396 |
|
|
case EINPROGRESS: |
| 397 |
|
|
case EWOULDBLOCK: |
| 398 |
|
|
#if EAGAIN != EWOULDBLOCK |
| 399 |
|
|
case EAGAIN: |
| 400 |
|
|
#endif |
| 401 |
|
|
case EALREADY: |
| 402 |
|
|
case EINTR: |
| 403 |
|
|
#ifdef ERESTART |
| 404 |
|
|
case ERESTART: |
| 405 |
|
|
#endif |
| 406 |
|
|
return 1; |
| 407 |
|
|
default: |
| 408 |
|
|
return 0; |
| 409 |
|
|
} |
| 410 |
|
|
} |
| 411 |
|
|
|
| 412 |
|
|
/* |
| 413 |
|
|
* comm_settimeout() - set the socket timeout |
| 414 |
|
|
* |
| 415 |
|
|
* Set the timeout for the fd |
| 416 |
|
|
*/ |
| 417 |
|
|
void |
| 418 |
|
|
comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
| 419 |
|
|
{ |
| 420 |
|
|
assert(fd->flags.open); |
| 421 |
|
|
|
| 422 |
|
|
fd->timeout = CurrentTime + (timeout / 1000); |
| 423 |
|
|
fd->timeout_handler = callback; |
| 424 |
|
|
fd->timeout_data = cbdata; |
| 425 |
|
|
} |
| 426 |
|
|
|
| 427 |
|
|
/* |
| 428 |
|
|
* comm_setflush() - set a flush function |
| 429 |
|
|
* |
| 430 |
|
|
* A flush function is simply a function called if found during |
| 431 |
|
|
* comm_timeouts(). Its basically a second timeout, except in this case |
| 432 |
|
|
* I'm too lazy to implement multiple timeout functions! :-) |
| 433 |
|
|
* its kinda nice to have it separate, since this is designed for |
| 434 |
|
|
* flush functions, and when comm_close() is implemented correctly |
| 435 |
|
|
* with close functions, we _actually_ don't call comm_close() here .. |
| 436 |
|
|
* -- originally Adrian's notes |
| 437 |
|
|
* comm_close() is replaced with fd_close() in fdlist.c |
| 438 |
|
|
*/ |
| 439 |
|
|
void |
| 440 |
|
|
comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
| 441 |
|
|
{ |
| 442 |
|
|
assert(fd->flags.open); |
| 443 |
|
|
|
| 444 |
|
|
fd->flush_timeout = CurrentTime + (timeout / 1000); |
| 445 |
|
|
fd->flush_handler = callback; |
| 446 |
|
|
fd->flush_data = cbdata; |
| 447 |
|
|
} |
| 448 |
|
|
|
| 449 |
|
|
/* |
| 450 |
|
|
* comm_checktimeouts() - check the socket timeouts |
| 451 |
|
|
* |
| 452 |
|
|
* All this routine does is call the given callback/cbdata, without closing |
| 453 |
|
|
* down the file descriptor. When close handlers have been implemented, |
| 454 |
|
|
* this will happen. |
| 455 |
|
|
*/ |
| 456 |
|
|
void |
| 457 |
|
|
comm_checktimeouts(void *notused) |
| 458 |
|
|
{ |
| 459 |
|
|
int i; |
| 460 |
|
|
fde_t *F; |
| 461 |
|
|
PF *hdl; |
| 462 |
|
|
void *data; |
| 463 |
|
|
|
| 464 |
|
|
for (i = 0; i < FD_HASH_SIZE; i++) |
| 465 |
|
|
for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop) |
| 466 |
|
|
{ |
| 467 |
|
|
assert(F->flags.open); |
| 468 |
|
|
fd_next_in_loop = F->hnext; |
| 469 |
|
|
|
| 470 |
|
|
/* check flush functions */ |
| 471 |
|
|
if (F->flush_handler && F->flush_timeout > 0 && |
| 472 |
|
|
F->flush_timeout < CurrentTime) |
| 473 |
|
|
{ |
| 474 |
|
|
hdl = F->flush_handler; |
| 475 |
|
|
data = F->flush_data; |
| 476 |
|
|
comm_setflush(F, 0, NULL, NULL); |
| 477 |
|
|
hdl(F, data); |
| 478 |
|
|
} |
| 479 |
|
|
|
| 480 |
|
|
/* check timeouts */ |
| 481 |
|
|
if (F->timeout_handler && F->timeout > 0 && |
| 482 |
|
|
F->timeout < CurrentTime) |
| 483 |
|
|
{ |
| 484 |
|
|
/* Call timeout handler */ |
| 485 |
|
|
hdl = F->timeout_handler; |
| 486 |
|
|
data = F->timeout_data; |
| 487 |
|
|
comm_settimeout(F, 0, NULL, NULL); |
| 488 |
|
|
hdl(F, data); |
| 489 |
|
|
} |
| 490 |
|
|
} |
| 491 |
|
|
} |
| 492 |
|
|
|
| 493 |
|
|
/* |
| 494 |
|
|
* void comm_connect_tcp(int fd, const char *host, unsigned short port, |
| 495 |
|
|
* struct sockaddr *clocal, int socklen, |
| 496 |
|
|
* CNCB *callback, void *data, int aftype, int timeout) |
| 497 |
|
|
* Input: An fd to connect with, a host and port to connect to, |
| 498 |
|
|
* a local sockaddr to connect from + length(or NULL to use the |
| 499 |
|
|
* default), a callback, the data to pass into the callback, the |
| 500 |
|
|
* address family. |
| 501 |
|
|
* Output: None. |
| 502 |
|
|
* Side-effects: A non-blocking connection to the host is started, and |
| 503 |
|
|
* if necessary, set up for selection. The callback given |
| 504 |
|
|
* may be called now, or it may be called later. |
| 505 |
|
|
*/ |
| 506 |
|
|
void |
| 507 |
|
|
comm_connect_tcp(fde_t *fd, const char *host, unsigned short port, |
| 508 |
|
|
struct sockaddr *clocal, int socklen, CNCB *callback, |
| 509 |
|
|
void *data, int aftype, int timeout) |
| 510 |
|
|
{ |
| 511 |
|
|
struct addrinfo hints, *res; |
| 512 |
|
|
char portname[PORTNAMELEN+1]; |
| 513 |
|
|
|
| 514 |
|
|
assert(callback); |
| 515 |
|
|
fd->connect.callback = callback; |
| 516 |
|
|
fd->connect.data = data; |
| 517 |
|
|
|
| 518 |
|
|
fd->connect.hostaddr.ss.ss_family = aftype; |
| 519 |
|
|
fd->connect.hostaddr.ss_port = htons(port); |
| 520 |
|
|
|
| 521 |
|
|
/* Note that we're using a passed sockaddr here. This is because |
| 522 |
|
|
* generally you'll be bind()ing to a sockaddr grabbed from |
| 523 |
|
|
* getsockname(), so this makes things easier. |
| 524 |
|
|
* XXX If NULL is passed as local, we should later on bind() to the |
| 525 |
|
|
* virtual host IP, for completeness. |
| 526 |
|
|
* -- adrian |
| 527 |
|
|
*/ |
| 528 |
|
|
if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0)) |
| 529 |
|
|
{ |
| 530 |
|
|
/* Failure, call the callback with COMM_ERR_BIND */ |
| 531 |
|
|
comm_connect_callback(fd, COMM_ERR_BIND); |
| 532 |
|
|
/* ... and quit */ |
| 533 |
|
|
return; |
| 534 |
|
|
} |
| 535 |
|
|
|
| 536 |
|
|
/* Next, if we have been given an IP, get the addr and skip the |
| 537 |
|
|
* DNS check (and head direct to comm_connect_tryconnect(). |
| 538 |
|
|
*/ |
| 539 |
|
|
memset(&hints, 0, sizeof(hints)); |
| 540 |
|
|
hints.ai_family = AF_UNSPEC; |
| 541 |
|
|
hints.ai_socktype = SOCK_STREAM; |
| 542 |
|
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
| 543 |
|
|
|
| 544 |
|
|
snprintf(portname, PORTNAMELEN, "%d", port); |
| 545 |
|
|
|
| 546 |
|
|
if (irc_getaddrinfo(host, portname, &hints, &res)) |
| 547 |
|
|
{ |
| 548 |
|
|
/* 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; |
| 552 |
db |
871 |
if (aftype == AF_INET6) |
| 553 |
|
|
gethost_byname_type(host, fd->dns_query, T_AAAA); |
| 554 |
|
|
else |
| 555 |
|
|
gethost_byname_type(host, fd->dns_query, T_A); |
| 556 |
adx |
30 |
} |
| 557 |
|
|
else |
| 558 |
|
|
{ |
| 559 |
|
|
/* We have a valid IP, so we just call tryconnect */ |
| 560 |
|
|
/* Make sure we actually set the timeout here .. */ |
| 561 |
|
|
assert(res != NULL); |
| 562 |
|
|
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
| 563 |
|
|
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
| 564 |
|
|
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
| 565 |
|
|
irc_freeaddrinfo(res); |
| 566 |
|
|
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
| 567 |
|
|
comm_connect_tryconnect(fd, NULL); |
| 568 |
|
|
} |
| 569 |
|
|
} |
| 570 |
|
|
|
| 571 |
|
|
/* |
| 572 |
|
|
* comm_connect_callback() - call the callback, and continue with life |
| 573 |
|
|
*/ |
| 574 |
|
|
static void |
| 575 |
|
|
comm_connect_callback(fde_t *fd, int status) |
| 576 |
|
|
{ |
| 577 |
|
|
CNCB *hdl; |
| 578 |
|
|
|
| 579 |
|
|
/* This check is gross..but probably necessary */ |
| 580 |
|
|
if (fd->connect.callback == NULL) |
| 581 |
|
|
return; |
| 582 |
|
|
|
| 583 |
|
|
/* Clear the connect flag + handler */ |
| 584 |
|
|
hdl = fd->connect.callback; |
| 585 |
|
|
fd->connect.callback = NULL; |
| 586 |
|
|
|
| 587 |
|
|
/* Clear the timeout handler */ |
| 588 |
|
|
comm_settimeout(fd, 0, NULL, NULL); |
| 589 |
|
|
|
| 590 |
|
|
/* Call the handler */ |
| 591 |
|
|
hdl(fd, status, fd->connect.data); |
| 592 |
|
|
} |
| 593 |
|
|
|
| 594 |
|
|
/* |
| 595 |
|
|
* comm_connect_timeout() - this gets called when the socket connection |
| 596 |
|
|
* times out. This *only* can be called once connect() is initially |
| 597 |
|
|
* called .. |
| 598 |
|
|
*/ |
| 599 |
|
|
static void |
| 600 |
|
|
comm_connect_timeout(fde_t *fd, void *notused) |
| 601 |
|
|
{ |
| 602 |
|
|
/* error! */ |
| 603 |
|
|
comm_connect_callback(fd, COMM_ERR_TIMEOUT); |
| 604 |
|
|
} |
| 605 |
|
|
|
| 606 |
|
|
/* |
| 607 |
|
|
* comm_connect_dns_callback() - called at the completion of the DNS request |
| 608 |
|
|
* |
| 609 |
|
|
* The DNS request has completed, so if we've got an error, return it, |
| 610 |
|
|
* otherwise we initiate the connect() |
| 611 |
|
|
*/ |
| 612 |
|
|
static void |
| 613 |
|
|
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
| 614 |
|
|
{ |
| 615 |
|
|
fde_t *F = vptr; |
| 616 |
|
|
|
| 617 |
|
|
if (reply == NULL) |
| 618 |
|
|
{ |
| 619 |
|
|
MyFree(F->dns_query); |
| 620 |
|
|
F->dns_query = NULL; |
| 621 |
|
|
comm_connect_callback(F, COMM_ERR_DNS); |
| 622 |
|
|
return; |
| 623 |
|
|
} |
| 624 |
|
|
|
| 625 |
|
|
/* No error, set a 10 second timeout */ |
| 626 |
|
|
comm_settimeout(F, 30*1000, comm_connect_timeout, NULL); |
| 627 |
|
|
|
| 628 |
|
|
/* Copy over the DNS reply info so we can use it in the connect() */ |
| 629 |
|
|
/* |
| 630 |
|
|
* Note we don't fudge the refcount here, because we aren't keeping |
| 631 |
|
|
* the DNS record around, and the DNS cache is gone anyway.. |
| 632 |
|
|
* -- adrian |
| 633 |
|
|
*/ |
| 634 |
|
|
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
| 635 |
|
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
| 636 |
|
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
| 637 |
|
|
F->connect.hostaddr.ss_port; |
| 638 |
|
|
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
| 639 |
|
|
|
| 640 |
|
|
/* Now, call the tryconnect() routine to try a connect() */ |
| 641 |
|
|
MyFree(F->dns_query); |
| 642 |
|
|
F->dns_query = NULL; |
| 643 |
|
|
comm_connect_tryconnect(F, NULL); |
| 644 |
|
|
} |
| 645 |
|
|
|
| 646 |
|
|
/* static void comm_connect_tryconnect(int fd, void *notused) |
| 647 |
|
|
* Input: The fd, the handler data(unused). |
| 648 |
|
|
* Output: None. |
| 649 |
|
|
* Side-effects: Try and connect with pending connect data for the FD. If |
| 650 |
|
|
* we succeed or get a fatal error, call the callback. |
| 651 |
|
|
* Otherwise, it is still blocking or something, so register |
| 652 |
|
|
* to select for a write event on this FD. |
| 653 |
|
|
*/ |
| 654 |
|
|
static void |
| 655 |
|
|
comm_connect_tryconnect(fde_t *fd, void *notused) |
| 656 |
|
|
{ |
| 657 |
|
|
int retval; |
| 658 |
|
|
|
| 659 |
|
|
/* This check is needed or re-entrant s_bsd_* like sigio break it. */ |
| 660 |
|
|
if (fd->connect.callback == NULL) |
| 661 |
|
|
return; |
| 662 |
|
|
|
| 663 |
|
|
/* Try the connect() */ |
| 664 |
|
|
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
| 665 |
|
|
fd->connect.hostaddr.ss_len); |
| 666 |
|
|
|
| 667 |
|
|
/* Error? */ |
| 668 |
|
|
if (retval < 0) |
| 669 |
|
|
{ |
| 670 |
|
|
#ifdef _WIN32 |
| 671 |
|
|
errno = WSAGetLastError(); |
| 672 |
|
|
#endif |
| 673 |
|
|
/* |
| 674 |
|
|
* If we get EISCONN, then we've already connect()ed the socket, |
| 675 |
|
|
* which is a good thing. |
| 676 |
|
|
* -- adrian |
| 677 |
|
|
*/ |
| 678 |
|
|
if (errno == EISCONN) |
| 679 |
|
|
comm_connect_callback(fd, COMM_OK); |
| 680 |
|
|
else if (ignoreErrno(errno)) |
| 681 |
|
|
/* Ignore error? Reschedule */ |
| 682 |
|
|
comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect, |
| 683 |
|
|
NULL, 0); |
| 684 |
|
|
else |
| 685 |
|
|
/* Error? Fail with COMM_ERR_CONNECT */ |
| 686 |
|
|
comm_connect_callback(fd, COMM_ERR_CONNECT); |
| 687 |
|
|
return; |
| 688 |
|
|
} |
| 689 |
|
|
|
| 690 |
|
|
/* If we get here, we've suceeded, so call with COMM_OK */ |
| 691 |
|
|
comm_connect_callback(fd, COMM_OK); |
| 692 |
|
|
} |
| 693 |
|
|
|
| 694 |
|
|
/* |
| 695 |
|
|
* comm_errorstr() - return an error string for the given error condition |
| 696 |
|
|
*/ |
| 697 |
|
|
const char * |
| 698 |
|
|
comm_errstr(int error) |
| 699 |
|
|
{ |
| 700 |
|
|
if (error < 0 || error >= COMM_ERR_MAX) |
| 701 |
|
|
return "Invalid error number!"; |
| 702 |
|
|
return comm_err_str[error]; |
| 703 |
|
|
} |
| 704 |
|
|
|
| 705 |
|
|
/* |
| 706 |
|
|
* comm_open() - open a socket |
| 707 |
|
|
* |
| 708 |
|
|
* This is a highly highly cut down version of squid's comm_open() which |
| 709 |
|
|
* for the most part emulates socket(), *EXCEPT* it fails if we're about |
| 710 |
|
|
* to run out of file descriptors. |
| 711 |
|
|
*/ |
| 712 |
|
|
int |
| 713 |
|
|
comm_open(fde_t *F, int family, int sock_type, int proto, const char *note) |
| 714 |
|
|
{ |
| 715 |
|
|
int fd; |
| 716 |
|
|
|
| 717 |
|
|
/* First, make sure we aren't going to run out of file descriptors */ |
| 718 |
|
|
if (number_fd >= hard_fdlimit) |
| 719 |
|
|
{ |
| 720 |
|
|
errno = ENFILE; |
| 721 |
|
|
return -1; |
| 722 |
|
|
} |
| 723 |
|
|
|
| 724 |
|
|
/* |
| 725 |
|
|
* Next, we try to open the socket. We *should* drop the reserved FD |
| 726 |
|
|
* limit if/when we get an error, but we can deal with that later. |
| 727 |
|
|
* XXX !!! -- adrian |
| 728 |
|
|
*/ |
| 729 |
|
|
fd = socket(family, sock_type, proto); |
| 730 |
|
|
if (fd < 0) |
| 731 |
|
|
{ |
| 732 |
|
|
#ifdef _WIN32 |
| 733 |
|
|
errno = WSAGetLastError(); |
| 734 |
|
|
#endif |
| 735 |
|
|
return -1; /* errno will be passed through, yay.. */ |
| 736 |
|
|
} |
| 737 |
|
|
|
| 738 |
|
|
execute_callback(setup_socket_cb, fd); |
| 739 |
|
|
|
| 740 |
|
|
/* update things in our fd tracking */ |
| 741 |
|
|
fd_open(F, fd, 1, note); |
| 742 |
|
|
return 0; |
| 743 |
|
|
} |
| 744 |
|
|
|
| 745 |
|
|
/* |
| 746 |
|
|
* comm_accept() - accept an incoming connection |
| 747 |
|
|
* |
| 748 |
|
|
* This is a simple wrapper for accept() which enforces FD limits like |
| 749 |
|
|
* comm_open() does. Returned fd must be either closed or tagged with |
| 750 |
|
|
* fd_open (this function no longer does it). |
| 751 |
|
|
*/ |
| 752 |
|
|
int |
| 753 |
|
|
comm_accept(struct Listener *lptr, struct irc_ssaddr *pn) |
| 754 |
|
|
{ |
| 755 |
|
|
int newfd; |
| 756 |
|
|
socklen_t addrlen = sizeof(struct irc_ssaddr); |
| 757 |
|
|
|
| 758 |
|
|
if (number_fd >= hard_fdlimit) |
| 759 |
|
|
{ |
| 760 |
|
|
errno = ENFILE; |
| 761 |
|
|
return -1; |
| 762 |
|
|
} |
| 763 |
|
|
|
| 764 |
|
|
/* |
| 765 |
|
|
* Next, do the accept(). if we get an error, we should drop the |
| 766 |
|
|
* reserved fd limit, but we can deal with that when comm_open() |
| 767 |
|
|
* also does it. XXX -- adrian |
| 768 |
|
|
*/ |
| 769 |
|
|
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
| 770 |
|
|
if (newfd < 0) |
| 771 |
|
|
{ |
| 772 |
|
|
#ifdef _WIN32 |
| 773 |
|
|
errno = WSAGetLastError(); |
| 774 |
|
|
#endif |
| 775 |
|
|
return -1; |
| 776 |
|
|
} |
| 777 |
|
|
|
| 778 |
|
|
#ifdef IPV6 |
| 779 |
|
|
remove_ipv6_mapping(pn); |
| 780 |
|
|
#else |
| 781 |
|
|
pn->ss_len = addrlen; |
| 782 |
|
|
#endif |
| 783 |
|
|
|
| 784 |
|
|
execute_callback(setup_socket_cb, newfd); |
| 785 |
|
|
|
| 786 |
|
|
/* .. and return */ |
| 787 |
|
|
return newfd; |
| 788 |
|
|
} |
| 789 |
|
|
|
| 790 |
|
|
/* |
| 791 |
|
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
| 792 |
|
|
* 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 |
| 794 |
|
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
| 795 |
|
|
* |
| 796 |
|
|
*/ |
| 797 |
|
|
#ifdef IPV6 |
| 798 |
|
|
void |
| 799 |
|
|
remove_ipv6_mapping(struct irc_ssaddr *addr) |
| 800 |
|
|
{ |
| 801 |
|
|
if (addr->ss.ss_family == AF_INET6) |
| 802 |
|
|
{ |
| 803 |
|
|
struct sockaddr_in6 *v6; |
| 804 |
|
|
|
| 805 |
|
|
v6 = (struct sockaddr_in6*)addr; |
| 806 |
|
|
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) |
| 807 |
|
|
{ |
| 808 |
|
|
char v4ip[HOSTIPLEN]; |
| 809 |
|
|
struct sockaddr_in *v4 = (struct sockaddr_in*)addr; |
| 810 |
|
|
inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN); |
| 811 |
|
|
inet_pton(AF_INET, v4ip, &v4->sin_addr); |
| 812 |
|
|
addr->ss.ss_family = AF_INET; |
| 813 |
|
|
addr->ss_len = sizeof(struct sockaddr_in); |
| 814 |
|
|
} |
| 815 |
|
|
else |
| 816 |
|
|
addr->ss_len = sizeof(struct sockaddr_in6); |
| 817 |
|
|
} |
| 818 |
|
|
else |
| 819 |
|
|
addr->ss_len = sizeof(struct sockaddr_in); |
| 820 |
|
|
} |
| 821 |
|
|
#endif |