aboutsummaryrefslogtreecommitdiff
path: root/test/bsd_socket.c
blob: e03f8821dba9d4f74e0656d30fedb83e06c15c8c (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
#define _BSD_SOURCE

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>

#include "../picohttp.h"

int bsdsock_read(size_t count, char *buf, void *data)
{
	int fd = *((int*)data);
	
	ssize_t rb = 0;
	ssize_t r = 0;
	do {
		r = read(fd, buf+rb, count-rb);
		if( 0 < r ) {
			rb += r;
			continue;
		}
		if( !r ) {
			break;
		}

		if( EAGAIN == errno ||
		    EWOULDBLOCK == errno ) {
			usleep(100);
			continue;
		}
		return -3 + errno;
	} while( rb < count );
	return rb;
}

int bsdsock_write(size_t count, char *buf, void *data)
{
	int fd = *((int*)data);
	
	ssize_t wb = 0;
	ssize_t w = 0;
	do {
		w = write(fd, buf+wb, count-wb);
		if( 0 < w ) {
			wb += w;
			continue;
		}
		if( !w ) {
			break;
		}

		if( EAGAIN == errno ||
		    EWOULDBLOCK == errno ) {
			usleep(100);
			continue;
		}
		return -3 + errno;
	} while( wb < count );
	return wb;
}

int16_t bsdsock_getch(void *data)
{
	char ch;
	if( 1 != bsdsock_read(1, &ch, data) )
		return -1;
	return ch;
}

int bsdsock_putch(char ch, void *data)
{
	return bsdsock_write(1, &ch, data);
}

int sockfd = -1;

void bye(void)
{
	fputs("exiting\n", stderr);
	int const one = 1;
	/* allows for immediate reuse of address:port
	 * after program termination */
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
	shutdown(sockfd, SHUT_RDWR);
	close(sockfd);
}

int main(int argc, char *argv[])
{
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if( -1 == sockfd ) {
		perror("socket");
		return -1;
	}
	if( atexit(bye) ) {
		return -1;
	}

	struct sockaddr_in addr = {
		.sin_family = AF_INET,
		.sin_port   = htons(8000),
		.sin_addr   = 0
	};

	if( -1 == bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) ) {
		perror("bind");
		return -1;
	}

	if( -1 == listen(sockfd, 2) ) {
		perror("listen");
		return -1;
	}

	for(;;) {
		socklen_t addrlen = 0;
		int confd = accept(sockfd, (struct sockaddr*)&addr, &addrlen);
		if( -1 == confd ) {
			if( EAGAIN == errno ||
			    EWOULDBLOCK == errno ) {
				usleep(1000);
				continue;
			} else {
				perror("accept");
				return -1;
			}
		}

		struct picohttpIoOps ioops = {
			.read  = bsdsock_read,
			.write = bsdsock_write,
			.getch = bsdsock_getch,
			.putch = bsdsock_putch,
			.data = &confd
		};

		char const hellostr[] = "Hello World!\n";
		write(confd, hellostr, sizeof(hellostr));
		shutdown(confd, SHUT_RDWR);
		close(confd);
	}

	return 0;
}