bootip.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ip.h>
  4. #include "boot.h"
  5. static uchar fsip[IPaddrlen];
  6. uchar auip[IPaddrlen];
  7. static char mpoint[32];
  8. static int isvalidip(uchar*);
  9. static void netndb(char*, uchar*);
  10. static void netenv(char*, uchar*);
  11. void
  12. configip(int bargc, char **bargv, int needfs)
  13. {
  14. int argc, pid;
  15. char **argv, *p;
  16. Waitmsg *w;
  17. char **arg;
  18. char buf[32];
  19. fmtinstall('I', eipfmt);
  20. fmtinstall('M', eipfmt);
  21. fmtinstall('E', eipfmt);
  22. arg = malloc((bargc+1) * sizeof(char*));
  23. if(arg == nil)
  24. fatal("malloc");
  25. memmove(arg, bargv, bargc * sizeof(char*));
  26. arg[bargc] = 0;
  27. argc = bargc;
  28. argv = arg;
  29. strcpy(mpoint, "/net");
  30. ARGBEGIN {
  31. case 'x':
  32. p = ARGF();
  33. if(p != nil)
  34. snprint(mpoint, sizeof(mpoint), "/net%s", p);
  35. break;
  36. case 'g':
  37. case 'b':
  38. case 'h':
  39. case 'm':
  40. p = ARGF();
  41. USED(p);
  42. break;
  43. } ARGEND;
  44. /* bind in an ip interface */
  45. bind("#I", mpoint, MAFTER);
  46. bind("#l0", mpoint, MAFTER);
  47. bind("#l1", mpoint, MAFTER);
  48. bind("#l2", mpoint, MAFTER);
  49. bind("#l3", mpoint, MAFTER);
  50. werrstr("");
  51. /* let ipconfig configure the ip interface */
  52. switch(pid = fork()){
  53. case -1:
  54. fatal("configuring ip");
  55. case 0:
  56. exec("/boot/ipconfig", arg);
  57. fatal("execing /ipconfig");
  58. default:
  59. break;
  60. }
  61. /* wait for ipconfig to finish */
  62. for(;;){
  63. w = wait();
  64. if(w != nil && w->pid == pid){
  65. if(w->msg[0] != 0)
  66. fatal(w->msg);
  67. free(w);
  68. break;
  69. } else if(w == nil)
  70. fatal("configuring ip");
  71. free(w);
  72. }
  73. if(!needfs)
  74. return;
  75. /* if we didn't get a file and auth server, query user */
  76. netndb("fs", fsip);
  77. if(!isvalidip(fsip))
  78. netenv("fs", fsip);
  79. while(!isvalidip(fsip)){
  80. buf[0] = 0;
  81. outin("filesystem IP address", buf, sizeof(buf));
  82. if (parseip(fsip, buf) == -1)
  83. fprint(2, "configip: can't parse fs ip %s\n", buf);
  84. }
  85. netndb("auth", auip);
  86. if(!isvalidip(auip))
  87. netenv("auth", auip);
  88. while(!isvalidip(auip)){
  89. buf[0] = 0;
  90. outin("authentication server IP address", buf, sizeof(buf));
  91. if (parseip(auip, buf) == -1)
  92. fprint(2, "configip: can't parse auth ip %s\n", buf);
  93. }
  94. }
  95. static void
  96. setauthaddr(char *proto, int port)
  97. {
  98. char buf[128];
  99. snprint(buf, sizeof buf, "%s!%I!%d", proto, auip, port);
  100. authaddr = strdup(buf);
  101. }
  102. void
  103. configtcp(Method*)
  104. {
  105. configip(bargc, bargv, 1);
  106. setauthaddr("tcp", 567);
  107. }
  108. int
  109. connecttcp(void)
  110. {
  111. int fd;
  112. char buf[64];
  113. snprint(buf, sizeof buf, "tcp!%I!564", fsip);
  114. fd = dial(buf, 0, 0, 0);
  115. if (fd < 0)
  116. werrstr("dial %s: %r", buf);
  117. return fd;
  118. }
  119. void
  120. configil(Method*)
  121. {
  122. configip(bargc, bargv, 1);
  123. setauthaddr("tcp", 567);
  124. }
  125. int
  126. connectil(void)
  127. {
  128. char buf[64];
  129. snprint(buf, sizeof buf, "il!%I!17008", fsip);
  130. return dial(buf, 0, 0, 0);
  131. }
  132. static int
  133. isvalidip(uchar *ip)
  134. {
  135. if(ipcmp(ip, IPnoaddr) == 0)
  136. return 0;
  137. if(ipcmp(ip, v4prefix) == 0)
  138. return 0;
  139. return 1;
  140. }
  141. static void
  142. netenv(char *attr, uchar *ip)
  143. {
  144. int fd, n;
  145. char buf[128];
  146. ipmove(ip, IPnoaddr);
  147. snprint(buf, sizeof(buf), "#e/%s", attr);
  148. fd = open(buf, OREAD);
  149. if(fd < 0)
  150. return;
  151. n = read(fd, buf, sizeof(buf)-1);
  152. if(n <= 0)
  153. return;
  154. buf[n] = 0;
  155. if (parseip(ip, buf) == -1)
  156. fprint(2, "netenv: can't parse ip %s\n", buf);
  157. }
  158. static void
  159. netndb(char *attr, uchar *ip)
  160. {
  161. int fd, n, c;
  162. char buf[1024];
  163. char *p;
  164. ipmove(ip, IPnoaddr);
  165. snprint(buf, sizeof(buf), "%s/ndb", mpoint);
  166. fd = open(buf, OREAD);
  167. if(fd < 0)
  168. return;
  169. n = read(fd, buf, sizeof(buf)-1);
  170. close(fd);
  171. if(n <= 0)
  172. return;
  173. buf[n] = 0;
  174. n = strlen(attr);
  175. for(p = buf; ; p++){
  176. p = strstr(p, attr);
  177. if(p == nil)
  178. break;
  179. c = *(p-1);
  180. if(*(p + n) == '=' && (p == buf || c == '\n' || c == ' ' || c == '\t')){
  181. p += n+1;
  182. if (parseip(ip, p) == -1)
  183. fprint(2, "netndb: can't parse ip %s\n", p);
  184. return;
  185. }
  186. }
  187. return;
  188. }