-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho-server.c
More file actions
276 lines (230 loc) · 6.15 KB
/
echo-server.c
File metadata and controls
276 lines (230 loc) · 6.15 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define READBUF_SIZE 32
#define LISTEN_BACKLOG 2
#define DEFAULT_LISTEN_PORT 8001
#define SUCCESS (0)
#define FAILURE (-1)
struct client_conn {
int sockfd;
struct sockaddr_in sockaddr;
FILE *file_out;
char ipaddr_string[INET_ADDRSTRLEN];
};
static const char *sockaddr_to_string(struct sockaddr *sockaddr, char *buf,
int size)
{
struct in_addr *ipv4;
struct in6_addr *ipv6;
const char *ret = NULL;
switch(sockaddr->sa_family) {
case AF_INET:
ipv4 = &((struct sockaddr_in *)sockaddr)->sin_addr;
ret = inet_ntop(AF_INET, ipv4, buf, size);
break;
case AF_INET6:
ipv6 = &((struct sockaddr_in6 *)sockaddr)->sin6_addr;
ret = inet_ntop(AF_INET6, ipv6, buf, size);
break;
default:
ret = NULL;
}
return ret;
}
/**
* Read data from the socket fd and echo it back to the same `sockfd`
* `file_out`, if not NULL, is used for logging the data
* `buf` temporarily holds read data before echoing back
*/
static void do_echo(const struct client_conn *conn, char *buf, int size)
{
const char *ipaddr_string = conn->ipaddr_string;
FILE *fout = conn->file_out;
int sockfd = conn->sockfd;
int read_bytes;
int sent_bytes;
int i;
if (sockfd < 0 || buf == NULL || size < 1)
return;
while ((read_bytes = recv(sockfd, buf, size, 0)) > 0) {
if (fout)
fwrite(buf, 1, read_bytes, fout);
/* repeatedly try until we send out all the
* `read_bytes` */
for (sent_bytes = 0; sent_bytes != read_bytes;
sent_bytes += i) {
if ((i = send(sockfd, buf + sent_bytes,
read_bytes - sent_bytes, 0)) < 0) {
goto err_write;
}
}
}
if (read_bytes != 0)
fprintf(stderr, "Read error:%d client %s:%d\n", errno,
ipaddr_string, conn->sockaddr.sin_port);
return;
err_write:
fprintf(stderr, "Write error errno:%d client %s:%d\n", errno,
ipaddr_string, conn->sockaddr.sin_port);
return;
}
static void *start_server_thread(void *client)
{
const struct client_conn *conn = client;
const int size = READBUF_SIZE;
char *buf = malloc(size);
if (client == NULL)
goto err;
if (buf == NULL) {
fprintf(stderr, "Memory allocation failed\n");
goto err;
}
printf("Accepted a connection from %s:%d\n", conn->ipaddr_string,
conn->sockaddr.sin_port);
/* Start echoing */
do_echo(conn, buf, size);
printf("Connection closed %s:%d\n", conn->ipaddr_string,
conn->sockaddr.sin_port);
close(conn->sockfd);
err:
free(client);
free(buf);
return NULL;
}
static struct client_conn *
init_client_conn(int sockfd, struct sockaddr *sockaddr, FILE *file_out)
{
struct client_conn *client = malloc(sizeof(*client));
if (client == NULL)
return NULL;
client->sockfd = sockfd;
client->file_out = file_out;
memcpy(&client->sockaddr, sockaddr, sizeof(*sockaddr));
sockaddr_to_string(sockaddr, client->ipaddr_string, INET_ADDRSTRLEN);
return client;
}
static int init_server(char *ipaddr, int port, struct sockaddr_in *server)
{
struct in_addr local_ip;
int server_fd = -1;
memset(server, 0, sizeof(*server));
memset(&local_ip, 0, sizeof(local_ip));
/* local IP address to lisdten on */
if (ipaddr == NULL)
ipaddr = "0.0.0.0"; /* Any address */
if (inet_pton(AF_INET, ipaddr, &local_ip) != 1) {
fprintf(stderr, "%s : Not a valid IPv4 address", ipaddr);
goto err;
}
/* Create server socket */
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Unable to create a socket. errno=%d\n", errno);
goto err;
}
server->sin_family = AF_INET;
server->sin_port = htons(port); /* local port to listen on */
server->sin_addr.s_addr = local_ip.s_addr;
if (bind(server_fd, (struct sockaddr *)server, sizeof(*server))) {
fprintf(stderr, "Socket bind failed. errno=%d\n", errno);
close(server_fd);
goto err;
}
if (listen(server_fd, LISTEN_BACKLOG)) {
fprintf(stderr, "Listen failed. errno=%d\n", errno);
close(server_fd);
goto err;
}
printf("Listening on %s:%d\n", ipaddr, port);
return server_fd;
err:
return -1;
}
struct client_conn *accept_client_conn(int server_fd, FILE *file_out)
{
struct client_conn *client = NULL;
struct sockaddr client_sock;
socklen_t sock_len;
int client_fd = -1;
sock_len = sizeof(client_sock);
memset(&client_sock, 0, sock_len);
client_fd = accept(server_fd, &client_sock, &sock_len);
if (client_fd < 0) {
fprintf(stderr, "Accept failed. errno:%d\n", errno);
goto err;
}
client = init_client_conn(client_fd, &client_sock, file_out);
if (client == NULL) {
fprintf(stderr, "Internal error:%d. closing fd=%d\n", errno,
client_fd);
close(client_fd);
}
err:
return client;
}
/**
* Create a server listening on `ipaddr`:`port`
* write data received from client to `file_out`
*/
int runserver(char *ipaddr, int port, FILE *file_out)
{
struct sockaddr_in server_sock;
struct client_conn *client;
pthread_attr_t tattr;
pthread_t tinfo;
int server_fd = -1;
server_fd = init_server(ipaddr, port, &server_sock);
if (server_fd < 0)
goto err;
if (pthread_attr_init(&tattr) != 0 ||
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0)
goto err;
while (true) {
client = accept_client_conn(server_fd, file_out);
if (client == NULL)
goto err;
memset(&tinfo, 0, sizeof(tinfo));
pthread_create(&tinfo, &tattr, start_server_thread, client);
}
err:
/* Close the listening socket */
if (server_fd > -1)
close(server_fd);
return FAILURE;
}
int main(int argc, char *argv[])
{
int port = DEFAULT_LISTEN_PORT;
char *local_ip = NULL;
FILE *file_out = NULL;
char *end;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--echo") == 0) {
/* Echo locally */
file_out = stdout;
} else if (i < argc - 1 && strcmp(argv[i], "--ip") == 0) {
/* bind to this IP address */
local_ip = argv[++i];
} else if (i == argc-1) {
/* port number. listen on this port */
errno = 0;
port = strtol(argv[i], &end, 10);
if (*end != '\0' || port == 0 || errno != 0)
goto err;
} else {
goto err;
}
}
runserver(local_ip, port, file_out);
return SUCCESS;
err:
fprintf(stderr, "Error parsing command line\n");
return FAILURE;
}