summaryrefslogtreecommitdiff
path: root/tests/network/test1.c
blob: e37f4cb1c4c8ed629f7dddc48a7a2fdbcb968bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "port/netdb.h"			/* for getaddrinfo */
#include "port/stdlib.h"		/* for EXIT_XXX */
#include "port/stdio.h"			/* for fprintf */
#include "port/string.h"		/* for memset */
#include "port/stdbool.h"		/* for bool, true, false */

#include <unistd.h>			/* for close */
#include <errno.h>			/* for errno */
#include <fcntl.h>			/* for fcntl */
#include <sys/select.h>			/* for select */

static void wolf_network_sock_nonblocking( int fd ) {
	int flags;

	flags = fcntl( fd, F_GETFL, 0 /* ignored */ );
	flags |= O_NONBLOCK;
	flags = fcntl( fd, F_SETFL, flags );
}

#define MAX_IDLE_TIMEOUT 10

#define max(a,b) ((a) < (b) ? (b) : (a))

int main( int argc, char *argv[] ) {
	char *host;
	char *service;
	struct addrinfo hints;
	struct addrinfo *result;
	int error;
	int fd;
	int res;
	int idle_secs;

	if( argc != 3 ) {
		fprintf( stderr, "usage: test1 <host> <port>\n" );
		goto FAIL;
	}

	host = argv[1];
	service = argv[2];

#ifdef _WIN32
	WSADATA wsa_data;
	WSAStartup( MAKEWORD( 2, 2 ), &wsa_data );
#endif

	/* tell getaddrinfo what we want */
	memset( &hints, 0, sizeof( struct addrinfo ) );
	hints.ai_flags = AI_PASSIVE;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
 
	/* resolve the domain name into a list of addresses */
	error = getaddrinfo( host, service, &hints, &result );
	if( error != 0 ) {
		fprintf( stderr, "getaddrinfo failed: %s (%d)\n",
			gai_strerror( error ), error );
		goto FAIL;
	}

	fd = socket( result->ai_family, result->ai_socktype, result->ai_protocol );
	if( fd < 0 ) {
		fprintf( stderr, "socket failed: %s (%d)\n", strerror( errno ), errno );
		goto FAIL;
	}

	res = connect( fd, result->ai_addr, result->ai_addrlen );
	if( res < 0 ) {
		(void)close( fd );
		fprintf( stderr, "connect failed: %s (%d)\n", strerror( errno ), errno );
		goto FAIL;
	}

	wolf_network_sock_nonblocking( STDIN_FILENO );
	wolf_network_sock_nonblocking( STDOUT_FILENO );
	wolf_network_sock_nonblocking( fd );

	idle_secs = 0;

	do {
		int max_fd;
		fd_set read_set;
		fd_set write_set;
		fd_set error_set;
		struct timeval timeout;

		FD_ZERO( &read_set );
		FD_ZERO( &write_set );
		FD_ZERO( &error_set );
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;

		/* set up select mask and compute highest file descriptor number */
		max_fd = 0;
		max_fd = max( max_fd, STDIN_FILENO );
		max_fd = max( max_fd, STDOUT_FILENO );
		max_fd = max( max_fd, fd );

		FD_SET( STDIN_FILENO, &read_set );
		FD_SET( fd, &read_set );
		FD_SET( STDOUT_FILENO, &write_set );
		FD_SET( fd, &write_set );
		FD_SET( STDIN_FILENO, &error_set );
		FD_SET( STDOUT_FILENO, &error_set );
		FD_SET( fd, &error_set );
		
		res = select( max_fd + 1, &read_set, &write_set, &error_set, &timeout );
		if( res < 0 ) {
			if( errno == EINTR || errno == EAGAIN ) {
				/* skip */
			} else {
				/* fatal errors */
				fprintf( stderr, "select failed: %s (%d)\n",
					strerror( errno ), errno );
				(void)close( fd );
				goto FAIL;
			}
		} else if( res == 0 ) {
			/* timeout */
			idle_secs++;
			if( idle_secs > MAX_IDLE_TIMEOUT ) {
				fprintf( stderr, "Idle connection after %d seconds..terminating\n",
					idle_secs );
				goto END;
			}
		} else {
			/* something happened */
			idle_secs = 0;

			if( FD_ISSET( STDOUT_FILENO, &write_set ) ) {
//				res = write( STDOUT_FILENO, write_buf
			}
		}
	} while( true );

END:
	res = close( fd );
	if( res < 0 ) {
		fprintf( stderr, "close failed: %s (%d)\n", strerror( errno ), errno );
		goto FAIL;
	}
 
	freeaddrinfo( result );

	goto OK;

FAIL:
#ifdef _WIN32
	WSACleanup( );
#endif

	return EXIT_FAILURE;

OK:
	return EXIT_SUCCESS;
}