-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
187 lines (159 loc) · 3.6 KB
/
server.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* HTTP/2 minimalistic server
*/
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <event2/event.h>
#include "defines.h"
#include "http2.h"
#include "util.h"
#include "server.h"
/* libevent's structures */
struct event_base *evbase;
struct event *evsock;
static void usage(void);
int
main(int argc, char *argv[])
{
int sockfd;
int r;
char *server_port = SERVER_PORT_DEFAULT;
char ch;
/* Parse arguments */
while ((ch = getopt(argc, argv, "hp:a:")) != -1) {
switch (ch) {
case 'p':
server_port = optarg;
break;
case 'h':
default:
usage();
}
}
printf("HTTP/2 server\n");
/* Opens the listening socket */
sockfd = server_listen(server_port);
if (sockfd < 0) {
prterr("server_listen: failure.");
exit(1);
}
/* Creates libevent event_base struct */
evbase = event_base_new();
if (evbase == NULL) {
close(sockfd);
prterr("event_base_new: failure.");
exit(1);
}
/* Creates an event notification for the listening socket */
evsock = event_new(evbase, sockfd, EV_READ, server_accept, NULL);
if (evsock == NULL) {
close(sockfd);
event_base_free(evbase);
prterr("event_new: failure.");
exit(1);
}
if (event_add(evsock, NULL) < 0) {
close(sockfd);
event_base_free(evbase);
prterr("event_add: failure.");
exit(1);
}
/* Dispatch events */
r = event_base_dispatch(evbase);
if (r < 0)
prterr("event_base_dispatch: failure.");
close(sockfd);
event_base_free(evbase);
return 0;
}
void
server_accept(evutil_socket_t fd, short events, void *arg)
{
struct http2_connection *conn;
struct sockaddr_in addr;
char ip[INET_ADDRSTRLEN];
socklen_t addrlen;
int connfd;
/* Rearms the listening socket's event */
event_add(evsock, NULL);
/* Accepts incomming connection */
addrlen = sizeof(addr);
connfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
if (connfd < 0) {
prterr("accept: failed to accept an incoming connection.");
return;
}
/* Prints information on accepted connection */
inet_ntop(AF_INET, &addr.sin_addr, ip, sizeof(ip));
prtinfo("(%d) new connection received from %s:%d\n",
connfd, ip, ntohs(addr.sin_port));
/* Creates a new connection object */
conn = http2_connection_new(connfd, evbase);
if (conn == NULL) {
prterr("http2_connection_new: failure.");
close(connfd);
return;
}
/* Sends preface: first SETTINGS frame */
if (http2_settings_send(conn, NULL, 0) < 0) {
prterr("http2_settings_send: failure.");
http2_connection_free(conn);
return;
}
}
int
server_listen(char *port)
{
struct sockaddr_in *sin;
struct addrinfo h, *ai;
char ip[INET_ADDRSTRLEN];
int e;
int fd;
/* Gets local address */
memset(&h, 0, sizeof(h));
h.ai_family = AF_INET;
h.ai_socktype = SOCK_STREAM;
h.ai_flags = AI_PASSIVE;
e = getaddrinfo(NULL, port, &h, &ai);
if (e) {
prterr("getaddrinfo: %s.", gai_strerror(e));
exit(1);
}
if (ai == NULL) {
prterr("getaddrinfo: no address found.");
exit(1);
}
/* Opens the socket for listening */
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0) {
perror("socket");
exit(1);
}
if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
perror("bind");
exit(1);
}
if (listen(fd, 10) < 0) {
perror("listen");
exit(1);
}
sin = (struct sockaddr_in *)ai->ai_addr;
inet_ntop(AF_INET, &sin->sin_addr, ip, sizeof(ip));
printf("listening on %s:%d...\n", ip, ntohs(sin->sin_port));
return fd;
}
static void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-p port]\n", __progname);
exit(1);
}