summaryrefslogtreecommitdiff
path: root/comm/InetAddress.cc
blob: ffb374ef036811c183564f8b2a75ee2a76865480 (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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1997
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	INET Addresses
 *
 *	$Id$
 *	$CurLog$
 *	Created from Address.cc
 */

#include "InetAddress.h"

#include <sys/utsname.h>
#include <stdio.h>

#ifdef __osf__
extern "C" {
	unsigned short htons (unsigned short);
	unsigned short ntohs (unsigned short);
	int gethostname (char*, int);
#endif
#include <netdb.h>
#ifdef __osf__
}
#endif


//	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	??
//
/*?
IvlInetAddresses should only be allocated with new because they 
can be deleted and reallocated by \type{IvlSocket} class.
?*/
IvlInetAddress :: IvlInetAddress ()
: IvlAddress (),
  HostName (),
  HasHostName (false)
{
}

/*?
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 (INADDR_ANY) is used.
If the port number is 0, it will be allocated by the system.
?*/
IvlInetAddress :: IvlInetAddress (const char* name, sword port)
: IvlAddress (),
  HostName (),
  HasHostName (false)
{
	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.
?*/
IvlInetAddress :: IvlInetAddress (lword host, sword port)
: IvlAddress (),
  HostName (),
  HasHostName (false)
{
	Addr.sin_family = AF_INET;
	Addr.sin_port = htons (port);
	Addr.sin_addr.s_addr = host;
	Valid = true;
}

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

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

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

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


/*?
This is a global function (static member of class \typ{IvlInetAddress}.
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
IvlInetAddress :: 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
// Ca c'est les super fake declarations pour avoir la DOC
// merci au LRI...
/*?
Return the host number of the address.
?*/
lword
IvlInetAddress :: Host ()
{}

/*?
Return the port number of the address.
?*/
sword
IvlInetAddress :: Port ()
{}
#endif /* DOC */

/*?
Return a const string representing the hostname part of the address if any 
or NULL if it does not exist (INADDR_ANY, INADDR_LOOPBACK or INADDR_BROADCAST).
This is an interface to unix \fun{gethostbyaddr} system call.
?*/
const char*
IvlInetAddress :: GetHostName ()
{
	struct hostent	*host;
	unsigned long	saddr;

	if (HasHostName)
		return HostName;
	
	saddr = Addr.sin_addr.s_addr;
	host = gethostbyaddr ((char*) &saddr, sizeof (long), AF_INET);
	if (host)
		HostName = host->h_name;
#if 0
	} else 
		HostName = GetNoHostName();	
#endif
	else {
		struct utsname un;
		if (uname (&un) < 0) {
			Error (ErrFatal, "GetHostName", "cannot get name of local host");
			HostName = 0;
		}
		HostName = un.nodename;
	}
	HasHostName = true;
	return HostName;
}

/*?nodoc?*/
const char*
IvlInetAddress :: GetNoHostName ()
{
	const char* hname;
	unsigned long	saddr = Addr.sin_addr.s_addr;
	if (saddr == INADDR_ANY)
		hname = "ANY";
	else if (saddr == INADDR_LOOPBACK)
		hname = "LOOPBACK";
	else if (saddr == INADDR_BROADCAST)
		hname = "BROADCAST";
	else
		hname = "???";	
	return hname;
}

/*?
Return a newly allocated \typ{char*} pointing to a string representing 
the internet address as "hostname:port". If no host name can be found
then it can be "ANY" (INADDR_ANY), "LOOPBACK" (INADDR_LOOPBACK), "BROADCAST"
(INADDR_BROADCAST) or "???".
If argument \var{buf} is not null, return it's value in buf and do not
allocate any memory.
?*/
char*
IvlInetAddress :: StrRepr (char* buf)
{
	const char* hname;
	if (HasHostName) {
		hname = HostName;
		if (!hname)
			hname = GetNoHostName ();
	} else
		hname = GetHostName ();
	if (! buf)
		buf = new char [ strlen(hname) + 2 + 7 ];
	sprintf (buf, "%s::%d",hname, ntohs (Addr.sin_port));
	return buf;
}