]> andersk Git - libfaim.git/blame - aim_rxqueue.c
- Fri Jun 2 15:57:27 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
f1a5efe0 26 /*
27 * Read FLAP header. Six bytes:
28 *
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.
33 */
e88ba395 34 faim_mutex_lock(&conn->active);
f1a5efe0 35 if (read(conn->fd, generic, 6) < 6){
36 aim_conn_close(conn);
e88ba395 37 faim_mutex_unlock(&conn->active);
f1a5efe0 38 return -1;
39 }
e88ba395 40 faim_mutex_unlock(&conn->active);
9de3ca7e 41
b8d0da45 42 /*
43 * This shouldn't happen unless the socket breaks, the server breaks,
44 * or we break. We must handle it just in case.
45 */
46 if (generic[0] != 0x2a) {
f1a5efe0 47 faimdprintf(1, "Bad incoming data!");
b8d0da45 48 return -1;
49 }
9de3ca7e 50
9de3ca7e 51 /* allocate a new struct */
f1a5efe0 52 newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct));
53 if (!newrx)
54 return -1;
55 memset(newrx, 0x00, sizeof(struct command_rx_struct));
b8d0da45 56
f1a5efe0 57 newrx->lock = 1; /* lock the struct */
9de3ca7e 58
a25832e6 59 /* store channel -- byte 2 */
f1a5efe0 60 newrx->type = (char) generic[1];
9de3ca7e 61
62 /* store seqnum -- bytes 3 and 4 */
f1a5efe0 63 newrx->seqnum = aimutil_get16(generic+2);
9de3ca7e 64
65 /* store commandlen -- bytes 5 and 6 */
f1a5efe0 66 newrx->commandlen = aimutil_get16(generic+4);
9de3ca7e 67
f1a5efe0 68 newrx->nofree = 0; /* free by default */
b8d0da45 69
9de3ca7e 70 /* malloc for data portion */
f1a5efe0 71 newrx->data = (u_char *) malloc(newrx->commandlen);
72 if (!newrx->data) {
73 free(newrx);
74 return -1;
75 }
9de3ca7e 76
77 /* read the data portion of the packet */
e88ba395 78 faim_mutex_lock(&conn->active);
f1a5efe0 79 if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
80 free(newrx->data);
81 free(newrx);
b8d0da45 82 aim_conn_close(conn);
e88ba395 83 faim_mutex_unlock(&conn->active);
b8d0da45 84 return -1;
85 }
e88ba395 86 faim_mutex_unlock(&conn->active);
9de3ca7e 87
f1a5efe0 88 newrx->conn = conn;
9de3ca7e 89
f1a5efe0 90 newrx->next = NULL; /* this will always be at the bottom */
91 newrx->lock = 0; /* unlock */
9de3ca7e 92
93 /* enqueue this packet */
b8d0da45 94 if (sess->queue_incoming == NULL) {
f1a5efe0 95 sess->queue_incoming = newrx;
b8d0da45 96 } else {
97 struct command_rx_struct *cur;
98
99 /*
100 * This append operation takes a while. It might be faster
101 * if we maintain a pointer to the last entry in the queue
102 * and just update that. Need to determine if the overhead
103 * to maintain that is lower than the overhead for this loop.
104 */
105 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
106 ;
f1a5efe0 107 cur->next = newrx;
b8d0da45 108 }
9de3ca7e 109
f1a5efe0 110 newrx->conn->lastactivity = time(NULL);
9de3ca7e 111
112 return 0;
113}
114
115/*
b8d0da45 116 * Purge recieve queue of all handled commands (->handled==1). Also
117 * allows for selective freeing using ->nofree so that the client can
118 * keep the data for various purposes.
a25832e6 119 *
b8d0da45 120 * If ->nofree is nonzero, the frame will be delinked from the global list,
121 * but will not be free'ed. The client _must_ keep a pointer to the
122 * data -- libfaim will not! If the client marks ->nofree but
123 * does not keep a pointer, it's lost forever.
a25832e6 124 *
9de3ca7e 125 */
b8d0da45 126void aim_purge_rxqueue(struct aim_session_t *sess)
9de3ca7e 127{
b8d0da45 128 struct command_rx_struct *cur = NULL;
129 struct command_rx_struct *tmp;
9de3ca7e 130
b8d0da45 131 if (sess->queue_incoming == NULL)
132 return;
133
134 if (sess->queue_incoming->next == NULL) {
135 if (sess->queue_incoming->handled) {
136 tmp = sess->queue_incoming;
137 sess->queue_incoming = NULL;
138
139 if (!tmp->nofree) {
140 free(tmp->data);
141 free(tmp);
142 } else
143 tmp->next = NULL;
9de3ca7e 144 }
b8d0da45 145 return;
146 }
147
148 for(cur = sess->queue_incoming; cur->next != NULL; ) {
149 if (cur->next->handled) {
150 tmp = cur->next;
151 cur->next = tmp->next;
152 if (!tmp->nofree) {
153 free(tmp->data);
154 free(tmp);
155 } else
156 tmp->next = NULL;
157 }
158 cur = cur->next;
159
160 /*
161 * Be careful here. Because of the way we just
162 * manipulated the pointer, cur may be NULL and
163 * the for() will segfault doing the check unless
164 * we find this case first.
165 */
166 if (cur == NULL)
167 break;
168 }
169
170 return;
9de3ca7e 171}
This page took 0.076817 seconds and 5 git commands to generate.