PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

servers/lwip/inet_config.c

http://www.minix3.org/
C | 271 lines | 195 code | 45 blank | 31 comment | 74 complexity | 579aa9d17e02a91b3755b08c6189f0e6 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /*
  2. inet/inet_config.c
  3. Created: Nov 11, 1992 by Philip Homburg
  4. Modified: Apr 07, 2001 by Kees J. Bot
  5. Read the configuration file and fill in the xx_conf[] arrays.
  6. Copyright 1995 Philip Homburg
  7. */
  8. #define _MINIX_SOURCE 1
  9. #define _POSIX_SOURCE 1
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <minix/type.h>
  18. #include <minix/sysutil.h>
  19. #include <minix/syslib.h>
  20. #include "inet_config.h"
  21. #include "proto.h"
  22. #include "socket.h"
  23. struct eth_conf eth_conf[IP_PORT_MAX];
  24. struct psip_conf psip_conf[IP_PORT_MAX];
  25. struct ip_conf ip_conf[IP_PORT_MAX];
  26. struct tcp_conf tcp_conf[IP_PORT_MAX];
  27. struct udp_conf udp_conf[IP_PORT_MAX];
  28. dev_t ip_dev;
  29. int eth_conf_nr;
  30. int psip_conf_nr;
  31. int ip_conf_nr;
  32. int tcp_conf_nr;
  33. int udp_conf_nr;
  34. int ip_forward_directed_bcast= 0; /* Default is off */
  35. static int ifdefault= -1; /* Default network interface. */
  36. static void fatal(char *label)
  37. {
  38. printf("init: %s: %s\n", label, strerror(errno));
  39. exit(1);
  40. }
  41. static void check_mknod(char *device, mode_t mode, int minor)
  42. /* Check if a device exists with the proper device number. */
  43. {
  44. dev_t dev;
  45. dev= (ip_dev & 0xFF00) | minor;
  46. unlink(device);
  47. if (mknod(device, S_IFCHR | mode, dev) < 0) fatal(device);
  48. printf("mknod %s c %d %d\n", device, (ip_dev >> 8), minor);
  49. }
  50. static int cfg_fd;
  51. static char word[16];
  52. static unsigned char line[256], *lineptr;
  53. static unsigned linenr;
  54. static __dead void error(void)
  55. {
  56. printf("inet: error on line %u\n", linenr);
  57. exit(1);
  58. }
  59. static int nextline(void)
  60. {
  61. /* Read a line from the configuration file, to be used by subsequent
  62. * token() calls. Skip empty lines, and lines where the first character
  63. * after leading "whitespace" is '#'. The last line of the file need
  64. * not be terminated by a newline. Return 1 if a line was read in
  65. * successfully, and 0 on EOF or error.
  66. */
  67. unsigned char *lp, c;
  68. int r, skip;
  69. lineptr = lp = line;
  70. linenr++;
  71. skip = -1;
  72. while ((r = read(cfg_fd, &c, 1)) == 1) {
  73. if (c == '\n') {
  74. if (skip == 0)
  75. break;
  76. linenr++;
  77. skip = -1;
  78. continue;
  79. }
  80. if (skip == -1 && c > ' ')
  81. skip = (c == '#');
  82. if (skip == 0 && lp < (unsigned char *) line + sizeof(line)-1)
  83. *lp++ = c;
  84. }
  85. *lp = 0;
  86. return (r == 1 || lp != line);
  87. }
  88. static void token(int need)
  89. {
  90. /* Read a word from the configuration line. Return a null string on
  91. * EOL. Return a punctuation as a one character word. If 'need' is
  92. * true then an actual word is expected at this point, so err out if
  93. * not.
  94. */
  95. unsigned char *wp;
  96. static unsigned char c= '\n';
  97. wp= (unsigned char *) word;
  98. *wp = 0;
  99. while (c <= ' ') {
  100. if (*lineptr == 0) {
  101. if (need) error();
  102. return;
  103. }
  104. c = *lineptr++;
  105. }
  106. do {
  107. if (wp < (unsigned char *) word + sizeof(word)-1) *wp++ = c;
  108. c = (*lineptr != 0) ? *lineptr++ : ' ';
  109. if (word[0] == ';' || word[0] == '{' || word[0] == '}') {
  110. if (need) error();
  111. break;
  112. }
  113. } while (c > ' ' && c != ';' && c != '{' && c != '}');
  114. *wp = 0;
  115. }
  116. void inet_read_conf(void)
  117. {
  118. int ifno, enable;
  119. struct stat st;
  120. { static int first= 1;
  121. if (!first)
  122. panic(( "LWIP : read_conf: called a second time" ));
  123. first= 0;
  124. #if 0
  125. *(u8_t *)0 = 0xcc; /* INT 3 */
  126. #endif
  127. }
  128. /* Open the configuration file. */
  129. if ((cfg_fd= open(PATH_INET_CONF, O_RDONLY)) == -1)
  130. fatal(PATH_INET_CONF);
  131. while (nextline()) {
  132. token(1);
  133. char drv_name[128];
  134. unsigned instance;
  135. if (strncmp(word, "eth", 3) == 0) {
  136. ifno = strtol(word+3, NULL, 10);
  137. token(1);
  138. #if 1
  139. strncpy(drv_name, word, 128);
  140. #else
  141. sprintf(drv_name, "%s_debug", word);
  142. #endif
  143. token(1);
  144. instance = strtol(word, NULL, 10);
  145. } else {
  146. printf("inet: Unknown device '%s'\n", word);
  147. error();
  148. }
  149. enable= 7; /* 1 = IP, 2 = TCP, 4 = UDP */
  150. token(0);
  151. if (word[0] == '{') {
  152. token(0);
  153. while (word[0] != '}') {
  154. if (strcmp(word, "default") == 0) {
  155. if (ifdefault != -1) {
  156. printf(
  157. "inet: ip%d and ip%d can't both be default\n",
  158. ifdefault, ifno);
  159. error();
  160. }
  161. ifdefault= ifno;
  162. token(0);
  163. } else
  164. if (strcmp(word, "no") == 0) {
  165. token(1);
  166. if (strcmp(word, "ip") == 0) {
  167. enable= 0;
  168. } else
  169. if (strcmp(word, "tcp") == 0) {
  170. enable &= ~2;
  171. } else
  172. if (strcmp(word, "udp") == 0) {
  173. enable &= ~4;
  174. } else {
  175. printf(
  176. "inet: Can't do 'no %s'\n",
  177. word);
  178. exit(1);
  179. }
  180. token(0);
  181. } else {
  182. printf("inet: Unknown option '%s'\n",
  183. word);
  184. exit(1);
  185. }
  186. if (word[0] == ';') token(0);
  187. else
  188. if (word[0] != '}') error();
  189. }
  190. token(0);
  191. }
  192. if (word[0] != ';' && word[0] != 0) error();
  193. nic_assign_driver("eth", ifno, drv_name, instance, ifdefault == ifno);
  194. }
  195. if (ifdefault == -1) {
  196. printf("inet: No networks or no default network defined\n");
  197. exit(1);
  198. }
  199. /* Set umask 0 so we can creat mode 666 devices. */
  200. (void) umask(0);
  201. /* See what the device number of /dev/ip is. That's what we
  202. * used last time for the network devices, so we keep doing so.
  203. */
  204. if (stat("/dev/ip", &st) < 0) fatal("/dev/ip");
  205. ip_dev= st.st_rdev;
  206. /* create protocol devices */
  207. check_mknod("/dev/ip", 0600, SOCK_TYPE_IP);
  208. check_mknod("/dev/tcp", 0666, SOCK_TYPE_TCP);
  209. check_mknod("/dev/udp", 0666, SOCK_TYPE_UDP);
  210. /*
  211. * create hw devices, to configure ip we need also ip devices for each
  212. */
  213. check_mknod("/dev/ip0", 0600, SOCK_TYPES + 0);
  214. check_mknod("/dev/eth0", 0600, SOCK_TYPES + 0);
  215. check_mknod("/dev/ip1", 0600, SOCK_TYPES + 1);
  216. check_mknod("/dev/eth1", 0600, SOCK_TYPES + 1);
  217. check_mknod("/dev/ip2", 0600, SOCK_TYPES + 2);
  218. check_mknod("/dev/eth2", 0600, SOCK_TYPES + 2);
  219. check_mknod("/dev/ip3", 0600, SOCK_TYPES + 3);
  220. check_mknod("/dev/eth3", 0600, SOCK_TYPES + 3);
  221. check_mknod("/dev/ip4", 0600, SOCK_TYPES + 4);
  222. check_mknod("/dev/eth4", 0600, SOCK_TYPES + 4);
  223. }