summaryrefslogtreecommitdiff
path: root/comm/Address.cc
blob: 03391d78bce89d148decb5e6a8c98cb51f7bf9d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Addresses
 *
 *	$Id$
 *	$CurLog$
 */

#include "Address.h"
#include "error.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <memory.h>
#if 1
#include <sys/utsname.h>
#endif
#ifdef __osf__
extern "C" {
#endif
#include <netdb.h>
#ifdef __osf__
}
#endif

#ifdef __osf__
extern "C" {
	unsigned short htons (unsigned short);
	unsigned short ntohs (unsigned short);
	int gethostname (char*, int);
}
#endif

/*?class UchAddress
This class is a virtual base class: no objects of this class are ever created.
It implements the Unix type \typ{^{struct sockaddr}}, used for creating sockets.
Addresses are used mainly as arguments of constructors for the class
\typ{UchSocket} and its derived classes.
All its member functions are virtual.
These functions exist for all the derived classes but are only described here.
An address can be valid or invalid, depending on the constructor being able to create the address or not.
There are currently two derived classes, for Unix domain addresses and Internet domain addresses.

The class \typ{^{pUchAddress}} implements smart pointers to addresses.
Smart pointers behave like pointers but they manage a reference count on the
pointed to objects for an automatic reclaim of dynamically allocated objects.
?*/

/*?
Construct an invalid address.
?*/
UchAddress :: UchAddress ()
: Valid (false)
{
}

/*?nodoc?*/
UchAddress :: ~UchAddress ()
{
}

#ifdef DOC
// fake entries for inline functions

/*?
Return true if the address is valid.
?*/
bool 
UchAddress :: IsValid ()
{
}

/*?
Return one of \var{AF\_UNSPEC}, \var{AF\_UNIX} or \var{AF\_INET}.
Other values may be defined if supported by the system.
?*/
int
UchAddress :: Family ()
{
}

/*?nextdoc?*/
int
UchAddress :: Length ()
{
}

/*?
Return the address structure and its length.
\typ{^{SockAddr}} is a typedef for \typ{^{struct sockaddr}}.
?*/
SockAddr*
UchAddress :: GetSockAddr ()
{
}

/*?nodoc?*/
char*
UchAddress :: StrRepr (char* buf)
{
}
#endif	/* DOC */

#ifdef UNIX_SOCK

//	constructors for Unix addresses
//	a Unix domain address is just a filename
//	-> some problems: check for access, when to unlink it ?
//	to do : generate name from a template (for uniqueness)
//

/*?nodoc?*/
UchUnixAddress :: UchUnixAddress ()
: UchAddress ()
{
}

/*?
Create a Unix domain address associated to file \var{filename}.
The file is not unlinked when the address is destroyed;
it should be unlinked by the application whenever the last socket using this address is closed.
?*/
UchUnixAddress :: UchUnixAddress (const char* filename)
: UchAddress ()
{
	Addr.sun_family = AF_UNIX;
	strcpy (Addr.sun_path, filename);
	Valid = true;
	// should check access to file ???
	// unlink if already there ?
}

/*?nodoc?*/
UchUnixAddress :: ~UchUnixAddress ()
{
	// how to unlink the file ??
}

/*?nodoc?*/
int
UchUnixAddress :: Family ()
{
	return AF_UNIX;
}

/*?nodoc?*/
int
UchUnixAddress :: Length ()
{
	if (Valid)
		return (Addr.sun_path + strlen (Addr.sun_path)) - (char*) &Addr;
	return 0;
}

/*?nodoc?*/
SockAddr*
UchUnixAddress :: GetSockAddr ()
{
	return (SockAddr*) &Addr;
}

/*?nodoc?*/
char*
UchUnixAddress :: StrRepr (char* buf)
{
	sprintf (buf, "%s", Addr.sun_path);
	return buf;
}

#endif /* UNIX_SOCK */

//	constructors for inet addresses
//	inet address specified by:
//		hostname / port#	(ANY if hostname is NIL, LOOPBACK if hostname is "")
//		hostid / port#	(predefined hostids ANYADDR, ...)
//	todo:
//		hostname / service name	??
//
/*?nodoc?*/
UchInetAddress :: UchInetAddress ()
: UchAddress ()
{
}

/*?
Construct an inet address, from a hostname and a port number.
The address is valid only if the host is valid.
If \var{host} is the empty string, the loopback host is used;
if it is the nil pointer, the wildcard is used.
If the port number is 0, it will be allocated by the system.
?*/
UchInetAddress :: UchInetAddress (const char* name, sword port)
: UchAddress ()
{
	if (name && *name) {
		struct hostent *host = gethostbyname (name);
		if (! host)
			return;
		memcpy (&Addr.sin_addr, host->h_addr, host->h_length);
	} else
		Addr.sin_addr.s_addr = name ? INADDR_LOOPBACK : INADDR_ANY;
	Addr.sin_family = AF_INET;
	Addr.sin_port = htons (port);
	Valid = true;
}

/*?
Construct an inet address, from a host id (internet address) and a port number.
If the port number is 0, it is assigned by the system.
The host ids \var{^{ANYADDR}}, \var{^{LOOPBACK}} and \var{^{BROADCAST}} are predefined.
\var{ANYADDR} is used to receive messages from anywhere.
\var{LOOPBACK} is the local host address.
\var{BROADCAST} makes it possible to send data to all the hosts of a local area network.
?*/
UchInetAddress :: UchInetAddress (lword host, sword port)
: UchAddress ()
{
	Addr.sin_family = AF_INET;
	Addr.sin_port = htons (port);
	Addr.sin_addr.s_addr = host;
	Valid = true;
}

/*?nodoc?*/
UchInetAddress :: ~UchInetAddress ()
{
}

/*?nodoc?*/
int
UchInetAddress :: Family ()
{
	return AF_INET;
}

/*?nodoc?*/
int
UchInetAddress :: Length ()
{
	return sizeof (Addr);
}

/*?nodoc?*/
SockAddr*
UchInetAddress :: GetSockAddr ()
{
	return (SockAddr*) &Addr;
}

/*?nodoc?*/
char*
UchInetAddress :: StrRepr (char* buf)
{
	struct hostent	*host;
	char		*hname;
	unsigned long	saddr;
	
	saddr = Addr.sin_addr.s_addr;
	host = gethostbyaddr ((char*) &saddr, sizeof (long), AF_INET);
	if (host)
		hname = host->h_name;
	else
		if (saddr == INADDR_ANY)
			hname = "ANY";
		else if (saddr == INADDR_LOOPBACK)
			hname = "LOOPBACK";
		else if (saddr == INADDR_BROADCAST)
			hname = "BROADCAST";
		else
			hname = "???";
	sprintf (buf, "%s::%d", hname, ntohs (Addr.sin_port));
	return buf;
}

/*?
This is a global function (static member of class \typ{UchAddress}).
It creates an object of a derived class of \typ{UchAddress} from a generic address
(thus is cannot be replaced by a constructor).
A generic address is the following union of address structures
(it is typically returned by system calls like \fun{recvfrom}):
\begin{ccode}
typedef union {
    struct sockaddr sa;        // default
    struct sockaddr_un su;     // Unix
    struct sockaddr_in si;     // inet
} GEN_ADDR;
\end{ccode}
?*/
UchAddress*
UchAddress :: Decode (GEN_ADDR* addr, int alen)
{
	switch (addr->sa.sa_family) {
#ifdef UNIX_SOCK
		case AF_UNIX :
			addr->su.sun_path [alen] = 0;
			return new UchUnixAddress (addr->su.sun_path);
#endif
		case AF_INET :
			return new UchInetAddress (addr->si.sin_addr.s_addr, addr->si.sin_port);
		
		default :
			return 0;
	}
}	

/*?
This is a global function (static member of class \typ{UchInetAddress}.
It returns the internet address of the local host.
This is different from the predefined address \var{LOOPBACK}:
\var{LOOPBACK} is the same constant on each machine, so that it cannot
be communicated across the network.
The value returned by \fun{LoopBack} is the unique internet address of the local host.
Thus it can be communicated to another host across the network.
?*/
lword
UchInetAddress :: LoopBack ()
{
	static	lword	loopback = 0;
	
	if (loopback)
		return loopback;
	
	struct hostent	*host;

#if 0
	char name [128];
	gethostname (name, sizeof (name));
	host = gethostbyname (name);
#else
	struct utsname un;
	if (uname (&un) < 0) {
		Error (ErrFatal, "address", "cannot get name of local host");
		return 0;
	}
	host = gethostbyname (un.nodename);
#endif

	if (! host) {
		Error (ErrFatal, "address", "cannot get address of local host");
		return 0;
	}
	return loopback = * ((lword *) host->h_addr);
}

#ifdef DOC
/*?
Return the host number of the address.
?*/
lword
UchInetAddress :: Host ()
{}

/*?
Return the port number of the address.
?*/
sword
UchInetAddress :: Port ()
{}

#endif /* DOC */