Lines 65-72
Link Here
|
65 |
unsigned c_sectorsize; |
65 |
unsigned c_sectorsize; |
66 |
unsigned c_flags; /* flags (RO/RW) */ |
66 |
unsigned c_flags; /* flags (RO/RW) */ |
67 |
int c_diskfd; |
67 |
int c_diskfd; |
68 |
int c_sendfd; |
68 |
int c_netfd; |
69 |
int c_recvfd; |
|
|
70 |
time_t c_birthtime; |
69 |
time_t c_birthtime; |
71 |
char *c_path; |
70 |
char *c_path; |
72 |
uint64_t c_token; |
71 |
uint64_t c_token; |
Lines 138-147
Link Here
|
138 |
/* Use snprintf(3) so that we don't reenter stdio(3). */ |
137 |
/* Use snprintf(3) so that we don't reenter stdio(3). */ |
139 |
LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) { |
138 |
LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) { |
140 |
snprintf(buf, sizeof(buf), |
139 |
snprintf(buf, sizeof(buf), |
|
|
140 |
"token %llu\n" |
141 |
"path %s\n" |
141 |
"path %s\n" |
142 |
"send_seq %llu\n" |
142 |
"send_seq %llu\n" |
143 |
"recv_seq %llu\n" |
143 |
"recv_seq %llu\n" |
144 |
"", |
144 |
"", |
|
|
145 |
conn->c_token, |
145 |
conn->c_path, |
146 |
conn->c_path, |
146 |
conn->c_send_seq, conn->c_recv_seq); |
147 |
conn->c_send_seq, conn->c_recv_seq); |
147 |
write(STDERR_FILENO, buf, strlen(buf)); |
148 |
write(STDERR_FILENO, buf, strlen(buf)); |
Lines 527-550
Link Here
|
527 |
ip2str((struct sockaddr*)&conn->c_srcaddr), |
528 |
ip2str((struct sockaddr*)&conn->c_srcaddr), |
528 |
conn->c_path); |
529 |
conn->c_path); |
529 |
close(conn->c_diskfd); |
530 |
close(conn->c_diskfd); |
530 |
close(conn->c_sendfd); |
531 |
close(conn->c_netfd); |
531 |
close(conn->c_recvfd); |
|
|
532 |
free(conn->c_path); |
532 |
free(conn->c_path); |
533 |
free(conn); |
533 |
free(conn); |
534 |
} |
534 |
} |
535 |
} |
535 |
} |
536 |
} |
|
|
537 |
|
538 |
static struct ggd_connection * |
539 |
connection_find(struct g_gate_cinit *cinit) |
540 |
{ |
541 |
struct ggd_connection *conn; |
542 |
|
543 |
LIST_FOREACH(conn, &connections, c_next) { |
544 |
if (conn->c_token == cinit->gc_token) |
545 |
break; |
546 |
} |
547 |
return (conn); |
548 |
} |
536 |
} |
549 |
|
537 |
|
550 |
static struct ggd_connection * |
538 |
static struct ggd_connection * |
Lines 570-580
Link Here
|
570 |
} |
558 |
} |
571 |
conn->c_token = cinit->gc_token; |
559 |
conn->c_token = cinit->gc_token; |
572 |
memcpy(&conn->c_srcaddr, s, s->sa_len); |
560 |
memcpy(&conn->c_srcaddr, s, s->sa_len); |
573 |
conn->c_sendfd = conn->c_recvfd = -1; |
561 |
conn->c_diskfd = -1; |
574 |
if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) |
562 |
conn->c_netfd = sfd; |
575 |
conn->c_sendfd = sfd; |
|
|
576 |
else |
577 |
conn->c_recvfd = sfd; |
578 |
conn->c_mediasize = 0; |
563 |
conn->c_mediasize = 0; |
579 |
conn->c_sectorsize = 0; |
564 |
conn->c_sectorsize = 0; |
580 |
time(&conn->c_birthtime); |
565 |
time(&conn->c_birthtime); |
Lines 583-614
Link Here
|
583 |
g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(s), |
568 |
g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(s), |
584 |
conn->c_path); |
569 |
conn->c_path); |
585 |
return (conn); |
570 |
return (conn); |
586 |
} |
|
|
587 |
|
588 |
static int |
589 |
connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit, |
590 |
struct sockaddr *s, int sfd) |
591 |
{ |
592 |
if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) { |
593 |
if (conn->c_sendfd != -1) { |
594 |
g_gate_log(LOG_WARNING, |
595 |
"Send socket already exists [%s, %s].", ip2str(s), |
596 |
conn->c_path); |
597 |
return (EEXIST); |
598 |
} |
599 |
conn->c_sendfd = sfd; |
600 |
} else { |
601 |
if (conn->c_recvfd != -1) { |
602 |
g_gate_log(LOG_WARNING, |
603 |
"Receive socket already exists [%s, %s].", |
604 |
ip2str(s), conn->c_path); |
605 |
return (EEXIST); |
606 |
} |
607 |
conn->c_recvfd = sfd; |
608 |
} |
609 |
g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(s), |
610 |
conn->c_path); |
611 |
return (0); |
612 |
} |
571 |
} |
613 |
|
572 |
|
614 |
/* |
573 |
/* |
Lines 622-641
Link Here
|
622 |
LIST_REMOVE(conn, c_next); |
581 |
LIST_REMOVE(conn, c_next); |
623 |
g_gate_log(LOG_DEBUG, "Connection removed [%s %s].", |
582 |
g_gate_log(LOG_DEBUG, "Connection removed [%s %s].", |
624 |
ip2str((struct sockaddr*)&conn->c_srcaddr), conn->c_path); |
583 |
ip2str((struct sockaddr*)&conn->c_srcaddr), conn->c_path); |
625 |
if (conn->c_sendfd != -1) |
584 |
close(conn->c_netfd); |
626 |
close(conn->c_sendfd); |
585 |
if(conn->c_diskfd == -1) |
627 |
if (conn->c_recvfd != -1) |
586 |
close(conn->c_diskfd); |
628 |
close(conn->c_recvfd); |
|
|
629 |
close(conn->c_diskfd); |
630 |
free(conn->c_path); |
587 |
free(conn->c_path); |
631 |
free(conn); |
588 |
free(conn); |
632 |
} |
|
|
633 |
|
634 |
static int |
635 |
connection_ready(struct ggd_connection *conn) |
636 |
{ |
637 |
|
638 |
return (conn->c_sendfd != -1 && conn->c_recvfd != -1); |
639 |
} |
589 |
} |
640 |
|
590 |
|
641 |
static void |
591 |
static void |
Lines 742-748
Link Here
|
742 |
|
692 |
|
743 |
conn = arg; |
693 |
conn = arg; |
744 |
g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path); |
694 |
g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path); |
745 |
fd = conn->c_recvfd; |
695 |
fd = conn->c_netfd; |
746 |
for (;;) { |
696 |
for (;;) { |
747 |
/* |
697 |
/* |
748 |
* Get header packet. |
698 |
* Get header packet. |
Lines 886-892
Link Here
|
886 |
|
836 |
|
887 |
conn = arg; |
837 |
conn = arg; |
888 |
g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path); |
838 |
g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path); |
889 |
fd = conn->c_sendfd; |
839 |
fd = conn->c_netfd; |
890 |
for (;;) { |
840 |
for (;;) { |
891 |
/* |
841 |
/* |
892 |
* Get a request from the outgoing queue. |
842 |
* Get a request from the outgoing queue. |
Lines 937-942
Link Here
|
937 |
g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(from)); |
887 |
g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(from)); |
938 |
} |
888 |
} |
939 |
|
889 |
|
|
|
890 |
/* |
891 |
* handshake return 1 if sfd is closed; otherwise 0 so that caller has to close. |
892 |
*/ |
940 |
static int |
893 |
static int |
941 |
handshake(struct sockaddr *from, int sfd) |
894 |
handshake(struct sockaddr *from, int sfd) |
942 |
{ |
895 |
{ |
Lines 989-1024
Link Here
|
989 |
return (0); |
942 |
return (0); |
990 |
} |
943 |
} |
991 |
g_gate_log(LOG_DEBUG, "Initial packet received."); |
944 |
g_gate_log(LOG_DEBUG, "Initial packet received."); |
992 |
conn = connection_find(&cinit); |
945 |
|
993 |
if (conn != NULL) { |
946 |
/* |
994 |
/* |
947 |
* New connection, allocate space. |
995 |
* Connection should already exists. |
948 |
*/ |
996 |
*/ |
949 |
conn = connection_new(&cinit, from, sfd); |
997 |
g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).", |
950 |
if (conn == NULL) { |
998 |
(unsigned long)conn->c_token); |
951 |
sendfail(sfd, ENOMEM, |
999 |
if (connection_add(conn, &cinit, from, sfd) == -1) { |
952 |
"Cannot allocate new connection."); |
1000 |
connection_remove(conn); |
953 |
return (0); |
1001 |
return (0); |
|
|
1002 |
} |
1003 |
} else { |
1004 |
/* |
1005 |
* New connection, allocate space. |
1006 |
*/ |
1007 |
conn = connection_new(&cinit, from, sfd); |
1008 |
if (conn == NULL) { |
1009 |
sendfail(sfd, ENOMEM, |
1010 |
"Cannot allocate new connection."); |
1011 |
return (0); |
1012 |
} |
1013 |
g_gate_log(LOG_DEBUG, "New connection created (token=%lu).", |
1014 |
(unsigned long)conn->c_token); |
1015 |
} |
954 |
} |
|
|
955 |
g_gate_log(LOG_DEBUG, "New connection created (token=%lu).", |
956 |
(unsigned long)conn->c_token); |
1016 |
|
957 |
|
1017 |
ex = exports_find(from, &cinit, conn); |
958 |
ex = exports_find(from, &cinit, conn); |
1018 |
if (ex == NULL) { |
959 |
if (ex == NULL) { |
1019 |
connection_remove(conn); |
|
|
1020 |
sendfail(sfd, errno, NULL); |
960 |
sendfail(sfd, errno, NULL); |
1021 |
return (0); |
961 |
goto out; |
1022 |
} |
962 |
} |
1023 |
if (conn->c_mediasize == 0) { |
963 |
if (conn->c_mediasize == 0) { |
1024 |
conn->c_mediasize = g_gate_mediasize(conn->c_diskfd); |
964 |
conn->c_mediasize = g_gate_mediasize(conn->c_diskfd); |
Lines 1033-1048
Link Here
|
1033 |
g_gate_swap2n_sinit(&sinit); |
973 |
g_gate_swap2n_sinit(&sinit); |
1034 |
data = g_gate_send(sfd, &sinit, sizeof(sinit), 0); |
974 |
data = g_gate_send(sfd, &sinit, sizeof(sinit), 0); |
1035 |
g_gate_swap2h_sinit(&sinit); |
975 |
g_gate_swap2h_sinit(&sinit); |
1036 |
if (data == -1) { |
976 |
if (data == -1) |
1037 |
sendfail(sfd, errno, "Error while sending initial packet: %s.", |
977 |
sendfail(sfd, errno, "Error while sending initial packet: %s.", |
1038 |
strerror(errno)); |
978 |
strerror(errno)); |
1039 |
return (0); |
979 |
else |
1040 |
} |
|
|
1041 |
|
1042 |
if (connection_ready(conn)) { |
1043 |
connection_launch(conn); |
980 |
connection_launch(conn); |
1044 |
connection_remove(conn); |
981 |
out: |
1045 |
} |
982 |
connection_remove(conn); |
1046 |
return (1); |
983 |
return (1); |
1047 |
} |
984 |
} |
1048 |
|
985 |
|