command_server.c 4.97 KB
Newer Older
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
/*
	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/>.

*/

#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);
43
	int (*handle_raw_func)(char *command);
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
};


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)
{
81
82
	struct command_client *client;

83
	if (srv) {
84
85
86
87
88
89
90
91
		while (srv->clients) {
			client = srv->clients;
			srv->clients = client->next;
			
			close(client->fd);
			free(client);
		}
	
92
93
94
95
96
97
98
99
100
		close(srv->fd);
		free(srv);
	}
}


int command_server_handler_set(struct command_server *srv, 
    int (*func)(char *name, char *value))
{
101
102
	/* Remember who to call when a command is received */
	
103
104
105
106
107
	srv->handle_func = func;
	
	return 0;
}

108
109
110
111
112
113
114
115
116
117
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;
}

118
119
int command_server_fdset_add(struct command_server *srv, fd_set *set, int *high)
{
120
121
	/* Add all our sockets to the set 
	 */
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
	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)
{
139
140
141
142
143
144
145
	/* 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.
	 */
146
147
148
149
150
	char *namevalue;
	char *name;
	char *value;
	char *ptr;
	
151
152
153
154
155
156
157
158
159
160
161
	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);
	}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
}

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) {
179
180
181
					/* Fatal error on socket to client.
					   Clean up client
					 */
182
183
184
185
186
187
					client = *clientp;
					close(client->fd);
					*clientp = (*clientp)->next;
					free(client);
					nextp = clientp;
				} else if (ret == 1) {
188
189
					/* Data from client received.
					 */
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
					(*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;	
}