Subversion Repositories planix.SVN

Rev

Rev 34 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 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
 
292
	acfd = announce("tcp!*!7", adir);
293
	if(acfd < 0)
294
		return -1;
295
	for(;;){
296
		/* listen for a call */
297
		lcfd = listen(adir, ldir);
298
		if(lcfd < 0)
299
			return -1;
300
		/* fork a process to echo */
301
		switch(fork()){
302
		case -1:
303
			perror("forking");
304
			close(lcfd);
305
			break;
306
		case 0:
307
			/* accept the call and open the data file */
308
			dfd = accept(lcfd, ldir);
309
			if(dfd < 0)
310
				return -1;
311
 
312
			/* echo until EOF */
313
			while((n = read(dfd, buf, sizeof(buf))) > 0)
314
				write(dfd, buf, n);
315
			exits(0);
316
		default:
317
			close(lcfd);
318
			break;
319
		}
320
	}
321
}
322
.EE
323
.SH SOURCE
324
.BR /sys/src/libc/9sys ,
325
.B /sys/src/libc/port
326
.SH "SEE ALSO"
327
.IR auth (2),
328
.IR ip (3),
329
.IR ndb (8)
330
.SH DIAGNOSTICS
331
.IR Dial ,
332
.IR announce ,
333
and
334
.I listen
335
return \-1 if they fail.
336
.I Hangup
337
returns nonzero if it fails.