4 * This file contains the management routines for the receive
5 * (incoming packet) queue. The actual packet handlers are in
10 #include <sys/socket.h>
13 * Since not all implementations support MSG_WAITALL, define
14 * an alternate guarenteed read function...
16 * We keep recv() for systems that can do it because it means
17 * a single system call for the entire packet, where read may
18 * take more for a badly fragmented packet.
21 static int aim_recv(int fd, void *buf, size_t count)
24 return recv(fd, buf, count, MSG_WAITALL);
26 int left, ret, cur = 0;
31 ret = read(fd, ((unsigned char *)buf)+cur, left);
46 * Grab a single command sequence off the socket, and enqueue
47 * it in the incoming event queue in a seperate struct.
49 faim_export int aim_get_command(struct aim_session_t *sess, struct aim_conn_t *conn)
51 unsigned char generic[6];
52 struct command_rx_struct *newrx = NULL;
57 if (conn->fd < 3) /* can happen when people abuse the interface */
61 * Rendezvous (client-client) connections do not speak
62 * FLAP, so this function will break on them.
64 if (conn->type == AIM_CONN_TYPE_RENDEZVOUS)
65 return aim_get_command_rendezvous(sess, conn);
66 if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT)
70 * Read FLAP header. Six bytes:
72 * 0 char -- Always 0x2a
73 * 1 char -- Channel ID. Usually 2 -- 1 and 4 are used during login.
74 * 2 short -- Sequence number
75 * 4 short -- Number of data bytes that follow.
77 faim_mutex_lock(&conn->active);
78 if (aim_recv(conn->fd, generic, 6) < 6){
80 faim_mutex_unlock(&conn->active);
85 * This shouldn't happen unless the socket breaks, the server breaks,
86 * or we break. We must handle it just in case.
88 if (generic[0] != 0x2a) {
89 faimdprintf(1, "Bad incoming data!");
91 faim_mutex_unlock(&conn->active);
95 /* allocate a new struct */
96 if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct)))) {
97 faim_mutex_unlock(&conn->active);
100 memset(newrx, 0x00, sizeof(struct command_rx_struct));
102 newrx->lock = 1; /* lock the struct */
104 /* we're doing OSCAR if we're here */
105 newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
107 /* store channel -- byte 2 */
108 newrx->hdr.oscar.type = (char) generic[1];
110 /* store seqnum -- bytes 3 and 4 */
111 newrx->hdr.oscar.seqnum = aimutil_get16(generic+2);
113 /* store commandlen -- bytes 5 and 6 */
114 newrx->commandlen = aimutil_get16(generic+4);
116 newrx->nofree = 0; /* free by default */
118 /* malloc for data portion */
119 if (!(newrx->data = (u_char *) malloc(newrx->commandlen))) {
121 faim_mutex_unlock(&conn->active);
125 /* read the data portion of the packet */
126 if (aim_recv(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
129 aim_conn_close(conn);
130 faim_mutex_unlock(&conn->active);
133 faim_mutex_unlock(&conn->active);
137 newrx->next = NULL; /* this will always be at the bottom */
138 newrx->lock = 0; /* unlock */
140 /* enqueue this packet */
141 if (sess->queue_incoming == NULL) {
142 sess->queue_incoming = newrx;
144 struct command_rx_struct *cur;
147 * This append operation takes a while. It might be faster
148 * if we maintain a pointer to the last entry in the queue
149 * and just update that. Need to determine if the overhead
150 * to maintain that is lower than the overhead for this loop.
152 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
157 newrx->conn->lastactivity = time(NULL);
163 * Purge recieve queue of all handled commands (->handled==1). Also
164 * allows for selective freeing using ->nofree so that the client can
165 * keep the data for various purposes.
167 * If ->nofree is nonzero, the frame will be delinked from the global list,
168 * but will not be free'ed. The client _must_ keep a pointer to the
169 * data -- libfaim will not! If the client marks ->nofree but
170 * does not keep a pointer, it's lost forever.
173 faim_export void aim_purge_rxqueue(struct aim_session_t *sess)
175 struct command_rx_struct *cur = NULL;
176 struct command_rx_struct *tmp;
178 if (sess->queue_incoming == NULL)
181 if (sess->queue_incoming->next == NULL) {
182 if (sess->queue_incoming->handled) {
183 tmp = sess->queue_incoming;
184 sess->queue_incoming = NULL;
187 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
188 free(tmp->hdr.oft.hdr2);
197 for(cur = sess->queue_incoming; cur->next != NULL; ) {
198 if (cur->next->handled) {
200 cur->next = tmp->next;
202 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
203 free(tmp->hdr.oft.hdr2);
212 * Be careful here. Because of the way we just
213 * manipulated the pointer, cur may be NULL and
214 * the for() will segfault doing the check unless
215 * we find this case first.
225 * Since aim_get_command will aim_conn_kill dead connections, we need
226 * to clean up the rxqueue of unprocessed connections on that socket.
228 * XXX: this is something that was handled better in the old connection
229 * handling method, but eh.
231 faim_internal void aim_rxqueue_cleanbyconn(struct aim_session_t *sess, struct aim_conn_t *conn)
233 struct command_rx_struct *currx;
235 for (currx = sess->queue_incoming; currx; currx = currx->next) {
236 if ((!currx->handled) && (currx->conn == conn))