/** * aim_gettlv16 - Retrieve the Nth TLV in chain as a 16bit integer. * @list: Source TLV chain * @type: TLV type to search for * @nth: Index of TLV to return * * Same as aim_gettlv(), except that the return value is a * 16bit integer instead of an aim_tlv_t. * */ faim_internal fu16_t aim_gettlv16(aim_tlvlist_t *list, const fu16_t t, const int n) { aim_tlv_t *tlv; if (!(tlv = aim_gettlv(list, t, n))) return 0; /* erm */ return aimutil_get16(tlv->value); }
guint16 byte_stream_get16(ByteStream *bs) { if (byte_stream_empty(bs) < 2) return 0; /* XXX throw an exception */ bs->offset += 2; return aimutil_get16(bs->data + bs->offset - 2); }
faim_internal fu16_t aimbs_get16(aim_bstream_t *bs) { if (aim_bstream_empty(bs) < 2) return 0; /* XXX throw an exception */ bs->offset += 2; return aimutil_get16(bs->data + bs->offset - 2); }
guint16 aimbs_get16(aim_bstream_t *bs) { if (aim_bstream_empty(bs) < 2) { return 0; /* XXX throw an exception */ } bs->offset += 2; return aimutil_get16(bs->data + bs->offset - 2); }
/** * This should be used to read ODC and OFT framing info. It should * NOT be used to read the payload sent across the connection (IMs, * file data, etc), and it should NOT be used to read proxy negotiation * headers. * * Unlike flap_connection_recv_cb(), this only reads one frame at a * time. This is done so that the watcher can be changed during the * handling of the frame. If the watcher is changed then this * function will not read in any more data. This happens when * reading the payload of a direct IM frame, or when we're * receiving a file from the remote user. Once the data has been * read, the watcher will be switched back to this function to * continue reading the next frame. */ void peer_connection_recv_cb(gpointer data, gint source, PurpleInputCondition cond) { PeerConnection *conn; gssize read; conn = data; /* Start reading a new ODC/OFT frame */ if (conn->buffer_incoming.data == NULL) { /* Read the first 6 bytes (magic string and frame length) */ read = recv(conn->fd, conn->header + conn->header_received, 6 - conn->header_received, 0); /* Check if the remote user closed the connection */ if (read == 0) { peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED, NULL); return; } /* If there was an error then close the connection */ if (read < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) /* No worries */ return; peer_connection_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION, g_strerror(errno)); return; } conn->lastactivity = time(NULL); /* If we don't even have the first 6 bytes then do nothing */ conn->header_received += read; if (conn->header_received < 6) return; /* All ODC/OFT frames must start with a magic string */ if (memcmp(conn->magic, conn->header, 4)) { purple_debug_warning("oscar", "Expecting magic string to " "be %c%c%c%c but received magic string %c%c%c%c. " "Closing connection.\n", conn->magic[0], conn->magic[1], conn->magic[2], conn->magic[3], conn->header[0], conn->header[1], conn->header[2], conn->header[3]); peer_connection_destroy(conn, OSCAR_DISCONNECT_INVALID_DATA, NULL); return; } /* Initialize a new temporary ByteStream for incoming data */ conn->buffer_incoming.len = aimutil_get16(&conn->header[4]) - 6; conn->buffer_incoming.data = g_new(guint8, conn->buffer_incoming.len); conn->buffer_incoming.offset = 0; } /* Read data into the temporary buffer until it is complete */ read = recv(conn->fd, &conn->buffer_incoming.data[conn->buffer_incoming.offset], conn->buffer_incoming.len - conn->buffer_incoming.offset, 0); /* Check if the remote user closed the connection */ if (read == 0) { peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED, NULL); return; } if (read < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) /* No worries */ return; peer_connection_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION, g_strerror(errno)); return; } conn->lastactivity = time(NULL); conn->buffer_incoming.offset += read; if (conn->buffer_incoming.offset < conn->buffer_incoming.len) /* Waiting for more data to arrive */ return; /* We have a complete ODC/OFT frame! Handle it and continue reading */ byte_stream_rewind(&conn->buffer_incoming); if (conn->type == OSCAR_CAPABILITY_DIRECTIM) { peer_odc_recv_frame(conn, &conn->buffer_incoming); } else if (conn->type == OSCAR_CAPABILITY_SENDFILE) { peer_oft_recv_frame(conn, &conn->buffer_incoming); } g_free(conn->buffer_incoming.data); conn->buffer_incoming.data = NULL; conn->header_received = 0; }
static void peer_proxy_connection_recv_cb(gpointer data, gint source, PurpleInputCondition cond) { PeerConnection *conn; gssize read; ProxyFrame *frame; conn = data; frame = conn->frame; /* Start reading a new proxy frame */ if (frame == NULL) { /* Read the first 12 bytes (frame length and header) */ read = recv(conn->fd, conn->proxy_header + conn->proxy_header_received, 12 - conn->proxy_header_received, 0); /* Check if the proxy server closed the connection */ if (read == 0) { purple_debug_info("oscar", "Peer proxy server closed connection\n"); peer_connection_trynext(conn); return; } /* If there was an error then close the connection */ if (read < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) /* No worries */ return; purple_debug_info("oscar", "Lost connection with peer proxy server\n"); peer_connection_trynext(conn); return; } conn->lastactivity = time(NULL); /* If we don't even have the first 12 bytes then do nothing */ conn->proxy_header_received += read; if (conn->proxy_header_received < 12) return; /* We only support a specific version of the proxy protocol */ if (aimutil_get16(&conn->proxy_header[2]) != PEER_PROXY_PACKET_VERSION) { purple_debug_warning("oscar", "Expected peer proxy protocol " "version %u but received version %u. Closing " "connection.\n", PEER_PROXY_PACKET_VERSION, aimutil_get16(&conn->proxy_header[2])); peer_connection_trynext(conn); return; } /* Initialize a new temporary ProxyFrame for incoming data */ frame = g_new0(ProxyFrame, 1); frame->payload.len = aimutil_get16(&conn->proxy_header[0]) - 10; frame->version = aimutil_get16(&conn->proxy_header[2]); frame->type = aimutil_get16(&conn->proxy_header[4]); frame->unknown = aimutil_get16(&conn->proxy_header[6]); frame->flags = aimutil_get16(&conn->proxy_header[10]); if (frame->payload.len > 0) frame->payload.data = g_new(guint8, frame->payload.len); conn->frame = frame; } /* If this frame has a payload then attempt to read it */ if (frame->payload.len - frame->payload.offset > 0) { /* Read data into the temporary buffer until it is complete */ read = recv(conn->fd, &frame->payload.data[frame->payload.offset], frame->payload.len - frame->payload.offset, 0); /* Check if the proxy server closed the connection */ if (read == 0) { purple_debug_info("oscar", "Peer proxy server closed connection\n"); g_free(frame->payload.data); g_free(frame); conn->frame = NULL; peer_connection_trynext(conn); return; } /* If there was an error then close the connection */ if (read < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) /* No worries */ return; purple_debug_info("oscar", "Lost connection with peer proxy server\n"); g_free(frame->payload.data); g_free(frame); conn->frame = NULL; peer_connection_trynext(conn); return; } frame->payload.offset += read; } conn->lastactivity = time(NULL); if (frame->payload.offset < frame->payload.len) /* Waiting for more data to arrive */ return; /* We have a complete proxy frame! Handle it and continue reading */ conn->frame = NULL; byte_stream_rewind(&frame->payload); peer_proxy_recv_frame(conn, frame); g_free(frame->payload.data); g_free(frame); conn->proxy_header_received = 0; }
/** * 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. * * This is called by flap_connection_recv_cb and * flap_connection_recv_cb_ssl for unencrypted/encrypted connections. */ static void flap_connection_recv(FlapConnection *conn) { gpointer buf; gsize buflen; gssize read; /* 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) { buf = conn->header + conn->header_received; buflen = 6 - conn->header_received; /* Read the first 6 bytes (the FLAP header) */ if (conn->gsc) read = purple_ssl_read(conn->gsc, buf, buflen); else read = recv(conn->fd, buf, buflen, 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, g_strerror(errno)); break; } conn->od->gc->last_received = time(NULL); /* 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; } /* 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; } buflen = conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset; if (buflen) { buf = &conn->buffer_incoming.data.data[conn->buffer_incoming.data.offset]; /* Read data into the temporary FlapFrame until it is complete */ if (conn->gsc) read = purple_ssl_read(conn->gsc, buf, buflen); else read = recv(conn->fd, buf, buflen, 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, g_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; } }