bootip.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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("%r");
  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: %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(!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. parseip(fsip, buf);
  83. }
  84. netndb("auth", auip);
  85. if(!isvalidip(auip))
  86. netenv("auth", auip);
  87. while(!isvalidip(auip)){
  88. buf[0] = 0;
  89. outin("authentication server IP address", buf, sizeof(buf));
  90. parseip(auip, buf);
  91. }
  92. }
  93. static void
  94. setauthaddr(char *proto, int port)
  95. {
  96. char buf[128];
  97. snprint(buf, sizeof buf, "%s!%I!%d", proto, auip, port);
  98. authaddr = strdup(buf);
  99. }
  100. void
  101. configtcp(Method*)
  102. {
  103. configip(bargc, bargv, 1);
  104. setauthaddr("tcp", 567);
  105. }
  106. int
  107. connecttcp(void)
  108. {
  109. char buf[64];
  110. snprint(buf, sizeof buf, "tcp!%I!564", fsip);
  111. return dial(buf, 0, 0, 0);
  112. }
  113. void
  114. configil(Method*)
  115. {
  116. configip(bargc, bargv, 1);
  117. setauthaddr("tcp", 567);
  118. }
  119. int
  120. connectil(void)
  121. {
  122. char buf[64];
  123. snprint(buf, sizeof buf, "il!%I!17008", fsip);
  124. return dial(buf, 0, 0, 0);
  125. }
  126. static int
  127. isvalidip(uchar *ip)
  128. {
  129. if(ipcmp(ip, IPnoaddr) == 0)
  130. return 0;
  131. if(ipcmp(ip, v4prefix) == 0)
  132. return 0;
  133. return 1;
  134. }
  135. static void
  136. netenv(char *attr, uchar *ip)
  137. {
  138. int fd, n;
  139. char buf[128];
  140. ipmove(ip, IPnoaddr);
  141. snprint(buf, sizeof(buf), "#e/%s", attr);
  142. fd = open(buf, OREAD);
  143. if(fd < 0)
  144. return;
  145. n = read(fd, buf, sizeof(buf)-1);
  146. if(n <= 0)
  147. return;
  148. buf[n] = 0;
  149. parseip(ip, buf);
  150. }
  151. static void
  152. netndb(char *attr, uchar *ip)
  153. {
  154. int fd, n, c;
  155. char buf[1024];
  156. char *p;
  157. ipmove(ip, IPnoaddr);
  158. snprint(buf, sizeof(buf), "%s/ndb", mpoint);
  159. fd = open(buf, OREAD);
  160. if(fd < 0)
  161. return;
  162. n = read(fd, buf, sizeof(buf)-1);
  163. close(fd);
  164. if(n <= 0)
  165. return;
  166. buf[n] = 0;
  167. n = strlen(attr);
  168. for(p = buf;;){
  169. p = strstr(p, attr);
  170. if(p == nil)
  171. break;
  172. c = *(p-1);
  173. if(*(p + n) == '=' && (p == buf || c == '\n' || c == ' ' || c == '\t')){
  174. p += n+1;
  175. parseip(ip, p);
  176. return;
  177. }
  178. p++;
  179. }
  180. return;
  181. }