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)
17 unsigned char generic[6];
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 == AIM_CONN_TYPE_RENDEZVOUS)
31 return aim_get_command_rendezvous(sess, conn);
32 if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT)
36 * Read FLAP header. Six bytes:
38 * 0 char -- Always 0x2a
39 * 1 char -- Channel ID. Usually 2 -- 1 and 4 are used during login.
40 * 2 short -- Sequence number
41 * 4 short -- Number of data bytes that follow.
43 faim_mutex_lock(&conn->active);
44 if (read(conn->fd, generic, 6) < 6){
45 aim_conn_kill(sess, &conn);
46 faim_mutex_unlock(&conn->active);
51 * This shouldn't happen unless the socket breaks, the server breaks,
52 * or we break. We must handle it just in case.
54 if (generic[0] != 0x2a) {
55 faimdprintf(1, "Bad incoming data!");
56 faim_mutex_unlock(&conn->active);
60 /* allocate a new struct */
61 if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct)))) {
62 faim_mutex_unlock(&conn->active);
65 memset(newrx, 0x00, sizeof(struct command_rx_struct));
67 newrx->lock = 1; /* lock the struct */
69 /* we're doing OSCAR if we're here */
70 newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
72 /* store channel -- byte 2 */
73 newrx->hdr.oscar.type = (char) generic[1];
75 /* store seqnum -- bytes 3 and 4 */
76 newrx->hdr.oscar.seqnum = aimutil_get16(generic+2);
78 /* store commandlen -- bytes 5 and 6 */
79 newrx->commandlen = aimutil_get16(generic+4);
81 newrx->nofree = 0; /* free by default */
83 /* malloc for data portion */
84 if (!(newrx->data = (u_char *) malloc(newrx->commandlen))) {
86 faim_mutex_unlock(&conn->active);
90 /* read the data portion of the packet */
91 if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
94 aim_conn_kill(sess, &conn);
95 faim_mutex_unlock(&conn->active);
98 faim_mutex_unlock(&conn->active);
102 newrx->next = NULL; /* this will always be at the bottom */
103 newrx->lock = 0; /* unlock */
105 /* enqueue this packet */
106 if (sess->queue_incoming == NULL) {
107 sess->queue_incoming = newrx;
109 struct command_rx_struct *cur;
112 * This append operation takes a while. It might be faster
113 * if we maintain a pointer to the last entry in the queue
114 * and just update that. Need to determine if the overhead
115 * to maintain that is lower than the overhead for this loop.
117 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
122 newrx->conn->lastactivity = time(NULL);
128 * Purge recieve queue of all handled commands (->handled==1). Also
129 * allows for selective freeing using ->nofree so that the client can
130 * keep the data for various purposes.
132 * If ->nofree is nonzero, the frame will be delinked from the global list,
133 * but will not be free'ed. The client _must_ keep a pointer to the
134 * data -- libfaim will not! If the client marks ->nofree but
135 * does not keep a pointer, it's lost forever.
138 void aim_purge_rxqueue(struct aim_session_t *sess)
140 struct command_rx_struct *cur = NULL;
141 struct command_rx_struct *tmp;
143 if (sess->queue_incoming == NULL)
146 if (sess->queue_incoming->next == NULL) {
147 if (sess->queue_incoming->handled) {
148 tmp = sess->queue_incoming;
149 sess->queue_incoming = NULL;
152 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
153 free(tmp->hdr.oft.hdr2);
162 for(cur = sess->queue_incoming; cur->next != NULL; ) {
163 if (cur->next->handled) {
165 cur->next = tmp->next;
167 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
168 free(tmp->hdr.oft.hdr2);
177 * Be careful here. Because of the way we just
178 * manipulated the pointer, cur may be NULL and
179 * the for() will segfault doing the check unless
180 * we find this case first.
190 * Since aim_get_command will aim_conn_kill dead connections, we need
191 * to clean up the rxqueue of unprocessed connections on that socket.
193 * XXX: this is something that was handled better in the old connection
194 * handling method, but eh.
196 void aim_rxqueue_cleanbyconn(struct aim_session_t *sess, struct aim_conn_t *conn)
198 struct command_rx_struct *currx;
200 for (currx = sess->queue_incoming; currx; currx = currx->next) {
201 if ((!currx->handled) && (currx->conn == conn))