Blob


1 .TH DIAL 3
2 .SH NAME
3 dial, announce, listen, accept, reject, netmkaddr, getnetconninfo, freenetconninfo, 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 *dir, 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 call.
98 .\" .B data
99 .\" file in the line directory.
100 .\" The
101 .\" .B addr
102 .\" file in the line directory contains the address called.
103 If the network allows the local address to be set,
104 as is the case with UDP and TCP port numbers, and
105 .IR local
106 is non-zero, the local address will be set to
107 .IR local .
108 .IR Dial 's
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 .PP
174 .EX
175 typedef struct NetConnInfo NetConnInfo;
176 struct NetConnInfo
178 char *dir; /* connection directory */
179 char *root; /* network root */
180 char *spec; /* binding spec */
181 char *lsys; /* local system */
182 char *lserv; /* local service */
183 char *rsys; /* remote system */
184 char *rserv; /* remote service */
185 char *laddr; /* local address */
186 char *raddr; /* remote address */
187 };
188 .EE
189 .PP
190 The information is obtained from the
191 `line directory'
192 .IR dir ,
193 or if
194 .I dir
195 is nil, from the connection file descriptor
196 .IR fd .
197 .I Getnetconninfo
198 returns either a completely specified structure, or
199 nil if either the structure can't be allocated or the
200 network directory can't be determined.
201 The structure
202 is freed using
203 .IR freenetconninfo .
204 .\" .PP
205 .\" .I Setnetmtpt
206 .\" copies the name of the network mount point into
207 .\" the buffer
208 .\" .IR to ,
209 .\" whose length is
210 .\" .IR tolen .
211 .\" It exists to merge two pre-existing conventions for specifying
212 .\" the mount point.
213 .\" Commands that take a network mount point as a parameter
214 .\" (such as
215 .\" .BR dns ,
216 .\" .BR cs
217 .\" (see
218 .\" .IR ndb (8)),
219 .\" and
220 .\" .IR ipconfig (8))
221 .\" should now call
222 .\" .IR setnetmtpt .
223 .\" If
224 .\" .I from
225 .\" is
226 .\" .BR nil ,
227 .\" the mount point is set to the default,
228 .\" .BR /net .
229 .\" If
230 .\" .I from
231 .\" points to a string starting with a slash,
232 .\" the mount point is that path.
233 .\" Otherwise, the mount point is the string pointed to by
234 .\" .I from
235 .\" appended to the string
236 .\" .BR /net .
237 .\" The last form is obsolete and is should be avoided.
238 .\" It exists only to aid in conversion.
239 .SH EXAMPLES
240 Make a call and return an open file descriptor to
241 use for communications:
242 .IP
243 .EX
244 int callkremvax(void)
246 return dial("kremvax", 0, 0, 0);
248 .EE
249 .PP
250 Connect to a Unix socket served by
251 .IR acme (4):
252 .IP
253 .EX
254 int dialacme(void)
256 return dial("unix!/tmp/ns.ken.:0/acme", 0, 0, 0);
258 .EE
259 .PP
260 Announce as
261 .B kremvax
262 on TCP/IP and
263 loop forever receiving calls and echoing back
264 to the caller anything sent:
265 .IP
266 .EX
267 int
268 bekremvax(void)
270 int dfd, acfd, lcfd;
271 char adir[40], ldir[40];
272 int n;
273 char buf[256];
275 acfd = announce("tcp!*!7", adir);
276 if(acfd < 0)
277 return -1;
278 for(;;){
279 /* listen for a call */
280 lcfd = listen(adir, ldir);
281 if(lcfd < 0)
282 return -1;
283 /* fork a process to echo */
284 switch(fork()){
285 case -1:
286 perror("forking");
287 close(lcfd);
288 break;
289 case 0:
290 /* accept the call and open the data file */
291 dfd = accept(lcfd, ldir);
292 if(dfd < 0)
293 return -1;
295 /* echo until EOF */
296 while((n = read(dfd, buf, sizeof(buf))) > 0)
297 write(dfd, buf, n);
298 exits(0);
299 default:
300 close(lcfd);
301 break;
305 .EE
306 .SH SOURCE
307 .B \*9/src/lib9/dial.c
308 .br
309 .B \*9/src/lib9/announce.c
310 .br
311 .B \*9/src/lib9/_p9dialparse.c
312 .br
313 .B \*9/src/lib9/getnetconn.c
314 .SH DIAGNOSTICS
315 .IR Dial ,
316 .IR announce ,
317 and
318 .I listen
319 return \-1 if they fail.
320 .SH BUGS
321 To avoid name conflicts with the underlying system,
322 .IR dial ,
323 .IR announce ,
324 .IR listen ,
325 .IR netmkaddr ,
326 and
327 .I reject
328 are preprocessor macros defined as
329 .IR p9dial ,
330 .IR p9announce ,
331 and so on;
332 see
333 .IR intro (3).