minor fixes for safecopy & safemap tests
[minix.git] / servers / lwip / inet_config.c
blob631df1c723731a8288384630c28c0ca6cbd85812
1 /*
2 inet/inet_config.c
4 Created: Nov 11, 1992 by Philip Homburg
6 Modified: Apr 07, 2001 by Kees J. Bot
7 Read the configuration file and fill in the xx_conf[] arrays.
9 Copyright 1995 Philip Homburg
12 #define _MINIX_SOURCE 1
13 #define _POSIX_SOURCE 1
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <minix/type.h>
23 #include <minix/sysutil.h>
24 #include <minix/syslib.h>
25 #include "inet_config.h"
27 #include "proto.h"
28 #include <minix/netsock.h>
31 struct eth_conf eth_conf[IP_PORT_MAX];
32 struct psip_conf psip_conf[IP_PORT_MAX];
33 struct ip_conf ip_conf[IP_PORT_MAX];
34 struct tcp_conf tcp_conf[IP_PORT_MAX];
35 struct udp_conf udp_conf[IP_PORT_MAX];
36 dev_t ip_dev;
38 int eth_conf_nr;
39 int psip_conf_nr;
40 int ip_conf_nr;
41 int tcp_conf_nr;
42 int udp_conf_nr;
44 int ip_forward_directed_bcast= 0; /* Default is off */
46 static int ifdefault= -1; /* Default network interface. */
48 static void fatal(char *label)
50 printf("init: %s: %s\n", label, strerror(errno));
51 exit(1);
54 static void check_mknod(char *device, mode_t mode, int minor)
55 /* Check if a device exists with the proper device number. */
57 dev_t dev;
59 dev= (ip_dev & 0xFF00) | minor;
61 unlink(device);
62 if (mknod(device, S_IFCHR | mode, dev) < 0) fatal(device);
63 printf("mknod %s c %d %d\n", device, (ip_dev >> 8), minor);
66 static int cfg_fd;
67 static char word[16];
68 static unsigned char line[256], *lineptr;
69 static unsigned linenr;
71 static __dead void error(void)
73 printf("inet: error on line %u\n", linenr);
74 exit(1);
77 static int nextline(void)
79 /* Read a line from the configuration file, to be used by subsequent
80 * token() calls. Skip empty lines, and lines where the first character
81 * after leading "whitespace" is '#'. The last line of the file need
82 * not be terminated by a newline. Return 1 if a line was read in
83 * successfully, and 0 on EOF or error.
85 unsigned char *lp, c;
86 int r, skip;
88 lineptr = lp = line;
89 linenr++;
90 skip = -1;
92 while ((r = read(cfg_fd, &c, 1)) == 1) {
93 if (c == '\n') {
94 if (skip == 0)
95 break;
97 linenr++;
98 skip = -1;
99 continue;
102 if (skip == -1 && c > ' ')
103 skip = (c == '#');
105 if (skip == 0 && lp < (unsigned char *) line + sizeof(line)-1)
106 *lp++ = c;
109 *lp = 0;
110 return (r == 1 || lp != line);
113 static void token(int need)
115 /* Read a word from the configuration line. Return a null string on
116 * EOL. Return a punctuation as a one character word. If 'need' is
117 * true then an actual word is expected at this point, so err out if
118 * not.
120 unsigned char *wp;
121 static unsigned char c= '\n';
123 wp= (unsigned char *) word;
124 *wp = 0;
126 while (c <= ' ') {
127 if (*lineptr == 0) {
128 if (need) error();
129 return;
131 c = *lineptr++;
134 do {
135 if (wp < (unsigned char *) word + sizeof(word)-1) *wp++ = c;
136 c = (*lineptr != 0) ? *lineptr++ : ' ';
137 if (word[0] == ';' || word[0] == '{' || word[0] == '}') {
138 if (need) error();
139 break;
141 } while (c > ' ' && c != ';' && c != '{' && c != '}');
142 *wp = 0;
145 void inet_read_conf(void)
147 int ifno, enable;
148 struct stat st;
150 { static int first= 1;
151 if (!first)
152 panic(( "LWIP : read_conf: called a second time" ));
153 first= 0;
154 #if 0
155 *(u8_t *)0 = 0xcc; /* INT 3 */
156 #endif
160 /* Open the configuration file. */
161 if ((cfg_fd= open(PATH_INET_CONF, O_RDONLY)) == -1)
162 fatal(PATH_INET_CONF);
164 while (nextline()) {
165 token(1);
166 char drv_name[128];
167 unsigned instance;
169 if (strncmp(word, "eth", 3) == 0) {
171 ifno = strtol(word+3, NULL, 10);
172 token(1);
173 #if 1
174 strncpy(drv_name, word, 128);
175 #else
176 sprintf(drv_name, "%s_debug", word);
177 #endif
178 token(1);
179 instance = strtol(word, NULL, 10);
180 } else {
181 printf("inet: Unknown device '%s'\n", word);
182 error();
185 enable= 7; /* 1 = IP, 2 = TCP, 4 = UDP */
187 token(0);
188 if (word[0] == '{') {
189 token(0);
190 while (word[0] != '}') {
191 if (strcmp(word, "default") == 0) {
192 if (ifdefault != -1) {
193 printf(
194 "inet: ip%d and ip%d can't both be default\n",
195 ifdefault, ifno);
196 error();
198 ifdefault= ifno;
199 token(0);
200 } else
201 if (strcmp(word, "no") == 0) {
202 token(1);
203 if (strcmp(word, "ip") == 0) {
204 enable= 0;
205 } else
206 if (strcmp(word, "tcp") == 0) {
207 enable &= ~2;
208 } else
209 if (strcmp(word, "udp") == 0) {
210 enable &= ~4;
211 } else {
212 printf(
213 "inet: Can't do 'no %s'\n",
214 word);
215 exit(1);
217 token(0);
218 } else {
219 printf("inet: Unknown option '%s'\n",
220 word);
221 exit(1);
223 if (word[0] == ';') token(0);
224 else
225 if (word[0] != '}') error();
227 token(0);
229 if (word[0] != ';' && word[0] != 0) error();
231 nic_assign_driver("eth", ifno, drv_name, instance, ifdefault == ifno);
234 if (ifdefault == -1) {
235 printf("inet: No networks or no default network defined\n");
236 exit(1);
239 /* Set umask 0 so we can creat mode 666 devices. */
240 (void) umask(0);
242 /* See what the device number of /dev/ip is. That's what we
243 * used last time for the network devices, so we keep doing so.
245 if (stat("/dev/ip", &st) < 0) fatal("/dev/ip");
246 ip_dev= st.st_rdev;
248 /* create protocol devices */
249 check_mknod("/dev/ip", 0600, SOCK_TYPE_IP);
250 check_mknod("/dev/tcp", 0666, SOCK_TYPE_TCP);
251 check_mknod("/dev/udp", 0666, SOCK_TYPE_UDP);
254 * create hw devices, to configure ip we need also ip devices for each
256 check_mknod("/dev/ip0", 0600, SOCK_TYPES + 0);
257 check_mknod("/dev/eth0", 0600, SOCK_TYPES + 0);
259 check_mknod("/dev/ip1", 0600, SOCK_TYPES + 1);
260 check_mknod("/dev/eth1", 0600, SOCK_TYPES + 1);
262 check_mknod("/dev/ip2", 0600, SOCK_TYPES + 2);
263 check_mknod("/dev/eth2", 0600, SOCK_TYPES + 2);
265 check_mknod("/dev/ip3", 0600, SOCK_TYPES + 3);
266 check_mknod("/dev/eth3", 0600, SOCK_TYPES + 3);
268 check_mknod("/dev/ip4", 0600, SOCK_TYPES + 4);
269 check_mknod("/dev/eth4", 0600, SOCK_TYPES + 4);