Blob


1 .TH DIAL 3
2 .SH NAME
3 dial, hangup, announce, listen, accept, reject, netmkaddr, dialparse \- 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 announce(char *addr, char *dir)
14 .PP
15 .B
16 int listen(char *dir, char *newdir)
17 .PP
18 .B
19 int accept(int ctl, char *dir)
20 .PP
21 .B
22 int reject(int ctl, char *dir, char *cause)
23 .PP
24 .B
25 char* netmkaddr(char *addr, char *defnet, char *defservice)
26 .\" .PP
27 .\" .B
28 .\" void setnetmtpt(char *to, int tolen, char *from)
29 .\" .PP
30 .\" .B
31 .\" NetConnInfo* getnetconninfo(char *conndir, int fd)
32 .\" .PP
33 .\" .B
34 .\" void freenetconninfo(NetConnINfo*)
35 .PP
36 .B
37 int dialparse(char *addr, char **net, char **unix, u32int *host, int *port)
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
48 .BR tcp ,
49 .BR udp ,
50 .BR unix ,
51 or the special token,
52 .BR net .
53 .B Net
54 is a free variable that stands for any network in common
55 between the source and the host
56 .IR netaddr .
57 .I Netaddr
58 can be a host name, a domain name, or a network address.
59 .\" or a meta-name of the form
60 .\" .BI $ attribute\f1,
61 .\" which
62 .\" is replaced by
63 .\" .I value
64 .\" from the value-attribute pair
65 .\" .IB attribute = value
66 .\" most closely associated with the source host in the
67 .\" network data base (see
68 .\" .IR ndb (6)).
69 .PP
70 On Plan 9, the
71 .I dir
72 argument is a path name to a
73 .I line directory
74 that has files for accessing the connection.
75 To keep the same function signatures,
76 the Unix port of these routines uses strings of the form
77 .BI /dev/fd/ n
78 instead of line directory paths.
79 These strings should be treated as opaque data and ignored.
80 .PP
81 .I Dial
82 makes a call to destination
83 .I addr
84 on a multiplexed network.
85 If the network in
86 .I addr
87 is
88 .BR net ,
89 .I dial
90 will try in succession all
91 networks in common between source and destination
92 until a call succeeds.
93 It returns a file descriptor open for reading and writing the
94 .B data
95 file in the line directory.
96 The
97 .B addr
98 file in the line directory contains the address called.
99 .\" If the network allows the local address to be set,
100 .\" as is the case with UDP and TCP port numbers, and
101 .\" .IR local
102 .\" is non-zero, the local address will be set to
103 .\" .IR local .
104 .IR Dial 's
105 .IR local ,
106 .IR dir ,
107 and
108 .I cfdp
109 arguments
110 are not supported and must be zero.
111 .PP
112 .I Announce
113 and
114 .I listen
115 are the complements of
116 .IR dial .
117 .I Announce
118 establishes a network
119 name to which calls can be made.
120 Like
121 .IR dial ,
122 .I announce
123 returns an open
124 .B ctl
125 file.
126 The
127 .I netaddr
128 used in announce may be a local address or an asterisk,
129 to indicate all local addresses, e.g.
130 .BR tcp!*!echo .
131 The
132 .I listen
133 routine takes as its first argument the
134 .I dir
135 of a previous
136 .IR announce .
137 When a call is received,
138 .I listen
139 returns an open
140 .B ctl
141 file for the line the call was received on.
142 It sets
143 .I newdir
144 to the path name of the new line directory.
145 .I Accept
146 accepts a call received by
147 .IR listen ,
148 while
149 .I reject
150 refuses the call because of
151 .IR cause .
152 .I Accept
153 returns a file descriptor for the data file opened
154 .BR ORDWR .
155 .PP
156 .I Netmkaddr
157 makes an address suitable for dialing or announcing.
158 It takes an address along with a default network and service to use
159 if they are not specified in the address.
160 It returns a pointer to static data holding the actual address to use.
161 .PP
162 .I Dialparse
163 parses a network address as described above
164 into a network name, a Unix domain socket address,
165 an IPv4 host address, and an IPv4 port number.
166 .\" .PP
167 .\" .I Getnetconninfo
168 .\" returns a structure containing information about a
169 .\" network connection. The structure is:
170 .\" .EX
171 .\" typedef struct NetConnInfo NetConnInfo;
172 .\" struct NetConnInfo
173 .\" {
174 .\" char *dir; /* connection directory */
175 .\" char *root; /* network root */
176 .\" char *spec; /* binding spec */
177 .\" char *lsys; /* local system */
178 .\" char *lserv; /* local service */
179 .\" char *rsys; /* remote system */
180 .\" char *rserv; /* remote service */
181 .\" };
182 .\" .EE
183 .\" .PP
184 .\" The information is obtained from the connection directory,
185 .\" .IR conndir .
186 .\" If
187 .\" .I conndir
188 .\" is nil, the directory is obtained by performing
189 .\" .IR fd2path (3)
190 .\" on
191 .\" .IR fd .
192 .\" .I Getnetconninfo
193 .\" returns either a completely specified structure, or
194 .\" nil if either the structure can't be allocated or the
195 .\" network directory can't be determined.
196 .\" The structure
197 .\" is freed using
198 .\" .IR freenetconninfo .
199 .\" .PP
200 .\" .I Setnetmtpt
201 .\" copies the name of the network mount point into
202 .\" the buffer
203 .\" .IR to ,
204 .\" whose length is
205 .\" .IR tolen .
206 .\" It exists to merge two pre-existing conventions for specifying
207 .\" the mount point.
208 .\" Commands that take a network mount point as a parameter
209 .\" (such as
210 .\" .BR dns ,
211 .\" .BR cs
212 .\" (see
213 .\" .IR ndb (8)),
214 .\" and
215 .\" .IR ipconfig (8))
216 .\" should now call
217 .\" .IR setnetmtpt .
218 .\" If
219 .\" .I from
220 .\" is
221 .\" .BR nil ,
222 .\" the mount point is set to the default,
223 .\" .BR /net .
224 .\" If
225 .\" .I from
226 .\" points to a string starting with a slash,
227 .\" the mount point is that path.
228 .\" Otherwise, the mount point is the string pointed to by
229 .\" .I from
230 .\" appended to the string
231 .\" .BR /net .
232 .\" The last form is obsolete and is should be avoided.
233 .\" It exists only to aid in conversion.
234 .SH EXAMPLES
235 Make a call and return an open file descriptor to
236 use for communications:
237 .IP
238 .EX
239 int callkremvax(void)
241 return dial("kremvax", 0, 0, 0);
243 .EE
244 .PP
245 Connect to a Unix socket served by
246 .IR acme (4):
247 .IP
248 .EX
249 int dialacme(void)
251 return dial("unix!/tmp/ns.ken.:0/acme", 0, 0, 0);
253 .EE
254 .PP
255 Announce as
256 .B kremvax
257 on TCP/IP and
258 loop forever receiving calls and echoing back
259 to the caller anything sent:
260 .IP
261 .EX
262 int
263 bekremvax(void)
265 int dfd, acfd, lcfd;
266 char adir[40], ldir[40];
267 int n;
268 char buf[256];
270 acfd = announce("tcp!*!7", adir);
271 if(acfd < 0)
272 return -1;
273 for(;;){
274 /* listen for a call */
275 lcfd = listen(adir, ldir);
276 if(lcfd < 0)
277 return -1;
278 /* fork a process to echo */
279 switch(fork()){
280 case -1:
281 perror("forking");
282 close(lcfd);
283 break;
284 case 0:
285 /* accept the call and open the data file */
286 dfd = accept(lcfd, ldir);
287 if(dfd < 0)
288 return -1;
290 /* echo until EOF */
291 while((n = read(dfd, buf, sizeof(buf))) > 0)
292 write(dfd, buf, n);
293 exits(0);
294 default:
295 close(lcfd);
296 break;
300 .EE
301 .SH SOURCE
302 .B \*9/src/lib9/dial.c
303 .br
304 .B \*9/src/lib9/announce.c
305 .br
306 .B \*9/src/lib9/_p9dialparse.c
307 .SH DIAGNOSTICS
308 .IR Dial ,
309 .IR announce ,
310 and
311 .I listen
312 return \-1 if they fail.