4 * This file contains the management routines for the receive
5 * (incoming packet) queue. The actual packet handlers are in
12 * Grab a single command sequence off the socket, and enqueue
13 * it in the incoming event queue in a seperate struct.
15 int aim_get_command(struct aim_session_t *sess, struct aim_conn_t *conn)
18 struct command_rx_struct *newrx = NULL;
23 if (conn->fd < 3) /* can happen when people abuse the interface */
27 * Read FLAP header. Six bytes:
29 * 0 char -- Always 0x2a
30 * 1 char -- Channel ID. Usually 2 -- 1 and 4 are used during login.
31 * 2 short -- Sequence number
32 * 4 short -- Number of data bytes that follow.
34 if (read(conn->fd, generic, 6) < 6){
40 * This shouldn't happen unless the socket breaks, the server breaks,
41 * or we break. We must handle it just in case.
43 if (generic[0] != 0x2a) {
44 faimdprintf(1, "Bad incoming data!");
48 /* allocate a new struct */
49 newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct));
52 memset(newrx, 0x00, sizeof(struct command_rx_struct));
54 newrx->lock = 1; /* lock the struct */
56 /* store channel -- byte 2 */
57 newrx->type = (char) generic[1];
59 /* store seqnum -- bytes 3 and 4 */
60 newrx->seqnum = aimutil_get16(generic+2);
62 /* store commandlen -- bytes 5 and 6 */
63 newrx->commandlen = aimutil_get16(generic+4);
65 newrx->nofree = 0; /* free by default */
67 /* malloc for data portion */
68 newrx->data = (u_char *) malloc(newrx->commandlen);
74 /* read the data portion of the packet */
75 if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
84 newrx->next = NULL; /* this will always be at the bottom */
85 newrx->lock = 0; /* unlock */
87 /* enqueue this packet */
88 if (sess->queue_incoming == NULL) {
89 sess->queue_incoming = newrx;
91 struct command_rx_struct *cur;
94 * This append operation takes a while. It might be faster
95 * if we maintain a pointer to the last entry in the queue
96 * and just update that. Need to determine if the overhead
97 * to maintain that is lower than the overhead for this loop.
99 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
104 newrx->conn->lastactivity = time(NULL);
110 * Purge recieve queue of all handled commands (->handled==1). Also
111 * allows for selective freeing using ->nofree so that the client can
112 * keep the data for various purposes.
114 * If ->nofree is nonzero, the frame will be delinked from the global list,
115 * but will not be free'ed. The client _must_ keep a pointer to the
116 * data -- libfaim will not! If the client marks ->nofree but
117 * does not keep a pointer, it's lost forever.
120 void aim_purge_rxqueue(struct aim_session_t *sess)
122 struct command_rx_struct *cur = NULL;
123 struct command_rx_struct *tmp;
125 if (sess->queue_incoming == NULL)
128 if (sess->queue_incoming->next == NULL) {
129 if (sess->queue_incoming->handled) {
130 tmp = sess->queue_incoming;
131 sess->queue_incoming = NULL;
142 for(cur = sess->queue_incoming; cur->next != NULL; ) {
143 if (cur->next->handled) {
145 cur->next = tmp->next;
155 * Be careful here. Because of the way we just
156 * manipulated the pointer, cur may be NULL and
157 * the for() will segfault doing the check unless
158 * we find this case first.