Changeset 5ebff60 for protocols/oscar/rxqueue.c
- Timestamp:
- 2015-02-20T22:50:54Z (10 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/oscar/rxqueue.c
raf359b4 r5ebff60 7 7 */ 8 8 9 #include <aim.h> 9 #include <aim.h> 10 10 11 11 #include <sys/socket.h> … … 16 16 int aim_recv(int fd, void *buf, size_t count) 17 17 { 18 int left, cur; 18 int left, cur; 19 19 20 20 for (cur = 0, left = count; left; ) { 21 21 int ret; 22 23 ret = recv(fd, ((unsigned char *) buf)+cur, left, 0);22 23 ret = recv(fd, ((unsigned char *) buf) + cur, left, 0); 24 24 25 25 /* Of course EOF is an error, only morons disagree with that. */ 26 if (ret <= 0) 26 if (ret <= 0) { 27 27 return -1; 28 } 28 29 29 30 cur += ret; … … 42 43 int red = 0; 43 44 44 if (!bs || (fd < 0) || (count < 0)) 45 if (!bs || (fd < 0) || (count < 0)) { 45 46 return -1; 46 47 if (count > (bs->len - bs->offset)) 47 } 48 49 if (count > (bs->len - bs->offset)) { 48 50 count = bs->len - bs->offset; /* truncate to remaining space */ 49 51 52 } 50 53 if (count) { 51 54 52 55 red = aim_recv(fd, bs->data + bs->offset, count); 53 56 54 if (red <= 0) 57 if (red <= 0) { 55 58 return -1; 59 } 56 60 } 57 61 … … 63 67 int aim_bstream_init(aim_bstream_t *bs, guint8 *data, int len) 64 68 { 65 66 if (!bs) 69 70 if (!bs) { 67 71 return -1; 72 } 68 73 69 74 bs->data = data; … … 87 92 { 88 93 89 if (off > bs->len) 94 if (off > bs->len) { 90 95 return -1; 96 } 91 97 92 98 bs->offset = off; … … 106 112 { 107 113 108 if (aim_bstream_empty(bs) < n) 109 return 0; /* XXX throw an exception */ 110 114 if (aim_bstream_empty(bs) < n) { 115 return 0; /* XXX throw an exception */ 116 117 } 111 118 bs->offset += n; 112 119 … … 116 123 guint8 aimbs_get8(aim_bstream_t *bs) 117 124 { 118 119 if (aim_bstream_empty(bs) < 1) 120 return 0; /* XXX throw an exception */ 121 125 126 if (aim_bstream_empty(bs) < 1) { 127 return 0; /* XXX throw an exception */ 128 129 } 122 130 bs->offset++; 123 131 124 132 return aimutil_get8(bs->data + bs->offset - 1); 125 133 } … … 127 135 guint16 aimbs_get16(aim_bstream_t *bs) 128 136 { 129 130 if (aim_bstream_empty(bs) < 2) 131 return 0; /* XXX throw an exception */ 132 137 138 if (aim_bstream_empty(bs) < 2) { 139 return 0; /* XXX throw an exception */ 140 141 } 133 142 bs->offset += 2; 134 143 135 144 return aimutil_get16(bs->data + bs->offset - 2); 136 145 } … … 138 147 guint32 aimbs_get32(aim_bstream_t *bs) 139 148 { 140 141 if (aim_bstream_empty(bs) < 4) 142 return 0; /* XXX throw an exception */ 143 149 150 if (aim_bstream_empty(bs) < 4) { 151 return 0; /* XXX throw an exception */ 152 153 } 144 154 bs->offset += 4; 145 155 146 156 return aimutil_get32(bs->data + bs->offset - 4); 147 157 } … … 149 159 guint8 aimbs_getle8(aim_bstream_t *bs) 150 160 { 151 152 if (aim_bstream_empty(bs) < 1) 153 return 0; /* XXX throw an exception */ 154 161 162 if (aim_bstream_empty(bs) < 1) { 163 return 0; /* XXX throw an exception */ 164 165 } 155 166 bs->offset++; 156 167 157 168 return aimutil_getle8(bs->data + bs->offset - 1); 158 169 } … … 160 171 guint16 aimbs_getle16(aim_bstream_t *bs) 161 172 { 162 163 if (aim_bstream_empty(bs) < 2) 164 return 0; /* XXX throw an exception */ 165 173 174 if (aim_bstream_empty(bs) < 2) { 175 return 0; /* XXX throw an exception */ 176 177 } 166 178 bs->offset += 2; 167 179 168 180 return aimutil_getle16(bs->data + bs->offset - 2); 169 181 } … … 171 183 guint32 aimbs_getle32(aim_bstream_t *bs) 172 184 { 173 174 if (aim_bstream_empty(bs) < 4) 175 return 0; /* XXX throw an exception */ 176 185 186 if (aim_bstream_empty(bs) < 4) { 187 return 0; /* XXX throw an exception */ 188 189 } 177 190 bs->offset += 4; 178 191 179 192 return aimutil_getle32(bs->data + bs->offset - 4); 180 193 } … … 183 196 { 184 197 185 if (aim_bstream_empty(bs) < 1) 186 return 0; /* XXX throw an exception */ 187 198 if (aim_bstream_empty(bs) < 1) { 199 return 0; /* XXX throw an exception */ 200 201 } 188 202 bs->offset += aimutil_put8(bs->data + bs->offset, v); 189 203 … … 194 208 { 195 209 196 if (aim_bstream_empty(bs) < 2) 197 return 0; /* XXX throw an exception */ 198 210 if (aim_bstream_empty(bs) < 2) { 211 return 0; /* XXX throw an exception */ 212 213 } 199 214 bs->offset += aimutil_put16(bs->data + bs->offset, v); 200 215 … … 205 220 { 206 221 207 if (aim_bstream_empty(bs) < 4) 208 return 0; /* XXX throw an exception */ 209 222 if (aim_bstream_empty(bs) < 4) { 223 return 0; /* XXX throw an exception */ 224 225 } 210 226 bs->offset += aimutil_put32(bs->data + bs->offset, v); 211 227 … … 216 232 { 217 233 218 if (aim_bstream_empty(bs) < 1) 219 return 0; /* XXX throw an exception */ 220 234 if (aim_bstream_empty(bs) < 1) { 235 return 0; /* XXX throw an exception */ 236 237 } 221 238 bs->offset += aimutil_putle8(bs->data + bs->offset, v); 222 239 … … 227 244 { 228 245 229 if (aim_bstream_empty(bs) < 2) 230 return 0; /* XXX throw an exception */ 231 246 if (aim_bstream_empty(bs) < 2) { 247 return 0; /* XXX throw an exception */ 248 249 } 232 250 bs->offset += aimutil_putle16(bs->data + bs->offset, v); 233 251 … … 238 256 { 239 257 240 if (aim_bstream_empty(bs) < 4) 241 return 0; /* XXX throw an exception */ 242 258 if (aim_bstream_empty(bs) < 4) { 259 return 0; /* XXX throw an exception */ 260 261 } 243 262 bs->offset += aimutil_putle32(bs->data + bs->offset, v); 244 263 … … 249 268 { 250 269 251 if (aim_bstream_empty(bs) < len) 270 if (aim_bstream_empty(bs) < len) { 252 271 return 0; 272 } 253 273 254 274 memcpy(buf, bs->data + bs->offset, len); … … 262 282 guint8 *ob; 263 283 264 if (!(ob = g_malloc(len))) 284 if (!(ob = g_malloc(len))) { 265 285 return NULL; 286 } 266 287 267 288 if (aimbs_getrawbuf(bs, ob, len) < len) { … … 277 298 guint8 *ob; 278 299 279 if (!(ob = g_malloc(len +1)))300 if (!(ob = g_malloc(len + 1))) { 280 301 return NULL; 302 } 281 303 282 304 if (aimbs_getrawbuf(bs, ob, len) < len) { … … 284 306 return NULL; 285 307 } 286 308 287 309 ob[len] = '\0'; 288 310 289 return (char *) ob;311 return (char *) ob; 290 312 } 291 313 … … 293 315 { 294 316 295 if (aim_bstream_empty(bs) < len) 296 return 0; /* XXX throw an exception */ 297 317 if (aim_bstream_empty(bs) < len) { 318 return 0; /* XXX throw an exception */ 319 320 } 298 321 memcpy(bs->data + bs->offset, v, len); 299 322 bs->offset += len; … … 305 328 { 306 329 307 if (aim_bstream_empty(srcbs) < len) 330 if (aim_bstream_empty(srcbs) < len) { 308 331 return 0; /* XXX throw exception (underrun) */ 309 332 310 if (aim_bstream_empty(bs) < len) 333 } 334 if (aim_bstream_empty(bs) < len) { 311 335 return 0; /* XXX throw exception (overflow) */ 312 336 337 } 313 338 memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len); 314 339 bs->offset += len; … … 319 344 320 345 /** 321 * aim_frame_destroy - free aim_frame_t 322 * @frame: the frame to free 323 * 324 * returns -1 on error; 0 on success. 346 * aim_frame_destroy - free aim_frame_t 347 * @frame: the frame to free 348 * 349 * returns -1 on error; 0 on success. 325 350 * 326 351 */ … … 331 356 332 357 g_free(frame); 333 } 358 } 334 359 335 360 … … 344 369 aim_frame_t *newrx; 345 370 guint16 payloadlen; 346 347 if (!sess || !conn) 371 372 if (!sess || !conn) { 348 373 return 0; 349 350 if (conn->fd == -1) 374 } 375 376 if (conn->fd == -1) { 351 377 return -1; /* its a aim_conn_close()'d connection */ 352 378 379 } 353 380 /* KIDS, THIS IS WHAT HAPPENS IF YOU USE CODE WRITTEN FOR GUIS IN A DAEMON! 354 381 355 382 And wouldn't it make sense to return something that prevents this function 356 383 from being called again IMMEDIATELY (and making the program suck up all 357 384 CPU time)?... 358 385 359 386 if (conn->fd < 3) 360 387 return 0; 361 388 */ 362 389 363 if (conn->status & AIM_CONN_STATUS_INPROGRESS) 390 if (conn->status & AIM_CONN_STATUS_INPROGRESS) { 364 391 return aim_conn_completeconnect(sess, conn); 392 } 365 393 366 394 aim_bstream_init(&flaphdr, flaphdr_raw, sizeof(flaphdr_raw)); … … 368 396 /* 369 397 * Read FLAP header. Six bytes: 370 * 398 * 371 399 * 0 char -- Always 0x2a 372 400 * 1 char -- Channel ID. Usually 2 -- 1 and 4 are used during login. 373 * 2 short -- Sequence number 401 * 2 short -- Sequence number 374 402 * 4 short -- Number of data bytes that follow. 375 403 */ … … 391 419 aim_conn_close(conn); 392 420 return -1; 393 } 421 } 394 422 395 423 /* allocate a new struct */ 396 if (!(newrx = (aim_frame_t *) g_new0(aim_frame_t,1)))424 if (!(newrx = (aim_frame_t *) g_new0(aim_frame_t, 1))) { 397 425 return -1; 426 } 398 427 399 428 /* we're doing FLAP if we're here */ 400 429 newrx->hdrtype = AIM_FRAMETYPE_FLAP; 401 430 402 431 newrx->hdr.flap.type = aimbs_get8(&flaphdr); 403 432 newrx->hdr.flap.seqnum = aimbs_get16(&flaphdr); … … 422 451 return -1; 423 452 } 424 } else 453 } else { 425 454 aim_bstream_init(&newrx->data, NULL, 0); 455 } 426 456 427 457 … … 432 462 newrx->next = NULL; /* this will always be at the bottom */ 433 463 434 if (!sess->queue_incoming) 464 if (!sess->queue_incoming) { 435 465 sess->queue_incoming = newrx; 436 else {466 } else { 437 467 aim_frame_t *cur; 438 468 439 for (cur = sess->queue_incoming; cur->next; cur = cur->next) 469 for (cur = sess->queue_incoming; cur->next; cur = cur->next) { 440 470 ; 471 } 441 472 cur->next = newrx; 442 473 } … … 444 475 newrx->conn->lastactivity = time(NULL); 445 476 446 return 0; 477 return 0; 447 478 } 448 479 … … 450 481 * Purge recieve queue of all handled commands (->handled==1). Also 451 482 * allows for selective freeing using ->nofree so that the client can 452 * keep the data for various purposes. 453 * 454 * If ->nofree is nonzero, the frame will be delinked from the global list, 483 * keep the data for various purposes. 484 * 485 * If ->nofree is nonzero, the frame will be delinked from the global list, 455 486 * but will not be free'ed. The client _must_ keep a pointer to the 456 487 * data -- libfaim will not! If the client marks ->nofree but … … 466 497 467 498 *prev = cur->next; 468 469 if (!cur->nofree) 499 500 if (!cur->nofree) { 470 501 aim_frame_destroy(cur); 471 472 } else 502 } 503 504 } else { 473 505 prev = &cur->next; 506 } 474 507 } 475 508 … … 489 522 490 523 for (currx = sess->queue_incoming; currx; currx = currx->next) { 491 if ((!currx->handled) && (currx->conn == conn)) 524 if ((!currx->handled) && (currx->conn == conn)) { 492 525 currx->handled = 1; 493 } 526 } 527 } 494 528 return; 495 529 }
Note: See TracChangeset
for help on using the changeset viewer.