Blob


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