1
0

dial 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. .TH DIAL 2
  2. .SH NAME
  3. dial, hangup, announce, listen, accept, reject, netmkaddr, setnetmtpt, getnetconninfo, freenetconninfo \- make and break network connections
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .PP
  9. .B
  10. int dial(char *addr, char *local, char *dir, int *cfdp)
  11. .PP
  12. .B
  13. int hangup(int ctl)
  14. .PP
  15. .B
  16. int announce(char *addr, char *dir)
  17. .PP
  18. .B
  19. int listen(char *dir, char *newdir)
  20. .PP
  21. .B
  22. int accept(int ctl, char *dir)
  23. .PP
  24. .B
  25. int reject(int ctl, char *dir, char *cause)
  26. .PP
  27. .B
  28. char* netmkaddr(char *addr, char *defnet, char *defservice)
  29. .PP
  30. .B
  31. void setnetmtpt(char *to, int tolen, char *from)
  32. .PP
  33. .B
  34. NetConnInfo* getnetconninfo(char *conndir, int fd)
  35. .PP
  36. .B
  37. void freenetconninfo(NetConnInfo*)
  38. .SH DESCRIPTION
  39. For these routines,
  40. .I addr
  41. is a network address of the form
  42. .IB network ! netaddr ! service\f1,
  43. .IB network ! netaddr\f1,
  44. or simply
  45. .IR netaddr .
  46. .I Network
  47. is any directory listed in
  48. .B /net
  49. or the special token,
  50. .BR net .
  51. .B Net
  52. is a free variable that stands for any network in common
  53. between the source and the host
  54. .IR netaddr .
  55. .I Netaddr
  56. can be a host name, a domain name, a network address,
  57. or a meta-name of the form
  58. .BI $ attribute\f1,
  59. which
  60. is replaced by
  61. .I value
  62. from the value-attribute pair
  63. .IB attribute = value
  64. most closely associated with the source host in the
  65. network data base (see
  66. .IR ndb (6)).
  67. .PP
  68. If a connection attempt is successful and
  69. .I dir
  70. is non-zero,
  71. the path name of a
  72. .I line directory
  73. that has files for accessing the connection
  74. is copied into
  75. .IR dir .
  76. The path name, including terminating
  77. .SM NUL\c
  78. , is guaranteed to fit in
  79. .L NETPATHLEN
  80. (40)
  81. bytes.
  82. One line directory exists for each possible connection.
  83. The
  84. .B data
  85. file in the line directory should be used to communicate with the destination.
  86. The
  87. .B ctl
  88. file in the line directory can be used to send commands to the line.
  89. See
  90. .IR ip (3)
  91. for messages that can be written to the
  92. .B ctl
  93. file.
  94. The last close of the
  95. .B data
  96. or
  97. .B ctl
  98. file will close the connection.
  99. .PP
  100. .I Dial
  101. makes a call to destination
  102. .I addr
  103. on a multiplexed network.
  104. If the network in
  105. .I addr
  106. is
  107. .BR net ,
  108. .I dial
  109. will try in parallel all addresses on
  110. networks in common between source and destination
  111. until a call succeeds.
  112. It returns a file descriptor open for reading and writing the
  113. .B data
  114. file in the line directory.
  115. The
  116. .B addr
  117. file in the line directory contains the address called.
  118. If the network allows the local address to be set,
  119. as is the case with UDP and TCP port numbers, and
  120. .IR local
  121. is non-zero, the local address will be set to
  122. .IR local .
  123. If
  124. .I cfdp
  125. is non-zero,
  126. .BI * cfdp
  127. is set to a file descriptor open for reading and
  128. writing the control file.
  129. .PP
  130. .I Hangup
  131. is a means of forcing a connection to hang up without
  132. closing the
  133. .B ctl
  134. and
  135. .B data
  136. files.
  137. .P
  138. .I Announce
  139. and
  140. .I listen
  141. are the complements of
  142. .IR dial .
  143. .I Announce
  144. establishes a network
  145. name to which calls can be made.
  146. Like
  147. .IR dial ,
  148. .I announce
  149. returns an open
  150. .B ctl
  151. file.
  152. The
  153. .I netaddr
  154. used in announce may be a local address or an asterisk,
  155. to indicate all local addresses, e.g.
  156. .BR tcp!*!echo .
  157. The
  158. .I listen
  159. routine takes as its first argument the
  160. .I dir
  161. of a previous
  162. .IR announce .
  163. When a call is received,
  164. .I listen
  165. returns an open
  166. .B ctl
  167. file for the line the call was received on.
  168. It sets
  169. .I newdir
  170. to the path name of the new line directory.
  171. .I Accept
  172. accepts a call received by
  173. .IR listen ,
  174. while
  175. .I reject
  176. refuses the call because of
  177. .IR cause .
  178. .I Accept
  179. returns a file descriptor for the data file opened
  180. .BR ORDWR .
  181. .PP
  182. .I Netmkaddr
  183. makes an address suitable for dialing or announcing.
  184. It takes an address along with a default network and service to use
  185. if they are not specified in the address.
  186. It returns a pointer to static data holding the actual address to use.
  187. .PP
  188. .I Getnetconninfo
  189. returns a structure containing information about a
  190. network connection. The structure is:
  191. .EX
  192. typedef struct NetConnInfo NetConnInfo;
  193. struct NetConnInfo
  194. {
  195. char *dir; /* connection directory */
  196. char *root; /* network root */
  197. char *spec; /* binding spec */
  198. char *lsys; /* local system */
  199. char *lserv; /* local service */
  200. char *rsys; /* remote system */
  201. char *rserv; /* remote service */
  202. char *laddr; /* local address */
  203. char *raddr; /* remote address */
  204. };
  205. .EE
  206. .PP
  207. The information is obtained from the connection directory,
  208. .IR conndir .
  209. If
  210. .I conndir
  211. is nil, the directory is obtained by performing
  212. .IR fd2path (2)
  213. on
  214. .IR fd .
  215. .I Getnetconninfo
  216. returns either a completely specified structure, or
  217. nil if either the structure can't be allocated or the
  218. network directory can't be determined.
  219. The structure
  220. is freed using
  221. .IR freenetconninfo .
  222. .PP
  223. .I Setnetmtpt
  224. copies the name of the network mount point into
  225. the buffer
  226. .IR to ,
  227. whose length is
  228. .IR tolen .
  229. It exists to merge two pre-existing conventions for specifying
  230. the mount point.
  231. Commands that take a network mount point as a parameter
  232. (such as
  233. .BR dns ,
  234. .BR cs
  235. (see
  236. .IR ndb (8)),
  237. and
  238. .IR ipconfig (8))
  239. should now call
  240. .IR setnetmtpt .
  241. If
  242. .I from
  243. is
  244. .BR nil ,
  245. the mount point is set to the default,
  246. .BR /net .
  247. If
  248. .I from
  249. points to a string starting with a slash,
  250. the mount point is that path.
  251. Otherwise, the mount point is the string pointed to by
  252. .I from
  253. appended to the string
  254. .BR /net .
  255. The last form is obsolete and is should be avoided.
  256. It exists only to aid in conversion.
  257. .SH EXAMPLES
  258. Make a call and return an open file descriptor to
  259. use for communications:
  260. .IP
  261. .EX
  262. int callkremvax(void)
  263. {
  264. return dial("kremvax", 0, 0, 0);
  265. }
  266. .EE
  267. .PP
  268. Call the local authentication server:
  269. .IP
  270. .EX
  271. int dialauth(char *service)
  272. {
  273. return dial(netmkaddr("$auth", 0, service), 0, 0, 0);
  274. }
  275. .EE
  276. .PP
  277. Announce as
  278. .B kremvax
  279. on TCP/IP and
  280. loop forever receiving calls and echoing back
  281. to the caller anything sent:
  282. .IP
  283. .EX
  284. int
  285. bekremvax(void)
  286. {
  287. int dfd, acfd, lcfd;
  288. char adir[NETPATHLEN], ldir[NETPATHLEN];
  289. int n;
  290. char buf[256];
  291. acfd = announce("tcp!*!7", adir);
  292. if(acfd < 0)
  293. return -1;
  294. for(;;){
  295. /* listen for a call */
  296. lcfd = listen(adir, ldir);
  297. if(lcfd < 0)
  298. return -1;
  299. /* fork a process to echo */
  300. switch(fork()){
  301. case -1:
  302. perror("forking");
  303. close(lcfd);
  304. break;
  305. case 0:
  306. /* accept the call and open the data file */
  307. dfd = accept(lcfd, ldir);
  308. if(dfd < 0)
  309. return -1;
  310. /* echo until EOF */
  311. while((n = read(dfd, buf, sizeof(buf))) > 0)
  312. write(dfd, buf, n);
  313. exits(0);
  314. default:
  315. close(lcfd);
  316. break;
  317. }
  318. }
  319. }
  320. .EE
  321. .SH SOURCE
  322. .BR /sys/src/libc/9sys ,
  323. .B /sys/src/libc/port
  324. .SH "SEE ALSO"
  325. .IR auth (2),
  326. .IR ip (3),
  327. .IR ndb (8)
  328. .SH DIAGNOSTICS
  329. .IR Dial ,
  330. .IR announce ,
  331. and
  332. .I listen
  333. return \-1 if they fail.
  334. .I Hangup
  335. returns nonzero if it fails.