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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include "vfer_api.h"
00051
00052
00053
00054
00055
00056 static int Initialized = -1;
00057 static int Debug_Initialized = -1;
00058
00059
00060 static int vfer_internal_init();
00061
00062
00063
00064
00065
00066 #define PROTOCOL_INIT_TEST ({ \
00067 if (Initialized == -1) \
00068 vfer_internal_init(); \
00069 -1;})
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 vfer_fd vfer_socket(int socktype) {
00088 int sockfd, icmp_fd;
00089 int i;
00090 char is_df_set;
00091 vfer_sock* sock;
00092
00093 if (!PROTOCOL_INIT_TEST) return VFER_IMPL;
00094 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_socket", "Entered");
00095
00096 if (socktype != SOCK_STREAM && socktype != SOCK_DGRAM) {
00097 return VFER_BADTYPE;
00098 }
00099
00100 if (socktype == SOCK_STREAM) {
00101 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_socket", "only socktype of SOCK_DGRAM supported");
00102 return VFER_BADTYPE;
00103 }
00104
00105 for (i = 0; i < MAX_SOCKETS; i++) {
00106 pthread_mutex_lock(&(sockets.s[i].mutex));
00107 if (sockets.s[i].state == CONN_DISCONNECTED) {
00108 sock = &(sockets.s[i]);
00109 sock->state = CONN_ACQUIRED;
00110 pthread_mutex_unlock(&(sockets.s[i].mutex));
00111 break;
00112 }
00113 pthread_mutex_unlock(&(sockets.s[i].mutex));
00114 }
00115 if (i == MAX_SOCKETS) {
00116 return VFER_IMPL;
00117 }
00118
00119 Datagrams_Clear(&(sock->recvd_frames));
00120
00121 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
00122 perror("vfer_socket :: socket");
00123 pthread_mutex_lock(&(sock->mutex));
00124 sock->state = CONN_DISCONNECTED;
00125 pthread_mutex_unlock(&(sock->mutex));
00126 return VFER_IMPL;
00127 }
00128
00129 if (fcntl(sockfd, F_SETFD, O_NONBLOCK) == -1) {
00130 perror("vfer_socket :: fcntl");
00131 pthread_mutex_lock(&(sock->mutex));
00132 sock->state = CONN_DISCONNECTED;
00133 pthread_mutex_unlock(&(sock->mutex));
00134 return VFER_IMPL;
00135 }
00136
00137
00138 SET_DF_BIT(sockfd, is_df_set);
00139 if(is_df_set < 0){
00140 sock->is_df_set = 0;
00141 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "api.c", "vfer_socket", "Unable to set DF bit. Continuing without PMTUD");
00142 }else{
00143 sock->is_df_set = 1;
00144 }
00145
00146
00147 if(sock->is_df_set == 0){
00148 icmp_fd = -1;
00149 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "api.c", "vfer_socket", "DF bit not set so ICMP not needed.");
00150 }else if ((icmp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP)) == -1) {
00151 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "api.c", "vfer_socket", "Unable to create ICMP socket. Continuing anyways");
00152 }else if (fcntl(icmp_fd, F_SETFD, O_NONBLOCK) == -1) {
00153 icmp_fd = -1;
00154 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "api.c", "vfer_socket", "Unable to remove blocking on ICMP socket. Continuing without ICMP.");
00155 }else{
00156 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "api.c", "vfer_socket", "ICMP socket created.");
00157 }
00158
00159 sock->fd = sockfd;
00160 sock->icmp_fd = icmp_fd;
00161 sock->type = socktype;
00162 sock->err = 0;
00163 sock->opts = 0;
00164 sock->state = CONN_ACQUIRED;
00165 sock->select_mark = 0;
00166 sock->select_result = 0;
00167 sock->select_active = 0;
00168 sock->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
00169 sock->close_timeout = 0;
00170 sock->accept_timeout = 0;
00171 sock->snd_timeout = 0;
00172 sock->rcv_timeout = 0;
00173 return i;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 int vfer_close(vfer_fd vfd) {
00190 vfer_sock* sock;
00191
00192 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
00193 sock = &(sockets.s[vfd]);
00194 if (sock->err != 0) { return VFER_BADSOCK; }
00195
00196 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_close", "Entered vfd[%d]", vfd);
00197
00198 pthread_mutex_lock(&(sock->mutex));
00199 switch (sock->state) {
00200 case CONN_DISCONNECTED:
00201 case CONN_DISCONNECTING:
00202 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_close", "Socket already disconnected or disconnecting");
00203 pthread_mutex_unlock(&(sock->mutex));
00204 return VFER_BADSOCK;
00205 case CONN_CONNECTED:
00206
00207 Control_Close(sock);
00208 break;
00209 case CONN_LISTENING:
00210 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_close", "Closing listening socket");
00211 Accept_Queue_Delete(sock->accept_queue);
00212 sock->state = CONN_DISCONNECTED;
00213 pthread_mutex_unlock(&(sock->mutex));
00214 Control_Unregister_Socket(sock);
00215 return 0;
00216 default:
00217 close(sock->fd);
00218 sock->state = CONN_DISCONNECTED;
00219 }
00220 pthread_mutex_unlock(&(sock->mutex));
00221 return 0;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 int vfer_connect(vfer_fd vfd, const struct sockaddr *serv_addr, int addrlen) {
00250 vfer_sock* sock;
00251 int ret;
00252
00253 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
00254 sock = &(sockets.s[vfd]);
00255 if (sock->err != 0) { return VFER_BADSOCK; }
00256
00257 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_connect", "Entered");
00258
00259
00260 pthread_mutex_lock(&(sock->mutex));
00261 if (sock->state != CONN_BOUND) {
00262 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_connect", "socket not bound");
00263 pthread_mutex_unlock(&(sock->mutex));
00264 return VFER_BADSOCK;
00265 }
00266 memcpy(&sock->addr, serv_addr, addrlen);
00267 sock->addr_len = sizeof(sock->addr);
00268
00269 if ((ret = Control_Connect(sock)) == -1) {
00270 pthread_mutex_unlock(&(sock->mutex));
00271 return sock->err;
00272 } else if (ret == -2) {
00273 pthread_mutex_unlock(&(sock->mutex));
00274 return VFER_INPROGRESS;
00275 }
00276
00277 pthread_mutex_unlock(&(sock->mutex));
00278
00279 return 0;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 int vfer_bind(vfer_fd vfd, struct sockaddr *addr, int len) {
00299 int err;
00300 int val;
00301 int ret;
00302 int num_packets;
00303 vfer_sock* sock;
00304
00305 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
00306 sock = &(sockets.s[vfd]);
00307 if (sock->err != 0) { return VFER_BADSOCK; }
00308 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_bind", "Entered");
00309
00310
00311 if (sock->icmp_fd == -1 || bind(sock->icmp_fd, addr, len) < 0) {
00312 sock->icmp_fd = -1;
00313 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "api.c", "vfer_bind", "Unable to bind ICMP socket. Continuing without ICMP.");
00314 }else{
00315 DEBUG_PMTUD_PRINT(DEBUG_PMTUD, "api.c", "vfer_bind", "ICMP socket bind succeeded.");
00316 }
00317
00318
00319 pthread_mutex_lock(&(sock->mutex));
00320 switch (sock->state) {
00321 case CONN_BOUND:
00322 pthread_mutex_unlock(&(sock->mutex));
00323 return VFER_BADSOCK;
00324 case CONN_ACQUIRED:
00325 break;
00326 default:
00327 pthread_mutex_unlock(&(sock->mutex));
00328 return VFER_BADSOCK;
00329 }
00330
00331 if (bind(sock->fd, addr, len) < 0) {
00332 err = errno;
00333 pthread_mutex_unlock(&(sock->mutex));
00334 perror("vfer_bin :: bind");
00335 if (err == EINVAL) {
00336 return VFER_NOBIND;
00337 }
00338 return VFER_BADADDR;
00339 }
00340
00341
00342 num_packets = DEFAULT_RECV_BUF_MAXPACKETS;
00343 val = CALC_UDP_RECV_BUF_SIZE(num_packets);
00344 do {
00345 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_bind", "Trying to set SO_RCVBUF to %d", val);
00346 ret = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val));
00347 if (ret != 0) {
00348 if (errno == ENOBUFS && num_packets > 100) {
00349 num_packets -= 100;
00350 val = CALC_UDP_RECV_BUF_SIZE(num_packets);
00351 } else {
00352 perror("vfer_bind :: setsockopt");
00353 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_bind", "Could not set RCVBUF size to [%d]", val);
00354 pthread_mutex_unlock(&(sock->mutex));
00355 return VFER_NOBIND;
00356 }
00357 }
00358 } while (ret != 0);
00359 sock->udp_recv_buf_size = val;
00360
00361
00362 num_packets = DEFAULT_SEND_BUF_MAXPACKETS;
00363 val = CALC_UDP_SEND_BUF_SIZE(num_packets);
00364 do {
00365 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_bind", "Trying to set SO_SNDBUF to %d", val);
00366 ret = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));
00367 if (ret != 0) {
00368 if (errno == ENOBUFS && num_packets > 100) {
00369 num_packets -= 100;
00370 val = CALC_UDP_SEND_BUF_SIZE(num_packets);
00371 } else {
00372 perror("vfer_bind :: setsockopt");
00373 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_bind", "Could not set SNDBUF size to [%d]", val);
00374 pthread_mutex_unlock(&(sock->mutex));
00375 return VFER_NOBIND;
00376 }
00377 }
00378 } while (ret != 0);
00379 sock->udp_send_buf_size = val;
00380
00381 sock->state = CONN_BOUND;
00382 pthread_mutex_unlock(&(sock->mutex));
00383
00384 return 0;
00385 }
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 int vfer_listen(vfer_fd vfd, int backlog) {
00401 vfer_sock* sock;
00402
00403 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS) || (backlog < 1)) { return VFER_BADSOCK; }
00404 sock = &(sockets.s[vfd]);
00405 if (sock->err != 0) { return VFER_BADSOCK; }
00406
00407 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_listen", "Entered");
00408 pthread_mutex_lock(&(sock->mutex));
00409 if (sock->state != CONN_BOUND) {
00410 pthread_mutex_unlock(&(sock->mutex));
00411 return VFER_BADSOCK;
00412 }
00413
00414 if (!(sock->accept_queue = Accept_Queue_Create(backlog))) {
00415 pthread_mutex_unlock(&(sock->mutex));
00416 return VFER_NOLISTEN;
00417 }
00418
00419 sock->state = CONN_LISTENING;
00420 pthread_mutex_unlock(&(sock->mutex));
00421 Control_Register_Socket(sock);
00422 return 0;
00423 }
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 vfer_fd vfer_accept(vfer_fd vfd, struct sockaddr *addr, socklen_t *len) {
00449 vfer_sock* accepted_socket;
00450 vfer_sock* sock;
00451 int ret, i;
00452
00453 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS) || !addr || !len) { return VFER_BADSOCK; }
00454 sock = &(sockets.s[vfd]);
00455 if (sock->err != 0) { return VFER_BADSOCK; }
00456
00457 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_accept", "Entered");
00458
00459 pthread_mutex_lock(&(sock->mutex));
00460 if (sock->state != CONN_LISTENING) {
00461 pthread_mutex_unlock(&(sock->mutex));
00462 return VFER_BADSOCK;
00463 }
00464 pthread_mutex_unlock(&(sock->mutex));
00465
00466
00467 for (i = 0; i < MAX_SOCKETS; i++) {
00468 pthread_mutex_lock(&(sockets.s[i].mutex));
00469 if (sockets.s[i].state == CONN_DISCONNECTED) {
00470 accepted_socket = &(sockets.s[i]);
00471 accepted_socket->state = CONN_ACQUIRED;
00472
00473 break;
00474 }
00475 pthread_mutex_unlock(&(sockets.s[i].mutex));
00476 }
00477 if (i == MAX_SOCKETS) {
00478 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_accept", "reached connections limit");
00479 return VFER_IMPL;
00480 }
00481
00482
00483 ret = Control_Accept(sock, accepted_socket);
00484 if (ret == 0) {
00485 ret = i;
00486 } else {
00487
00488 accepted_socket->err = 0;
00489 accepted_socket->state = CONN_DISCONNECTED;
00490 }
00491 pthread_mutex_unlock(&(accepted_socket->mutex));
00492 return ret;
00493 }
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 int vfer_setsockopt(vfer_fd vfd, int optname, void *optval, int optlen) {
00523 int ival;
00524
00525 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS) || (sockets.s[vfd].err != 0)) { return VFER_BADSOCK; }
00526 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_setsockopt", "Entered with optname[%d]", optname);
00527
00528 pthread_mutex_lock(&(sockets.s[vfd].mutex));
00529 switch (optname) {
00530 case VFERSO_QTTL:
00531
00532 break;
00533 case VFERSO_SNDSIZE:
00534
00535 if (optlen != sizeof(int)) {
00536 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00537 return VFER_BADOPT;
00538 }
00539
00540 ival = *((int*)(optval));
00541 if (ival < 1) {
00542 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00543 return VFER_BADOPT;
00544 }
00545 sockets.s[vfd].ccontrol.mtu = ival;
00546 break;
00547 case VFERSO_RELFUN:
00548 if (optlen != sizeof(relfun_t*)) {
00549 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00550 return VFER_BADOPT;
00551 }
00552 sockets.s[vfd].relfun = optval;
00553 break;
00554 case VFERSO_CONNECTTIMEOUT:
00555
00556 if (optlen != sizeof(int)) {
00557 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00558 return VFER_BADOPT;
00559 }
00560 sockets.s[vfd].connect_timeout = *((int*)(optval));
00561 break;
00562 case VFERSO_CLOSETIMEOUT:
00563
00564 if (optlen != sizeof(int)) {
00565 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00566 return VFER_BADOPT;
00567 }
00568 sockets.s[vfd].close_timeout = *((int*)(optval));
00569 break;
00570 case VFERSO_ACCEPTTIMEOUT:
00571
00572 if (optlen != sizeof(int)) {
00573 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00574 return VFER_BADOPT;
00575 }
00576 sockets.s[vfd].accept_timeout = *((int*)(optval));
00577 break;
00578 case VFERSO_SNDTIMEOUT:
00579
00580 if (optlen != sizeof(int)) {
00581 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00582 return VFER_BADOPT;
00583 }
00584 sockets.s[vfd].snd_timeout = *((int*)(optval));
00585 break;
00586 case VFERSO_RCVTIMEOUT:
00587
00588 if (optlen != sizeof(int)) {
00589 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00590 return VFER_BADOPT;
00591 }
00592 sockets.s[vfd].rcv_timeout = *((int*)(optval));
00593 break;
00594 case VFERSO_NOPMTUD:
00595
00596 if (optlen != sizeof(int)) {
00597 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00598 return VFER_BADOPT;
00599 }
00600 if(1 == *((int*)(optval))){
00601 sockets.s[vfd].pmtu.probe_state = NO_PMTUD;
00602 }
00603 break;
00604 case VFERSO_NONBLOCK:
00605 if (*((int*)(optval)) == 1) {
00606 sockets.s[vfd].opts |= optname;
00607 } else {
00608 sockets.s[vfd].opts &= (~optname);
00609 }
00610 break;
00611 default:
00612 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00613 return VFER_BADOPT;
00614 }
00615 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00616 return 0;
00617 }
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643 int vfer_getsockopt(vfer_fd vfd, int optname, void *optval, int *optlen) {
00644
00645 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS) || !optval || !optlen || (sockets.s[vfd].err != 0)) { return VFER_BADSOCK; }
00646 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_getsockopt", "Entered with optname[%d]", optname);
00647
00648 pthread_mutex_lock(&(sockets.s[vfd].mutex));
00649 if (*optlen <= 0) {
00650 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00651 return VFER_BADOPT;
00652 }
00653
00654 switch (optname) {
00655 case VFERSO_QTTL:
00656
00657 break;
00658 case VFERSO_SNDSIZE:
00659
00660 break;
00661 case VFERSO_NOPMTUD:
00662
00663 *((char*)(optval)) = SOCK_OPT_ISSET((&(sockets.s[vfd])), VFERSO_NOPMTUD);
00664 *optlen = 1;
00665 break;
00666 case VFERSO_NONBLOCK:
00667
00668 *((char*)(optval)) = SOCK_OPT_ISSET((&(sockets.s[vfd])), VFERSO_NONBLOCK);
00669 *optlen = 1;
00670 break;
00671 case VFERSO_RELFUN:
00672
00673 break;
00674 case VFERSO_CONNECTTIMEOUT:
00675 if (*optlen < sizeof(int)) {
00676 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00677 return VFER_BADOPT;
00678 }
00679 *((int*)(optval)) = sockets.s[vfd].connect_timeout;
00680 *optlen = sizeof(int);
00681 break;
00682 case VFERSO_CLOSETIMEOUT:
00683 if (*optlen < sizeof(int)) {
00684 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00685 return VFER_BADOPT;
00686 }
00687 *((int*)(optval)) = sockets.s[vfd].close_timeout;
00688 *optlen = sizeof(int);
00689 break;
00690 case VFERSO_ACCEPTTIMEOUT:
00691 if (*optlen < sizeof(int)) {
00692 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00693 return VFER_BADOPT;
00694 }
00695 *((int*)(optval)) = sockets.s[vfd].accept_timeout;
00696 *optlen = sizeof(int);
00697 break;
00698 case VFERSO_SNDTIMEOUT:
00699 if (*optlen < sizeof(int)) {
00700 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00701 return VFER_BADOPT;
00702 }
00703 *((int*)(optval)) = sockets.s[vfd].snd_timeout;
00704 *optlen = sizeof(int);
00705 break;
00706 case VFERSO_RCVTIMEOUT:
00707 if (*optlen < sizeof(int)) {
00708 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00709 return VFER_BADOPT;
00710 }
00711 *((int*)(optval)) = sockets.s[vfd].rcv_timeout;
00712 *optlen = sizeof(int);
00713 break;
00714 default:
00715 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00716 return VFER_BADOPT;
00717 }
00718 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00719 return 0;
00720 }
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745 vfer_stats vfer_sockstats(vfer_fd vfd) {
00746 vfer_stats stats;
00747 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return stats; }
00748 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_sockstats", "Entered");
00749
00750 pthread_mutex_lock(&(sockets.s[vfd].mutex));
00751 stats = sockets.s[vfd].stats;
00752 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00753 return stats;
00754 }
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764 int vfer_sockerror(vfer_fd vfd) {
00765 int err;
00766 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return 0; }
00767
00768
00769 pthread_mutex_lock(&(sockets.s[vfd].mutex));
00770 err = sockets.s[vfd].err;
00771 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00772 return err;
00773 }
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783 char* vfer_errortext(int err) {
00784 if (!PROTOCOL_INIT_TEST) return 0;
00785 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_errortext", "Entered");
00786
00787 switch (err) {
00788 case 0:
00789 return "success";
00790 case VFER_BADTYPE:
00791 return "invalid socket type";
00792 case VFER_IMPL:
00793 return "internal implemenation error";
00794 case VFER_BADSOCK:
00795 return "bad socket passed as argument";
00796 case VFER_TIMEOUT:
00797 return "connection attempt timedout";
00798 case VFER_REFUSED:
00799 return "connection refused";
00800 case VFER_NOCONN:
00801 return "not able to connect on a socket";
00802 case VFER_NOBIND:
00803 return "can't bind the socket";
00804 case VFER_BADADDR:
00805 return "bad adress passed as argument";
00806 case VFER_NOLISTEN:
00807 return "can't listen on socket";
00808 case VFER_NOACCEPT:
00809 return "socket not able to perform accept because it is not listening";
00810 case VFER_NOTSTREAM:
00811 return "socket is not of type SOCK_STREAM";
00812 case VFER_WOULDBLOCK:
00813 return "socket is non-blocking and there are no waiting connections";
00814 case VFER_BADOPT:
00815 return "unknown or illegal option/length";
00816 case VFER_BADFD:
00817 return "error using mmap on fd";
00818 case VFER_INVAL:
00819 return "argument value is invalid";
00820 case VFER_UNCONN:
00821 return "socket is not connected";
00822 default:
00823 return "unrecognized error";
00824 }
00825 }
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847 int vfer_sendfile(vfer_fd vfd, int fd, off_t offset, size_t size) {
00848 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
00849 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_sendfile", "Entered fd[%d] offset[%d] size[%d]", fd, (int)(offset), (int)(size));
00850
00851 return vfer__sendfile( (vfer_trnsm_fn) vfer_send,
00852 (vfer_trnsm_arg) vfd, MAX_FRAME_SIZE, fd,
00853 offset, size);
00854 }
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873 int vfer__sendfile(vfer_trnsm_fn fn, vfer_trnsm_arg arg,
00874 int frame_size, int fd, off_t offset, size_t size) {
00875 void* ptr;
00876 int bytes_sent, err, n, to_send;
00877 size_t to_map, bytes_mapped;
00878 int page_size;
00879 size_t mmap_chunk_size;
00880
00881
00882 page_size = getpagesize();
00883 mmap_chunk_size = (MMAP_CHUNK_SIZE / page_size) * page_size;
00884
00885 bytes_mapped = 0;
00886 while (bytes_mapped != size) {
00887 if ((size - bytes_mapped) >= mmap_chunk_size) {
00888 to_map = mmap_chunk_size;
00889 } else {
00890 to_map = size - bytes_mapped;
00891 }
00892 ptr = mmap(0, to_map, PROT_READ, MAP_PRIVATE, fd, bytes_mapped + offset);
00893 if (ptr == MAP_FAILED) {
00894 err = errno;
00895 perror("vfer__sendfile :: mmap");
00896 DEBUG_PRINT(DEBUG_API, "api.c", "vfer__sendfile", "mmap failed with err[%d]", err);
00897 switch (err) {
00898 case EBADF:
00899 return VFER_BADFD;
00900 default:
00901 return VFER_INVAL;
00902 }
00903 }
00904
00905
00906 bytes_sent = 0;
00907 while (bytes_sent != to_map) {
00908 if ((to_map - bytes_sent) >= frame_size) {
00909 to_send = frame_size;
00910 } else {
00911 to_send = to_map - bytes_sent;
00912 }
00913 n = fn(arg, ptr + bytes_sent, to_send);
00914 if (n < 0) {
00915 DEBUG_PRINT(DEBUG_API, "api.c", "vfer__sendfile", "send function failed with err[%d]", n);
00916 munmap(ptr, to_map);
00917 return n;
00918 }
00919 bytes_sent += n;
00920 }
00921 bytes_mapped += to_map;
00922 munmap(ptr, to_map);
00923 }
00924 return bytes_mapped;
00925 }
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949 int vfer_send(vfer_fd vfd, const void * buf, size_t len) {
00950 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
00951
00952
00953 pthread_mutex_lock(&(sockets.s[vfd].mutex));
00954 if (sockets.s[vfd].state == CONN_CONNECTED) {
00955 if (len > MAX_FRAME_SIZE) {
00956 return VFER_INVAL;
00957 }
00958 return Control_Send(&(sockets.s[vfd]), buf, len);
00959 }
00960 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
00961 return VFER_UNCONN;
00962 }
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979 int vfer_recvfile(vfer_fd vfd, int fd, off_t offset, size_t size) {
00980 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
00981 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_recvfile", "Entered fd[%d] offset[%d] size[%d]", fd, (int)(offset), (int)(size));
00982
00983 return vfer__recvfile( (vfer_trnsm_fn) vfer_recv,
00984 (vfer_trnsm_arg) vfd, MAX_FRAME_SIZE, fd,
00985 offset, size);
00986 }
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006 int vfer__recvfile(vfer_trnsm_fn fn, vfer_trnsm_arg arg,
01007 uint32_t frame_size, int fd, off_t offset, size_t size) {
01008 char file_buf[frame_size];
01009 int i, n;
01010
01011
01012 lseek(fd, offset, SEEK_SET);
01013
01014
01015
01016 i = n = 0;
01017 while (size != 0 && ((n = fn(arg, file_buf, frame_size)) > 0)) {
01018 if (n > size) {
01019 n = size;
01020 }
01021 if (write(fd, file_buf, n) != n) {
01022 perror("vfer_recvfile :: write");
01023 DEBUG_PRINT(DEBUG_API, "api.c", "vfer__recvfile", "write failed on frame[%d]", i);
01024 return VFER_INVAL;
01025 }
01026 i++;
01027 size -= n;
01028 }
01029 if (n < 0) {
01030 DEBUG_PRINT(DEBUG_API, "api.c", "vfer__recvfile", "receive function failed on frame[%d] error[%d]", i, n);
01031 return n;
01032 }
01033 return size;
01034 }
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053 int vfer_recv(vfer_fd vfd, void *buf, size_t len) {
01054 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
01055
01056
01057 if (len > MAX_FRAME_SIZE) { return VFER_INVAL; }
01058
01059 pthread_mutex_lock(&(sockets.s[vfd].mutex));
01060 if (sockets.s[vfd].state != CONN_CONNECTED &&
01061 sockets.s[vfd].state != CONN_DISCONNECTED &&
01062 sockets.s[vfd].state != CONN_DISCONNECTING) {
01063 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_recv", "socket not in CONN_CONNECTED/DISCONNECTED/DISCONNECTING states but state[%d]",
01064 sockets.s[vfd].state);
01065 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
01066 return VFER_UNCONN;
01067 }
01068 return Control_Recv(&(sockets.s[vfd]), buf, len);
01069 }
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084 int vfer_selectmark(vfer_fd vfd, int mark) {
01085 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
01086 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_selectmark", "Entered");
01087 if ((mark ^ (VFER_READABLE | VFER_WRITABLE | VFER_EXCEPTION)) != 0) {
01088 return VFER_INVAL;
01089 }
01090 pthread_mutex_lock(&(sockets.s[vfd].mutex));
01091 sockets.s[vfd].select_mark = mark;
01092 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
01093 return 0;
01094 }
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108 int vfer_selecttest(vfer_fd vfd) {
01109 int ret;
01110 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS)) { return VFER_BADSOCK; }
01111 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_selecttest", "Entered");
01112 pthread_mutex_lock(&(sockets.s[vfd].mutex));
01113 ret = (int)(sockets.s[vfd].select_result);
01114 pthread_mutex_unlock(&(sockets.s[vfd].mutex));
01115 return ret;
01116 }
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136 int vfer_select(int len, vfer_fd *vfds, struct timeval *timeout) {
01137 int i;
01138 int count;
01139 int ret;
01140 struct timespec timedwait_timeout;
01141
01142 if (!PROTOCOL_INIT_TEST) { return VFER_BADSOCK; }
01143 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_select", "Entered");
01144
01145
01146
01147
01148
01149 count = 0;
01150 pthread_mutex_lock(&(sockets.select_mutex));
01151 for (i = 0; i < len; i++) {
01152 if ((vfds[i] < 0) || (vfds[i] > MAX_SOCKETS)) {
01153 return VFER_BADSOCK;
01154 }
01155 pthread_mutex_lock(&(sockets.s[vfds[i]].mutex));
01156 if (sockets.s[vfds[i]].select_mark != 0) {
01157 sockets.s[vfds[i]].select_active = 1;
01158 sockets.s[vfds[i]].select_result = 0;
01159 count++;
01160 }
01161 pthread_mutex_unlock(&(sockets.s[vfds[i]].mutex));
01162 }
01163
01164 if (count == 0) {
01165 pthread_mutex_unlock(&(sockets.select_mutex));
01166 return 0;
01167 }
01168
01169 timedwait_timeout.tv_sec = timeout->tv_sec;
01170 timedwait_timeout.tv_nsec = timeout->tv_usec * 1000;
01171
01172
01173 do {
01174 ret = pthread_cond_timedwait(&(sockets.select_cond), &(sockets.select_mutex), &(timedwait_timeout));
01175 } while (ret != 0 && ret != ETIMEDOUT);
01176
01177
01178 count = 0;
01179 for (i = 0; i < len; i++) {
01180 pthread_mutex_lock(&(sockets.s[vfds[i]].mutex));
01181 if (sockets.s[vfds[i]].select_active == 1) {
01182 if (sockets.s[vfds[i]].select_result != 0) {
01183 count++;
01184 }
01185 sockets.s[vfds[i]].select_active = 0;
01186 }
01187 pthread_mutex_unlock(&(sockets.s[vfds[i]].mutex));
01188 }
01189 pthread_mutex_unlock(&(sockets.select_mutex));
01190 return count;
01191 }
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215 int vfer_debug(FILE* debug_file, FILE* error_file, const char* layers) {
01216 int len;
01217 DEBUG_ACCEPTQ = 0;
01218 DEBUG_API = 0;
01219 DEBUG_CTL = 0;
01220 DEBUG_PACKET = 0;
01221 DEBUG_CCTL = 0;
01222 DEBUG_PMTUD = 0;
01223
01224 if (layers == NULL) {
01225 len = 0;
01226 } else {
01227 len = strlen(layers);
01228 }
01229 while (len > 0) {
01230 switch (layers[len-1]) {
01231 case 'q':
01232 DEBUG_ACCEPTQ = 1;
01233 break;
01234 case 'a':
01235 DEBUG_API = 1;
01236 break;
01237 case 'c':
01238 DEBUG_CTL = 1;
01239 break;
01240 case 'C':
01241 DEBUG_CCTL = 1;
01242 break;
01243 case 'p':
01244 DEBUG_PACKET = 1;
01245 break;
01246 case 'P':
01247 DEBUG_PMTUD = 1;
01248 break;
01249 }
01250 len--;
01251 }
01252 debug_out = debug_file;
01253 error_out = error_file;
01254
01255
01256 Debug_Initialized = 0;
01257 if (!PROTOCOL_INIT_TEST) return -1;
01258 return 0;
01259 }
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274 static int vfer_internal_init() {
01275 int i;
01276 #ifdef TSCI2
01277 int tsci2_methods;
01278 #endif
01279 if (Debug_Initialized == -1) {
01280
01281 debug_out = NULL;
01282 error_out = stdout;
01283 DEBUG_ACCEPTQ = 0;
01284 DEBUG_API = 0;
01285 DEBUG_CTL = 0;
01286 DEBUG_CCTL = 0;
01287 DEBUG_PACKET = 0;
01288 }
01289
01290 sockets.num_active = 0;
01291 sockets.max_fd = 0;
01292 FD_ZERO(&(sockets.fds));
01293 pthread_mutex_init(&(sockets.mutex), NULL);
01294 pthread_mutex_init (&(sockets.select_mutex), NULL);
01295 pthread_cond_init(&(sockets.select_cond), NULL);
01296
01297 for (i = 0; i < MAX_SOCKETS; i++) {
01298 pthread_mutex_init (&(sockets.s[i].mutex), NULL);
01299 pthread_cond_init(&(sockets.s[i].cond), NULL);
01300
01301 sockets.s[i].send_frames.first = sockets.s[i].send_frames.last = NULL;
01302 sockets.s[i].recv_frames.first = sockets.s[i].recv_frames.last = NULL;
01303 sockets.s[i].recvd_frames.first = sockets.s[i].recvd_frames.last = NULL;
01304 sockets.s[i].send_frames.num = 0;
01305 sockets.s[i].recv_frames.num = 0;
01306 sockets.s[i].recvd_frames.num = 0;
01307 sockets.s[i].recvd_frames.length = 0;
01308 sockets.s[i].recv_frames.length = 0;
01309 sockets.s[i].send_frames.length = 0;
01310 pthread_mutex_init(&(sockets.s[i]).mutx_readable, NULL);
01311 pthread_cond_init(&(sockets.s[i]).cond_readable, NULL);
01312 sockets.s[i].state = CONN_DISCONNECTED;
01313 }
01314
01315
01316 #ifdef TSCI2
01317 tsci2_methods = TSCI2_DAEMON | TSCI2_CLIENT | TSCI2_FALLBACK;
01318 tsci2_methods = tsci2_init(tsci2_methods);
01319 switch ( tsci2_methods ) {
01320 case TSCI2_DAEMON:
01321 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_internal_init", "using method TSCI2_DAEMON for gettimeofday");
01322 break;
01323 case TSCI2_CLIENT:
01324 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_internal_init", "using method TSCI2_CLIENT for gettimeofday");
01325 break;
01326 case TSCI2_FALLBACK:
01327 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_internal_init", "using method TSCI2_FALLBACK for gettimeofday");
01328 break;
01329 default:
01330 DEBUG_PRINT(DEBUG_API, "api.c", "vfer_internal_init", "TSCI2 initialization failed");
01331 }
01332 #endif
01333
01334
01335 srand(time(NULL));
01336
01337
01338 ReqMem = 0;
01339
01340 Initialized = 1;
01341 return 0;
01342 }
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358 int vfer_getsockname (vfer_fd vfd, struct sockaddr *name, socklen_t* len) {
01359 if (!PROTOCOL_INIT_TEST || (vfd < 0) || (vfd > MAX_SOCKETS))
01360 return VFER_BADSOCK;
01361
01362 vfer_sock* sock = &(sockets.s[vfd]);
01363 if (sock->err != 0)
01364 return VFER_BADSOCK;
01365
01366
01367 if (getsockname(sock->fd, name, len) != 0) {
01368 perror("getsockname failed");
01369 abort();
01370 }
01371 return 0;
01372 }
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384 bool vfer_is_stream (vfer_fd fd) {
01385 return false;
01386 }
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397 ssize_t vfer_max_frame_size (vfer_fd fd) {
01398 return MAX_FRAME_SIZE;
01399 }