diff options
Diffstat (limited to 'src/testspdy/test_new_connection.c')
-rw-r--r-- | src/testspdy/test_new_connection.c | 86 |
1 files changed, 44 insertions, 42 deletions
diff --git a/src/testspdy/test_new_connection.c b/src/testspdy/test_new_connection.c index b07a0e84..e4e2b2e2 100644 --- a/src/testspdy/test_new_connection.c +++ b/src/testspdy/test_new_connection.c | |||
@@ -23,9 +23,9 @@ | |||
23 | * @author Andrey Uzunov | 23 | * @author Andrey Uzunov |
24 | * @author Tatsuhiro Tsujikawa | 24 | * @author Tatsuhiro Tsujikawa |
25 | */ | 25 | */ |
26 | 26 | ||
27 | //TODO child exits with ret val 1 sometimes | 27 | //TODO child exits with ret val 1 sometimes |
28 | 28 | ||
29 | #include "platform.h" | 29 | #include "platform.h" |
30 | #include "microspdy.h" | 30 | #include "microspdy.h" |
31 | #include <sys/wait.h> | 31 | #include <sys/wait.h> |
@@ -45,7 +45,7 @@ int | |||
45 | spdylay_printf(const char *format, ...) | 45 | spdylay_printf(const char *format, ...) |
46 | { | 46 | { |
47 | (void)format; | 47 | (void)format; |
48 | 48 | ||
49 | return 0; | 49 | return 0; |
50 | } | 50 | } |
51 | 51 | ||
@@ -54,10 +54,10 @@ spdylay_fprintf(FILE *stream, const char *format, ...) | |||
54 | { | 54 | { |
55 | (void)stream; | 55 | (void)stream; |
56 | (void)format; | 56 | (void)format; |
57 | 57 | ||
58 | return 0; | 58 | return 0; |
59 | } | 59 | } |
60 | 60 | ||
61 | void | 61 | void |
62 | killchild(int pid, char *message) | 62 | killchild(int pid, char *message) |
63 | { | 63 | { |
@@ -73,7 +73,7 @@ killparent(int pid, char *message) | |||
73 | kill(pid, SIGKILL); | 73 | kill(pid, SIGKILL); |
74 | _exit(2); | 74 | _exit(2); |
75 | } | 75 | } |
76 | 76 | ||
77 | 77 | ||
78 | /***** | 78 | /***** |
79 | * start of code needed to utilize spdylay | 79 | * start of code needed to utilize spdylay |
@@ -222,7 +222,7 @@ static ssize_t send_callback(spdylay_session *session, | |||
222 | { | 222 | { |
223 | (void)session; | 223 | (void)session; |
224 | (void)flags; | 224 | (void)flags; |
225 | 225 | ||
226 | struct Connection *connection; | 226 | struct Connection *connection; |
227 | ssize_t rv; | 227 | ssize_t rv; |
228 | connection = (struct Connection*)user_data; | 228 | connection = (struct Connection*)user_data; |
@@ -254,7 +254,7 @@ static ssize_t recv_callback(spdylay_session *session, | |||
254 | { | 254 | { |
255 | (void)session; | 255 | (void)session; |
256 | (void)flags; | 256 | (void)flags; |
257 | 257 | ||
258 | struct Connection *connection; | 258 | struct Connection *connection; |
259 | ssize_t rv; | 259 | ssize_t rv; |
260 | connection = (struct Connection*)user_data; | 260 | connection = (struct Connection*)user_data; |
@@ -288,7 +288,7 @@ static void before_ctrl_send_callback(spdylay_session *session, | |||
288 | void *user_data) | 288 | void *user_data) |
289 | { | 289 | { |
290 | (void)user_data; | 290 | (void)user_data; |
291 | 291 | ||
292 | if(type == SPDYLAY_SYN_STREAM) { | 292 | if(type == SPDYLAY_SYN_STREAM) { |
293 | struct Request *req; | 293 | struct Request *req; |
294 | int stream_id = frame->syn_stream.stream_id; | 294 | int stream_id = frame->syn_stream.stream_id; |
@@ -305,7 +305,7 @@ static void on_ctrl_send_callback(spdylay_session *session, | |||
305 | spdylay_frame *frame, void *user_data) | 305 | spdylay_frame *frame, void *user_data) |
306 | { | 306 | { |
307 | (void)user_data; | 307 | (void)user_data; |
308 | 308 | ||
309 | char **nv; | 309 | char **nv; |
310 | const char *name = NULL; | 310 | const char *name = NULL; |
311 | int32_t stream_id; | 311 | int32_t stream_id; |
@@ -332,7 +332,7 @@ static void on_ctrl_recv_callback(spdylay_session *session, | |||
332 | spdylay_frame *frame, void *user_data) | 332 | spdylay_frame *frame, void *user_data) |
333 | { | 333 | { |
334 | (void)user_data; | 334 | (void)user_data; |
335 | 335 | ||
336 | struct Request *req; | 336 | struct Request *req; |
337 | char **nv; | 337 | char **nv; |
338 | const char *name = NULL; | 338 | const char *name = NULL; |
@@ -402,7 +402,7 @@ static void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags, | |||
402 | { | 402 | { |
403 | (void)user_data; | 403 | (void)user_data; |
404 | (void)flags; | 404 | (void)flags; |
405 | 405 | ||
406 | struct Request *req; | 406 | struct Request *req; |
407 | req = spdylay_session_get_stream_user_data(session, stream_id); | 407 | req = spdylay_session_get_stream_user_data(session, stream_id); |
408 | if(req) { | 408 | if(req) { |
@@ -426,7 +426,7 @@ static void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags, | |||
426 | } else { | 426 | } else { |
427 | /* TODO add support gzip */ | 427 | /* TODO add support gzip */ |
428 | fwrite(data, 1, len, stdout); | 428 | fwrite(data, 1, len, stdout); |
429 | 429 | ||
430 | //check if the data is correct | 430 | //check if the data is correct |
431 | if(strcmp(RESPONSE_BODY, (char *)data) != 0) | 431 | if(strcmp(RESPONSE_BODY, (char *)data) != 0) |
432 | killparent(parent, "\nreceived data is not the same"); | 432 | killparent(parent, "\nreceived data is not the same"); |
@@ -464,7 +464,7 @@ static int select_next_proto_cb(SSL* ssl, | |||
464 | void *arg) | 464 | void *arg) |
465 | { | 465 | { |
466 | (void)ssl; | 466 | (void)ssl; |
467 | 467 | ||
468 | int rv; | 468 | int rv; |
469 | uint16_t *spdy_proto_version; | 469 | uint16_t *spdy_proto_version; |
470 | /* spdylay_select_next_protocol() selects SPDY protocol version the | 470 | /* spdylay_select_next_protocol() selects SPDY protocol version the |
@@ -667,6 +667,8 @@ static void fetch_uri(const struct URI *uri) | |||
667 | 667 | ||
668 | /* Establish connection and setup SSL */ | 668 | /* Establish connection and setup SSL */ |
669 | fd = connect_to(req.host, req.port); | 669 | fd = connect_to(req.host, req.port); |
670 | if (-1 == fd) | ||
671 | abort (); | ||
670 | ssl_ctx = SSL_CTX_new(SSLv23_client_method()); | 672 | ssl_ctx = SSL_CTX_new(SSLv23_client_method()); |
671 | if(ssl_ctx == NULL) { | 673 | if(ssl_ctx == NULL) { |
672 | dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL)); | 674 | dief("SSL_CTX_new", ERR_error_string(ERR_get_error(), NULL)); |
@@ -811,32 +813,32 @@ static int parse_uri(struct URI *res, const char *uri) | |||
811 | /***** | 813 | /***** |
812 | * end of code needed to utilize spdylay | 814 | * end of code needed to utilize spdylay |
813 | */ | 815 | */ |
814 | 816 | ||
815 | 817 | ||
816 | /***** | 818 | /***** |
817 | * start of code needed to utilize microspdy | 819 | * start of code needed to utilize microspdy |
818 | */ | 820 | */ |
819 | 821 | ||
820 | void | 822 | void |
821 | new_session_callback (void *cls, | 823 | new_session_callback (void *cls, |
822 | struct SPDY_Session * session) | 824 | struct SPDY_Session * session) |
823 | { | 825 | { |
824 | char ipstr[1024]; | 826 | char ipstr[1024]; |
825 | 827 | ||
826 | struct sockaddr *addr; | 828 | struct sockaddr *addr; |
827 | socklen_t addr_len = SPDY_get_remote_addr(session, &addr); | 829 | socklen_t addr_len = SPDY_get_remote_addr(session, &addr); |
828 | 830 | ||
829 | if(!addr_len) | 831 | if(!addr_len) |
830 | { | 832 | { |
831 | printf("SPDY_get_remote_addr"); | 833 | printf("SPDY_get_remote_addr"); |
832 | abort(); | 834 | abort(); |
833 | } | 835 | } |
834 | 836 | ||
835 | if(strcmp(CLS,cls)!=0) | 837 | if(strcmp(CLS,cls)!=0) |
836 | { | 838 | { |
837 | killchild(child,"wrong cls"); | 839 | killchild(child,"wrong cls"); |
838 | } | 840 | } |
839 | 841 | ||
840 | if(AF_INET == addr->sa_family) | 842 | if(AF_INET == addr->sa_family) |
841 | { | 843 | { |
842 | struct sockaddr_in * addr4 = (struct sockaddr_in *) addr; | 844 | struct sockaddr_in * addr4 = (struct sockaddr_in *) addr; |
@@ -845,7 +847,7 @@ new_session_callback (void *cls, | |||
845 | killchild(child,"inet_ntop"); | 847 | killchild(child,"inet_ntop"); |
846 | } | 848 | } |
847 | printf("New connection from: %s:%i\n", ipstr, ntohs(addr4->sin_port)); | 849 | printf("New connection from: %s:%i\n", ipstr, ntohs(addr4->sin_port)); |
848 | 850 | ||
849 | loop = 0; | 851 | loop = 0; |
850 | } | 852 | } |
851 | #if HAVE_INET6 | 853 | #if HAVE_INET6 |
@@ -857,7 +859,7 @@ new_session_callback (void *cls, | |||
857 | killchild(child,"inet_ntop"); | 859 | killchild(child,"inet_ntop"); |
858 | } | 860 | } |
859 | printf("New connection from: %s:%i\n", ipstr, ntohs(addr6->sin6_port)); | 861 | printf("New connection from: %s:%i\n", ipstr, ntohs(addr6->sin6_port)); |
860 | 862 | ||
861 | loop = 0; | 863 | loop = 0; |
862 | } | 864 | } |
863 | #endif | 865 | #endif |
@@ -870,7 +872,7 @@ new_session_callback (void *cls, | |||
870 | /***** | 872 | /***** |
871 | * end of code needed to utilize microspdy | 873 | * end of code needed to utilize microspdy |
872 | */ | 874 | */ |
873 | 875 | ||
874 | //child process | 876 | //child process |
875 | void | 877 | void |
876 | childproc(int parent) | 878 | childproc(int parent) |
@@ -879,7 +881,7 @@ childproc(int parent) | |||
879 | struct sigaction act; | 881 | struct sigaction act; |
880 | int rv; | 882 | int rv; |
881 | char *uristr; | 883 | char *uristr; |
882 | 884 | ||
883 | memset(&act, 0, sizeof(struct sigaction)); | 885 | memset(&act, 0, sizeof(struct sigaction)); |
884 | act.sa_handler = SIG_IGN; | 886 | act.sa_handler = SIG_IGN; |
885 | sigaction(SIGPIPE, &act, 0); | 887 | sigaction(SIGPIPE, &act, 0); |
@@ -909,14 +911,14 @@ parentproc(int child) | |||
909 | fd_set except_fd_set; | 911 | fd_set except_fd_set; |
910 | int maxfd = -1; | 912 | int maxfd = -1; |
911 | struct SPDY_Daemon *daemon; | 913 | struct SPDY_Daemon *daemon; |
912 | 914 | ||
913 | SPDY_init(); | 915 | SPDY_init(); |
914 | 916 | ||
915 | daemon = SPDY_start_daemon(port, | 917 | daemon = SPDY_start_daemon(port, |
916 | DATA_DIR "cert-and-key.pem", | 918 | DATA_DIR "cert-and-key.pem", |
917 | DATA_DIR "cert-and-key.pem", | 919 | DATA_DIR "cert-and-key.pem", |
918 | &new_session_callback,NULL,NULL,NULL,CLS,SPDY_DAEMON_OPTION_END); | 920 | &new_session_callback,NULL,NULL,NULL,CLS,SPDY_DAEMON_OPTION_END); |
919 | 921 | ||
920 | if(NULL==daemon){ | 922 | if(NULL==daemon){ |
921 | printf("no daemon\n"); | 923 | printf("no daemon\n"); |
922 | return 1; | 924 | return 1; |
@@ -939,14 +941,14 @@ parentproc(int child) | |||
939 | timeout.tv_sec = timeoutlong / 1000; | 941 | timeout.tv_sec = timeoutlong / 1000; |
940 | timeout.tv_usec = (timeoutlong % 1000) * 1000; | 942 | timeout.tv_usec = (timeoutlong % 1000) * 1000; |
941 | } | 943 | } |
942 | 944 | ||
943 | maxfd = SPDY_get_fdset (daemon, | 945 | maxfd = SPDY_get_fdset (daemon, |
944 | &read_fd_set, | 946 | &read_fd_set, |
945 | &write_fd_set, | 947 | &write_fd_set, |
946 | &except_fd_set); | 948 | &except_fd_set); |
947 | 949 | ||
948 | ret = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout); | 950 | ret = select(maxfd+1, &read_fd_set, &write_fd_set, &except_fd_set, &timeout); |
949 | 951 | ||
950 | switch(ret) { | 952 | switch(ret) { |
951 | case -1: | 953 | case -1: |
952 | printf("select error: %i\n", errno); | 954 | printf("select error: %i\n", errno); |
@@ -964,18 +966,18 @@ parentproc(int child) | |||
964 | while(loop && waitpid(child,&childstatus,WNOHANG) != child); | 966 | while(loop && waitpid(child,&childstatus,WNOHANG) != child); |
965 | 967 | ||
966 | SPDY_stop_daemon(daemon); | 968 | SPDY_stop_daemon(daemon); |
967 | 969 | ||
968 | SPDY_deinit(); | 970 | SPDY_deinit(); |
969 | 971 | ||
970 | if(loop) | 972 | if(loop) |
971 | return WEXITSTATUS(childstatus); | 973 | return WEXITSTATUS(childstatus); |
972 | if(waitpid(child,&childstatus,WNOHANG) == child) | 974 | if(waitpid(child,&childstatus,WNOHANG) == child) |
973 | return WEXITSTATUS(childstatus); | 975 | return WEXITSTATUS(childstatus); |
974 | 976 | ||
975 | kill(child,SIGKILL); | 977 | kill(child,SIGKILL); |
976 | 978 | ||
977 | waitpid(child,&childstatus,0); | 979 | waitpid(child,&childstatus,0); |
978 | 980 | ||
979 | return 0; | 981 | return 0; |
980 | } | 982 | } |
981 | 983 | ||
@@ -983,21 +985,21 @@ int main() | |||
983 | { | 985 | { |
984 | port = get_port(14123); | 986 | port = get_port(14123); |
985 | parent = getpid(); | 987 | parent = getpid(); |
986 | 988 | ||
987 | child = fork(); | 989 | child = fork(); |
988 | if (child == -1) | 990 | if (child == -1) |
989 | { | 991 | { |
990 | fprintf(stderr, "can't fork, error %d\n", errno); | 992 | fprintf(stderr, "can't fork, error %d\n", errno); |
991 | exit(EXIT_FAILURE); | 993 | exit(EXIT_FAILURE); |
992 | } | 994 | } |
993 | 995 | ||
994 | if (child == 0) | 996 | if (child == 0) |
995 | { | 997 | { |
996 | childproc(parent); | 998 | childproc(parent); |
997 | _exit(0); | 999 | _exit(0); |
998 | } | 1000 | } |
999 | else | 1001 | else |
1000 | { | 1002 | { |
1001 | int ret = parentproc(child); | 1003 | int ret = parentproc(child); |
1002 | exit(ret); | 1004 | exit(ret); |
1003 | } | 1005 | } |