]>
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){ |
68ac63c2 | 45 | aim_conn_kill(sess, &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!"); |
b69540e3 | 56 | faim_mutex_unlock(&conn->active); |
b8d0da45 | 57 | return -1; |
58 | } | |
9de3ca7e | 59 | |
9de3ca7e | 60 | /* allocate a new struct */ |
b69540e3 | 61 | if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct)))) { |
62 | faim_mutex_unlock(&conn->active); | |
f1a5efe0 | 63 | return -1; |
b69540e3 | 64 | } |
f1a5efe0 | 65 | memset(newrx, 0x00, sizeof(struct command_rx_struct)); |
b8d0da45 | 66 | |
f1a5efe0 | 67 | newrx->lock = 1; /* lock the struct */ |
9de3ca7e | 68 | |
b69540e3 | 69 | /* we're doing OSCAR if we're here */ |
70 | newrx->hdrtype = AIM_FRAMETYPE_OSCAR; | |
71 | ||
a25832e6 | 72 | /* store channel -- byte 2 */ |
b69540e3 | 73 | newrx->hdr.oscar.type = (char) generic[1]; |
9de3ca7e | 74 | |
75 | /* store seqnum -- bytes 3 and 4 */ | |
b69540e3 | 76 | newrx->hdr.oscar.seqnum = aimutil_get16(generic+2); |
9de3ca7e | 77 | |
78 | /* store commandlen -- bytes 5 and 6 */ | |
f1a5efe0 | 79 | newrx->commandlen = aimutil_get16(generic+4); |
9de3ca7e | 80 | |
f1a5efe0 | 81 | newrx->nofree = 0; /* free by default */ |
b8d0da45 | 82 | |
9de3ca7e | 83 | /* malloc for data portion */ |
b69540e3 | 84 | if (!(newrx->data = (u_char *) malloc(newrx->commandlen))) { |
f1a5efe0 | 85 | free(newrx); |
b69540e3 | 86 | faim_mutex_unlock(&conn->active); |
f1a5efe0 | 87 | return -1; |
88 | } | |
9de3ca7e | 89 | |
90 | /* read the data portion of the packet */ | |
f1a5efe0 | 91 | if (read(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){ |
92 | free(newrx->data); | |
93 | free(newrx); | |
68ac63c2 | 94 | aim_conn_kill(sess, &conn); |
e88ba395 | 95 | faim_mutex_unlock(&conn->active); |
b8d0da45 | 96 | return -1; |
97 | } | |
e88ba395 | 98 | faim_mutex_unlock(&conn->active); |
9de3ca7e | 99 | |
f1a5efe0 | 100 | newrx->conn = conn; |
9de3ca7e | 101 | |
f1a5efe0 | 102 | newrx->next = NULL; /* this will always be at the bottom */ |
103 | newrx->lock = 0; /* unlock */ | |
9de3ca7e | 104 | |
105 | /* enqueue this packet */ | |
b8d0da45 | 106 | if (sess->queue_incoming == NULL) { |
f1a5efe0 | 107 | sess->queue_incoming = newrx; |
b8d0da45 | 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 | ; | |
f1a5efe0 | 119 | cur->next = newrx; |
b8d0da45 | 120 | } |
9de3ca7e | 121 | |
f1a5efe0 | 122 | newrx->conn->lastactivity = time(NULL); |
9de3ca7e | 123 | |
124 | return 0; | |
125 | } | |
126 | ||
127 | /* | |
b8d0da45 | 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. | |
a25832e6 | 131 | * |
b8d0da45 | 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. | |
a25832e6 | 136 | * |
9de3ca7e | 137 | */ |
b8d0da45 | 138 | void aim_purge_rxqueue(struct aim_session_t *sess) |
9de3ca7e | 139 | { |
b8d0da45 | 140 | struct command_rx_struct *cur = NULL; |
141 | struct command_rx_struct *tmp; | |
9de3ca7e | 142 | |
b8d0da45 | 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) { | |
b69540e3 | 152 | if (tmp->hdrtype == AIM_FRAMETYPE_OFT) |
153 | free(tmp->hdr.oft.hdr2); | |
b8d0da45 | 154 | free(tmp->data); |
155 | free(tmp); | |
156 | } else | |
157 | tmp->next = NULL; | |
9de3ca7e | 158 | } |
b8d0da45 | 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) { | |
b69540e3 | 167 | if (tmp->hdrtype == AIM_FRAMETYPE_OFT) |
168 | free(tmp->hdr.oft.hdr2); | |
b8d0da45 | 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; | |
9de3ca7e | 187 | } |
68ac63c2 | 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 | */ | |
196 | void 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 | } |