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 * Rendezvous (client-client) connections do not speak
28 * FLAP, so this function will break on them.
30 if (conn->type > 0x01000)
34 * Read FLAP header. Six bytes:
36 * 0 char -- Always 0x2a
37 * 1 char -- Channel ID. Usually 2 -- 1 and 4 are used during login.
38 * 2 short -- Sequence number
39 * 4 short -- Number of data bytes that follow.
41 faim_mutex_lock(&conn->active);
42 if (read(conn->fd, generic, 6) < 6){
44 faim_mutex_unlock(&conn->active);
47 faim_mutex_unlock(&conn->active);
50 * This shouldn't happen unless the socket breaks, the server breaks,
51 * or we break. We must handle it just in case.
53 if (generic[0] != 0x2a) {
54 faimdprintf(1, "Bad incoming data!");
58 /* allocate a new struct */
59 newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct));
62 memset(newrx, 0x00, sizeof(struct command_rx_struct));
64 newrx->lock = 1; /* lock the struct */
66 /* store channel -- byte 2 */
67 newrx->type = (char) generic[1];
69 /* store seqnum -- bytes 3 and 4 */
70 newrx->seqnum = aimutil_get16(generic+2);
72 /* store commandlen -- bytes 5 and 6 */
73 newrx->commandlen = aimutil_get16(generic+4);
75 newrx->nofree = 0; /* free by default */
77 /* malloc for data portion */
78 newrx->data = (u_char *) malloc(newrx->commandlen);
84 /* read the data portion of the packet */
85 faim_mutex_lock(&conn->active);
86 if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
90 faim_mutex_unlock(&conn->active);
93 faim_mutex_unlock(&conn->active);
97 newrx->next = NULL; /* this will always be at the bottom */
98 newrx->lock = 0; /* unlock */
100 /* enqueue this packet */
101 if (sess->queue_incoming == NULL) {
102 sess->queue_incoming = newrx;
104 struct command_rx_struct *cur;
107 * This append operation takes a while. It might be faster
108 * if we maintain a pointer to the last entry in the queue
109 * and just update that. Need to determine if the overhead
110 * to maintain that is lower than the overhead for this loop.
112 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
117 newrx->conn->lastactivity = time(NULL);
123 * Purge recieve queue of all handled commands (->handled==1). Also
124 * allows for selective freeing using ->nofree so that the client can
125 * keep the data for various purposes.
127 * If ->nofree is nonzero, the frame will be delinked from the global list,
128 * but will not be free'ed. The client _must_ keep a pointer to the
129 * data -- libfaim will not! If the client marks ->nofree but
130 * does not keep a pointer, it's lost forever.
133 void aim_purge_rxqueue(struct aim_session_t *sess)
135 struct command_rx_struct *cur = NULL;
136 struct command_rx_struct *tmp;
138 if (sess->queue_incoming == NULL)
141 if (sess->queue_incoming->next == NULL) {
142 if (sess->queue_incoming->handled) {
143 tmp = sess->queue_incoming;
144 sess->queue_incoming = NULL;
155 for(cur = sess->queue_incoming; cur->next != NULL; ) {
156 if (cur->next->handled) {
158 cur->next = tmp->next;
168 * Be careful here. Because of the way we just
169 * manipulated the pointer, cur may be NULL and
170 * the for() will segfault doing the check unless
171 * we find this case first.