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