summaryrefslogtreecommitdiff
path: root/src/inivyd.c
blob: d5accdaa14c59735dad12b0cce2eea8994482249 (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
/*
 *	IvyDaemon, an Ivy gateway for short-lived agents
 * 
 *	Copyright (C) 1999
 *	Centre d'Études de la Navigation Aérienne
 *
 *	Main file for the super-daemon
 *
 *	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
 *
 */

/*
 * The super-demon (in.ivyd):
 *
 *     It is either launched by inetd with the socket SOCK_DGRAM on file descriptor 0,
 *     or at boot time with the -boot flag.
 *
 * The daemon (ivyd):
 *	
 *     It sends the super-daemon a message connmsg of type MSG_SERVER that contains
 *     the port number of the socket it connected to.
 *
 * The clients (ivyecho, ...):
 *
 *     They send the super-daemon a message connmsg of type MSG_CLIENT 
 *     to ask for the port number
 *
 * L'identification de l'emetteur du paquet est le numero d'utilisateur.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <netdb.h>
#include <errno.h>

#include "ivyd.h"


const char* const dumpfilename = "/tmp/in.ivyd.db";
const int dumpfilemode = 0644;
const char* const servicename = "ivyd";
const char* const port_env_variable = "INIVYD_PORT";
const int default_port = DEFAULT_INIVYD_PORT;

/*
 * The association table managed by the super-daemon
 */

typedef struct {
	short	s_port;	/* port number */
	short	s_key;	/* server id */
} serverinfo;

#define TABSIZ 64	/* 64 simultaneous servers are allowed */
serverinfo	si_table [TABSIZ];

serverinfo*
GetInfo (int key)
{
	register serverinfo *si;
	register serverinfo *res = 0;

	for (si = si_table; si - si_table < TABSIZ; ++si) {
		if (si->s_port == 0) {
			if (! res)
				res = si;
			continue;
		}
		if (si->s_key != key)
			continue;
		res = si;
		break;
	}
	return res;
}

/*
 * DumpInfo ()
 *
 * Dumps to a file the contents of the table
 */

int
DumpInfo ()
{
	register serverinfo *si;
	register int fd = creat (dumpfilename, dumpfilemode);
	char buf [512];

	if (fd == -1)
		return 0;

	for (si = si_table; si - si_table < TABSIZ; ++si) {
		if (! si->s_port)
			continue;
		(void) sprintf (buf, "user %5d, port %d\n", si->s_key, ntohs (si->s_port));
		(void) write (fd, buf, strlen (buf));
	}
	(void) close (fd);
	return 1;
}


void
SigHup (int sig)
{
	DumpInfo ();
}

void
main (int argc, const char **argv)
{
	struct servent *serv;
	u_short serv_port = 0;
	struct sockaddr_in from;
	int fromlen;
	struct connmsg msg;
	register serverinfo *si;
	int boot = 0;
	int debug = 0;

	(void) signal (SIGHUP, SigHup);
	
	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;
	
	++argv;
	while (argc > 1) {
		if (strcmp (*argv, "-boot") == 0 || strcmp (*argv, "-b") == 0)
			boot = 1;
		else if (strcmp (*argv, "-debug") == 0 || strcmp (*argv, "-d") == 0)
			debug = 1;
		else
			fprintf (stderr, "bad option %s\n", *argv);
		++argv;
		--argc;
	}

	/* special case when launched at boot time (ie. not from inetd) */

	if (boot) {
		/* in normal mode, we behave as a daemon with no terminal */
		if (!debug) {
			int s;

			/* first let's fork... */
			if (fork ())
				exit (0);
	
			/* ...then let's close all descriptors... */
			for (s = getdtablesize (); --s >= 0;)
				close (s);

			/* ... and get rid of our control terminal */
			if (-1 != open ("/dev/tty", 2)) {
				(void) ioctl (0, TIOCNOTTY, 0);
				close (0);
			}
		} else
			close (0);

		/* Now it's time to open the socket for requests */
		if (-1 == socket (AF_INET, SOCK_DGRAM, 0))
			exit (1);
		memset (&from, 0, sizeof from);
		from.sin_family = AF_INET;
		from.sin_port = serv_port;
		from.sin_addr.s_addr = INADDR_ANY;
		if (-1 == bind (0, &from, sizeof from))
			exit (1);
	}		

	/* now we can handle messages and requests */
	for (;;) {
		fromlen = sizeof from;
		if (recvfrom (0, (char *)&msg, sizeof msg, 0, (struct sockaddr *)&from, &fromlen) <= 0) {
			if (errno == EINTR)
				continue;
			exit (1);
		}

		si = GetInfo (msg.msg_key);
		switch (msg.msg_type) {

		/* message from server: we store the information */
		case MSG_SERVER:
			si->s_port = msg.msg_port;
			si->s_key  = msg.msg_key;
			break;

		/* message from a client: we retrieve and send the information */
		case MSG_CLIENT:
			msg.msg_port = si->s_port;
			msg.msg_type = MSG_REPLY;
			(void) sendto (0, (char *)&msg, sizeof msg, 0, (struct sockaddr *)&from, fromlen);
			break;
		}

	}
}