-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_echoserver.c
101 lines (88 loc) · 2.17 KB
/
example_echoserver.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
/*
* This file is part of libsimplenet.
*
* Copyright (C) 2010 Greg Leclercq <ggl@0x80.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 or version 3.0 only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include <errno.h>
#include <syslog.h>
#include <stdio.h>
#include "network_socket.h"
#include "network_server.h"
/** Simple echo callback.
*
*/
int
client_callback_do_request(void *data,
struct simple_buffer *bufwrite,
struct simple_buffer *bufread,
int *done)
{
simple_buffer_append(bufwrite, (char *) data, strlen(data));
simple_buffer_append(bufwrite,
simple_buffer_get_head(bufread),
simple_buffer_size(bufread));
simple_buffer_clear(bufread);
*done = 1;
return 0;
}
void
usage(char **argv)
{
fprintf(stderr, "%s tcp <ip> <port>\n", argv[0]);
fprintf(stderr, "%s unix <path_to_socket>\n", argv[0]);
}
int
main(int argc, char **argv)
{
if (argc < 3) {
usage(argv);
exit(EINVAL);
}
int socket_type;
void *conf;
struct socket_config_unix unixcf;
if (strncmp("unix", argv[1], 4) == 0) {
unixcf.path = "/tmp/simpleserver.socket";
unixcf.backlog = 2;
socket_type = SOCKET_UNIX;
conf = &unixcf;
};
struct socket_config_tcp tcpcf;
if (strncmp("tcp", argv[1], 3) == 0) {
if (argc < 4) {
usage(argv);
exit(EINVAL);
}
tcpcf.ip = "127.0.0.1";
tcpcf.port = 12345;
tcpcf.backlog = 2;
conf = &tcpcf;
socket_type = SOCKET_TCP;
};
struct server *server = server_new(socket_type, 2);
if (server == NULL)
exit(errno);
struct server_callbacks callbacks = {
.log = syslog,
.accept = NULL,
.do_request = client_callback_do_request
};
int err = server_init(server, &callbacks, "hello, ", SERVER_NONBLOCKING);
if (err) goto fail_server_init;
server_listen(server, conf);
fail_server_init:
server_stop(server, err);
exit(err);
}
/* vim: ts=8:sw=8:noet
*/