bootip.c 3.3 KB

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