Fixed extern declaration from pointer to array
[minix.git] / servers / inet / inet.c
blob0e43e7f514d26081eab275a163ff6d8bcbf8130b
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
6 The valid messages and their parameters are:
8 from FS:
9 __________________________________________________________________
10 | | | | | | |
11 | m_type | DEVICE | PROC_NR | COUNT | POSITION | ADDRESS |
12 |_______________|___________|_________|_______|__________|_________|
13 | | | | | | |
14 | NW_OPEN | minor dev | proc nr | mode | | |
15 |_______________|___________|_________|_______|__________|_________|
16 | | | | | | |
17 | NW_CLOSE | minor dev | proc nr | | | |
18 |_______________|___________|_________|_______|__________|_________|
19 | | | | | | |
20 | NW_IOCTL | minor dev | proc nr | | NWIO.. | address |
21 |_______________|___________|_________|_______|__________|_________|
22 | | | | | | |
23 | NW_READ | minor dev | proc nr | count | | address |
24 |_______________|___________|_________|_______|__________|_________|
25 | | | | | | |
26 | NW_WRITE | minor dev | proc nr | count | | address |
27 |_______________|___________|_________|_______|__________|_________|
28 | | | | | | |
29 | NW_CANCEL | minor dev | proc nr | | | |
30 |_______________|___________|_________|_______|__________|_________|
32 from DL_ETH:
33 _______________________________________________________________________
34 | | | | | | |
35 | m_type | DL_PORT | DL_PROC | DL_COUNT | DL_STAT | DL_TIME |
36 |_______________|___________|_________|__________|____________|_________|
37 | | | | | | |
38 | DL_CONF_REPLY | minor dev | proc nr | rd_count | 0 | stat | time |
39 |_______________|___________|_________|__________|____________|_________|
40 | | | | | | |
41 | DL_TASK_REPLY | minor dev | proc nr | rd_count | err | stat | time |
42 |_______________|___________|_________|__________|____________|_________|
45 #include "inet.h"
47 #define _MINIX_SOURCE 1
49 #include <fcntl.h>
50 #include <unistd.h>
51 #include <sys/svrctl.h>
52 #include <minix/ds.h>
53 #include <minix/endpoint.h>
55 #include "mq.h"
56 #include "qp.h"
57 #include "proto.h"
58 #include "generic/type.h"
60 #include "generic/arp.h"
61 #include "generic/assert.h"
62 #include "generic/buf.h"
63 #include "generic/clock.h"
64 #include "generic/eth.h"
65 #include "generic/event.h"
66 #include "generic/ip.h"
67 #include "generic/psip.h"
68 #include "generic/rand256.h"
69 #include "generic/sr.h"
70 #include "generic/tcp.h"
71 #include "generic/udp.h"
73 THIS_FILE
75 #define RANDOM_DEV_NAME "/dev/random"
77 int this_proc; /* Process number of this server. */
79 /* Killing Solaris */
80 int killer_inet= 0;
82 #ifdef BUF_CONSISTENCY_CHECK
83 extern int inet_buf_debug;
84 #endif
86 #if HZ_DYNAMIC
87 u32_t system_hz;
88 #endif
90 _PROTOTYPE( void main, (void) );
92 FORWARD _PROTOTYPE( void nw_conf, (void) );
93 FORWARD _PROTOTYPE( void nw_init, (void) );
95 /* SEF functions and variables. */
96 FORWARD _PROTOTYPE( void sef_local_startup, (void) );
97 FORWARD _PROTOTYPE( int sef_cb_init_fresh, (int type, sef_init_info_t *info) );
99 PUBLIC void main()
101 mq_t *mq;
102 int r;
103 int source, m_type;
105 /* SEF local startup. */
106 sef_local_startup();
108 while (TRUE)
110 #ifdef BUF_CONSISTENCY_CHECK
111 if (inet_buf_debug)
113 static int buf_debug_count= 0;
115 if (++buf_debug_count >= inet_buf_debug)
117 buf_debug_count= 0;
118 if (!bf_consistency_check())
119 break;
122 #endif
123 if (ev_head)
125 ev_process();
126 continue;
128 if (clck_call_expire)
130 clck_expire_timers();
131 continue;
133 mq= mq_get();
134 if (!mq)
135 ip_panic(("out of messages"));
137 r= sef_receive(ANY, &mq->mq_mess);
138 if (r<0)
140 ip_panic(("unable to receive: %d", r));
142 reset_time();
143 source= mq->mq_mess.m_source;
144 m_type= mq->mq_mess.m_type;
145 if (source == FS_PROC_NR)
147 sr_rec(mq);
149 else if (is_notify(m_type))
151 if (_ENDPOINT_P(source) == CLOCK)
153 clck_tick(&mq->mq_mess);
154 mq_free(mq);
156 else if (_ENDPOINT_P(source) == PM_PROC_NR)
158 /* signaled */
159 /* probably SIGTERM */
160 mq_free(mq);
162 else
164 /* A driver is (re)started. */
165 eth_check_drivers(&mq->mq_mess);
166 mq_free(mq);
169 else if (m_type == DL_CONF_REPLY || m_type == DL_TASK_REPLY ||
170 m_type == DL_NAME_REPLY || m_type == DL_STAT_REPLY)
172 eth_rec(&mq->mq_mess);
173 mq_free(mq);
175 else
177 printf("inet: got bad message type 0x%x from %d\n",
178 mq->mq_mess.m_type, mq->mq_mess.m_source);
179 mq_free(mq);
182 ip_panic(("task is not allowed to terminate"));
185 /*===========================================================================*
186 * sef_local_startup *
187 *===========================================================================*/
188 PRIVATE void sef_local_startup()
190 /* Register init callbacks. */
191 sef_setcb_init_fresh(sef_cb_init_fresh);
192 sef_setcb_init_restart(sef_cb_init_restart_fail);
194 /* No live update support for now. */
196 /* Let SEF perform startup. */
197 sef_startup();
200 /*===========================================================================*
201 * sef_cb_init_fresh *
202 *===========================================================================*/
203 PRIVATE int sef_cb_init_fresh(int type, sef_init_info_t *info)
205 /* Initialize the inet server. */
206 int r;
207 int timerand, fd;
208 u32_t tasknr;
209 struct fssignon device;
210 u8_t randbits[32];
211 struct timeval tv;
213 #if DEBUG
214 printf("Starting inet...\n");
215 printf("%s\n", version);
216 #endif
218 #if HZ_DYNAMIC
219 system_hz = sys_hz();
220 #endif
222 /* Read configuration. */
223 nw_conf();
225 /* Get a random number */
226 timerand= 1;
227 fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK);
228 if (fd != -1)
230 r= read(fd, randbits, sizeof(randbits));
231 if (r == sizeof(randbits))
232 timerand= 0;
233 else
235 printf("inet: unable to read random data from %s: %s\n",
236 RANDOM_DEV_NAME, r == -1 ? strerror(errno) :
237 r == 0 ? "EOF" : "not enough data");
239 close(fd);
241 else
243 printf("inet: unable to open random device %s: %s\n",
244 RANDOM_DEV_NAME, strerror(errno));
246 if (timerand)
248 printf("inet: using current time for random-number seed\n");
249 r= gettimeofday(&tv, NULL);
250 if (r == -1)
252 printf("sysutime failed: %s\n", strerror(errno));
253 exit(1);
255 memcpy(randbits, &tv, sizeof(tv));
257 init_rand256(randbits);
259 /* Our new identity as a server. */
260 r= ds_retrieve_label_num("inet", &tasknr);
261 if (r != OK)
262 ip_panic(("inet: ds_retrieve_label_num failed for 'inet': %d", r));
263 this_proc= tasknr;
265 /* Register the device group. */
266 device.dev= ip_dev;
267 device.style= STYLE_CLONE;
268 if (svrctl(FSSIGNON, (void *) &device) == -1) {
269 printf("inet: error %d on registering ethernet devices\n",
270 errno);
271 pause();
274 #ifdef BUF_CONSISTENCY_CHECK
275 inet_buf_debug= (getenv("inetbufdebug") &&
276 (strcmp(getenv("inetbufdebug"), "on") == 0));
277 inet_buf_debug= 100;
278 if (inet_buf_debug)
280 ip_warning(( "buffer consistency check enabled" ));
282 #endif
284 if (getenv("killerinet"))
286 ip_warning(( "killer inet active" ));
287 killer_inet= 1;
290 nw_init();
292 return(OK);
295 PRIVATE void nw_conf()
297 read_conf();
298 eth_prep();
299 arp_prep();
300 psip_prep();
301 ip_prep();
302 tcp_prep();
303 udp_prep();
306 PRIVATE void nw_init()
308 mq_init();
309 bf_init();
310 clck_init();
311 sr_init();
312 qp_init();
313 eth_init();
314 arp_init();
315 psip_init();
316 ip_init();
317 tcp_init();
318 udp_init();
321 PUBLIC void panic0(file, line)
322 char *file;
323 int line;
325 printf("panic at %s, %d: ", file, line);
328 PUBLIC void inet_panic()
330 printf("\ninet stacktrace: ");
331 util_stacktrace();
332 (panic)("INET","aborted due to a panic",NO_NUM);
333 for(;;);
336 #if !NDEBUG
337 PUBLIC void bad_assertion(file, line, what)
338 char *file;
339 int line;
340 char *what;
342 panic0(file, line);
343 printf("assertion \"%s\" failed", what);
344 panic();
348 PUBLIC void bad_compare(file, line, lhs, what, rhs)
349 char *file;
350 int line;
351 int lhs;
352 char *what;
353 int rhs;
355 panic0(file, line);
356 printf("compare (%d) %s (%d) failed", lhs, what, rhs);
357 panic();
359 #endif /* !NDEBUG */
362 * $PchId: inet.c,v 1.23 2005/06/28 14:27:22 philip Exp $