command_server.c 5.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
	Command server

	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2013

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	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.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 */
/*
	command_server.c
	
	library for opening a tcp listening socket on which clients can
	send commands.
	
	commands can be in the form of 'key=value,key=value,key=value' which
	can be handled by registering a handler function with
	command_server_handler_set().
	
	If commands are in another format they can be handled by registering
	a handler function with command_server_handler_raw_set()
	
 */
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "tcp_listen.h"
 
struct command_client {
	struct command_client *next;
	
	int fd;
	char buffer[200];
	int buflen;
};

struct command_server {
	int fd;

	struct command_client *clients;
	int (*handle_func)(char *name, char *value);
57
	int (*handle_raw_func)(char *command);
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
};


static int default_func(char *name, char *value)
{
	printf("No command handler installed!!!");
	
	return -1;
}


struct command_server *command_server_create(int port, int local, int backlog)
{
	struct command_server *srv;
	
	srv = calloc(1, sizeof(struct command_server));
	if (!srv)
		goto err_alloc;
	
	srv->fd = tcp_listen(port, local, backlog);
	if (srv->fd < 0)
		goto err_listen;
	
	ioctl(srv->fd, FIONBIO, &(int){ 1 });
	
	srv->handle_func = default_func;
	
	return srv;

err_listen:
	close(srv->fd);
err_alloc:
	return NULL;
}

void command_server_destroy(struct command_server *srv)
{
95
96
	struct command_client *client;

97
	if (srv) {
98
99
100
101
102
103
104
105
		while (srv->clients) {
			client = srv->clients;
			srv->clients = client->next;
			
			close(client->fd);
			free(client);
		}
	
106
107
108
109
110
111
112
113
114
		close(srv->fd);
		free(srv);
	}
}


int command_server_handler_set(struct command_server *srv, 
    int (*func)(char *name, char *value))
{
115
116
	/* Remember who to call when a command is received */
	
117
118
119
120
121
	srv->handle_func = func;
	
	return 0;
}

122
123
124
125
126
127
128
129
130
131
int command_server_handler_raw_set(struct command_server *srv, 
    int (*func)(char *command))
{
	/* Remember who to call when a command is received */
	
	srv->handle_raw_func = func;
	
	return 0;
}

132
133
int command_server_fdset_add(struct command_server *srv, fd_set *set, int *high)
{
134
135
	/* Add all our sockets to the set 
	 */
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
	struct command_client *client;
	
	for (client = srv->clients; client; client = client->next) {
		FD_SET(client->fd, set);
		if (client->fd >= *high)
			*high = client->fd + 1;
	}
	
	FD_SET(srv->fd, set);
	if (srv->fd >= *high)
		*high = srv->fd + 1;
		
	return 0;
}

void command_server_handle(struct command_server *srv, struct command_client *client)
{
153
154
155
156
157
158
159
	/* A command has been received.
	   Split it in a key, value pair and pass it to application handler
	   
	   Some applications have a command format which is not (completly) in
	   a key=value format. They can get the 'raw' string and still enjoy
	   the other benefits of this lib.
	 */
160
161
162
163
164
	char *namevalue;
	char *name;
	char *value;
	char *ptr;
	
165
166
167
168
169
170
171
172
173
174
175
	if (srv->handle_raw_func) {
		srv->handle_raw_func(client->buffer);
	} else {
		do {
			namevalue = client->buffer;
			name = strtok_r(namevalue, ",=", &ptr);
			value = strtok_r(NULL, ",=", &ptr);
			if (name && value)
				srv->handle_func(name, value);
		} while (name && value);
	}
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
}

int command_server_fdset_handle(struct command_server *srv, fd_set *set)
{
	struct command_client *client, **clientp, **nextp;

	for (clientp = &srv->clients; *clientp; clientp = nextp) {
	
		nextp = &(*clientp)->next;
		
		if (FD_ISSET((*clientp)->fd, set)) {
			/* received something */
			int ret;

			do {
				ret = read((*clientp)->fd, (*clientp)->buffer+(*clientp)->buflen, 1);
				if ((ret < 0 && errno != EAGAIN) || ret == 0) {
193
194
195
					/* Fatal error on socket to client.
					   Clean up client
					 */
196
197
198
199
200
201
					client = *clientp;
					close(client->fd);
					*clientp = (*clientp)->next;
					free(client);
					nextp = clientp;
				} else if (ret == 1) {
202
203
					/* Data from client received.
					 */
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
					(*clientp)->buflen += 1;
					if ((*clientp)->buffer[(*clientp)->buflen-1] == '\n' ||
					    (*clientp)->buffer[(*clientp)->buflen-1] == '\r') {
						if ((*clientp)->buflen > 1) {
							(*clientp)->buffer[(*clientp)->buflen] = 0;
							command_server_handle(srv, *clientp);
							(*clientp)->buffer[0] = '9';
						}
						(*clientp)->buflen = 0;
					}
					if ((*clientp)->buflen >= 200)
						(*clientp)->buflen = 0;
				}
			} while (ret > 0);
		}
	}

	if (FD_ISSET(srv->fd, set)) {
		/* New client */
		int client_fd;
		
		client_fd = tcp_accept(srv->fd);
		if (client_fd < 0)
			return -1;
		ioctl(client_fd, FIONBIO, &(int){ 1 });
		
		client = calloc(1, sizeof(struct command_client));
		if (!client) {
			close(client_fd);
		} else {
			client->fd = client_fd;
			client->buflen = 0;
			client->next = srv->clients;
			srv->clients = client;
		}
	}
	return 0;	
}