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