1 /* this file contains the interface of the network software with rest of
2 minix. Furthermore it contains the main loop of the network task.
4 Copyright 1995 Philip Homburg
11 #include <sys/svrctl.h>
13 #include <minix/endpoint.h>
14 #include <minix/chardriver.h>
16 #include <sys/types.h>
22 #include "generic/type.h"
24 #include "generic/arp.h"
25 #include "generic/assert.h"
26 #include "generic/buf.h"
27 #include "generic/clock.h"
28 #include "generic/eth.h"
29 #include "generic/event.h"
30 #include "generic/ip.h"
31 #include "generic/psip.h"
32 #include "generic/rand256.h"
33 #include "generic/sr.h"
34 #include "generic/tcp.h"
35 #include "generic/udp.h"
39 #define RANDOM_DEV_NAME "/dev/random"
41 endpoint_t this_proc
; /* Process number of this server. */
46 #ifdef BUF_CONSISTENCY_CHECK
47 extern int inet_buf_debug
;
54 static void nw_conf(void);
55 static void nw_init(void);
56 static void ds_event(void);
58 /* SEF functions and variables. */
59 static void sef_local_startup(void);
60 static int sef_cb_init_fresh(int type
, sef_init_info_t
*info
);
62 int main(int argc
, char *argv
[])
68 /* SEF local startup. */
73 #ifdef BUF_CONSISTENCY_CHECK
76 static int buf_debug_count
= 0;
78 if (++buf_debug_count
>= inet_buf_debug
)
81 if (!bf_consistency_check())
97 r
= sef_receive_status(ANY
, &mess
, &ipc_status
);
100 ip_panic(("unable to receive: %d", r
));
103 if (mess
.m_source
== VFS_PROC_NR
)
105 sr_rec(&mess
, ipc_status
);
107 else if (is_ipc_notify(ipc_status
))
109 if (mess
.m_source
== CLOCK
)
113 else if (mess
.m_source
== DS_PROC_NR
)
115 /* DS notifies us of an event. */
120 printf("inet: got unexpected notify from %d\n",
124 else if (mess
.m_type
== DL_CONF_REPLY
||
125 mess
.m_type
== DL_TASK_REPLY
||
126 mess
.m_type
== DL_STAT_REPLY
)
132 printf("inet: got bad message type 0x%x from %d\n",
133 mess
.m_type
, mess
.m_source
);
136 ip_panic(("task is not allowed to terminate"));
140 /*===========================================================================*
141 * sef_local_startup *
142 *===========================================================================*/
143 static void sef_local_startup()
145 /* Register init callbacks. */
146 sef_setcb_init_fresh(sef_cb_init_fresh
);
147 sef_setcb_init_restart(sef_cb_init_fresh
);
149 /* No live update support for now. */
151 /* Let SEF perform startup. */
155 /*===========================================================================*
156 * sef_cb_init_fresh *
157 *===========================================================================*/
158 static int sef_cb_init_fresh(int type
, sef_init_info_t
*info
)
160 /* Initialize the inet server. */
168 printf("Starting inet...\n");
169 printf("%s\n", version
);
173 system_hz
= sys_hz();
176 /* Read configuration. */
179 /* Get a random number */
181 fd
= open(RANDOM_DEV_NAME
, O_RDONLY
| O_NONBLOCK
);
184 r
= read(fd
, randbits
, sizeof(randbits
));
185 if (r
== sizeof(randbits
))
189 printf("inet: unable to read random data from %s: %s\n",
190 RANDOM_DEV_NAME
, r
== -1 ? strerror(errno
) :
191 r
== 0 ? "EOF" : "not enough data");
197 printf("inet: unable to open random device %s: %s\n",
198 RANDOM_DEV_NAME
, strerror(errno
));
202 printf("inet: using current time for random-number seed\n");
203 r
= gettimeofday(&tv
, NULL
);
206 printf("sysutime failed: %s\n", strerror(errno
));
209 memcpy(randbits
, &tv
, sizeof(tv
));
211 init_rand256(randbits
);
213 /* Our new identity as a server. */
214 this_proc
= info
->endpoint
;
216 #ifdef BUF_CONSISTENCY_CHECK
217 inet_buf_debug
= (getenv("inetbufdebug") &&
218 (strcmp(getenv("inetbufdebug"), "on") == 0));
222 ip_warning(( "buffer consistency check enabled" ));
226 if (getenv("killerinet"))
228 ip_warning(( "killer inet active" ));
234 /* Subscribe to driver events for network drivers. */
235 r
= ds_subscribe("drv\\.net\\..*", DSF_INITIAL
| DSF_OVERWRITE
);
237 ip_panic(("inet: can't subscribe to driver events"));
240 /* Drop root privileges */
241 if ((pw
= getpwnam(SERVICE_LOGIN
)) == NULL
) {
242 printf("inet: unable to retrieve uid of SERVICE_LOGIN, "
243 "still running as root");
244 } else if (setuid(pw
->pw_uid
) != 0) {
245 ip_panic(("inet: unable to drop privileges"));
248 /* Announce we are up. INET announces its presence to VFS just like
249 * any other character driver.
251 chardriver_announce();
256 static void nw_conf()
267 static void nw_init()
282 /*===========================================================================*
284 *===========================================================================*/
285 static void ds_event()
287 char key
[DS_MAX_KEYLEN
];
288 char *driver_prefix
= (char *) "drv.net.";
292 endpoint_t owner_endpoint
;
296 prefix_len
= strlen(driver_prefix
);
298 /* We may get one notification for multiple updates from DS. Get events
299 * and owners from DS, until DS tells us that there are no more.
301 while ((r
= ds_check(key
, &type
, &owner_endpoint
)) == OK
) {
302 r
= ds_retrieve_u32(key
, &value
);
304 printf("inet: ds_event: ds_retrieve_u32 failed\n");
308 /* Only check for network driver up events. */
309 if(strncmp(key
, driver_prefix
, prefix_len
)
310 || value
!= DS_DRIVER_UP
) {
314 /* The driver label comes after the prefix. */
315 label
= key
+ prefix_len
;
317 /* A driver is (re)started. */
318 eth_check_driver(label
, owner_endpoint
);
322 printf("inet: ds_event: ds_check failed: %d\n", r
);
325 void panic0(file
, line
)
329 printf("panic at %s, %d: ", file
, line
);
333 void inet_panic(void)
335 printf("\ninet stacktrace: ");
337 (panic
)("aborted due to a panic");
343 void bad_assertion(file
, line
, what
)
349 printf("assertion \"%s\" failed", what
);
355 void bad_compare(file
, line
, lhs
, what
, rhs
)
363 printf("compare (%d) %s (%d) failed", lhs
, what
, rhs
);
369 * $PchId: inet.c,v 1.23 2005/06/28 14:27:22 philip Exp $