]>
Commit | Line | Data |
---|---|---|
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 | 15 | int aim_get_command(struct aim_session_t *sess, struct aim_conn_t *conn) |
9de3ca7e | 16 | { |
b69540e3 | 17 | unsigned char generic[6]; |
f1a5efe0 | 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 | */ | |
b69540e3 | 30 | if (conn->type == AIM_CONN_TYPE_RENDEZVOUS) |
31 | return aim_get_command_rendezvous(sess, conn); | |
7392c79f | 32 | if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) |
33 | return 0; | |
040457cc | 34 | |
f1a5efe0 | 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 | */ | |
e88ba395 | 43 | faim_mutex_lock(&conn->active); |
f1a5efe0 | 44 | if (read(conn->fd, generic, 6) < 6){ |
1a8c261b | 45 | aim_conn_close(conn); |
e88ba395 | 46 | faim_mutex_unlock(&conn->active); |
f1a5efe0 | 47 | return -1; |
48 | } | |
9de3ca7e | 49 | |
b8d0da45 | 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) { | |
f1a5efe0 | 55 | faimdprintf(1, "Bad incoming data!"); |
1a8c261b | 56 | aim_conn_close(conn); |
b69540e3 | 57 | faim_mutex_unlock(&conn->active); |
b8d0da45 | 58 | return -1; |
59 | } | |
9de3ca7e | 60 | |
9de3ca7e | 61 | /* allocate a new struct */ |
b69540e3 | 62 | if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct)))) { |
63 | faim_mutex_unlock(&conn->active); | |
f1a5efe0 | 64 | return -1; |
b69540e3 | 65 | } |
f1a5efe0 | 66 | memset(newrx, 0x00, sizeof(struct command_rx_struct)); |
b8d0da45 | 67 | |
f1a5efe0 | 68 | newrx->lock = 1; /* lock the struct */ |
9de3ca7e | 69 | |
b69540e3 | 70 | /* we're doing OSCAR if we're here */ |
71 | newrx->hdrtype = AIM_FRAMETYPE_OSCAR; | |
72 | ||
a25832e6 | 73 | /* store channel -- byte 2 */ |
b69540e3 | 74 | newrx->hdr.oscar.type = (char) generic[1]; |
9de3ca7e | 75 | |
76 | /* store seqnum -- bytes 3 and 4 */ | |
b69540e3 | 77 | newrx->hdr.oscar.seqnum = aimutil_get16(generic+2); |
9de3ca7e | 78 | |
79 | /* store commandlen -- bytes 5 and 6 */ | |
f1a5efe0 | 80 | newrx->commandlen = aimutil_get16(generic+4); |
9de3ca7e | 81 | |
f1a5efe0 | 82 | newrx->nofree = 0; /* free by default */ |
b8d0da45 | 83 | |
9de3ca7e | 84 | /* malloc for data portion */ |
b69540e3 | 85 | if (!(newrx->data = (u_char *) malloc(newrx->commandlen))) { |
f1a5efe0 | 86 | free(newrx); |
b69540e3 | 87 | faim_mutex_unlock(&conn->active); |
f1a5efe0 | 88 | return -1; |
89 | } | |
9de3ca7e | 90 | |
91 | /* read the data portion of the packet */ | |
f1a5efe0 | 92 | if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){ |
93 | free(newrx->data); | |
94 | free(newrx); | |
1a8c261b | 95 | aim_conn_close(conn); |
e88ba395 | 96 | faim_mutex_unlock(&conn->active); |
b8d0da45 | 97 | return -1; |
98 | } | |
e88ba395 | 99 | faim_mutex_unlock(&conn->active); |
9de3ca7e | 100 | |
f1a5efe0 | 101 | newrx->conn = conn; |
9de3ca7e | 102 | |
f1a5efe0 | 103 | newrx->next = NULL; /* this will always be at the bottom */ |
104 | newrx->lock = 0; /* unlock */ | |
9de3ca7e | 105 | |
106 | /* enqueue this packet */ | |
b8d0da45 | 107 | if (sess->queue_incoming == NULL) { |
f1a5efe0 | 108 | sess->queue_incoming = newrx; |
b8d0da45 | 109 | } else { |
110 | struct command_rx_struct *cur; | |
111 | ||
112 | /* | |
113 | * This append operation takes a while. It might be faster | |
114 | * if we maintain a pointer to the last entry in the queue | |
115 | * and just update that. Need to determine if the overhead | |
116 | * to maintain that is lower than the overhead for this loop. | |
117 | */ | |
118 | for (cur = sess->queue_incoming; cur->next; cur = cur->next) | |
119 | ; | |
f1a5efe0 | 120 | cur->next = newrx; |
b8d0da45 | 121 | } |
9de3ca7e | 122 | |
f1a5efe0 | 123 | newrx->conn->lastactivity = time(NULL); |
9de3ca7e | 124 | |
125 | return 0; | |
126 | } | |
127 | ||
128 | /* | |
b8d0da45 | 129 | * Purge recieve queue of all handled commands (->handled==1). Also |
130 | * allows for selective freeing using ->nofree so that the client can | |
131 | * keep the data for various purposes. | |
a25832e6 | 132 | * |
b8d0da45 | 133 | * If ->nofree is nonzero, the frame will be delinked from the global list, |
134 | * but will not be free'ed. The client _must_ keep a pointer to the | |
135 | * data -- libfaim will not! If the client marks ->nofree but | |
136 | * does not keep a pointer, it's lost forever. | |
a25832e6 | 137 | * |
9de3ca7e | 138 | */ |
b8d0da45 | 139 | void aim_purge_rxqueue(struct aim_session_t *sess) |
9de3ca7e | 140 | { |
b8d0da45 | 141 | struct command_rx_struct *cur = NULL; |
142 | struct command_rx_struct *tmp; | |
9de3ca7e | 143 | |
b8d0da45 | 144 | if (sess->queue_incoming == NULL) |
145 | return; | |
146 | ||
147 | if (sess->queue_incoming->next == NULL) { | |
148 | if (sess->queue_incoming->handled) { | |
149 | tmp = sess->queue_incoming; | |
150 | sess->queue_incoming = NULL; | |
151 | ||
152 | if (!tmp->nofree) { | |
b69540e3 | 153 | if (tmp->hdrtype == AIM_FRAMETYPE_OFT) |
154 | free(tmp->hdr.oft.hdr2); | |
b8d0da45 | 155 | free(tmp->data); |
156 | free(tmp); | |
157 | } else | |
158 | tmp->next = NULL; | |
9de3ca7e | 159 | } |
b8d0da45 | 160 | return; |
161 | } | |
162 | ||
163 | for(cur = sess->queue_incoming; cur->next != NULL; ) { | |
164 | if (cur->next->handled) { | |
165 | tmp = cur->next; | |
166 | cur->next = tmp->next; | |
167 | if (!tmp->nofree) { | |
b69540e3 | 168 | if (tmp->hdrtype == AIM_FRAMETYPE_OFT) |
169 | free(tmp->hdr.oft.hdr2); | |
b8d0da45 | 170 | free(tmp->data); |
171 | free(tmp); | |
172 | } else | |
173 | tmp->next = NULL; | |
174 | } | |
175 | cur = cur->next; | |
176 | ||
177 | /* | |
178 | * Be careful here. Because of the way we just | |
179 | * manipulated the pointer, cur may be NULL and | |
180 | * the for() will segfault doing the check unless | |
181 | * we find this case first. | |
182 | */ | |
183 | if (cur == NULL) | |
184 | break; | |
185 | } | |
186 | ||
187 | return; | |
9de3ca7e | 188 | } |
68ac63c2 | 189 | |
190 | /* | |
191 | * Since aim_get_command will aim_conn_kill dead connections, we need | |
192 | * to clean up the rxqueue of unprocessed connections on that socket. | |
193 | * | |
194 | * XXX: this is something that was handled better in the old connection | |
195 | * handling method, but eh. | |
196 | */ | |
197 | void aim_rxqueue_cleanbyconn(struct aim_session_t *sess, struct aim_conn_t *conn) | |
198 | { | |
199 | struct command_rx_struct *currx; | |
200 | ||
201 | for (currx = sess->queue_incoming; currx; currx = currx->next) { | |
202 | if ((!currx->handled) && (currx->conn == conn)) | |
203 | currx->handled = 1; | |
204 | } | |
205 | return; | |
206 | } |