📄 flap_connection.c
字号:
* of your modules has just given you a SNAC of the group 0x0004 to send * you. Maybe an IM destined for some twit in Greenland. So you start * at the top of your connection list, looking for a connection that * claims to support group 0x0004. You find one. Why, that neat BOS * connection of yours can do that. So you send it on its way. * * Now, say, that fellow from Greenland has friends and they all want to * meet up with you in a lame chat room. This has landed you a SNAC * in the family 0x000e and you have to admit you're a bit lost. You've * searched your connection list for someone who wants to make your life * easy and deliver this SNAC for you, but there isn't one there. * * Here comes the good bit. Without even letting anyone know, particularly * the module that decided to send this SNAC, and definitely not that twit * in Greenland, you send out a service request. In this request, you have * marked the need for a connection supporting group 0x000e. A few seconds * later, you receive a service redirect with an IP address and a cookie in * it. Great, you say. Now I have something to do. Off you go, making * that connection. One of the first things you get from this new server * is a message saying that indeed it does support the group you were looking * for. So you continue and send rate confirmation and all that. * * Then you remember you had that SNAC to send, and now you have a means to * do it, and you do, and everyone is happy. Except the Greenlander, who is * still stuck in the bitter cold. * * Oh, and this is useful for building the Migration SNACs, too. In the * future, this may help convince me to implement rate limit mitigation * for real. We'll see. * * Just to make me look better, I'll say that I've known about this great * scheme for quite some time now. But I still haven't convinced myself * to make libfaim work that way. It would take a fair amount of effort, * and probably some client API changes as well. (Whenever I don't want * to do something, I just say it would change the client API. Then I * instantly have a couple of supporters of not doing it.) * * Generally, addgroup is only called by the internal handling of the * server ready SNAC. So if you want to do something before that, you'll * have to be more creative. That is done rather early, though, so I don't * think you have to worry about it. Unless you're me. I care deeply * about such inane things. * *//** * Find a FlapConnection that supports the given oscar * family. */FlapConnection *flap_connection_findbygroup(OscarData *od, guint16 group){ GSList *cur; for (cur = od->oscar_connections; cur != NULL; cur = cur->next) { FlapConnection *conn; GSList *l; conn = cur->data; for (l = conn->groups; l != NULL; l = l->next) { if (GPOINTER_TO_UINT(l->data) == group) return conn; } } return NULL;}/** * Locates a connection of the specified type in the * specified session. * * TODO: Use flap_connection_findbygroup everywhere and get rid of this. * * @param od The session to search. * @param type The type of connection to look for. * * @return Returns the first connection found of the given target type, * or NULL if none could be found. */FlapConnection *flap_connection_getbytype(OscarData *od, int type){ GSList *cur; for (cur = od->oscar_connections; cur != NULL; cur = cur->next) { FlapConnection *conn; conn = cur->data; if ((conn->type == type) && (conn->connected)) return conn; } return NULL;}FlapConnection *flap_connection_getbytype_all(OscarData *od, int type){ GSList *cur; for (cur = od->oscar_connections; cur; cur = cur->next) { FlapConnection *conn; conn = cur->data; if (conn->type == type) return conn; } return NULL;}/** * Allocate a new FLAP frame. * * @param channel The FLAP channel. This is almost always 2. */FlapFrame *flap_frame_new(OscarData *od, guint16 channel, int datalen){ FlapFrame *frame; frame = g_new0(FlapFrame, 1); frame->channel = channel; if (datalen > 0) byte_stream_new(&frame->data, datalen); return frame;}static voidparse_snac(OscarData *od, FlapConnection *conn, FlapFrame *frame){ aim_module_t *cur; aim_modsnac_t snac; if (byte_stream_empty(&frame->data) < 10) return; snac.family = byte_stream_get16(&frame->data); snac.subtype = byte_stream_get16(&frame->data); snac.flags = byte_stream_get16(&frame->data); snac.id = byte_stream_get32(&frame->data); /* SNAC flags are apparently uniform across all SNACs, so we handle them here */ if (snac.flags & 0x0001) { /* * This means the SNAC will be followed by another SNAC with * related information. We don't need to do anything about * this here. */ } if (snac.flags & 0x8000) { /* * This packet contains the version of the family that this SNAC is * in. You get this when your SSI module is version 2 or higher. * For now we have no need for this, but you could always save * it as a part of aim_modnsac_t, or something. The format is... * 2 byte length of total mini-header (which is 6 bytes), then TLV * of type 0x0001, length 0x0002, value is the 2 byte version * number */ byte_stream_advance(&frame->data, byte_stream_get16(&frame->data)); } for (cur = (aim_module_t *)od->modlistv; cur; cur = cur->next) { if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && (cur->family != snac.family)) continue; if (cur->snachandler(od, conn, cur, frame, &snac, &frame->data)) return; }}static voidparse_fakesnac(OscarData *od, FlapConnection *conn, FlapFrame *frame, guint16 family, guint16 subtype){ aim_module_t *cur; aim_modsnac_t snac; snac.family = family; snac.subtype = subtype; snac.flags = snac.id = 0; for (cur = (aim_module_t *)od->modlistv; cur; cur = cur->next) { if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && (cur->family != snac.family)) continue; if (cur->snachandler(od, conn, cur, frame, &snac, &frame->data)) return; }}static voidparse_flap_ch4(OscarData *od, FlapConnection *conn, FlapFrame *frame){ GSList *tlvlist; char *msg = NULL; guint16 code = 0; aim_rxcallback_t userfunc; if (byte_stream_empty(&frame->data) == 0) { /* XXX should do something with this */ return; } /* An ICQ account is logging in */ if (conn->type == SNAC_FAMILY_AUTH) { parse_fakesnac(od, conn, frame, 0x0017, 0x0003); return; } tlvlist = aim_tlvlist_read(&frame->data); if (aim_tlv_gettlv(tlvlist, 0x0009, 1)) code = aim_tlv_get16(tlvlist, 0x0009, 1); if (aim_tlv_gettlv(tlvlist, 0x000b, 1)) msg = aim_tlv_getstr(tlvlist, 0x000b, 1); if ((userfunc = aim_callhandler(od, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNERR))) userfunc(od, conn, frame, code, msg); aim_tlvlist_free(tlvlist); g_free(msg);}/** * Takes a new incoming FLAP frame and sends it to the appropriate * handler function to be parsed. */static voidparse_flap(OscarData *od, FlapConnection *conn, FlapFrame *frame){ if (frame->channel == 0x01) { guint32 flap_version = byte_stream_get32(&frame->data); if (flap_version != 0x00000001) { /* Error! */ purple_debug_warning("oscar", "Expecting FLAP version " "0x00000001 but received FLAP version %08lx. Closing connection.\n", flap_version); flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_INVALID_DATA, NULL); } else conn->connected = TRUE; } else if (frame->channel == 0x02) { parse_snac(od, conn, frame); } else if (frame->channel == 0x04) { parse_flap_ch4(od, conn, frame); } else if (frame->channel == 0x05) { /* TODO: Reset our keepalive watchdog? */ }}/** * Read in all available data on the socket for a given connection. * All complete FLAPs handled immedate after they're received. * Incomplete FLAP data is stored locally and appended to the next * time this callback is triggered. */voidflap_connection_recv_cb(gpointer data, gint source, PurpleInputCondition cond){ FlapConnection *conn; ssize_t read; conn = data; /* Read data until we run out of data and break out of the loop */ while (TRUE) { /* Start reading a new FLAP */ if (conn->buffer_incoming.data.data == NULL) { /* Read the first 6 bytes (the FLAP header) */ read = recv(conn->fd, conn->header + conn->header_received, 6 - conn->header_received, 0); /* Check if the FLAP server closed the connection */ if (read == 0) { flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED, NULL); break; } /* If there was an error then close the connection */ if (read < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) /* No worries */ break; /* Error! */ flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION, strerror(errno)); break; } /* If we don't even have a complete FLAP header then do nothing */ conn->header_received += read; if (conn->header_received < 6) break; /* All FLAP frames must start with the byte 0x2a */ if (aimutil_get8(&conn->header[0]) != 0x2a) { flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_INVALID_DATA, NULL); break; } /* Verify the sequence number sent by the server. */#if 0 /* TODO: Need to initialize conn->seqnum_in somewhere before we can use this. */ if (aimutil_get16(&conn->header[1]) != conn->seqnum_in++) { /* Received an out-of-order FLAP! */ flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_INVALID_DATA, NULL); break; }#endif /* Initialize a new temporary FlapFrame for incoming data */ conn->buffer_incoming.channel = aimutil_get8(&conn->header[1]); conn->buffer_incoming.seqnum = aimutil_get16(&conn->header[2]); conn->buffer_incoming.data.len = aimutil_get16(&conn->header[4]); conn->buffer_incoming.data.data = g_new(guint8, conn->buffer_incoming.data.len); conn->buffer_incoming.data.offset = 0; } if (conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset) { /* Read data into the temporary FlapFrame until it is complete */ read = recv(conn->fd, &conn->buffer_incoming.data.data[conn->buffer_incoming.data.offset], conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset, 0); /* Check if the FLAP server closed the connection */ if (read == 0) { flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED, NULL); break; } if (read < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) /* No worries */ break; /* Error! */ flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION, strerror(errno)); break; } conn->buffer_incoming.data.offset += read; if (conn->buffer_incoming.data.offset < conn->buffer_incoming.data.len) /* Waiting for more data to arrive */ break; } /* We have a complete FLAP! Handle it and continue reading */ byte_stream_rewind(&conn->buffer_incoming.data); parse_flap(conn->od, conn, &conn->buffer_incoming); conn->lastactivity = time(NULL); g_free(conn->buffer_incoming.data.data); conn->buffer_incoming.data.data = NULL; conn->header_received = 0; }}static voidsend_cb(gpointer data, gint source, PurpleInputCondition cond){ FlapConnection *conn; int writelen, ret; conn = data; writelen = purple_circ_buffer_get_max_read(conn->buffer_outgoing); if (writelen == 0) { purple_input_remove(conn->watcher_outgoing); conn->watcher_outgoing = 0; return; } ret = send(conn->fd, conn->buffer_outgoing->outptr, writelen, 0); if (ret <= 0) { if (ret < 0 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) /* No worries */ return; /* Error! */ purple_input_remove(conn->watcher_outgoing); conn->watcher_outgoing = 0; close(conn->fd); conn->fd = -1; flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION, strerror(errno)); return; } purple_circ_buffer_mark_read(conn->buffer_outgoing, ret);}static voidflap_connection_send_byte_stream(ByteStream *bs, FlapConnection *conn, size_t count){ if (conn == NULL) return; /* Make sure we don't send past the end of the bs */ if (count > byte_stream_empty(bs)) count = byte_stream_empty(bs); /* truncate to remaining space */ if (count == 0) return; /* Add everything to our outgoing buffer */ purple_circ_buffer_append(conn->buffer_outgoing, bs->data, count); /* If we haven't already started writing stuff, then start the cycle */ if ((conn->watcher_outgoing == 0) && (conn->fd >= 0)) { conn->watcher_outgoing = purple_input_add(conn->fd, PURPLE_INPUT_WRITE, send_cb, conn); send_cb(conn, conn->fd, 0); }}static voidsendframe_flap(FlapConnection *conn, FlapFrame *frame){ ByteStream bs; int payloadlen, bslen; payloadlen = byte_stream_curpos(&frame->data); byte_stream_new(&bs, 6 + payloadlen); /* FLAP header */ byte_stream_put8(&bs, 0x2a); byte_stream_put8(&bs, frame->channel); byte_stream_put16(&bs, frame->seqnum); byte_stream_put16(&bs, payloadlen); /* Payload */ byte_stream_rewind(&frame->data); byte_stream_putbs(&bs, &frame->data, payloadlen); bslen = byte_stream_curpos(&bs); byte_stream_rewind(&bs); flap_connection_send_byte_stream(&bs, conn, bslen); g_free(bs.data); /* XXX byte_stream_free */}voidflap_connection_send(FlapConnection *conn, FlapFrame *frame){ frame->seqnum = ++(conn->seqnum_out); sendframe_flap(conn, frame); flap_frame_destroy(frame);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -