| 1 |
/* |
| 2 |
* conn.c |
| 3 |
* |
| 4 |
* Does all this gloriously nifty connection handling stuff... |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#define FAIM_INTERNAL |
| 9 |
#define FAIM_NEED_CONN_INTERNAL |
| 10 |
#include <aim.h> |
| 11 |
|
| 12 |
#ifndef _WIN32 |
| 13 |
#include <netdb.h> |
| 14 |
#include <sys/socket.h> |
| 15 |
#include <netinet/in.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#ifdef _WIN32 |
| 19 |
#include "win32dep.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
/* |
| 23 |
* In OSCAR, every connection has a set of SNAC groups associated |
| 24 |
* with it. These are the groups that you can send over this connection |
| 25 |
* without being guarenteed a "Not supported" SNAC error. |
| 26 |
* |
| 27 |
* The grand theory of things says that these associations transcend |
| 28 |
* what libfaim calls "connection types" (conn->type). You can probably |
| 29 |
* see the elegance here, but since I want to revel in it for a bit, you |
| 30 |
* get to hear it all spelled out. |
| 31 |
* |
| 32 |
* So let us say that you have your core BOS connection running. One |
| 33 |
* of your modules has just given you a SNAC of the group 0x0004 to send |
| 34 |
* you. Maybe an IM destined for some twit in Greenland. So you start |
| 35 |
* at the top of your connection list, looking for a connection that |
| 36 |
* claims to support group 0x0004. You find one. Why, that neat BOS |
| 37 |
* connection of yours can do that. So you send it on its way. |
| 38 |
* |
| 39 |
* Now, say, that fellow from Greenland has friends and they all want to |
| 40 |
* meet up with you in a lame chat room. This has landed you a SNAC |
| 41 |
* in the family 0x000e and you have to admit you're a bit lost. You've |
| 42 |
* searched your connection list for someone who wants to make your life |
| 43 |
* easy and deliver this SNAC for you, but there isn't one there. |
| 44 |
* |
| 45 |
* Here comes the good bit. Without even letting anyone know, particularly |
| 46 |
* the module that decided to send this SNAC, and definitly not that twit |
| 47 |
* in Greenland, you send out a service request. In this request, you have |
| 48 |
* marked the need for a connection supporting group 0x000e. A few seconds |
| 49 |
* later, you receive a service redirect with an IP address and a cookie in |
| 50 |
* it. Great, you say. Now I have something to do. Off you go, making |
| 51 |
* that connection. One of the first things you get from this new server |
| 52 |
* is a message saying that indeed it does support the group you were looking |
| 53 |
* for. So you continue and send rate confirmation and all that. |
| 54 |
* |
| 55 |
* Then you remember you had that SNAC to send, and now you have a means to |
| 56 |
* do it, and you do, and everyone is happy. Except the Greenlander, who is |
| 57 |
* still stuck in the bitter cold. |
| 58 |
* |
| 59 |
* Oh, and this is useful for building the Migration SNACs, too. In the |
| 60 |
* future, this may help convince me to implement rate limit mitigation |
| 61 |
* for real. We'll see. |
| 62 |
* |
| 63 |
* Just to make me look better, I'll say that I've known about this great |
| 64 |
* scheme for quite some time now. But I still haven't convinced myself |
| 65 |
* to make libfaim work that way. It would take a fair amount of effort, |
| 66 |
* and probably some client API changes as well. (Whenever I don't want |
| 67 |
* to do something, I just say it would change the client API. Then I |
| 68 |
* instantly have a couple of supporters of not doing it.) |
| 69 |
* |
| 70 |
* Generally, addgroup is only called by the internal handling of the |
| 71 |
* server ready SNAC. So if you want to do something before that, you'll |
| 72 |
* have to be more creative. That is done rather early, though, so I don't |
| 73 |
* think you have to worry about it. Unless you're me. I care deeply |
| 74 |
* about such inane things. |
| 75 |
* |
| 76 |
*/ |
| 77 |
faim_internal void aim_conn_addgroup(aim_conn_t *conn, fu16_t group) |
| 78 |
{ |
| 79 |
aim_conn_inside_t *ins = (aim_conn_inside_t *)conn->inside; |
| 80 |
struct snacgroup *sg; |
| 81 |
|
| 82 |
if (!(sg = malloc(sizeof(struct snacgroup)))) |
| 83 |
return; |
| 84 |
|
| 85 |
faimdprintf(aim_conn_getsess(conn), 1, "adding group 0x%04x\n", group); |
| 86 |
sg->group = group; |
| 87 |
|
| 88 |
sg->next = ins->groups; |
| 89 |
ins->groups = sg; |
| 90 |
|
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
faim_export aim_conn_t *aim_conn_findbygroup(aim_session_t *sess, fu16_t group) |
| 95 |
{ |
| 96 |
aim_conn_t *cur; |
| 97 |
|
| 98 |
for (cur = sess->connlist; cur; cur = cur->next) { |
| 99 |
aim_conn_inside_t *ins = (aim_conn_inside_t *)cur->inside; |
| 100 |
struct snacgroup *sg; |
| 101 |
|
| 102 |
for (sg = ins->groups; sg; sg = sg->next) { |
| 103 |
if (sg->group == group) |
| 104 |
return cur; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
static void connkill_snacgroups(struct snacgroup **head) |
| 112 |
{ |
| 113 |
struct snacgroup *sg; |
| 114 |
|
| 115 |
for (sg = *head; sg; ) { |
| 116 |
struct snacgroup *tmp; |
| 117 |
|
| 118 |
tmp = sg->next; |
| 119 |
free(sg); |
| 120 |
sg = tmp; |
| 121 |
} |
| 122 |
|
| 123 |
*head = NULL; |
| 124 |
|
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
static void connkill_rates(struct rateclass **head) |
| 129 |
{ |
| 130 |
struct rateclass *rc; |
| 131 |
|
| 132 |
for (rc = *head; rc; ) { |
| 133 |
struct rateclass *tmp; |
| 134 |
struct snacpair *sp; |
| 135 |
|
| 136 |
tmp = rc->next; |
| 137 |
|
| 138 |
for (sp = rc->members; sp; ) { |
| 139 |
struct snacpair *tmpsp; |
| 140 |
|
| 141 |
tmpsp = sp->next; |
| 142 |
free(sp); |
| 143 |
sp = tmpsp; |
| 144 |
} |
| 145 |
free(rc); |
| 146 |
|
| 147 |
rc = tmp; |
| 148 |
} |
| 149 |
|
| 150 |
*head = NULL; |
| 151 |
|
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
static void connkill_real(aim_session_t *sess, aim_conn_t **deadconn) |
| 156 |
{ |
| 157 |
|
| 158 |
aim_rxqueue_cleanbyconn(sess, *deadconn); |
| 159 |
aim_tx_cleanqueue(sess, *deadconn); |
| 160 |
|
| 161 |
if ((*deadconn)->fd != -1) |
| 162 |
aim_conn_close(*deadconn); |
| 163 |
|
| 164 |
/* |
| 165 |
* XXX ->priv should never be touched by the library. I know |
| 166 |
* it used to be, but I'm getting rid of all that. Use |
| 167 |
* ->internal instead. |
| 168 |
*/ |
| 169 |
if ((*deadconn)->priv) |
| 170 |
free((*deadconn)->priv); |
| 171 |
|
| 172 |
/* |
| 173 |
* This will free ->internal if it necessary... |
| 174 |
*/ |
| 175 |
if ((*deadconn)->type == AIM_CONN_TYPE_CHAT) |
| 176 |
aim_conn_kill_chat(sess, *deadconn); |
| 177 |
|
| 178 |
if ((*deadconn)->inside) { |
| 179 |
aim_conn_inside_t *inside = (aim_conn_inside_t *)(*deadconn)->inside; |
| 180 |
|
| 181 |
connkill_snacgroups(&inside->groups); |
| 182 |
connkill_rates(&inside->rates); |
| 183 |
|
| 184 |
free(inside); |
| 185 |
} |
| 186 |
|
| 187 |
free(*deadconn); |
| 188 |
*deadconn = NULL; |
| 189 |
|
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Clears out connection list, killing remaining connections. |
| 195 |
* |
| 196 |
* @param sess Session to be cleared |
| 197 |
*/ |
| 198 |
static void aim_connrst(aim_session_t *sess) |
| 199 |
{ |
| 200 |
|
| 201 |
if (sess->connlist) { |
| 202 |
aim_conn_t *cur = sess->connlist, *tmp; |
| 203 |
|
| 204 |
while (cur) { |
| 205 |
tmp = cur->next; |
| 206 |
aim_conn_close(cur); |
| 207 |
connkill_real(sess, &cur); |
| 208 |
cur = tmp; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
sess->connlist = NULL; |
| 213 |
|
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Reset a connection to default values. |
| 219 |
* Initializes and/or resets a connection structure. |
| 220 |
* |
| 221 |
* @param deadconn Connection to be reset |
| 222 |
*/ |
| 223 |
static void aim_conn_init(aim_conn_t *deadconn) |
| 224 |
{ |
| 225 |
|
| 226 |
if (!deadconn) |
| 227 |
return; |
| 228 |
|
| 229 |
deadconn->fd = -1; |
| 230 |
deadconn->subtype = -1; |
| 231 |
deadconn->type = -1; |
| 232 |
deadconn->seqnum = 0; |
| 233 |
deadconn->lastactivity = 0; |
| 234 |
deadconn->forcedlatency = 0; |
| 235 |
deadconn->handlerlist = NULL; |
| 236 |
deadconn->priv = NULL; |
| 237 |
memset(deadconn->inside, 0, sizeof(aim_conn_inside_t)); |
| 238 |
|
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* aim_conn_getnext - Gets a new connection structure. |
| 244 |
* @sess: Session |
| 245 |
* |
| 246 |
* Allocate a new empty connection structure. |
| 247 |
* |
| 248 |
*/ |
| 249 |
static aim_conn_t *aim_conn_getnext(aim_session_t *sess) |
| 250 |
{ |
| 251 |
aim_conn_t *newconn; |
| 252 |
|
| 253 |
if (!(newconn = malloc(sizeof(aim_conn_t)))) |
| 254 |
return NULL; |
| 255 |
memset(newconn, 0, sizeof(aim_conn_t)); |
| 256 |
|
| 257 |
if (!(newconn->inside = malloc(sizeof(aim_conn_inside_t)))) { |
| 258 |
free(newconn); |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
memset(newconn->inside, 0, sizeof(aim_conn_inside_t)); |
| 262 |
|
| 263 |
aim_conn_init(newconn); |
| 264 |
|
| 265 |
newconn->next = sess->connlist; |
| 266 |
sess->connlist = newconn; |
| 267 |
|
| 268 |
return newconn; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* aim_conn_kill - Close and free a connection. |
| 273 |
* @sess: Session for the connection |
| 274 |
* @deadconn: Connection to be freed |
| 275 |
* |
| 276 |
* Close, clear, and free a connection structure. Should never be |
| 277 |
* called from within libfaim. |
| 278 |
* |
| 279 |
*/ |
| 280 |
faim_export void aim_conn_kill(aim_session_t *sess, aim_conn_t **deadconn) |
| 281 |
{ |
| 282 |
aim_conn_t *cur, **prev; |
| 283 |
|
| 284 |
if (!deadconn || !*deadconn) |
| 285 |
return; |
| 286 |
|
| 287 |
for (prev = &sess->connlist; (cur = *prev); ) { |
| 288 |
if (cur == *deadconn) { |
| 289 |
*prev = cur->next; |
| 290 |
break; |
| 291 |
} |
| 292 |
prev = &cur->next; |
| 293 |
} |
| 294 |
|
| 295 |
if (!cur) |
| 296 |
return; /* oops */ |
| 297 |
|
| 298 |
connkill_real(sess, &cur); |
| 299 |
|
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* aim_conn_close - Close a connection |
| 305 |
* @deadconn: Connection to close |
| 306 |
* |
| 307 |
* Close (but not free) a connection. |
| 308 |
* |
| 309 |
* This leaves everything untouched except for clearing the |
| 310 |
* handler list and setting the fd to -1 (used to recognize |
| 311 |
* dead connections). It will also remove cookies if necessary. |
| 312 |
* |
| 313 |
*/ |
| 314 |
faim_export void aim_conn_close(aim_conn_t *deadconn) |
| 315 |
{ |
| 316 |
aim_rxcallback_t userfunc; |
| 317 |
|
| 318 |
if (deadconn->fd >= 3) |
| 319 |
close(deadconn->fd); |
| 320 |
|
| 321 |
deadconn->fd = -1; |
| 322 |
|
| 323 |
if ((userfunc = aim_callhandler(deadconn->sessv, deadconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNDEAD))) |
| 324 |
userfunc(deadconn->sessv, NULL, deadconn); |
| 325 |
|
| 326 |
if (deadconn->handlerlist) |
| 327 |
aim_clearhandlers(deadconn); |
| 328 |
|
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* aim_getconn_type - Find a connection of a specific type |
| 334 |
* @sess: Session to search |
| 335 |
* @type: Type of connection to look for |
| 336 |
* |
| 337 |
* Searches for a connection of the specified type in the |
| 338 |
* specified session. Returns the first connection of that |
| 339 |
* type found. |
| 340 |
* |
| 341 |
* XXX except for RENDEZVOUS, all uses of this should be removed and |
| 342 |
* use aim_conn_findbygroup() instead. |
| 343 |
*/ |
| 344 |
faim_export aim_conn_t *aim_getconn_type(aim_session_t *sess, int type) |
| 345 |
{ |
| 346 |
aim_conn_t *cur; |
| 347 |
|
| 348 |
for (cur = sess->connlist; cur; cur = cur->next) { |
| 349 |
if ((cur->type == type) && |
| 350 |
!(cur->status & AIM_CONN_STATUS_INPROGRESS)) |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
return cur; |
| 355 |
} |
| 356 |
|
| 357 |
faim_export aim_conn_t *aim_getconn_type_all(aim_session_t *sess, int type) |
| 358 |
{ |
| 359 |
aim_conn_t *cur; |
| 360 |
|
| 361 |
for (cur = sess->connlist; cur; cur = cur->next) { |
| 362 |
if (cur->type == type) |
| 363 |
break; |
| 364 |
} |
| 365 |
|
| 366 |
return cur; |
| 367 |
} |
| 368 |
|
| 369 |
/* If you pass -1 for the fd, you'll get what you ask for. Gibberish. */ |
| 370 |
faim_export aim_conn_t *aim_getconn_fd(aim_session_t *sess, int fd) |
| 371 |
{ |
| 372 |
aim_conn_t *cur; |
| 373 |
|
| 374 |
for (cur = sess->connlist; cur; cur = cur->next) { |
| 375 |
if (cur->fd == fd) |
| 376 |
break; |
| 377 |
} |
| 378 |
|
| 379 |
return cur; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* aim_proxyconnect - An extrememly quick and dirty SOCKS5 interface. |
| 384 |
* @sess: Session to connect |
| 385 |
* @host: Host to connect to |
| 386 |
* @port: Port to connect to |
| 387 |
* @statusret: Return value of the connection |
| 388 |
* |
| 389 |
* Attempts to connect to the specified host via the configured |
| 390 |
* proxy settings, if present. If no proxy is configured for |
| 391 |
* this session, the connection is done directly. |
| 392 |
* |
| 393 |
* XXX this is really awful. |
| 394 |
* |
| 395 |
*/ |
| 396 |
static int aim_proxyconnect(aim_session_t *sess, const char *host, fu16_t port, fu32_t *statusret) |
| 397 |
{ |
| 398 |
int fd = -1; |
| 399 |
|
| 400 |
if (strlen(sess->socksproxy.server)) { /* connecting via proxy */ |
| 401 |
int i; |
| 402 |
unsigned char buf[512]; |
| 403 |
struct sockaddr_in sa; |
| 404 |
struct hostent *hp; |
| 405 |
char *proxy; |
| 406 |
unsigned short proxyport = 1080; |
| 407 |
|
| 408 |
for(i=0;i<(int)strlen(sess->socksproxy.server);i++) { |
| 409 |
if (sess->socksproxy.server[i] == ':') { |
| 410 |
proxyport = atoi(&(sess->socksproxy.server[i+1])); |
| 411 |
break; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
proxy = (char *)malloc(i+1); |
| 416 |
strncpy(proxy, sess->socksproxy.server, i); |
| 417 |
proxy[i] = '\0'; |
| 418 |
|
| 419 |
if (!(hp = gethostbyname(proxy))) { |
| 420 |
faimdprintf(sess, 0, "proxyconnect: unable to resolve proxy name\n"); |
| 421 |
*statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR); |
| 422 |
return -1; |
| 423 |
} |
| 424 |
free(proxy); |
| 425 |
|
| 426 |
memset(&sa.sin_zero, 0, 8); |
| 427 |
sa.sin_port = htons(proxyport); |
| 428 |
memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); |
| 429 |
sa.sin_family = hp->h_addrtype; |
| 430 |
|
| 431 |
fd = socket(hp->h_addrtype, SOCK_STREAM, 0); |
| 432 |
if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { |
| 433 |
faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n"); |
| 434 |
close(fd); |
| 435 |
return -1; |
| 436 |
} |
| 437 |
|
| 438 |
i = 0; |
| 439 |
buf[0] = 0x05; /* SOCKS version 5 */ |
| 440 |
if (strlen(sess->socksproxy.username)) { |
| 441 |
buf[1] = 0x02; /* two methods */ |
| 442 |
buf[2] = 0x00; /* no authentication */ |
| 443 |
buf[3] = 0x02; /* username/password authentication */ |
| 444 |
i = 4; |
| 445 |
} else { |
| 446 |
buf[1] = 0x01; |
| 447 |
buf[2] = 0x00; |
| 448 |
i = 3; |
| 449 |
} |
| 450 |
if (write(fd, buf, i) < i) { |
| 451 |
*statusret = errno; |
| 452 |
close(fd); |
| 453 |
return -1; |
| 454 |
} |
| 455 |
if (read(fd, buf, 2) < 2) { |
| 456 |
*statusret = errno; |
| 457 |
close(fd); |
| 458 |
return -1; |
| 459 |
} |
| 460 |
|
| 461 |
if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
| 462 |
*statusret = EINVAL; |
| 463 |
close(fd); |
| 464 |
return -1; |
| 465 |
} |
| 466 |
|
| 467 |
/* check if we're doing username authentication */ |
| 468 |
if (buf[1] == 0x02) { |
| 469 |
i = aimutil_put8(buf, 0x01); /* version 1 */ |
| 470 |
i += aimutil_put8(buf+i, strlen(sess->socksproxy.username)); |
| 471 |
i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username)); |
| 472 |
i += aimutil_put8(buf+i, strlen(sess->socksproxy.password)); |
| 473 |
i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password)); |
| 474 |
if (write(fd, buf, i) < i) { |
| 475 |
*statusret = errno; |
| 476 |
close(fd); |
| 477 |
return -1; |
| 478 |
} |
| 479 |
if (read(fd, buf, 2) < 2) { |
| 480 |
*statusret = errno; |
| 481 |
close(fd); |
| 482 |
return -1; |
| 483 |
} |
| 484 |
if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
| 485 |
*statusret = EINVAL; |
| 486 |
close(fd); |
| 487 |
return -1; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
i = aimutil_put8(buf, 0x05); |
| 492 |
i += aimutil_put8(buf+i, 0x01); /* CONNECT */ |
| 493 |
i += aimutil_put8(buf+i, 0x00); /* reserved */ |
| 494 |
i += aimutil_put8(buf+i, 0x03); /* address type: host name */ |
| 495 |
i += aimutil_put8(buf+i, strlen(host)); |
| 496 |
i += aimutil_putstr(buf+i, host, strlen(host)); |
| 497 |
i += aimutil_put16(buf+i, port); |
| 498 |
|
| 499 |
if (write(fd, buf, i) < i) { |
| 500 |
*statusret = errno; |
| 501 |
close(fd); |
| 502 |
return -1; |
| 503 |
} |
| 504 |
|
| 505 |
if (read(fd, buf, 10) < 10) { |
| 506 |
*statusret = errno; |
| 507 |
close(fd); |
| 508 |
return -1; |
| 509 |
} |
| 510 |
if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
| 511 |
*statusret = EINVAL; |
| 512 |
close(fd); |
| 513 |
return -1; |
| 514 |
} |
| 515 |
|
| 516 |
} else { /* connecting directly */ |
| 517 |
struct sockaddr_in sa; |
| 518 |
struct hostent *hp; |
| 519 |
|
| 520 |
if (!(hp = gethostbyname(host))) { |
| 521 |
*statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR); |
| 522 |
return -1; |
| 523 |
} |
| 524 |
|
| 525 |
memset(&sa, 0, sizeof(struct sockaddr_in)); |
| 526 |
sa.sin_port = htons(port); |
| 527 |
memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); |
| 528 |
sa.sin_family = hp->h_addrtype; |
| 529 |
|
| 530 |
fd = socket(hp->h_addrtype, SOCK_STREAM, 0); |
| 531 |
|
| 532 |
if (sess->nonblocking) |
| 533 |
fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */ |
| 534 |
|
| 535 |
if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { |
| 536 |
if (sess->nonblocking) { |
| 537 |
if ((errno == EINPROGRESS) || (errno == EINTR)) { |
| 538 |
if (statusret) |
| 539 |
*statusret |= AIM_CONN_STATUS_INPROGRESS; |
| 540 |
return fd; |
| 541 |
} |
| 542 |
} |
| 543 |
close(fd); |
| 544 |
fd = -1; |
| 545 |
} |
| 546 |
} |
| 547 |
return fd; |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* aim_cloneconn - clone an aim_conn_t |
| 552 |
* @sess: session containing parent |
| 553 |
* @src: connection to clone |
| 554 |
* |
| 555 |
* A new connection is allocated, and the values are filled in |
| 556 |
* appropriately. Note that this function sets the new connnection's |
| 557 |
* ->priv pointer to be equal to that of its parent: only the pointer |
| 558 |
* is copied, not the data it points to. |
| 559 |
* |
| 560 |
* This function returns a pointer to the new aim_conn_t, or %NULL on |
| 561 |
* error |
| 562 |
*/ |
| 563 |
faim_internal aim_conn_t *aim_cloneconn(aim_session_t *sess, aim_conn_t *src) |
| 564 |
{ |
| 565 |
aim_conn_t *conn; |
| 566 |
|
| 567 |
if (!(conn = aim_conn_getnext(sess))) |
| 568 |
return NULL; |
| 569 |
|
| 570 |
conn->fd = src->fd; |
| 571 |
conn->type = src->type; |
| 572 |
conn->subtype = src->subtype; |
| 573 |
conn->seqnum = src->seqnum; |
| 574 |
conn->priv = src->priv; |
| 575 |
conn->internal = src->internal; |
| 576 |
conn->lastactivity = src->lastactivity; |
| 577 |
conn->forcedlatency = src->forcedlatency; |
| 578 |
conn->sessv = src->sessv; |
| 579 |
aim_clonehandlers(sess, conn, src); |
| 580 |
|
| 581 |
if (src->inside) { |
| 582 |
/* |
| 583 |
* XXX should clone this section as well, but since currently |
| 584 |
* this function only gets called for some of that rendezvous |
| 585 |
* crap, and not on SNAC connections, its probably okay for |
| 586 |
* now. |
| 587 |
* |
| 588 |
*/ |
| 589 |
} |
| 590 |
|
| 591 |
return conn; |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* aim_newconn - Open a new connection |
| 596 |
* @sess: Session to create connection in |
| 597 |
* @type: Type of connection to create |
| 598 |
* @dest: Host to connect to (in "host:port" syntax) |
| 599 |
* |
| 600 |
* Opens a new connection to the specified dest host of specified |
| 601 |
* type, using the proxy settings if available. If @host is %NULL, |
| 602 |
* the connection is allocated and returned, but no connection |
| 603 |
* is made. |
| 604 |
* |
| 605 |
* FIXME: Return errors in a more sane way. |
| 606 |
* |
| 607 |
*/ |
| 608 |
faim_export aim_conn_t *aim_newconn(aim_session_t *sess, int type, const char *dest) |
| 609 |
{ |
| 610 |
aim_conn_t *connstruct; |
| 611 |
fu16_t port = FAIM_LOGIN_PORT; |
| 612 |
char *host; |
| 613 |
int i, ret; |
| 614 |
|
| 615 |
if (!(connstruct = aim_conn_getnext(sess))) |
| 616 |
return NULL; |
| 617 |
|
| 618 |
connstruct->sessv = (void *)sess; |
| 619 |
connstruct->type = type; |
| 620 |
|
| 621 |
if (!dest) { /* just allocate a struct */ |
| 622 |
connstruct->fd = -1; |
| 623 |
connstruct->status = 0; |
| 624 |
return connstruct; |
| 625 |
} |
| 626 |
|
| 627 |
/* |
| 628 |
* As of 23 Jul 1999, AOL now sends the port number, preceded by a |
| 629 |
* colon, in the BOS redirect. This fatally breaks all previous |
| 630 |
* libfaims. Bad, bad AOL. |
| 631 |
* |
| 632 |
* We put this here to catch every case. |
| 633 |
* |
| 634 |
*/ |
| 635 |
|
| 636 |
for(i = 0; i < (int)strlen(dest); i++) { |
| 637 |
if (dest[i] == ':') { |
| 638 |
port = atoi(&(dest[i+1])); |
| 639 |
break; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
host = (char *)malloc(i+1); |
| 644 |
strncpy(host, dest, i); |
| 645 |
host[i] = '\0'; |
| 646 |
|
| 647 |
if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) { |
| 648 |
connstruct->fd = -1; |
| 649 |
connstruct->status = (errno | AIM_CONN_STATUS_CONNERR); |
| 650 |
free(host); |
| 651 |
return connstruct; |
| 652 |
} else |
| 653 |
connstruct->fd = ret; |
| 654 |
|
| 655 |
free(host); |
| 656 |
|
| 657 |
return connstruct; |
| 658 |
} |
| 659 |
|
| 660 |
/** |
| 661 |
* aim_conngetmaxfd - Return the highest valued file discriptor in session |
| 662 |
* @sess: Session to search |
| 663 |
* |
| 664 |
* Returns the highest valued filed descriptor of all open |
| 665 |
* connections in @sess. |
| 666 |
* |
| 667 |
*/ |
| 668 |
faim_export int aim_conngetmaxfd(aim_session_t *sess) |
| 669 |
{ |
| 670 |
int j; |
| 671 |
aim_conn_t *cur; |
| 672 |
|
| 673 |
for (cur = sess->connlist, j = 0; cur; cur = cur->next) { |
| 674 |
if (cur->fd > j) |
| 675 |
j = cur->fd; |
| 676 |
} |
| 677 |
|
| 678 |
return j; |
| 679 |
} |
| 680 |
|
| 681 |
/** |
| 682 |
* aim_conn_in_sess - Predicate to test the precense of a connection in a sess |
| 683 |
* @sess: Session to look in |
| 684 |
* @conn: Connection to look for |
| 685 |
* |
| 686 |
* Searches @sess for the passed connection. Returns 1 if its present, |
| 687 |
* zero otherwise. |
| 688 |
* |
| 689 |
*/ |
| 690 |
faim_export int aim_conn_in_sess(aim_session_t *sess, aim_conn_t *conn) |
| 691 |
{ |
| 692 |
aim_conn_t *cur; |
| 693 |
|
| 694 |
for (cur = sess->connlist; cur; cur = cur->next) { |
| 695 |
if (cur == conn) |
| 696 |
return 1; |
| 697 |
} |
| 698 |
|
| 699 |
return 0; |
| 700 |
} |
| 701 |
|
| 702 |
/** |
| 703 |
* aim_select - Wait for a socket with data or timeout |
| 704 |
* @sess: Session to wait on |
| 705 |
* @timeout: How long to wait |
| 706 |
* @status: Return status |
| 707 |
* |
| 708 |
* Waits for a socket with data or for timeout, whichever comes first. |
| 709 |
* See select(2). |
| 710 |
* |
| 711 |
* Return codes in *status: |
| 712 |
* -1 error in select() (%NULL returned) |
| 713 |
* 0 no events pending (%NULL returned) |
| 714 |
* 1 outgoing data pending (%NULL returned) |
| 715 |
* 2 incoming data pending (connection with pending data returned) |
| 716 |
* |
| 717 |
*/ |
| 718 |
faim_export aim_conn_t *aim_select(aim_session_t *sess, struct timeval *timeout, int *status) |
| 719 |
{ |
| 720 |
aim_conn_t *cur; |
| 721 |
fd_set fds, wfds; |
| 722 |
int maxfd, i, haveconnecting = 0; |
| 723 |
|
| 724 |
if (!sess->connlist) { |
| 725 |
*status = -1; |
| 726 |
return NULL; |
| 727 |
} |
| 728 |
|
| 729 |
FD_ZERO(&fds); |
| 730 |
FD_ZERO(&wfds); |
| 731 |
|
| 732 |
for (cur = sess->connlist, maxfd = 0; cur; cur = cur->next) { |
| 733 |
if (cur->fd == -1) { |
| 734 |
/* don't let invalid/dead connections sit around */ |
| 735 |
*status = 2; |
| 736 |
return cur; |
| 737 |
} else if (cur->status & AIM_CONN_STATUS_INPROGRESS) { |
| 738 |
FD_SET(cur->fd, &wfds); |
| 739 |
|
| 740 |
haveconnecting++; |
| 741 |
} |
| 742 |
FD_SET(cur->fd, &fds); |
| 743 |
if (cur->fd > maxfd) |
| 744 |
maxfd = cur->fd; |
| 745 |
} |
| 746 |
|
| 747 |
/* |
| 748 |
* If we have data waiting to be sent, return |
| 749 |
* |
| 750 |
* We have to not do this if theres at least one |
| 751 |
* connection thats still connecting, since that connection |
| 752 |
* may have queued data and this return would prevent |
| 753 |
* the connection from ever completing! This is a major |
| 754 |
* inadequacy of the libfaim way of doing things. It means |
| 755 |
* that nothing can transmit as long as there's connecting |
| 756 |
* sockets. Evil. |
| 757 |
* |
| 758 |
* But its still better than having blocking connects. |
| 759 |
* |
| 760 |
*/ |
| 761 |
if (!haveconnecting && sess->queue_outgoing) { |
| 762 |
*status = 1; |
| 763 |
return NULL; |
| 764 |
} |
| 765 |
|
| 766 |
if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) { |
| 767 |
for (cur = sess->connlist; cur; cur = cur->next) { |
| 768 |
if ((FD_ISSET(cur->fd, &fds)) || |
| 769 |
((cur->status & AIM_CONN_STATUS_INPROGRESS) && |
| 770 |
FD_ISSET(cur->fd, &wfds))) { |
| 771 |
*status = 2; |
| 772 |
return cur; |
| 773 |
} |
| 774 |
} |
| 775 |
*status = 0; /* shouldn't happen */ |
| 776 |
} else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */ |
| 777 |
*status = 0; |
| 778 |
else |
| 779 |
*status = i; /* can be 0 or -1 */ |
| 780 |
|
| 781 |
return NULL; /* no waiting or error, return */ |
| 782 |
} |
| 783 |
|
| 784 |
/** |
| 785 |
* aim_conn_setlatency - Set a forced latency value for connection |
| 786 |
* @conn: Conn to set latency for |
| 787 |
* @newval: Number of seconds to force between transmits |
| 788 |
* |
| 789 |
* Causes @newval seconds to be spent between transmits on a connection. |
| 790 |
* |
| 791 |
* This is my lame attempt at overcoming not understanding the rate |
| 792 |
* limiting. |
| 793 |
* |
| 794 |
* XXX: This should really be replaced with something that scales and |
| 795 |
* backs off like the real rate limiting does. |
| 796 |
* |
| 797 |
*/ |
| 798 |
faim_export int aim_conn_setlatency(aim_conn_t *conn, int newval) |
| 799 |
{ |
| 800 |
|
| 801 |
if (!conn) |
| 802 |
return -1; |
| 803 |
|
| 804 |
conn->forcedlatency = newval; |
| 805 |
conn->lastactivity = 0; /* reset this just to make sure */ |
| 806 |
|
| 807 |
return 0; |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* aim_setupproxy - Configure a proxy for this session |
| 812 |
* @sess: Session to set proxy for |
| 813 |
* @server: SOCKS server |
| 814 |
* @username: SOCKS username |
| 815 |
* @password: SOCKS password |
| 816 |
* |
| 817 |
* Call this with your SOCKS5 proxy server parameters before |
| 818 |
* the first call to aim_newconn(). If called with all %NULL |
| 819 |
* args, it will clear out a previously set proxy. |
| 820 |
* |
| 821 |
* Set username and password to %NULL if not applicable. |
| 822 |
* |
| 823 |
*/ |
| 824 |
faim_export void aim_setupproxy(aim_session_t *sess, const char *server, const char *username, const char *password) |
| 825 |
{ |
| 826 |
/* clear out the proxy info */ |
| 827 |
if (!server || !strlen(server)) { |
| 828 |
memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server)); |
| 829 |
memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username)); |
| 830 |
memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password)); |
| 831 |
return; |
| 832 |
} |
| 833 |
|
| 834 |
strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server)); |
| 835 |
if (username && strlen(username)) |
| 836 |
strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username)); |
| 837 |
if (password && strlen(password)) |
| 838 |
strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password)); |
| 839 |
|
| 840 |
return; |
| 841 |
} |
| 842 |
|
| 843 |
static void defaultdebugcb(aim_session_t *sess, int level, const char *format, va_list va) |
| 844 |
{ |
| 845 |
|
| 846 |
vfprintf(stderr, format, va); |
| 847 |
|
| 848 |
return; |
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* Initializes a session structure by setting the initial values |
| 853 |
* stuff in the aim_session_t struct. |
| 854 |
* |
| 855 |
* @param sess Session to initialize. |
| 856 |
* @param nonblocking Set to true if you want connections to be non-blocking. |
| 857 |
* @param debuglevel Level of debugging output (zero is least). |
| 858 |
*/ |
| 859 |
faim_export void aim_session_init(aim_session_t *sess, bool nonblocking, int debuglevel) |
| 860 |
{ |
| 861 |
|
| 862 |
if (!sess) |
| 863 |
return; |
| 864 |
|
| 865 |
memset(sess, 0, sizeof(aim_session_t)); |
| 866 |
aim_connrst(sess); |
| 867 |
sess->queue_outgoing = NULL; |
| 868 |
sess->queue_incoming = NULL; |
| 869 |
aim_initsnachash(sess); |
| 870 |
sess->msgcookies = NULL; |
| 871 |
sess->nonblocking = nonblocking; |
| 872 |
sess->debug = debuglevel; |
| 873 |
sess->debugcb = defaultdebugcb; |
| 874 |
sess->modlistv = NULL; |
| 875 |
sess->snacid_next = 0x00000001; |
| 876 |
|
| 877 |
sess->locate.userinfo = NULL; |
| 878 |
sess->locate.torequest = NULL; |
| 879 |
sess->locate.requested = NULL; |
| 880 |
sess->locate.waiting_for_response = FALSE; |
| 881 |
sess->ssi.received_data = 0; |
| 882 |
sess->ssi.numitems = 0; |
| 883 |
sess->ssi.official = NULL; |
| 884 |
sess->ssi.local = NULL; |
| 885 |
sess->ssi.pending = NULL; |
| 886 |
sess->ssi.timestamp = (time_t)0; |
| 887 |
sess->ssi.waiting_for_ack = 0; |
| 888 |
sess->icq_info = NULL; |
| 889 |
sess->authinfo = NULL; |
| 890 |
sess->emailinfo = NULL; |
| 891 |
sess->oft_info = NULL; |
| 892 |
|
| 893 |
/* |
| 894 |
* This must always be set. Default to the queue-based |
| 895 |
* version for back-compatibility. |
| 896 |
*/ |
| 897 |
aim_tx_setenqueue(sess, AIM_TX_QUEUED, NULL); |
| 898 |
|
| 899 |
/* |
| 900 |
* Register all the modules for this session... |
| 901 |
*/ |
| 902 |
aim__registermodule(sess, misc_modfirst); /* load the catch-all first */ |
| 903 |
aim__registermodule(sess, service_modfirst); |
| 904 |
aim__registermodule(sess, locate_modfirst); |
| 905 |
aim__registermodule(sess, buddylist_modfirst); |
| 906 |
aim__registermodule(sess, msg_modfirst); |
| 907 |
aim__registermodule(sess, adverts_modfirst); |
| 908 |
aim__registermodule(sess, invite_modfirst); |
| 909 |
aim__registermodule(sess, admin_modfirst); |
| 910 |
aim__registermodule(sess, popups_modfirst); |
| 911 |
aim__registermodule(sess, bos_modfirst); |
| 912 |
aim__registermodule(sess, search_modfirst); |
| 913 |
aim__registermodule(sess, stats_modfirst); |
| 914 |
aim__registermodule(sess, translate_modfirst); |
| 915 |
aim__registermodule(sess, chatnav_modfirst); |
| 916 |
aim__registermodule(sess, chat_modfirst); |
| 917 |
aim__registermodule(sess, odir_modfirst); |
| 918 |
aim__registermodule(sess, bart_modfirst); |
| 919 |
/* missing 0x11 - 0x12 */ |
| 920 |
aim__registermodule(sess, ssi_modfirst); |
| 921 |
/* missing 0x14 */ |
| 922 |
aim__registermodule(sess, icq_modfirst); /* XXX - Make sure this isn't sent for AIM */ |
| 923 |
/* missing 0x16 */ |
| 924 |
aim__registermodule(sess, auth_modfirst); |
| 925 |
aim__registermodule(sess, email_modfirst); |
| 926 |
|
| 927 |
return; |
| 928 |
} |
| 929 |
|
| 930 |
/** |
| 931 |
* aim_session_kill - Deallocate a session |
| 932 |
* @sess: Session to kill |
| 933 |
* |
| 934 |
*/ |
| 935 |
faim_export void aim_session_kill(aim_session_t *sess) |
| 936 |
{ |
| 937 |
aim_cleansnacs(sess, -1); |
| 938 |
|
| 939 |
aim_logoff(sess); |
| 940 |
|
| 941 |
aim__shutdownmodules(sess); |
| 942 |
|
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
/** |
| 947 |
* aim_setdebuggingcb - Set the function to call when outputting debugging info |
| 948 |
* @sess: Session to change |
| 949 |
* @cb: Function to call |
| 950 |
* |
| 951 |
* The function specified is called whenever faimdprintf() is used within |
| 952 |
* libfaim, and the session's debugging level is greater tha nor equal to |
| 953 |
* the value faimdprintf was called with. |
| 954 |
* |
| 955 |
*/ |
| 956 |
faim_export int aim_setdebuggingcb(aim_session_t *sess, faim_debugging_callback_t cb) |
| 957 |
{ |
| 958 |
|
| 959 |
if (!sess) |
| 960 |
return -1; |
| 961 |
|
| 962 |
sess->debugcb = cb; |
| 963 |
|
| 964 |
return 0; |
| 965 |
} |
| 966 |
|
| 967 |
/** |
| 968 |
* aim_conn_isconnecting - Determine if a connection is connecting |
| 969 |
* @conn: Connection to examine |
| 970 |
* |
| 971 |
* Returns nonzero if the connection is in the process of |
| 972 |
* connecting (or if it just completed and aim_conn_completeconnect() |
| 973 |
* has yet to be called on it). |
| 974 |
* |
| 975 |
*/ |
| 976 |
faim_export int aim_conn_isconnecting(aim_conn_t *conn) |
| 977 |
{ |
| 978 |
|
| 979 |
if (!conn) |
| 980 |
return 0; |
| 981 |
|
| 982 |
return !!(conn->status & AIM_CONN_STATUS_INPROGRESS); |
| 983 |
} |
| 984 |
|
| 985 |
/* |
| 986 |
* XXX this is nearly as ugly as proxyconnect(). |
| 987 |
*/ |
| 988 |
faim_export int aim_conn_completeconnect(aim_session_t *sess, aim_conn_t *conn) |
| 989 |
{ |
| 990 |
aim_rxcallback_t userfunc; |
| 991 |
|
| 992 |
if (!conn || (conn->fd == -1)) |
| 993 |
return -1; |
| 994 |
|
| 995 |
if (!(conn->status & AIM_CONN_STATUS_INPROGRESS)) |
| 996 |
return -1; |
| 997 |
|
| 998 |
fcntl(conn->fd, F_SETFL, 0); |
| 999 |
|
| 1000 |
conn->status &= ~AIM_CONN_STATUS_INPROGRESS; |
| 1001 |
|
| 1002 |
if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE))) |
| 1003 |
userfunc(sess, NULL, conn); |
| 1004 |
|
| 1005 |
/* Flush out the queues if there was something waiting for this conn */ |
| 1006 |
aim_tx_flushqueue(sess); |
| 1007 |
|
| 1008 |
return 0; |
| 1009 |
} |
| 1010 |
|
| 1011 |
faim_export aim_session_t *aim_conn_getsess(aim_conn_t *conn) |
| 1012 |
{ |
| 1013 |
|
| 1014 |
if (!conn) |
| 1015 |
return NULL; |
| 1016 |
|
| 1017 |
return (aim_session_t *)conn->sessv; |
| 1018 |
} |
| 1019 |
|
| 1020 |
/* |
| 1021 |
* aim_logoff() |
| 1022 |
* |
| 1023 |
* Closes -ALL- open connections. |
| 1024 |
* |
| 1025 |
*/ |
| 1026 |
faim_export int aim_logoff(aim_session_t *sess) |
| 1027 |
{ |
| 1028 |
|
| 1029 |
aim_connrst(sess); /* in case we want to connect again */ |
| 1030 |
|
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* |
| 1035 |
* aim_flap_nop() |
| 1036 |
* |
| 1037 |
* No-op. WinAIM 4.x sends these _every minute_ to keep |
| 1038 |
* the connection alive. |
| 1039 |
*/ |
| 1040 |
faim_export int aim_flap_nop(aim_session_t *sess, aim_conn_t *conn) |
| 1041 |
{ |
| 1042 |
aim_frame_t *fr; |
| 1043 |
|
| 1044 |
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x05, 0))) |
| 1045 |
return -ENOMEM; |
| 1046 |
|
| 1047 |
aim_tx_enqueue(sess, fr); |
| 1048 |
|
| 1049 |
/* clean out SNACs over 60sec old */ |
| 1050 |
aim_cleansnacs(sess, 60); |
| 1051 |
|
| 1052 |
return 0; |
| 1053 |
} |