bootip.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ip.h>
  12. #include "boot.h"
  13. static uint8_t fsip[IPaddrlen];
  14. uint8_t auip[IPaddrlen];
  15. static char mpoint[32];
  16. static int isvalidip(uint8_t*);
  17. static void netndb(char*, uint8_t*);
  18. static void netenv(char*, uint8_t*);
  19. void
  20. configip(int bargc, char **bargv, int needfs)
  21. {
  22. Waitmsg *w;
  23. int argc, pid;
  24. char **arg, **argv, buf[32], *p;
  25. fmtinstall('I', eipfmt);
  26. fmtinstall('M', eipfmt);
  27. fmtinstall('E', eipfmt);
  28. arg = malloc((bargc+1) * sizeof(char*));
  29. if(arg == nil)
  30. fatal("malloc");
  31. memmove(arg, bargv, bargc * sizeof(char*));
  32. arg[bargc] = 0;
  33. print("ipconfig...");
  34. argc = bargc;
  35. argv = arg;
  36. strcpy(mpoint, "/net");
  37. ARGBEGIN {
  38. case 'x':
  39. p = ARGF();
  40. if(p != nil)
  41. snprint(mpoint, sizeof(mpoint), "/net%s", p);
  42. break;
  43. case 'g':
  44. case 'b':
  45. case 'h':
  46. case 'm':
  47. p = ARGF();
  48. USED(p);
  49. break;
  50. } ARGEND;
  51. /* bind in an ip interface */
  52. if(bind("#I", mpoint, MAFTER) < 0)
  53. fatal("bind #I\n");
  54. if(access("#l0", 0) == 0 && bind("#l0", mpoint, MAFTER) < 0)
  55. print("bind #l0: %r\n");
  56. if(access("#l1", 0) == 0 && bind("#l1", mpoint, MAFTER) < 0)
  57. print("bind #l1: %r\n");
  58. if(access("#l2", 0) == 0 && bind("#l2", mpoint, MAFTER) < 0)
  59. print("bind #l2: %r\n");
  60. if(access("#l3", 0) == 0 && bind("#l3", mpoint, MAFTER) < 0)
  61. print("bind #l3: %r\n");
  62. werrstr("");
  63. /* let ipconfig configure the ip interface */
  64. switch(pid = fork()){
  65. case -1:
  66. fatal("fork configuring ip");
  67. case 0:
  68. exec("/boot/ipconfig", arg);
  69. fatal("execing /ipconfig");
  70. default:
  71. break;
  72. }
  73. /* wait for ipconfig to finish */
  74. for(;;){
  75. w = wait();
  76. if(w != nil && w->pid == pid){
  77. if(w->msg[0] != 0)
  78. fatal(w->msg);
  79. free(w);
  80. break;
  81. } else if(w == nil)
  82. fatal("configuring ip");
  83. free(w);
  84. }
  85. if(!needfs)
  86. return;
  87. /* if we didn't get a file and auth server, query user */
  88. netndb("fs", fsip);
  89. if(!isvalidip(fsip))
  90. netenv("fs", fsip);
  91. while(!isvalidip(fsip)){
  92. outin("filesystem IP address", buf, sizeof(buf));
  93. if (parseip(fsip, buf) == -1)
  94. fprint(2, "configip: can't parse fs ip %s\n", buf);
  95. }
  96. netndb("auth", auip);
  97. if(!isvalidip(auip))
  98. netenv("auth", auip);
  99. while(!isvalidip(auip)){
  100. outin("authentication server IP address", buf, sizeof(buf));
  101. if (parseip(auip, buf) == -1)
  102. fprint(2, "configip: can't parse auth ip %s\n", buf);
  103. }
  104. free(arg);
  105. }
  106. static void
  107. setauthaddr(char *proto, int port)
  108. {
  109. char buf[128];
  110. snprint(buf, sizeof buf, "%s!%I!%d", proto, auip, port);
  111. authaddr = strdup(buf);
  112. }
  113. void
  114. configtcp(Method* m)
  115. {
  116. configip(bargc, bargv, 1);
  117. setauthaddr("tcp", 567);
  118. }
  119. int
  120. connecttcp(void)
  121. {
  122. int fd;
  123. char buf[64];
  124. snprint(buf, sizeof buf, "tcp!%I!5640", fsip);
  125. fd = dial(buf, 0, 0, 0);
  126. if (fd < 0)
  127. werrstr("dial %s: %r", buf);
  128. return fd;
  129. }
  130. static int
  131. isvalidip(uint8_t *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, uint8_t *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. buf[n] = 0;
  152. if (parseip(ip, buf) == -1)
  153. fprint(2, "netenv: can't parse ip %s\n", buf);
  154. }
  155. close(fd);
  156. }
  157. static void
  158. netndb(char *attr, uint8_t *ip)
  159. {
  160. int fd, n, c;
  161. char buf[1024];
  162. char *p;
  163. ipmove(ip, IPnoaddr);
  164. snprint(buf, sizeof(buf), "%s/ndb", mpoint);
  165. fd = open(buf, OREAD);
  166. if(fd < 0)
  167. return;
  168. n = read(fd, buf, sizeof(buf)-1);
  169. close(fd);
  170. if(n <= 0)
  171. return;
  172. buf[n] = 0;
  173. n = strlen(attr);
  174. for(p = buf; ; p++){
  175. p = strstr(p, attr);
  176. if(p == nil)
  177. break;
  178. c = *(p-1);
  179. if(*(p + n) == '=' && (p == buf || c == '\n' || c == ' ' || c == '\t')){
  180. p += n+1;
  181. if (parseip(ip, p) == -1)
  182. fprint(2, "netndb: can't parse ip %s\n", p);
  183. return;
  184. }
  185. }
  186. return;
  187. }