summaryrefslogtreecommitdiff
path: root/src/ivyd.c
blob: f53d6a3f6f6dcae9d745295fc6a9f2e93d0beac3 (plain)
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
277
278
279
280
281
/*
 *	IvyDaemon, an Ivy gateway for short-lived agents
 * 
 *	Copyright (C) 1999
 *	Centre d'Études de la Navigation Aérienne
 *
 *	Main file for the server
 *
 *	Author(s): Stephane Chatty <chatty@cenatoulouse.dgac.fr>
 *		from code by Patrick Amar <pa@lri.fr>
 *
 *	$Id$
 *
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <fcntl.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "ivy.h"
#include "ivyd.h"

const char* const servicename = "ivyd";
const char* const port_env_variable = "INIVYD_PORT";
const int default_port = DEFAULT_INIVYD_PORT;

int debug = 0;

static void
Usage (const char* progname)
{
	fprintf (stderr, "Usage: %s [-b bus][-d]\n", progname);
	fprintf (stderr, " -d: does not fork so as to facilitate debugging\n");
	fprintf (stderr, " -b bus: selects Ivy bus on which to relay messages\n");
	exit (1);
}


/* 
 * Procedure to register with the super-daemon
 */
int
SendPort (int key, int port)
{
	struct servent *serv;
	u_short serv_port;
	struct sockaddr_in sin;
	struct connmsg msg;
	int sock;
	int res;

	serv = getservbyname (servicename, 0);
	if (! serv) {
		char *pp = getenv (port_env_variable);
		serv_port = htons (pp ? atoi (pp) : default_port);
		if (serv_port <= 0)
			exit (0);
	} else
		serv_port = serv -> s_port;

	sock = socket (AF_INET, SOCK_DGRAM, 0);
	if (sock == -1)
		return 0;

	memset (&sin, 0, sizeof sin);
	sin.sin_family = AF_INET;
	sin.sin_port = serv_port;
	sin.sin_addr.s_addr = LOCALHOST;

#if 0
	msg.msg_key = getuid ();
#else
	msg.msg_key = key;
#endif
	msg.msg_port = port;
	msg.msg_type = MSG_SERVER;


	res = sendto (sock, (char *)&msg, sizeof msg, 0, (struct sockaddr *)&sin, sizeof sin);

	/* We only detect local errors, but we have no guarantee that there is someone
	   at the other end. We should fix that someday (by going connected?) */

	if (res == -1)
		goto failed;

	(void) close (sock);
	return 1;

failed:
	(void) close (sock);
	return 0;
}


int
InitSocket (int key)
{
	int s, i;
	struct sockaddr_in addr;

	s = socket (AF_INET, SOCK_STREAM, 0);
	if (s < 0) {
		perror ("socket");
		return 0;
	}

	memset (&addr, 0, sizeof addr);
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;

	if (bind (s, &addr, sizeof (addr)) == -1) {
		perror ("bind");
		return 0;
	}

	i = sizeof addr;
	if (getsockname (s, &addr, &i) == -1) {
		perror ("bind");
		return 0;
	}


	if (debug)
		printf ("Ivyd listens on port %d\n", ntohs (addr.sin_port));

	if (! SendPort (key, addr.sin_port)) {
		fprintf (stderr, "No Ivyd port server. Continuing with port = %d\n",
				ntohs (addr.sin_port));
	}

	/* je me debarrasse du terminal de controle */

/*	freopen ("/dev/tty", "r+", stderr); */
	for (i = 0; i < 32; i++)
		if (i != fileno (stderr) && i != s)
			close (i);

#ifndef SYSV
	i = open ("/dev/tty", 0);
	if (i != -1) {
		(void) ioctl (i, TIOCNOTTY, 0);
		close (i);
	}
#endif SYSV
	open ("/dev/null", 2); dup (0);


	if (listen (s, 5) == -1) {
		perror ("listen");
		return 0;
	}
	return s;
}


void
HandleRequests (void* ch, int fd, void* data)
{
	request req;
	char* p = (char*) &req;
	int n = sizeof (request);
	struct sockaddr_in addr;
	int len = sizeof (addr);

	if (debug)
		fprintf (stderr, "REQUEST on %d\n", fd);

	/* retrieve request */
	while (n > 0) {
#if 1
		int l = read (fd, p, n);
#else
		int l = recvfrom (fd, p, n, 0, (struct sockaddr*) &addr, &len);
#endif
		if (l < 0) {
			perror ("Error retrieving request from client");
			exit (0);
			return;
		} else if (l == 0) {
			if (debug)
				fprintf (stderr, "closing connection for %d\n", fd);
			IvyChannelClose (ch);
			close (fd);
			return;
		}
		n -= l;
		p += l;
	}

#if 0
	answer ans;
	ans.ok = 1;
	write (
#endif
	if (debug)
		fprintf (stderr, "REQUEST [%s]\n", req.buffer);
	IvySendMsg (req.buffer);
}

void
HandleConnections (void* ch, int fd, void* data)
{
	static struct sockaddr sa;
	static int acc;
	int newfd = accept (fd, &sa, &acc);
	if (debug)
		fprintf (stderr, "CONNECTION %d\n", newfd);
	IvyChannelSetUp (newfd, 0, 0, HandleRequests);
}

int
ExtractPort (const char* bus)
{
	int res = DEFAULT_BUS;
	const char* p;
	if (bus && (p = index (bus, ':'))) {
		res = atoi (p+1);
	}
	return res;
}

void
main (int argc, char **argv)
{
	int sock;
	char busbuf [128];
	int busport;
	const char* bus = 0;
	int c;

	/* handle args */
	while ((c = getopt (argc, argv, "b:d")) != EOF) {
		switch (c) {
		case 'b':
			strcpy (busbuf, optarg) ;
			bus = busbuf;
			break;
		case 'd':
			debug = 1;
			break;
		default:
			Usage (argv[0]);
		}
	}

	/* detach from tty */
	if (! debug)
		if (fork ())
			exit (0);

	/* determine port in bus address */
	busport = ExtractPort (bus);

	/* create server port */	
	sock = InitSocket (busport);

	/* this allows users to stop us cleanly */
	signal (SIGTERM, exit);

	
	/* Ivy initialization and main loop */
	IvyInit ("IVYD", 0, 0, 0, 0, 0);
	IvyChannelSetUp (sock, 0, 0, HandleConnections);
	IvyStart (bus);
	IvyMainLoop (0);
}