]> andersk Git - libfaim.git/blame_incremental - aim_rxqueue.c
- Sun Jul 16 22:59:39 GMT 2000
[libfaim.git] / aim_rxqueue.c
... / ...
CommitLineData
1/*
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.
7 */
8
9#include <faim/aim.h>
10
11/*
12 * Grab a single command sequence off the socket, and enqueue
13 * it in the incoming event queue in a seperate struct.
14 */
15int aim_get_command(struct aim_session_t *sess, struct aim_conn_t *conn)
16{
17 unsigned char generic[6];
18 struct command_rx_struct *newrx = NULL;
19
20 if (!sess || !conn)
21 return 0;
22
23 if (conn->fd < 3) /* can happen when people abuse the interface */
24 return 0;
25
26 /*
27 * Rendezvous (client-client) connections do not speak
28 * FLAP, so this function will break on them.
29 */
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)
33 return 0;
34
35 /*
36 * Read FLAP header. Six bytes:
37 *
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.
42 */
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);
47 return -1;
48 }
49
50 /*
51 * This shouldn't happen unless the socket breaks, the server breaks,
52 * or we break. We must handle it just in case.
53 */
54 if (generic[0] != 0x2a) {
55 faimdprintf(1, "Bad incoming data!");
56 faim_mutex_unlock(&conn->active);
57 return -1;
58 }
59
60 /* allocate a new struct */
61 if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct)))) {
62 faim_mutex_unlock(&conn->active);
63 return -1;
64 }
65 memset(newrx, 0x00, sizeof(struct command_rx_struct));
66
67 newrx->lock = 1; /* lock the struct */
68
69 /* we're doing OSCAR if we're here */
70 newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
71
72 /* store channel -- byte 2 */
73 newrx->hdr.oscar.type = (char) generic[1];
74
75 /* store seqnum -- bytes 3 and 4 */
76 newrx->hdr.oscar.seqnum = aimutil_get16(generic+2);
77
78 /* store commandlen -- bytes 5 and 6 */
79 newrx->commandlen = aimutil_get16(generic+4);
80
81 newrx->nofree = 0; /* free by default */
82
83 /* malloc for data portion */
84 if (!(newrx->data = (u_char *) malloc(newrx->commandlen))) {
85 free(newrx);
86 faim_mutex_unlock(&conn->active);
87 return -1;
88 }
89
90 /* read the data portion of the packet */
91 if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
92 free(newrx->data);
93 free(newrx);
94 aim_conn_kill(sess, &conn);
95 faim_mutex_unlock(&conn->active);
96 return -1;
97 }
98 faim_mutex_unlock(&conn->active);
99
100 newrx->conn = conn;
101
102 newrx->next = NULL; /* this will always be at the bottom */
103 newrx->lock = 0; /* unlock */
104
105 /* enqueue this packet */
106 if (sess->queue_incoming == NULL) {
107 sess->queue_incoming = newrx;
108 } else {
109 struct command_rx_struct *cur;
110
111 /*
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.
116 */
117 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
118 ;
119 cur->next = newrx;
120 }
121
122 newrx->conn->lastactivity = time(NULL);
123
124 return 0;
125}
126
127/*
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.
131 *
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.
136 *
137 */
138void aim_purge_rxqueue(struct aim_session_t *sess)
139{
140 struct command_rx_struct *cur = NULL;
141 struct command_rx_struct *tmp;
142
143 if (sess->queue_incoming == NULL)
144 return;
145
146 if (sess->queue_incoming->next == NULL) {
147 if (sess->queue_incoming->handled) {
148 tmp = sess->queue_incoming;
149 sess->queue_incoming = NULL;
150
151 if (!tmp->nofree) {
152 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
153 free(tmp->hdr.oft.hdr2);
154 free(tmp->data);
155 free(tmp);
156 } else
157 tmp->next = NULL;
158 }
159 return;
160 }
161
162 for(cur = sess->queue_incoming; cur->next != NULL; ) {
163 if (cur->next->handled) {
164 tmp = cur->next;
165 cur->next = tmp->next;
166 if (!tmp->nofree) {
167 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
168 free(tmp->hdr.oft.hdr2);
169 free(tmp->data);
170 free(tmp);
171 } else
172 tmp->next = NULL;
173 }
174 cur = cur->next;
175
176 /*
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.
181 */
182 if (cur == NULL)
183 break;
184 }
185
186 return;
187}
188
189/*
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.
192 *
193 * XXX: this is something that was handled better in the old connection
194 * handling method, but eh.
195 */
196void aim_rxqueue_cleanbyconn(struct aim_session_t *sess, struct aim_conn_t *conn)
197{
198 struct command_rx_struct *currx;
199
200 for (currx = sess->queue_incoming; currx; currx = currx->next) {
201 if ((!currx->handled) && (currx->conn == conn))
202 currx->handled = 1;
203 }
204 return;
205}
This page took 0.034658 seconds and 5 git commands to generate.