announce.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. static int nettrans(char*, char*, int na, char*, int);
  5. enum
  6. {
  7. Maxpath= 256,
  8. };
  9. /*
  10. * announce a network service.
  11. */
  12. int
  13. announce(char *addr, char *dir)
  14. {
  15. int ctl, n, m;
  16. char buf[Maxpath];
  17. char buf2[Maxpath];
  18. char netdir[Maxpath];
  19. char naddr[Maxpath];
  20. char *cp;
  21. /*
  22. * translate the address
  23. */
  24. if(nettrans(addr, naddr, sizeof(naddr), netdir, sizeof(netdir)) < 0)
  25. return -1;
  26. /*
  27. * get a control channel
  28. */
  29. ctl = open(netdir, ORDWR);
  30. if(ctl<0){
  31. werrstr("announce opening %s: %r", netdir);
  32. return -1;
  33. }
  34. cp = strrchr(netdir, '/');
  35. if(cp == nil){
  36. werrstr("announce arg format %s", netdir);
  37. close(ctl);
  38. return -1;
  39. }
  40. *cp = 0;
  41. /*
  42. * find out which line we have
  43. */
  44. n = snprint(buf, sizeof(buf), "%s/", netdir);
  45. m = read(ctl, &buf[n], sizeof(buf)-n-1);
  46. if(m <= 0){
  47. werrstr("announce reading %s: %r", netdir);
  48. close(ctl);
  49. return -1;
  50. }
  51. buf[n+m] = 0;
  52. /*
  53. * make the call
  54. */
  55. n = snprint(buf2, sizeof(buf2), "announce %s", naddr);
  56. if(write(ctl, buf2, n)!=n){
  57. werrstr("announce writing %s: %r", netdir);
  58. close(ctl);
  59. return -1;
  60. }
  61. /*
  62. * return directory etc.
  63. */
  64. if(dir){
  65. strncpy(dir, buf, NETPATHLEN);
  66. dir[NETPATHLEN-1] = 0;
  67. }
  68. return ctl;
  69. }
  70. /*
  71. * listen for an incoming call
  72. */
  73. int
  74. listen(char *dir, char *newdir)
  75. {
  76. int ctl, n, m;
  77. char buf[Maxpath];
  78. char *cp;
  79. /*
  80. * open listen, wait for a call
  81. */
  82. snprint(buf, sizeof(buf), "%s/listen", dir);
  83. ctl = open(buf, ORDWR);
  84. if(ctl < 0){
  85. werrstr("listen opening %s: %r", buf);
  86. return -1;
  87. }
  88. /*
  89. * find out which line we have
  90. */
  91. strncpy(buf, dir, sizeof(buf) - 1);
  92. buf[sizeof(buf) - 1] = 0;
  93. cp = strrchr(buf, '/');
  94. if(cp == nil){
  95. close(ctl);
  96. werrstr("listen arg format %s", dir);
  97. return -1;
  98. }
  99. *++cp = 0;
  100. n = cp-buf;
  101. m = read(ctl, cp, sizeof(buf) - n - 1);
  102. if(m <= 0){
  103. close(ctl);
  104. werrstr("listen reading %s/listen: %r", dir);
  105. return -1;
  106. }
  107. buf[n+m] = 0;
  108. /*
  109. * return directory etc.
  110. */
  111. if(newdir){
  112. strncpy(newdir, buf, NETPATHLEN);
  113. newdir[NETPATHLEN-1] = 0;
  114. }
  115. return ctl;
  116. }
  117. /*
  118. * accept a call, return an fd to the open data file
  119. */
  120. int
  121. accept(int ctl, char *dir)
  122. {
  123. char buf[Maxpath];
  124. char *num;
  125. long n;
  126. num = strrchr(dir, '/');
  127. if(num == nil)
  128. num = dir;
  129. else
  130. num++;
  131. n = snprint(buf, sizeof(buf), "accept %s", num);
  132. write(ctl, buf, n); /* ignore return value, network might not need accepts */
  133. snprint(buf, sizeof(buf), "%s/data", dir);
  134. return open(buf, ORDWR);
  135. }
  136. /*
  137. * reject a call, tell device the reason for the rejection
  138. */
  139. int
  140. reject(int ctl, char *dir, char *cause)
  141. {
  142. char buf[Maxpath];
  143. char *num;
  144. long n;
  145. num = strrchr(dir, '/');
  146. if(num == 0)
  147. num = dir;
  148. else
  149. num++;
  150. snprint(buf, sizeof(buf), "reject %s %s", num, cause);
  151. n = strlen(buf);
  152. if(write(ctl, buf, n) != n)
  153. return -1;
  154. return 0;
  155. }
  156. /*
  157. * perform the identity translation (in case we can't reach cs)
  158. */
  159. static int
  160. identtrans(char *netdir, char *addr, char *naddr, int na, char *file, int nf)
  161. {
  162. char proto[Maxpath];
  163. char *p;
  164. USED(nf);
  165. /* parse the protocol */
  166. strncpy(proto, addr, sizeof(proto));
  167. proto[sizeof(proto)-1] = 0;
  168. p = strchr(proto, '!');
  169. if(p)
  170. *p++ = 0;
  171. snprint(file, nf, "%s/%s/clone", netdir, proto);
  172. strncpy(naddr, p, na);
  173. naddr[na-1] = 0;
  174. return 1;
  175. }
  176. /*
  177. * call up the connection server and get a translation
  178. */
  179. static int
  180. nettrans(char *addr, char *naddr, int na, char *file, int nf)
  181. {
  182. int i, fd;
  183. char buf[Maxpath];
  184. char netdir[Maxpath];
  185. char *p, *p2;
  186. long n;
  187. /*
  188. * parse, get network directory
  189. */
  190. p = strchr(addr, '!');
  191. if(p == 0){
  192. werrstr("bad dial string: %s", addr);
  193. return -1;
  194. }
  195. if(*addr != '/'){
  196. strncpy(netdir, "/net", sizeof(netdir));
  197. netdir[sizeof(netdir) - 1] = 0;
  198. } else {
  199. for(p2 = p; *p2 != '/'; p2--)
  200. ;
  201. i = p2 - addr;
  202. if(i == 0 || i >= sizeof(netdir)){
  203. werrstr("bad dial string: %s", addr);
  204. return -1;
  205. }
  206. strncpy(netdir, addr, i);
  207. netdir[i] = 0;
  208. addr = p2 + 1;
  209. }
  210. /*
  211. * ask the connection server
  212. */
  213. snprint(buf, sizeof(buf), "%s/cs", netdir);
  214. fd = open(buf, ORDWR);
  215. if(fd < 0)
  216. return identtrans(netdir, addr, naddr, na, file, nf);
  217. if(write(fd, addr, strlen(addr)) < 0){
  218. close(fd);
  219. return -1;
  220. }
  221. seek(fd, 0, 0);
  222. n = read(fd, buf, sizeof(buf)-1);
  223. close(fd);
  224. if(n <= 0)
  225. return -1;
  226. buf[n] = 0;
  227. /*
  228. * parse the reply
  229. */
  230. p = strchr(buf, ' ');
  231. if(p == 0)
  232. return -1;
  233. *p++ = 0;
  234. strncpy(naddr, p, na);
  235. naddr[na-1] = 0;
  236. if(buf[0] == '/'){
  237. p = strchr(buf+1, '/');
  238. if(p == nil)
  239. p = buf;
  240. else
  241. p++;
  242. }
  243. snprint(file, nf, "%s/%s", netdir, p);
  244. return 0;
  245. }