]> andersk Git - libfaim.git/blame - aim_rxqueue.c
- Tue Jun 6 01:36:48 UTC 2000
[libfaim.git] / aim_rxqueue.c
CommitLineData
9de3ca7e 1/*
f1a5efe0 2 * aim_rxqueue.c
3 *
4 * This file contains the management routines for the receive
5 * (incoming packet) queue. The actual packet handlers are in
6 * aim_rxhandlers.c.
9de3ca7e 7 */
8
a25832e6 9#include <faim/aim.h>
9de3ca7e 10
11/*
f1a5efe0 12 * Grab a single command sequence off the socket, and enqueue
13 * it in the incoming event queue in a seperate struct.
a25832e6 14 */
f1a5efe0 15int aim_get_command(struct aim_session_t *sess, struct aim_conn_t *conn)
9de3ca7e 16{
f1a5efe0 17 u_char generic[6];
18 struct command_rx_struct *newrx = NULL;
9de3ca7e 19
f1a5efe0 20 if (!sess || !conn)
21 return 0;
9de3ca7e 22
f1a5efe0 23 if (conn->fd < 3) /* can happen when people abuse the interface */
9de3ca7e 24 return 0;
25
040457cc 26 /*
27 * Rendezvous (client-client) connections do not speak
28 * FLAP, so this function will break on them.
29 */
30 if (conn->type > 0x01000)
31 return 0;
32
f1a5efe0 33 /*
34 * Read FLAP header. Six bytes:
35 *
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.
40 */
e88ba395 41 faim_mutex_lock(&conn->active);
f1a5efe0 42 if (read(conn->fd, generic, 6) < 6){
43 aim_conn_close(conn);
e88ba395 44 faim_mutex_unlock(&conn->active);
f1a5efe0 45 return -1;
46 }
e88ba395 47 faim_mutex_unlock(&conn->active);
9de3ca7e 48
b8d0da45 49 /*
50 * This shouldn't happen unless the socket breaks, the server breaks,
51 * or we break. We must handle it just in case.
52 */
53 if (generic[0] != 0x2a) {
f1a5efe0 54 faimdprintf(1, "Bad incoming data!");
b8d0da45 55 return -1;
56 }
9de3ca7e 57
9de3ca7e 58 /* allocate a new struct */
f1a5efe0 59 newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct));
60 if (!newrx)
61 return -1;
62 memset(newrx, 0x00, sizeof(struct command_rx_struct));
b8d0da45 63
f1a5efe0 64 newrx->lock = 1; /* lock the struct */
9de3ca7e 65
a25832e6 66 /* store channel -- byte 2 */
f1a5efe0 67 newrx->type = (char) generic[1];
9de3ca7e 68
69 /* store seqnum -- bytes 3 and 4 */
f1a5efe0 70 newrx->seqnum = aimutil_get16(generic+2);
9de3ca7e 71
72 /* store commandlen -- bytes 5 and 6 */
f1a5efe0 73 newrx->commandlen = aimutil_get16(generic+4);
9de3ca7e 74
f1a5efe0 75 newrx->nofree = 0; /* free by default */
b8d0da45 76
9de3ca7e 77 /* malloc for data portion */
f1a5efe0 78 newrx->data = (u_char *) malloc(newrx->commandlen);
79 if (!newrx->data) {
80 free(newrx);
81 return -1;
82 }
9de3ca7e 83
84 /* read the data portion of the packet */
e88ba395 85 faim_mutex_lock(&conn->active);
f1a5efe0 86 if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
87 free(newrx->data);
88 free(newrx);
b8d0da45 89 aim_conn_close(conn);
e88ba395 90 faim_mutex_unlock(&conn->active);
b8d0da45 91 return -1;
92 }
e88ba395 93 faim_mutex_unlock(&conn->active);
9de3ca7e 94
f1a5efe0 95 newrx->conn = conn;
9de3ca7e 96
f1a5efe0 97 newrx->next = NULL; /* this will always be at the bottom */
98 newrx->lock = 0; /* unlock */
9de3ca7e 99
100 /* enqueue this packet */
b8d0da45 101 if (sess->queue_incoming == NULL) {
f1a5efe0 102 sess->queue_incoming = newrx;
b8d0da45 103 } else {
104 struct command_rx_struct *cur;
105
106 /*
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.
111 */
112 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
113 ;
f1a5efe0 114 cur->next = newrx;
b8d0da45 115 }
9de3ca7e 116
f1a5efe0 117 newrx->conn->lastactivity = time(NULL);
9de3ca7e 118
119 return 0;
120}
121
122/*
b8d0da45 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.
a25832e6 126 *
b8d0da45 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.
a25832e6 131 *
9de3ca7e 132 */
b8d0da45 133void aim_purge_rxqueue(struct aim_session_t *sess)
9de3ca7e 134{
b8d0da45 135 struct command_rx_struct *cur = NULL;
136 struct command_rx_struct *tmp;
9de3ca7e 137
b8d0da45 138 if (sess->queue_incoming == NULL)
139 return;
140
141 if (sess->queue_incoming->next == NULL) {
142 if (sess->queue_incoming->handled) {
143 tmp = sess->queue_incoming;
144 sess->queue_incoming = NULL;
145
146 if (!tmp->nofree) {
147 free(tmp->data);
148 free(tmp);
149 } else
150 tmp->next = NULL;
9de3ca7e 151 }
b8d0da45 152 return;
153 }
154
155 for(cur = sess->queue_incoming; cur->next != NULL; ) {
156 if (cur->next->handled) {
157 tmp = cur->next;
158 cur->next = tmp->next;
159 if (!tmp->nofree) {
160 free(tmp->data);
161 free(tmp);
162 } else
163 tmp->next = NULL;
164 }
165 cur = cur->next;
166
167 /*
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.
172 */
173 if (cur == NULL)
174 break;
175 }
176
177 return;
9de3ca7e 178}
This page took 0.088669 seconds and 5 git commands to generate.