bootip.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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(void)
  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("%r");
  25. memmove(arg, bargv, (bargc+1) * sizeof(char*));
  26. argc = bargc;
  27. argv = arg;
  28. strcpy(mpoint, "/net");
  29. ARGBEGIN {
  30. case 'x':
  31. p = ARGF();
  32. if(p != nil)
  33. snprint(mpoint, sizeof(mpoint), "/net%s", p);
  34. break;
  35. case 'g':
  36. case 'b':
  37. case 'h':
  38. case 'm':
  39. p = ARGF();
  40. USED(p);
  41. break;
  42. } ARGEND;
  43. /* bind in an ip interface */
  44. bind("#I", mpoint, MAFTER);
  45. bind("#l0", mpoint, MAFTER);
  46. bind("#l1", mpoint, MAFTER);
  47. bind("#l2", mpoint, MAFTER);
  48. bind("#l3", mpoint, MAFTER);
  49. werrstr("");
  50. print("ip...");
  51. /* let ipconfig configure the ip interface */
  52. switch(pid = fork()){
  53. case -1:
  54. fatal("configuring ip: %r");
  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 we didn't get a file and auth server, query user */
  74. netndb("fs", fsip);
  75. if(!isvalidip(fsip))
  76. netenv("fs", fsip);
  77. while(!isvalidip(fsip)){
  78. buf[0] = 0;
  79. outin("filesystem IP address", buf, sizeof(buf));
  80. parseip(fsip, buf);
  81. }
  82. netndb("auth", auip);
  83. if(!isvalidip(auip))
  84. netenv("auth", auip);
  85. while(!isvalidip(auip)){
  86. buf[0] = 0;
  87. outin("authentication server IP address", buf, sizeof(buf));
  88. parseip(auip, buf);
  89. }
  90. }
  91. static void
  92. setauthaddr(char *proto, int port)
  93. {
  94. char buf[128];
  95. snprint(buf, sizeof buf, "%s!%I!%d", proto, auip, port);
  96. authaddr = strdup(buf);
  97. }
  98. void
  99. configtcp(Method*)
  100. {
  101. sleep(100);
  102. print("t");
  103. sleep(100);
  104. configip();
  105. sleep(100);
  106. print(".");
  107. sleep(100);
  108. setauthaddr("tcp", 567);
  109. }
  110. int
  111. connecttcp(void)
  112. {
  113. char buf[64];
  114. snprint(buf, sizeof buf, "tcp!%I!564", fsip);
  115. return dial(buf, 0, 0, 0);
  116. }
  117. void
  118. configil(Method*)
  119. {
  120. configip();
  121. setauthaddr("tcp", 567);
  122. }
  123. int
  124. connectil(void)
  125. {
  126. char buf[64];
  127. snprint(buf, sizeof buf, "il!%I!17008", fsip);
  128. return dial(buf, 0, 0, 0);
  129. }
  130. static int
  131. isvalidip(uchar *ip)
  132. {
  133. if(ipcmp(ip, IPnoaddr) == 0)
  134. return 0;
  135. if(ipcmp(ip, v4prefix) == 0)
  136. return 0;
  137. return 1;
  138. }
  139. static void
  140. netenv(char *attr, uchar *ip)
  141. {
  142. int fd, n;
  143. char buf[128];
  144. ipmove(ip, IPnoaddr);
  145. snprint(buf, sizeof(buf), "#e/%s", attr);
  146. fd = open(buf, OREAD);
  147. if(fd < 0)
  148. return;
  149. n = read(fd, buf, sizeof(buf)-1);
  150. if(n <= 0)
  151. return;
  152. buf[n] = 0;
  153. parseip(ip, buf);
  154. }
  155. static void
  156. netndb(char *attr, uchar *ip)
  157. {
  158. int fd, n, c;
  159. char buf[1024];
  160. char *p;
  161. ipmove(ip, IPnoaddr);
  162. snprint(buf, sizeof(buf), "%s/ndb", mpoint);
  163. fd = open(buf, OREAD);
  164. if(fd < 0)
  165. return;
  166. n = read(fd, buf, sizeof(buf)-1);
  167. close(fd);
  168. if(n <= 0)
  169. return;
  170. buf[n] = 0;
  171. n = strlen(attr);
  172. for(p = buf;;){
  173. p = strstr(p, attr);
  174. if(p == nil)
  175. break;
  176. c = *(p-1);
  177. if(*(p + n) == '=' && (p == buf || c == '\n' || c == ' ' || c == '\t')){
  178. p += n+1;
  179. parseip(ip, p);
  180. return;
  181. }
  182. p++;
  183. }
  184. return;
  185. }