bootip.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // TODO: Fix later
  14. char* DEFAULT_IP = "10.0.2.2";
  15. static uint8_t fsip[IPaddrlen];
  16. uint8_t auip[IPaddrlen];
  17. static char mpoint[32];
  18. static int isvalidip(uint8_t*);
  19. static void netndb(char*, uint8_t*);
  20. static void netenv(char*, uint8_t*);
  21. void
  22. configip(int bargc, char **bargv, int needfs)
  23. {
  24. Waitmsg *w;
  25. int argc, pid;
  26. char **arg, **argv, buf[32], *p;
  27. fmtinstall('I', eipfmt);
  28. fmtinstall('M', eipfmt);
  29. fmtinstall('E', eipfmt);
  30. arg = malloc((bargc+1) * sizeof(char*));
  31. if(arg == nil)
  32. fatal("malloc");
  33. memmove(arg, bargv, bargc * sizeof(char*));
  34. arg[bargc] = 0;
  35. print("ipconfig...");
  36. argc = bargc;
  37. argv = arg;
  38. strcpy(mpoint, "/net");
  39. ARGBEGIN {
  40. case 'x':
  41. p = ARGF();
  42. if(p != nil)
  43. snprint(mpoint, sizeof(mpoint), "/net%s", p);
  44. break;
  45. case 'g':
  46. case 'b':
  47. case 'h':
  48. case 'm':
  49. p = ARGF();
  50. USED(p);
  51. break;
  52. } ARGEND;
  53. /* bind in an ip interface */
  54. if(bind("#I", mpoint, MAFTER) < 0)
  55. fatal("bind #I\n");
  56. if(access("#l0", 0) == 0 && bind("#l0", mpoint, MAFTER) < 0)
  57. print("bind #l0: %r\n");
  58. if(access("#l1", 0) == 0 && bind("#l1", mpoint, MAFTER) < 0)
  59. print("bind #l1: %r\n");
  60. if(access("#l2", 0) == 0 && bind("#l2", mpoint, MAFTER) < 0)
  61. print("bind #l2: %r\n");
  62. if(access("#l3", 0) == 0 && bind("#l3", mpoint, MAFTER) < 0)
  63. print("bind #l3: %r\n");
  64. werrstr("");
  65. /* let ipconfig configure the ip interface */
  66. switch(pid = fork()){
  67. case -1:
  68. fatal("fork configuring ip");
  69. case 0:
  70. exec("/boot/ipconfig", arg);
  71. fatal("execing /ipconfig");
  72. default:
  73. break;
  74. }
  75. /* wait for ipconfig to finish */
  76. for(;;){
  77. w = wait();
  78. if(w != nil && w->pid == pid){
  79. if(w->msg[0] != 0)
  80. fatal(w->msg);
  81. free(w);
  82. break;
  83. } else if(w == nil)
  84. fatal("configuring ip");
  85. free(w);
  86. }
  87. if(!needfs)
  88. return;
  89. /* if we didn't get a file and auth server, query user */
  90. netndb("fs", fsip);
  91. if(!isvalidip(fsip))
  92. netenv("fs", fsip);
  93. while(!isvalidip(fsip)){
  94. strcpy(buf, DEFAULT_IP);
  95. outin("filesystem IP address", buf, sizeof(buf));
  96. if (parseip(fsip, buf) == -1)
  97. fprint(2, "configip: can't parse fs ip %s\n", buf);
  98. }
  99. netndb("auth", auip);
  100. if(!isvalidip(auip))
  101. netenv("auth", auip);
  102. while(!isvalidip(auip)){
  103. strcpy(buf, DEFAULT_IP);
  104. outin("authentication server IP address", buf, sizeof(buf));
  105. if (parseip(auip, buf) == -1)
  106. fprint(2, "configip: can't parse auth ip %s\n", buf);
  107. }
  108. }
  109. static void
  110. setauthaddr(char *proto, int port)
  111. {
  112. char buf[128];
  113. snprint(buf, sizeof buf, "%s!%I!%d", proto, auip, port);
  114. authaddr = strdup(buf);
  115. }
  116. void
  117. configtcp(Method* m)
  118. {
  119. configip(bargc, bargv, 1);
  120. setauthaddr("tcp", 567);
  121. }
  122. int
  123. connecttcp(void)
  124. {
  125. int fd;
  126. char buf[64];
  127. snprint(buf, sizeof buf, "tcp!%I!5640", fsip);
  128. fd = dial(buf, 0, 0, 0);
  129. if (fd < 0)
  130. werrstr("dial %s: %r", buf);
  131. return fd;
  132. }
  133. static int
  134. isvalidip(uint8_t *ip)
  135. {
  136. if(ipcmp(ip, IPnoaddr) == 0)
  137. return 0;
  138. if(ipcmp(ip, v4prefix) == 0)
  139. return 0;
  140. return 1;
  141. }
  142. static void
  143. netenv(char *attr, uint8_t *ip)
  144. {
  145. int fd, n;
  146. char buf[128];
  147. ipmove(ip, IPnoaddr);
  148. snprint(buf, sizeof(buf), "#e/%s", attr);
  149. fd = open(buf, OREAD);
  150. if(fd < 0)
  151. return;
  152. n = read(fd, buf, sizeof(buf)-1);
  153. if(n <= 0)
  154. return;
  155. buf[n] = 0;
  156. if (parseip(ip, buf) == -1)
  157. fprint(2, "netenv: can't parse ip %s\n", buf);
  158. }
  159. static void
  160. netndb(char *attr, uint8_t *ip)
  161. {
  162. int fd, n, c;
  163. char buf[1024];
  164. char *p;
  165. ipmove(ip, IPnoaddr);
  166. snprint(buf, sizeof(buf), "%s/ndb", mpoint);
  167. fd = open(buf, OREAD);
  168. if(fd < 0)
  169. return;
  170. n = read(fd, buf, sizeof(buf)-1);
  171. close(fd);
  172. if(n <= 0)
  173. return;
  174. buf[n] = 0;
  175. n = strlen(attr);
  176. for(p = buf; ; p++){
  177. p = strstr(p, attr);
  178. if(p == nil)
  179. break;
  180. c = *(p-1);
  181. if(*(p + n) == '=' && (p == buf || c == '\n' || c == ' ' || c == '\t')){
  182. p += n+1;
  183. if (parseip(ip, p) == -1)
  184. fprint(2, "netndb: can't parse ip %s\n", p);
  185. return;
  186. }
  187. }
  188. return;
  189. }