bootip.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /* let ipconfig configure the ip interface */
  51. switch(pid = fork()){
  52. case -1:
  53. fatal("configuring ip: %r");
  54. case 0:
  55. exec("/boot/ipconfig", arg);
  56. fatal("execing /ipconfig");
  57. default:
  58. break;
  59. }
  60. /* wait for ipconfig to finish */
  61. for(;;){
  62. w = wait();
  63. if(w != nil && w->pid == pid){
  64. if(w->msg[0] != 0)
  65. fatal(w->msg);
  66. free(w);
  67. break;
  68. } else if(w == nil)
  69. fatal("configuring ip");
  70. free(w);
  71. }
  72. /* if we didn't get a file and auth server, query user */
  73. netndb("fs", fsip);
  74. if(!isvalidip(fsip))
  75. netenv("fs", fsip);
  76. while(!isvalidip(fsip)){
  77. buf[0] = 0;
  78. outin("filesystem IP address", buf, sizeof(buf));
  79. parseip(fsip, buf);
  80. }
  81. netndb("auth", auip);
  82. if(!isvalidip(auip))
  83. netenv("auth", auip);
  84. while(!isvalidip(auip)){
  85. buf[0] = 0;
  86. outin("authentication server IP address", buf, sizeof(buf));
  87. parseip(auip, buf);
  88. }
  89. }
  90. static void
  91. setauthaddr(char *proto, int port)
  92. {
  93. char buf[128];
  94. snprint(buf, sizeof buf, "%s!%I!%d", proto, auip, port);
  95. authaddr = strdup(buf);
  96. }
  97. void
  98. configtcp(Method*)
  99. {
  100. sleep(100);
  101. print("t");
  102. sleep(100);
  103. configip();
  104. sleep(100);
  105. print(".");
  106. sleep(100);
  107. setauthaddr("tcp", 567);
  108. }
  109. int
  110. connecttcp(void)
  111. {
  112. char buf[64];
  113. snprint(buf, sizeof buf, "tcp!%I!564", fsip);
  114. return dial(buf, 0, 0, 0);
  115. }
  116. void
  117. configil(Method*)
  118. {
  119. configip();
  120. setauthaddr("tcp", 567);
  121. }
  122. int
  123. connectil(void)
  124. {
  125. char buf[64];
  126. snprint(buf, sizeof buf, "il!%I!17008", fsip);
  127. return dial(buf, 0, 0, 0);
  128. }
  129. static int
  130. isvalidip(uchar *ip)
  131. {
  132. if(ipcmp(ip, IPnoaddr) == 0)
  133. return 0;
  134. if(ipcmp(ip, v4prefix) == 0)
  135. return 0;
  136. return 1;
  137. }
  138. static void
  139. netenv(char *attr, uchar *ip)
  140. {
  141. int fd, n;
  142. char buf[128];
  143. ipmove(ip, IPnoaddr);
  144. snprint(buf, sizeof(buf), "#e/%s", attr);
  145. fd = open(buf, OREAD);
  146. if(fd < 0)
  147. return;
  148. n = read(fd, buf, sizeof(buf)-1);
  149. if(n <= 0)
  150. return;
  151. buf[n] = 0;
  152. parseip(ip, buf);
  153. }
  154. static void
  155. netndb(char *attr, uchar *ip)
  156. {
  157. int fd, n, c;
  158. char buf[1024];
  159. char *p;
  160. ipmove(ip, IPnoaddr);
  161. snprint(buf, sizeof(buf), "%s/ndb", mpoint);
  162. fd = open(buf, OREAD);
  163. if(fd < 0)
  164. return;
  165. n = read(fd, buf, sizeof(buf)-1);
  166. close(fd);
  167. if(n <= 0)
  168. return;
  169. buf[n] = 0;
  170. n = strlen(attr);
  171. for(p = buf;;){
  172. p = strstr(p, attr);
  173. if(p == nil)
  174. break;
  175. c = *(p-1);
  176. if(*(p + n) == '=' && (p == buf || c == '\n' || c == ' ' || c == '\t')){
  177. p += n+1;
  178. parseip(ip, p);
  179. return;
  180. }
  181. p++;
  182. }
  183. return;
  184. }