blob: 5c64528777742a33c5088facc9af9df4f14176d9 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH DIAL 3
2.SH NAME
rsc30f6ae12005-02-13 23:44:12 +00003dial, announce, listen, accept, reject, netmkaddr, getnetconninfo, freenetconninfo, dialparse \- make and break network connections
rsccfa37a72004-04-10 18:53:55 +00004.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.PP
9.B
10int dial(char *addr, char *local, char *dir, int *cfdp)
11.PP
12.B
rsccfa37a72004-04-10 18:53:55 +000013int announce(char *addr, char *dir)
14.PP
15.B
16int listen(char *dir, char *newdir)
17.PP
18.B
19int accept(int ctl, char *dir)
20.PP
21.B
22int reject(int ctl, char *dir, char *cause)
23.PP
24.B
25char* netmkaddr(char *addr, char *defnet, char *defservice)
rsc058b0112005-01-03 06:40:20 +000026.\" .PP
27.\" .B
28.\" void setnetmtpt(char *to, int tolen, char *from)
rsc30f6ae12005-02-13 23:44:12 +000029.PP
30.B
31NetConnInfo* getnetconninfo(char *dir, int fd)
32.PP
33.B
34void freenetconninfo(NetConnINfo*)
rsccfa37a72004-04-10 18:53:55 +000035.PP
36.B
rscc8b63422005-01-13 04:49:19 +000037int dialparse(char *addr, char **net, char **unix,
38.br
39.B
40 u32int *host, int *port)
rsccfa37a72004-04-10 18:53:55 +000041.SH DESCRIPTION
42For these routines,
43.I addr
44is a network address of the form
45.IB network ! netaddr ! service\f1,
46.IB network ! netaddr\f1,
47or simply
48.IR netaddr .
49.I Network
rsc058b0112005-01-03 06:40:20 +000050is
51.BR tcp ,
52.BR udp ,
53.BR unix ,
rsccfa37a72004-04-10 18:53:55 +000054or the special token,
55.BR net .
56.B Net
57is a free variable that stands for any network in common
58between the source and the host
59.IR netaddr .
60.I Netaddr
rsc058b0112005-01-03 06:40:20 +000061can 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)).
rsccfa37a72004-04-10 18:53:55 +000072.PP
rsc058b0112005-01-03 06:40:20 +000073On Plan 9, the
rsccfa37a72004-04-10 18:53:55 +000074.I dir
rsc058b0112005-01-03 06:40:20 +000075argument is a path name to a
rsccfa37a72004-04-10 18:53:55 +000076.I line directory
rsc058b0112005-01-03 06:40:20 +000077that has files for accessing the connection.
78To keep the same function signatures,
79the Unix port of these routines uses strings of the form
80.BI /dev/fd/ n
81instead of line directory paths.
82These strings should be treated as opaque data and ignored.
rsccfa37a72004-04-10 18:53:55 +000083.PP
84.I Dial
85makes a call to destination
86.I addr
87on a multiplexed network.
88If the network in
89.I addr
90is
91.BR net ,
92.I dial
93will try in succession all
94networks in common between source and destination
95until a call succeeds.
96It returns a file descriptor open for reading and writing the
rsc30f6ae12005-02-13 23:44:12 +000097call.
98.\" .B data
99.\" file in the line directory.
100.\" The
101.\" .B addr
102.\" file in the line directory contains the address called.
103If the network allows the local address to be set,
104as is the case with UDP and TCP port numbers, and
105.IR local
106is non-zero, the local address will be set to
107.IR local .
rsc058b0112005-01-03 06:40:20 +0000108.IR Dial 's
rsc30f6ae12005-02-13 23:44:12 +0000109.IR dir
rsccfa37a72004-04-10 18:53:55 +0000110and
rsc058b0112005-01-03 06:40:20 +0000111.I cfdp
112arguments
113are not supported and must be zero.
114.PP
rsccfa37a72004-04-10 18:53:55 +0000115.I Announce
116and
117.I listen
118are the complements of
119.IR dial .
120.I Announce
121establishes a network
122name to which calls can be made.
123Like
124.IR dial ,
125.I announce
126returns an open
127.B ctl
128file.
129The
130.I netaddr
131used in announce may be a local address or an asterisk,
132to indicate all local addresses, e.g.
133.BR tcp!*!echo .
134The
135.I listen
136routine takes as its first argument the
137.I dir
138of a previous
139.IR announce .
140When a call is received,
141.I listen
142returns an open
143.B ctl
144file for the line the call was received on.
145It sets
146.I newdir
147to the path name of the new line directory.
148.I Accept
149accepts a call received by
150.IR listen ,
151while
152.I reject
153refuses the call because of
154.IR cause .
155.I Accept
156returns a file descriptor for the data file opened
157.BR ORDWR .
158.PP
159.I Netmkaddr
160makes an address suitable for dialing or announcing.
161It takes an address along with a default network and service to use
162if they are not specified in the address.
163It returns a pointer to static data holding the actual address to use.
164.PP
rsc6215fd52006-07-23 02:56:37 +0000165.I Netmkaddr
166also translates Unix conventions into Plan 9 syntax.
167If
168.I addr
169is the name of a local file or Unix domain socket,
170.I netmkaddr
171will return
172.IB unix ! addr \fR.
173If
174.I addr
175is of the form
176.IB host : port \fR,
177.I netmkaddr
178will return
179.IB net ! host ! port \fR.
180.PP
rsc058b0112005-01-03 06:40:20 +0000181.I Dialparse
182parses a network address as described above
183into a network name, a Unix domain socket address,
184an IPv4 host address, and an IPv4 port number.
rsc30f6ae12005-02-13 23:44:12 +0000185.PP
186.I Getnetconninfo
187returns a structure containing information about a
188network connection. The structure is:
189.PP
190.EX
191 typedef struct NetConnInfo NetConnInfo;
192 struct NetConnInfo
193 {
194 char *dir; /* connection directory */
195 char *root; /* network root */
196 char *spec; /* binding spec */
197 char *lsys; /* local system */
198 char *lserv; /* local service */
199 char *rsys; /* remote system */
200 char *rserv; /* remote service */
201 char *laddr; /* local address */
202 char *raddr; /* remote address */
203 };
204.EE
205.PP
206The information is obtained from the
207`line directory'
208.IR dir ,
209or if
210.I dir
211is nil, from the connection file descriptor
212.IR fd .
213.I Getnetconninfo
214returns either a completely specified structure, or
215nil if either the structure can't be allocated or the
216network directory can't be determined.
217The structure
218is freed using
219.IR freenetconninfo .
rsc058b0112005-01-03 06:40:20 +0000220.\" .PP
221.\" .I Setnetmtpt
222.\" copies the name of the network mount point into
223.\" the buffer
224.\" .IR to ,
225.\" whose length is
226.\" .IR tolen .
227.\" It exists to merge two pre-existing conventions for specifying
228.\" the mount point.
229.\" Commands that take a network mount point as a parameter
230.\" (such as
231.\" .BR dns ,
232.\" .BR cs
233.\" (see
234.\" .IR ndb (8)),
235.\" and
236.\" .IR ipconfig (8))
237.\" should now call
238.\" .IR setnetmtpt .
239.\" If
240.\" .I from
241.\" is
242.\" .BR nil ,
243.\" the mount point is set to the default,
244.\" .BR /net .
245.\" If
246.\" .I from
247.\" points to a string starting with a slash,
248.\" the mount point is that path.
249.\" Otherwise, the mount point is the string pointed to by
250.\" .I from
251.\" appended to the string
252.\" .BR /net .
253.\" The last form is obsolete and is should be avoided.
254.\" It exists only to aid in conversion.
rsccfa37a72004-04-10 18:53:55 +0000255.SH EXAMPLES
256Make a call and return an open file descriptor to
257use for communications:
258.IP
259.EX
260int callkremvax(void)
261{
262 return dial("kremvax", 0, 0, 0);
263}
264.EE
265.PP
rsc058b0112005-01-03 06:40:20 +0000266Connect to a Unix socket served by
267.IR acme (4):
rsccfa37a72004-04-10 18:53:55 +0000268.IP
269.EX
rsc058b0112005-01-03 06:40:20 +0000270int dialacme(void)
rsccfa37a72004-04-10 18:53:55 +0000271{
rsc058b0112005-01-03 06:40:20 +0000272 return dial("unix!/tmp/ns.ken.:0/acme", 0, 0, 0);
rsccfa37a72004-04-10 18:53:55 +0000273}
274.EE
275.PP
276Announce as
277.B kremvax
278on TCP/IP and
279loop forever receiving calls and echoing back
280to the caller anything sent:
281.IP
282.EX
283int
284bekremvax(void)
285{
286 int dfd, acfd, lcfd;
287 char adir[40], ldir[40];
288 int n;
289 char buf[256];
290
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
311 /* echo until EOF */
312 while((n = read(dfd, buf, sizeof(buf))) > 0)
313 write(dfd, buf, n);
314 exits(0);
315 default:
316 close(lcfd);
317 break;
318 }
319 }
320}
321.EE
322.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000323.B \*9/src/lib9/dial.c
rsc058b0112005-01-03 06:40:20 +0000324.br
rscc3674de2005-01-11 17:37:33 +0000325.B \*9/src/lib9/announce.c
rsc058b0112005-01-03 06:40:20 +0000326.br
rscc3674de2005-01-11 17:37:33 +0000327.B \*9/src/lib9/_p9dialparse.c
rsc30f6ae12005-02-13 23:44:12 +0000328.br
329.B \*9/src/lib9/getnetconn.c
rsccfa37a72004-04-10 18:53:55 +0000330.SH DIAGNOSTICS
331.IR Dial ,
332.IR announce ,
333and
334.I listen
335return \-1 if they fail.
rscc8b63422005-01-13 04:49:19 +0000336.SH BUGS
337To avoid name conflicts with the underlying system,
338.IR dial ,
339.IR announce ,
340.IR listen ,
341.IR netmkaddr ,
342and
343.I reject
344are preprocessor macros defined as
345.IR p9dial ,
346.IR p9announce ,
347and so on;
348see
349.IR intro (3).