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