00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "vfer_packet.h"
00028
00029
00030
00031 static inline int Packet_Set_Header (packet* p, unsigned char type, uint32_t opts, uint32_t seq);
00032 static inline void Packet_Get_Header (packet* p);
00033 static inline void Packet_Get_Type (char* buf, unsigned char* type);
00034 static inline void Packet_Get_Version (char* buf, unsigned char* version);
00035 static inline void Packet_Compute_Delay (char* pack_buf, struct timeval* tv, packet* p);
00036 static inline packet* Packet_PrepIov (int num_bufs, int first_buf_len);
00037
00038
00039 static inline int Packet_Get_Opt (uint32_t opts, int opt);
00040 static inline void Packet_Set_Opt (uint32_t* opts, int opt);
00041 static inline void Packet_Unset_Opt (uint32_t* opts, int opt);
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 char* Packet_Str_Type(packet* p) {
00054 switch (p->type) {
00055 case REQUEST:
00056 return "REQUEST";
00057 case RESPONSE:
00058 return "RESPONSE";
00059 case DATA:
00060 return "DATA";
00061 case CC_ACK:
00062 return "CC_ACK";
00063 case CLOSE:
00064 return "CLOSE";
00065 default:
00066 return "UNKNOWN";
00067 }
00068 }
00069
00070
00071
00072
00073
00074
00075 char* Packet_Str(packet* p) {
00076 char* str;
00077 str = (char*)malloc(1024);
00078
00079 sprintf(str, "%u %s [%d us] opt[%u] v[%u] ", p->seq, Packet_Str_Type(p), p->delay, p->opts, p->version);
00080 switch (p->type) {
00081 case REQUEST:
00082 sprintf(str + strlen(str), "max_frame_len[%u] rcv_buf[%u] snd_buf[%u] init_frame[%u]",
00083 p->u.req.max_frame_size, p->u.req.recv_buf_size, p->u.req.send_buf_size, p->u.req.init_frame_num);
00084 break;
00085 case RESPONSE:
00086 sprintf(str + strlen(str), "max_frame_len[%u] rcv_buf[%u] snd_buf[%u] init_frame[%u] port[%u] req_delay[%d]",
00087 p->u.res.max_frame_size, p->u.res.recv_buf_size, p->u.res.send_buf_size, p->u.res.init_frame_num, p->u.res.connection_port, p->u.res.request_delay);
00088 break;
00089 case DATA:
00090 sprintf(str + strlen(str), "frame[%u] data_offset[%u] frame_len[%u] data_len[%u]",
00091 p->u.data.frame_num, p->u.data.data_offset, p->u.data.frame_len, p->u.data.data_len);
00092 break;
00093 case CC_ACK:
00094 sprintf(str + strlen(str), "last_seq[%u] lost[%u] oldest_f[%u] newest_f[%u] dlay_dlta[%d] dlay[%d] bytes[%d]", p->u.ccack.last_seq, p->u.ccack.packets_lost, p->u.ccack.oldest_frame, p->u.ccack.newest_frame, p->u.ccack.delay_delta, p->u.ccack.delay, p->u.ccack.bytes_total);
00095 if (Packet_Get_Opt(p->opts, OPT_VEC)) {
00096 sprintf(str + strlen(str), " intervals_frame[%u] num_intervals[%d]", p->u.ccack.intervals_frame_num, p->u.ccack.interval_count);
00097 }
00098 break;
00099 }
00100 return str;
00101 }
00102
00103
00104
00105
00106
00107 void Packet_Print (packet* p) {
00108 char* str;
00109 str = Packet_Str(p);
00110 printf("%s", str);
00111 free(str);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 packet* Packet_Form_Request(uint32_t opts, uint32_t init_seq_num,
00135 uint32_t max_frame_size, uint32_t recv_buf_size,
00136 uint32_t send_buf_size, uint16_t init_frame_num) {
00137 char* buf;
00138 int offset;
00139 packet* p;
00140
00141 if ((p = Packet_PrepIov(1, REQUEST_LENGTH)) == NULL) return NULL;
00142
00143 buf = p->iov[0].iov_base;
00144 offset = Packet_Set_Header(p, REQUEST, opts, init_seq_num);
00145
00146 p->u.req.max_frame_size = max_frame_size;
00147 max_frame_size = htonl(max_frame_size);
00148 memcpy(buf + offset, &max_frame_size, 4);
00149
00150 p->u.req.recv_buf_size = recv_buf_size;
00151 recv_buf_size = htonl(recv_buf_size);
00152 memcpy(buf + offset + 4, &recv_buf_size, 4);
00153
00154 p->u.req.send_buf_size = send_buf_size;
00155 send_buf_size = htonl(send_buf_size);
00156 memcpy(buf + offset + 8, &send_buf_size, 4);
00157
00158 p->u.req.init_frame_num = init_frame_num;
00159 init_frame_num = htons(init_frame_num);
00160 memcpy(buf + offset + 12, &init_frame_num, 2);
00161
00162 return p;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 packet* Packet_Form_Response(uint32_t opts, uint32_t init_seq_num, uint16_t init_frame_num,
00184 uint16_t connection_port, uint32_t max_frame_size,
00185 uint32_t recv_buf_size, uint32_t send_buf_size, int32_t request_delay) {
00186 char* buf;
00187 int offset;
00188 packet* p;
00189
00190 if ((p = Packet_PrepIov(1, RESPONSE_LENGTH)) == NULL) return NULL;
00191
00192 buf = p->iov[0].iov_base;
00193 offset = Packet_Set_Header(p, RESPONSE, opts, init_seq_num);
00194
00195 p->u.res.init_frame_num = init_frame_num;
00196 init_frame_num = htons(init_frame_num);
00197 memcpy(buf + offset, &init_frame_num, 2);
00198
00199 p->u.res.connection_port = connection_port;
00200 connection_port = htons(connection_port);
00201 memcpy(buf + offset + 2, &connection_port, 2);
00202
00203 p->u.res.max_frame_size = max_frame_size;
00204 max_frame_size = htonl(max_frame_size);
00205 memcpy(buf + offset + 4, &max_frame_size, 4);
00206
00207 p->u.res.recv_buf_size = recv_buf_size;
00208 recv_buf_size = htonl(recv_buf_size);
00209 memcpy(buf + offset + 8, &recv_buf_size, 4);
00210
00211 p->u.res.send_buf_size = send_buf_size;
00212 send_buf_size = htonl(send_buf_size);
00213 memcpy(buf + offset + 12, &send_buf_size, 4);
00214
00215 p->u.res.request_delay = request_delay;
00216 request_delay = htonl(request_delay);
00217 memcpy(buf + offset + 16, &request_delay, 4);
00218
00219 return p;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 packet* Packet_Form_Data(uint32_t opts, uint32_t seq_num, uint16_t frame_num,
00241 uint32_t data_offset, uint32_t frame_len, void* data, int data_len) {
00242 char* buf;
00243 int offset;
00244 packet* p;
00245
00246 if (frame_len != -1) {
00247 if ((p = Packet_PrepIov(2, DATA_LENGTH)) == NULL) return NULL;
00248 Packet_Set_Opt(&opts, OPT_FRAME_LEN);
00249 } else {
00250 if ((p = Packet_PrepIov(2, DATA_LENGTH)) == NULL) return NULL;
00251 Packet_Unset_Opt(&opts, OPT_FRAME_LEN);
00252 }
00253 buf = p->iov[0].iov_base;
00254 offset = Packet_Set_Header(p, DATA, opts, seq_num);
00255
00256 p->u.data.frame_num = frame_num;
00257 frame_num = htons(frame_num);
00258 memcpy(buf + offset, &frame_num, 2);
00259 offset += 2;
00260
00261 p->u.data.data_offset = data_offset;
00262 data_offset = htonl(data_offset);
00263 memcpy(buf + offset, &data_offset, 4);
00264 offset += 4;
00265
00266 if (frame_len != -1) {
00267 p->u.data.frame_len = frame_len;
00268 frame_len = htonl(frame_len);
00269 memcpy(buf + offset, &frame_len, 4);
00270 offset += 4;
00271 } else {
00272 offset += 4;
00273 }
00274
00275 p->u.data.data = data;
00276 p->u.data.data_len = data_len;
00277
00278 p->iov[1].iov_base = data;
00279 p->iov[1].iov_len = data_len;
00280
00281 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Form_Data", "iov[0]: %d ; iov[1]: %d ; frame_len[%u]", (int)(p->iov[0].iov_len), (int)(p->iov[1].iov_len), ntohl(frame_len));
00282 return p;
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 packet* Packet_Form_CCAck(uint32_t opts, uint32_t seq_num, uint32_t last_seq_received,
00305 uint16_t packets_lost, uint16_t oldest_frame, uint16_t newest_frame,
00306 int32_t delay_delta, int32_t delay, uint32_t bytes_total,
00307 uint16_t intervals_frame_num, intervals_t* intervals) {
00308 char* buf;
00309 int offset;
00310 packet* p;
00311 interval_t* current;
00312 uint32_t tmp;
00313
00314 if (intervals != NULL) {
00315
00316 if (intervals->head != NULL) {
00317
00318
00319
00320 if ((p = Packet_PrepIov(1, CCACK_LENGTH + 8*intervals->count + 6)) == NULL) return NULL;
00321 } else {
00322
00323
00324 if ((p = Packet_PrepIov(1, CCACK_LENGTH + 14)) == NULL) return NULL;
00325 }
00326 Packet_Set_Opt(&opts, OPT_VEC);
00327 } else {
00328 if ((p = Packet_PrepIov(1, CCACK_LENGTH)) == NULL) return NULL;
00329 }
00330 buf = p->iov[0].iov_base;
00331
00332 offset = Packet_Set_Header(p, CC_ACK, opts, seq_num);
00333
00334 p->u.ccack.last_seq = last_seq_received;
00335 last_seq_received = htonl(last_seq_received);
00336 memcpy(buf + offset, &last_seq_received, 4);
00337
00338 p->u.ccack.packets_lost = packets_lost;
00339 packets_lost = htons(packets_lost);
00340 memcpy(buf + offset + 4, &packets_lost, 2);
00341
00342 p->u.ccack.oldest_frame = oldest_frame;
00343 oldest_frame = htons(oldest_frame);
00344 memcpy(buf + offset + 6, &oldest_frame, 2);
00345
00346 p->u.ccack.newest_frame = newest_frame;
00347 newest_frame = htons(newest_frame);
00348 memcpy(buf + offset + 8, &newest_frame, 2);
00349
00350 p->u.ccack.delay_delta = delay_delta;
00351 delay_delta = htonl(delay_delta);
00352 memcpy(buf + offset + 10, &delay_delta, 4);
00353
00354 p->u.ccack.delay = delay;
00355 delay = htonl(delay);
00356 memcpy(buf + offset + 14, &delay, 4);
00357
00358
00359
00360 p->u.ccack.bytes_total = bytes_total;
00361 bytes_total = htonl(bytes_total);
00362 memcpy(buf + offset + 18, &bytes_total, 4);
00363
00364 if (intervals != NULL) {
00365
00366 if (intervals->head != NULL) {
00367
00368 p->u.ccack.interval_count = intervals->count;
00369 p->u.ccack.intervals_frame_num = intervals_frame_num;
00370 intervals_frame_num = htons(intervals_frame_num);
00371 memcpy(buf + offset + 22, &intervals_frame_num, 2);
00372
00373 tmp = htonl(intervals->count);
00374 memcpy(buf + offset + 24, &tmp, 4);
00375 offset += 28;
00376
00377 current = intervals->head;
00378
00379 while (current != NULL) {
00380
00381 tmp = htonl(current->start);
00382 memcpy(buf + offset, &tmp, 4);
00383 tmp = htonl(current->end);
00384
00385 memcpy(buf + offset + 4, &tmp, 4);
00386 offset += 8;
00387
00388 current = current->next;
00389 }
00390
00391 } else {
00392
00393 p->u.ccack.interval_count = 0;
00394 p->u.ccack.intervals_frame_num = intervals_frame_num;
00395 intervals_frame_num = htons(intervals_frame_num);
00396 memcpy(buf + offset + 22, &intervals_frame_num, 2);
00397 tmp = htonl(1);
00398 memcpy(buf + offset + 24, &tmp, 4);
00399 tmp = 0;
00400 memcpy(buf + offset + 28, &tmp, 4);
00401 memcpy(buf + offset + 32, &tmp, 4);
00402 }
00403 }
00404 return p;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 packet* Packet_Form_Close(uint32_t opts, uint32_t seq_num) {
00418 packet* p;
00419 if ((p = Packet_PrepIov(1, CLOSE_LENGTH)) == NULL) return NULL;
00420 Packet_Set_Header(p, CLOSE, opts, seq_num);
00421 return p;
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437 int Packet_Translate_Request(packet* p) {
00438 char* buf = p->iov[0].iov_base;
00439 int offset = TOPH_LENGTH;
00440
00441 if (p->iov[0].iov_len != REQUEST_LENGTH) return -1;
00442 Packet_Get_Header(p);
00443
00444 memcpy(&(p->u.req.max_frame_size), buf + offset, 4);
00445 p->u.req.max_frame_size = ntohl(p->u.req.max_frame_size);
00446
00447 memcpy(&(p->u.req.recv_buf_size), buf + offset + 4, 4);
00448 p->u.req.recv_buf_size = ntohl(p->u.req.recv_buf_size);
00449
00450 memcpy(&(p->u.req.send_buf_size), buf + offset + 8, 4);
00451 p->u.req.send_buf_size = ntohl(p->u.req.send_buf_size);
00452
00453 memcpy(&(p->u.req.init_frame_num), buf + offset + 12, 2);
00454 p->u.req.init_frame_num = ntohs(p->u.req.init_frame_num);
00455 return 0;
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 int Packet_Translate_Response(packet* p) {
00467 char* buf = p->iov[0].iov_base;
00468 int offset = TOPH_LENGTH;
00469
00470 if (p->iov[0].iov_len != RESPONSE_LENGTH) return -1;
00471 Packet_Get_Header(p);
00472
00473 memcpy(&(p->u.res.init_frame_num), buf + offset, 2);
00474 p->u.res.init_frame_num = ntohs(p->u.res.init_frame_num);
00475
00476 memcpy(&(p->u.res.connection_port), buf + offset + 2, 2);
00477 p->u.res.connection_port = ntohs(p->u.res.connection_port);
00478
00479 memcpy(&(p->u.res.max_frame_size), buf + offset + 4, 4);
00480 p->u.res.max_frame_size = ntohl(p->u.res.max_frame_size);
00481
00482 memcpy(&(p->u.res.recv_buf_size), buf + offset + 8, 4);
00483 p->u.res.recv_buf_size = ntohl(p->u.res.recv_buf_size);
00484
00485 memcpy(&(p->u.res.send_buf_size), buf + offset + 12, 4);
00486 p->u.res.send_buf_size = ntohl(p->u.res.send_buf_size);
00487
00488 memcpy(&(p->u.res.request_delay), buf + offset + 16, 4);
00489 p->u.res.request_delay = ntohl(p->u.res.request_delay);
00490
00491 return 0;
00492 }
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 int Packet_Translate_Data(packet* p) {
00503 char* buf = p->iov[0].iov_base;
00504 int offset = TOPH_LENGTH;
00505
00506 if (p->iov[0].iov_len < DATA_LENGTH) return -1;
00507 Packet_Get_Header(p);
00508
00509 memcpy(&(p->u.data.frame_num), buf + offset, 2);
00510 p->u.data.frame_num = ntohs(p->u.data.frame_num);
00511 offset += 2;
00512
00513 memcpy(&(p->u.data.data_offset), buf + offset, 4);
00514 p->u.data.data_offset = ntohl(p->u.data.data_offset);
00515 offset += 4;
00516
00517
00518 if (Packet_Get_Opt(p->opts, OPT_FRAME_LEN) == 1) {
00519 memcpy(&(p->u.data.frame_len), buf + offset, 4);
00520 p->u.data.frame_len = ntohl(p->u.data.frame_len);
00521 offset += 4;
00522 } else {
00523 p->u.data.frame_len = 0;
00524 offset += 4;
00525 }
00526
00527
00528
00529 p->u.data.data_len = p->iov[0].iov_len - offset;
00530 p->u.data.data = buf + offset;
00531 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Translate_Data", "len[%d] ; offset[%d] ; data_len[%u]\n", (int)(p->iov[0].iov_len), offset, (int)(p->u.data.data_len));
00532 return 0;
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 int Packet_Translate_CCAck(packet* p) {
00544 char* buf = p->iov[0].iov_base;
00545 int offset = TOPH_LENGTH;
00546
00547 if (p->iov[0].iov_len < CCACK_LENGTH) return -1;
00548 Packet_Get_Header(p);
00549
00550 memcpy(&(p->u.ccack.last_seq), buf + offset, 4);
00551 p->u.ccack.last_seq = ntohl(p->u.ccack.last_seq);
00552
00553 memcpy(&(p->u.ccack.packets_lost), buf + offset + 4, 2);
00554 p->u.ccack.packets_lost = ntohs(p->u.ccack.packets_lost);
00555
00556 memcpy(&(p->u.ccack.oldest_frame), buf + offset + 6, 2);
00557 p->u.ccack.oldest_frame = ntohs(p->u.ccack.oldest_frame);
00558
00559 memcpy(&(p->u.ccack.newest_frame), buf + offset + 8, 2);
00560 p->u.ccack.newest_frame = ntohs(p->u.ccack.newest_frame);
00561
00562 memcpy(&(p->u.ccack.delay_delta), buf + offset + 10, 4);
00563 p->u.ccack.delay_delta = ntohl(p->u.ccack.delay_delta);
00564
00565 memcpy(&(p->u.ccack.delay), buf + offset + 14, 4);
00566 p->u.ccack.delay = ntohl(p->u.ccack.delay);
00567
00568
00569
00570 memcpy(&(p->u.ccack.bytes_total), buf + offset + 18, 4);
00571 p->u.ccack.bytes_total = ntohl(p->u.ccack.bytes_total);
00572
00573 if (Packet_Get_Opt(p->opts, OPT_VEC)) {
00574
00575 memcpy(&(p->u.ccack.intervals_frame_num), buf + offset + 22, 2);
00576 p->u.ccack.intervals_frame_num = ntohs(p->u.ccack.intervals_frame_num);
00577
00578 memcpy(&(p->u.ccack.interval_count), buf + offset + 24, 4);
00579 p->u.ccack.interval_count = ntohl(p->u.ccack.interval_count);
00580
00581
00582
00583
00584 p->u.ccack.raw_intervals = (uint32_t*)(buf + offset + 28);
00585 } else {
00586 p->u.ccack.intervals_frame_num = 0;
00587 p->u.ccack.interval_count = 0;
00588 p->u.ccack.raw_intervals = NULL;
00589 }
00590 return 0;
00591 }
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601 int Packet_Translate_Close(packet* p) {
00602 if (p->iov[0].iov_len != CLOSE_LENGTH) return -1;
00603 Packet_Get_Header(p);
00604 return 0;
00605 }
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 inline void Packet_Set_Seq(packet* p, uint32_t seq) {
00619 char* buf = p->iov[0].iov_base;
00620
00621 p->seq = seq;
00622 seq = htonl(seq);
00623 memcpy(buf + 4, &(seq), 4);
00624 }
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637 static inline int Packet_Set_Header(packet* p, unsigned char type, uint32_t opts, uint32_t seq) {
00638
00639 char* buf = p->iov[0].iov_base;
00640
00641 p->type = type;
00642 p->version = VFER_VERSION;
00643 p->opts = opts;
00644
00645 opts = opts | ((0xF0000000 & (p->type << 24)) | (0x0F000000 & (p->version << 24)));
00646
00647
00648 opts = htonl(opts);
00649 memcpy(buf, &(opts), 4);
00650
00651 Packet_Set_Seq(p, seq);
00652 return TOPH_LENGTH;
00653 }
00654
00655
00656
00657
00658
00659 static inline void Packet_Get_Header(packet* p) {
00660 uint32_t tmp;
00661 char* buf = p->iov[0].iov_base;
00662
00663
00664 Packet_Get_Type(buf, &(p->type));
00665 Packet_Get_Version(buf, &(p->version));
00666
00667
00668 memcpy(&(tmp), buf, 4);
00669 ((char*)(&tmp))[0] = 0;
00670 tmp = ntohl(tmp);
00671 p->opts = 0;
00672 memcpy(&(p->opts), &tmp, 4);
00673
00674
00675 memcpy(&(p->seq), buf + 4, 4);
00676 p->seq = ntohl(p->seq);
00677
00678 }
00679
00680
00681
00682
00683
00684
00685
00686 static inline void Packet_Get_Type(char* buf, unsigned char* type) {
00687
00688 uint32_t tmp;
00689 memcpy(&(tmp), buf, 4);
00690 tmp = ntohl(tmp);
00691 *type = (tmp & 0xF0000000) >> 24;
00692 }
00693
00694
00695
00696
00697
00698
00699
00700 static inline void Packet_Get_Version(char* buf, unsigned char* version) {
00701
00702 uint32_t tmp;
00703 memcpy(&(tmp), buf, 4);
00704 tmp = ntohl(tmp);
00705 *version = (tmp & 0x0F000000) >> 24;
00706 }
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717 static inline void Packet_Compute_Delay(char* pack_buf, struct timeval* tv, packet* p) {
00718 uint32_t tmp;
00719 int micro;
00720 memcpy(&(tmp), pack_buf + 8, 4);
00721 tmp = ntohl(tmp);
00722 p->tv_sec = tmp;
00723 memcpy(&(tmp), pack_buf + 12, 4);
00724 tmp = ntohl(tmp);
00725 p->tv_usec = tmp;
00726
00727
00728 micro = (int)(((int)p->tv_usec - (int)tv->tv_usec));
00729 p->delay = (int32_t)((((int)p->tv_sec - (int)tv->tv_sec) * 1000000) + micro);
00730 if (p->delay == 0) {
00731
00732 if (((int)p->tv_usec - (int)tv->tv_usec) < 0)
00733 p->delay = -1;
00734 else
00735 p->delay = 1;
00736 }
00737 }
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748 static inline packet* Packet_PrepIov(int num_bufs, int first_buf_len) {
00749 char* buf;
00750 packet* p;
00751 if (ALLOC(p, struct packet*, struct packet, 1)== NULL) return NULL;
00752 if (ALLOC(p->iov, struct iovec*, struct iovec, num_bufs)== NULL) {
00753 RELEASE(p, sizeof(packet));
00754 return NULL;
00755 }
00756 if (ALLOC(buf, char*, char, first_buf_len) == NULL) {
00757 RELEASE(p->iov, sizeof(struct iovec));
00758 RELEASE(p, sizeof(packet));
00759 return NULL;
00760 }
00761 p->iov[0].iov_base = buf;
00762 p->iov[0].iov_len = first_buf_len;
00763 p->iovlen = num_bufs;
00764 return p;
00765 }
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 int Packet_Read(int fd, packet** p, struct sockaddr* addr, socklen_t* addr_len) {
00790 int n;
00791 unsigned char packet_type;
00792 unsigned char packet_version;
00793 int ret;
00794 struct timeval tv;
00795 int err_val_len;
00796 int err_val;
00797
00798
00799 err_val_len = 4;
00800 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err_val, &err_val_len) == -1) perror("Packet_Read :: getsockopt");
00801 if (err_val != 0) {
00802 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Read", "socket has a pending error[%d]:[%s]", err_val, strerror(err_val));
00803 return err_val;
00804 }
00805
00806
00807 n = recvfrom(fd, pack_buf, 65535, MSG_DONTWAIT, addr, addr_len);
00808
00809
00810
00811
00812
00813 GET_TIME_OF_DAY(&(tv));
00814
00815 if (n == -1) {
00816 ret = errno;
00817 if (ret != EAGAIN) {
00818 perror("packet_read :: recvfrom");
00819 }
00820 return ret;
00821 } else if (n == 0) {
00822 return -5;
00823 }
00824
00825 Packet_Get_Type(pack_buf, &packet_type);
00826 Packet_Get_Version(pack_buf, &packet_version);
00827
00828
00829
00830 if (packet_version != VFER_VERSION) {
00831 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Read", "received packet with bad version: version[%d] ; my version[%d]", packet_version, VFER_VERSION);
00832 return -3;
00833 } else if (packet_type != REQUEST &&
00834 packet_type != RESPONSE &&
00835 packet_type != DATA &&
00836 packet_type != CC_ACK &&
00837 packet_type != DATA_ACK &&
00838 packet_type != CLOSE) {
00839 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Read", "received packet with bad type: type[%d]", (int)(packet_type));
00840 return -2;
00841 }
00842
00843
00844 if (ALLOC(*p, struct packet*, struct packet, 1) == NULL) {
00845 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Read", "could not allocate packet");
00846 return -6;
00847 }
00848 if (ALLOC((*p)->iov, struct iovec*, struct iovec, 1) == NULL) {
00849 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Read", "could not allocate iovec array of size 1");
00850 RELEASE((*p), sizeof(struct packet));
00851 return -6;
00852 }
00853 if (ALLOC((*p)->iov[0].iov_base, char*, char, n) == NULL) {
00854 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Read", "could not allocate %d bytes", n);
00855 RELEASE((*p)->iov, sizeof(struct iovec));
00856 RELEASE((*p), sizeof(struct packet));
00857 return -6;
00858 }
00859
00860
00861 (*p)->iov[0].iov_len = n;
00862 (*p)->iovlen = 1;
00863 memcpy((*p)->iov[0].iov_base, pack_buf, n);
00864
00865
00866 switch (packet_type) {
00867 case REQUEST:
00868 ret = Packet_Translate_Request(*p);
00869 break;
00870 case RESPONSE:
00871 ret = Packet_Translate_Response(*p);
00872 break;
00873 case DATA:
00874 ret = Packet_Translate_Data(*p);
00875 break;
00876 case CC_ACK:
00877 ret = Packet_Translate_CCAck(*p);
00878 break;
00879 case DATA_ACK:
00880
00881 ret = -4;
00882 break;
00883 case CLOSE:
00884 ret = Packet_Translate_Close(*p);
00885 break;
00886 default:
00887 ret = -4;
00888 break;
00889 }
00890
00891 if (ret != 0) {
00892 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Read", "Packet discarded with error [%d]", ret);
00893 RELEASE((*p)->iov[0].iov_base, n);
00894 RELEASE((*p)->iov, sizeof(struct iovec));
00895 RELEASE((*p), sizeof(struct packet));
00896 }
00897
00898 Packet_Compute_Delay(pack_buf, &tv, *p);
00899
00900
00901 return ret;
00902 }
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913 int Packet_Read_ICMP(int icmp_fd, void* pmtu){
00914 if(icmp_fd == -1){
00915 return 1;
00916 }
00917 char buf[1024];
00918 int n = recvfrom(icmp_fd, &buf, 1024, MSG_DONTWAIT, NULL, NULL);
00919 path_mtu* pmtu_vals = (path_mtu*)pmtu;
00920
00921 if(n == -1){
00922 return 1;
00923 }else{
00924 int type = buf[20];
00925 int code = buf[21];
00926 int udp_size = ((int)buf[52] << 8) + buf[53] - 8;
00927
00928
00929
00930 if(type == DEST_UNREACHABLE && code == FRAG_NEEDED && udp_size == *(pmtu_vals->probe_size)){
00931 pmtu_vals->probe_state = MTU_FOUND;
00932 pmtu_vals->search_high = pmtu_vals->probe_size;
00933 struct timeval tv;
00934 GET_TIME_OF_DAY(&tv);
00935 pmtu_vals->next_probe_time = tv.tv_sec + PMTUD_TIME_INTERVAL;
00936 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "packet.c", "Packet_Read_ICMP", "Found MTU based on ICMP. Next probe begins at %lu", (long int)tv.tv_sec);
00937 }
00938
00939 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "packet.c", "Packet_Read_ICMP", "Received ICMP packet. [Size=> %d, Type=> %d, Code=>%d]", n, type, code);
00940 }
00941
00942 return 0;
00943 }
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956 int Packet_Write(packet* p, int fd, struct sockaddr* addr, socklen_t addr_len) {
00957 int n;
00958 int err_val;
00959 socklen_t err_val_len;
00960 struct timeval tv;
00961 uint32_t int32;
00962
00963 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Write", "sending packet: type[%s] iovlen[%d] fd[%d]", Packet_Str_Type(p), p->iovlen, fd);
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976 err_val_len = 4;
00977 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err_val, &err_val_len) == -1) perror("Packet_Write :: getsockopt");
00978 if (err_val != 0) {
00979 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Write", "socket has a pending error[%d]:[%s]", err_val, strerror(err_val));
00980 return err_val;
00981 }
00982
00983
00984
00985
00986 GET_TIME_OF_DAY(&(tv));
00987
00988 int32 = (uint32_t) (tv.tv_sec);
00989 int32 = htonl(int32);
00990 memcpy(p->iov[0].iov_base + 8, &(int32), 4);
00991
00992 int32 = (uint32_t) (tv.tv_usec);
00993 int32 = htonl(int32);
00994 memcpy(p->iov[0].iov_base + 12, &(int32), 4);
00995
00996
00997 if (addr != NULL) {
00998 if (p->iovlen != 1) {
00999 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Write", "can't send more than a vector of 1 element on unconnected socket");
01000 return -1;
01001 }
01002 n = sendto(fd, p->iov[0].iov_base, p->iov[0].iov_len, MSG_DONTWAIT, addr, addr_len);
01003 if (n == -1) {
01004 err_val = errno;
01005 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Write", "sendto generated an error, %s", strerror(err_val));
01006 return err_val;
01007 }
01008 } else {
01009 if ((n = writev(fd, p->iov, p->iovlen)) == -1) {
01010 err_val = errno;
01011 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Write", "writev generated an error, %s", strerror(err_val));
01012 return err_val;
01013 }
01014
01015
01016 }
01017
01018
01019 err_val_len = 4;
01020 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err_val, &err_val_len) == -1) perror("packet_write :: getsockopt");
01021 if (n == -1 || err_val != 0) {
01022 DEBUG_PRINT(DEBUG_PACKET, "packet.c", "Packet_Write", "socket generated error[%s]", strerror(err_val));
01023 return err_val;
01024 }
01025 DEBUG_PACK(DEBUG_PACKET, "packet.c", "Packet_Write", p, "Sent");
01026
01027 return 0;
01028 }
01029
01030
01031
01032
01033
01034
01035
01036
01037 inline uint16_t Packet_Gen_FrameNum() {
01038
01039 return 0;
01040 }
01041
01042
01043
01044
01045
01046
01047
01048 inline uint32_t Packet_Gen_SeqNum() {
01049
01050 return 0;
01051 }
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063 static inline void Packet_Set_Opt(uint32_t* opts, int opt) {
01064
01065 *opts = (*opts) | opt;
01066
01067 }
01068
01069
01070
01071
01072
01073
01074
01075 static inline void Packet_Unset_Opt(uint32_t* opts, int opt) {
01076
01077 *opts = (*opts) & (~opt);
01078
01079 }
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091 static inline int Packet_Get_Opt(uint32_t opts, int opt) {
01092
01093 return (opts & opt);
01094 }