summaryrefslogtreecommitdiff
path: root/src/inivyd.c
blob: 7f4a06f1c30f9ef1d499df7a3f5912c7a7bd085a (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
/*
 *	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
 *
 */

/*
 * Le super-demon wixd.
 *
 * Il est normalement lance par inetd, avec la chaussette SOCK_DGRAM
 * sur le descripteur 0, ou alors, si le symbole FROMBOOT est defini, il est
 * lance depuis /etc/rc.local.
 *
 * Le serveur wixd:
 *	Il envoie un paquet connmsg de type MSG_SERVER contenant le numero
 *	de port sur lequel il a connecte sa chaussette.
 *
 * Les clients wix (wx, wxcat, etc ...):
 *	Ils envoient un paquet connmsg de type MSG_CLIENT ou ils demandent
 *	le port.
 *
 * 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 = "/usr/tmp/wixd.db";
const int dumpfilemode = 0644;
const char* const servicename = "ivyd";
const char* const port_env_variable = "INIVYD_PORT";
const int default_port = 3006;

/*
 * Definition de la table geree par le super-demon
 */

typedef struct {
	short	s_port;	/* le port sur lequel le client communique avec son serveur */
	short	s_key;	/* le numero de processus du serveur wixd */
} serverinfo;

#define TABSIZ 64	/* 64 connections simultanees, ca devrait aller */
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;
	}
	
	if (boot) {
		if (!debug) {
			int s;

			if (fork ())
				exit (0);
	
			/* on ferme tous les descripteurs */
			for (s = getdtablesize (); --s >= 0;)
				close (s);

			/* et on se debarrasse du terminal de controle */
			if (-1 != open ("/dev/tty", 2)) {
				(void) ioctl (0, TIOCNOTTY, 0);
				close (0);
			}
		} else
			close (0);

		/*
		 * Ouvre la chaussette et l'attache a bon port
		 */
		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);
	}		

	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) {
		case MSG_SERVER:
			si->s_port = msg.msg_port;
			si->s_key  = msg.msg_key;
			break;

		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;
		}

	}
}