bootip.c 3.7 KB

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