summaryrefslogtreecommitdiff
path: root/ace/INET_Addr.cpp
blob: 09ef23182c54b8dddf8e6345a6d8f8d1f8c90ddc (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// INET_Addr.cpp
// $Id$

// Defines the Internet domain address family address format. 

#define ACE_BUILD_DLL
#include "ace/INET_Addr.h"

#if !defined (__ACE_INLINE__)
#include "ace/INET_Addr.i"
#endif /* __ACE_INLINE__ */

ACE_ALLOC_HOOK_DEFINE(ACE_INET_Addr)

// Transform the current address into string format.

int
ACE_INET_Addr::addr_to_string (char s[], size_t) const
{
  ACE_TRACE ("ACE_INET_Addr::addr_to_string");
  // This should check to make sure len is long enough...
  ACE_OS::sprintf (s, "%s:%d",
		   this->get_host_addr (), 
		   this->get_port_number ()); 
  return 0;
}

void
ACE_INET_Addr::dump (void) const
{
  ACE_TRACE ("ACE_INET_Addr::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  char s[MAXNAMELEN];
  ACE_OS::sprintf (s, "%s:%d", this->get_host_addr (), this->get_port_number ()); 
  ACE_DEBUG ((LM_DEBUG, "%s", s));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

// Compare two addresses for inequality.

int
ACE_INET_Addr::operator != (const ACE_Addr &sap) const
{
  ACE_TRACE ("ACE_INET_Addr::operator !=");
  return !((*this) == sap);
}

// Compare two addresses for equality.

int
ACE_INET_Addr::operator == (const ACE_Addr &sap) const
{
  ACE_TRACE ("ACE_INET_Addr::operator ==");
  const sockaddr_in &inet_sap = ((const ACE_INET_Addr &) sap).inet_addr_;

  return this->inet_addr_.sin_port == inet_sap.sin_port
    && ACE_OS::memcmp ((void *) &this->inet_addr_.sin_addr, 
		       (void *) &inet_sap.sin_addr,
		       sizeof (this->inet_addr_.sin_addr)) == 0;
}

ACE_INET_Addr::ACE_INET_Addr (void)
  : ACE_Addr (AF_INET, sizeof this->inet_addr_)
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  (void) ACE_OS::memset ((void *) &this->inet_addr_, 0, 
			 sizeof this->inet_addr_);  
}

int
ACE_INET_Addr::set (const ACE_INET_Addr &sa)
{
  ACE_TRACE ("ACE_INET_Addr::set");
  this->ACE_Addr::base_set (AF_INET, sizeof this->inet_addr_);
  (void) ACE_OS::memcpy ((void *) &this->inet_addr_, 
			 (void *) &sa.inet_addr_, 
			 sizeof this->inet_addr_);
  return 0;
}

// Transform the string into the current addressing format.

int
ACE_INET_Addr::string_to_addr (const char s[])
{
  ACE_TRACE ("ACE_INET_Addr::string_to_addr");
  // Need to make a duplicate since we'll be overwriting the string.
  char *t = ACE_OS::strdup (s);
  if (t == 0)
    return -1;

  char *ip_addr = ACE_OS::strchr (t, ':');
  int result;

  if (ip_addr == 0) // Assume it's a port number.
    {
      u_short port = ACE_OS::atoi (t);
      result = this->set (port, ACE_UINT32 (INADDR_ANY));
    }
  else
    {
      *ip_addr = '\0';
      u_short port = ACE_OS::atoi (ip_addr + 1); // Skip over ':'
      result = this->set (port, t); 
    }
  ACE_OS::free (ACE_MALLOC_T (t));
  return result;
}

int
ACE_INET_Addr::set (const char address[])
{
  ACE_TRACE ("ACE_INET_Addr::set");
  return this->string_to_addr (address);
}

ACE_INET_Addr::ACE_INET_Addr (const char address[])
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  this->set (address);
}

// Copy constructor.

ACE_INET_Addr::ACE_INET_Addr (const ACE_INET_Addr &sa)
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  this->set (sa);
}

// Initializes a ACE_INET_Addr from a PORT_NUMBER and an Internet
// address. 

int
ACE_INET_Addr::set (u_short port_number, 
		    ACE_UINT32 inet_address, 
		    int encode) 
{
  ACE_TRACE ("ACE_INET_Addr::set");
  this->ACE_Addr::base_set (AF_INET, sizeof this->inet_addr_);
  (void) ACE_OS::memset ((void *) &this->inet_addr_, 
			 0, sizeof this->inet_addr_);
  this->inet_addr_.sin_family = AF_INET;
#if defined (ACE_HAS_SIN_LEN)
  this->inet_addr_.sin_len = sizeof this->inet_addr_;
#endif /* ACE_HAS_SIN_LEN */

  if (encode)
    {
      inet_address = htonl (inet_address);
      this->inet_addr_.sin_port = htons (port_number);
    }
  else
    this->inet_addr_.sin_port = port_number;

  (void) ACE_OS::memcpy ((void *) &this->inet_addr_.sin_addr, 
			 (void *) &inet_address,
			 sizeof this->inet_addr_.sin_addr);
  return 0;
}

// Initializes a ACE_INET_Addr from a PORT_NUMBER and the remote
// HOST_NAME. 

int
ACE_INET_Addr::set (u_short port_number, 
		    const char host_name[], 
		    int encode)
{
  ACE_TRACE ("ACE_INET_Addr::set");
  ACE_UINT32 addr;
  
  this->ACE_Addr::base_set (AF_INET, sizeof this->inet_addr_);
  (void) ACE_OS::memset ((void *) &this->inet_addr_, 0, sizeof
			 this->inet_addr_); 

  // Yow, someone gave us a NULL host_name!
  if (host_name == 0)
    {
      errno = EINVAL;
      return -1;
    }
  else if (ACE_OS::inet_aton (host_name, (struct in_addr *) &addr) == 1)
    return this->set (port_number, encode ? ntohl (addr) : addr, encode);

  else 
    {
#if defined (VXWORKS)
      hostent *hp = ACE_OS::gethostbyname (host_name);
#else
      hostent hentry;
      ACE_HOSTENT_DATA buf;
      int error;

      hostent *hp = ACE_OS::gethostbyname_r (host_name, &hentry,
					     buf, &error);
#endif /* VXWORKS */
  
      if (hp == 0)
	{
	  errno = EINVAL;
	  return -1;
	}
      else
	{
	  (void) ACE_OS::memcpy ((void *) &addr, hp->h_addr, hp->h_length); 
	  return this->set (port_number, encode ? ntohl (addr) : addr, encode);
	}
    }
}
 
// Initializes a ACE_INET_Addr from a <port_name> and the remote
// <host_name>. 

int
ACE_INET_Addr::set (const char port_name[], 
		    const char host_name[])
{
  ACE_TRACE ("ACE_INET_Addr::set");

#if defined (VXWORKS)
  ACE_NOTSUP_RETURN (-1);
#else
  servent sentry;
  ACE_SERVENT_DATA buf;

  servent *sp = ACE_OS::getservbyname_r ((char *) port_name, 
					 "tcp", &sentry, buf);
  if (sp == 0)
    return -1;
  else
    return this->set (sp->s_port, host_name, 0);
#endif /* VXWORKS */
}

// Initializes a ACE_INET_Addr from a <port_name> and an Internet
// address.

int
ACE_INET_Addr::set (const char port_name[], 
		    ACE_UINT32 inet_address)
{
  ACE_TRACE ("ACE_INET_Addr::set");

#if defined (VXWORKS)
  ACE_NOTSUP_RETURN (-1);
#else
  servent sentry;
  ACE_SERVENT_DATA buf;

  servent *sp = ACE_OS::getservbyname_r ((char *) port_name, 
					 "tcp", &sentry, buf);
  if (sp == 0)
    return -1;  
  else
    return this->set (sp->s_port, inet_address, 0);
#endif /* VXWORKS */
}

// Creates a ACE_INET_Addr from a PORT_NUMBER and the remote
// HOST_NAME. 


ACE_INET_Addr::ACE_INET_Addr (u_short port_number, 
			      const char host_name[])
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  if (this->set (port_number, host_name) == -1)
    ACE_ERROR ((LM_ERROR, "ACE_INET_Addr::ACE_INET_Addr"));
}

// Creates a ACE_INET_Addr from a sockaddr_in structure.

int
ACE_INET_Addr::set (const sockaddr_in *addr, int len)
{
  ACE_TRACE ("ACE_INET_Addr::set");
  this->ACE_Addr::base_set (AF_INET, len);
  ACE_OS::memcpy ((void *) &this->inet_addr_, 
		  (void *) addr, len);
  return 0;
}

// Set a pointer to the address.
void 
ACE_INET_Addr::set_addr (void *addr, int len)
{
  ACE_TRACE ("ACE_INET_Addr::set_addr");

  this->ACE_Addr::base_set (AF_INET, len);
  ACE_OS::memcpy ((void *) &this->inet_addr_, 
		  (void *) addr, len);
}

// Creates a ACE_INET_Addr from a sockaddr_in structure.


ACE_INET_Addr::ACE_INET_Addr (const sockaddr_in *addr, int len)
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  this->set (addr, len);
}

// Creates a ACE_INET_Addr from a PORT_NUMBER and an Internet address.


ACE_INET_Addr::ACE_INET_Addr (u_short port_number, 
			      ACE_UINT32 inet_address)
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  if (this->set (port_number, inet_address) == -1)
    ACE_ERROR ((LM_ERROR, "ACE_INET_Addr::ACE_INET_Addr"));
}

// Creates a ACE_INET_Addr from a PORT_NAME and the remote
// HOST_NAME.

ACE_INET_Addr::ACE_INET_Addr (const char port_name[], 
			      const char host_name[])
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  if (this->set (port_name, host_name) == -1)
    ACE_ERROR ((LM_ERROR, "ACE_INET_Addr::ACE_INET_Addr"));
}

// Creates a ACE_INET_Addr from a PORT_NAME and an Internet address.


ACE_INET_Addr::ACE_INET_Addr (const char* port_name, 
			      ACE_UINT32 inet_address)
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  if (this->set (port_name, inet_address) == -1)
    ACE_ERROR ((LM_ERROR, "ACE_INET_Addr::ACE_INET_Addr"));
}

int
ACE_INET_Addr::get_host_name (char hostname[], size_t len) const
{
  ACE_TRACE ("ACE_INET_Addr::get_host_name");
#if defined (VXWORKS)
  char name [MAXHOSTNAMELEN + 1];
  int error = ::hostGetByAddr ((int) this->inet_addr_.sin_addr.s_addr, name);
  if (error == OK) 
    {
      if (ACE_OS::strlen (name) >= len) 
        return -1;
      else 
	{
	  ACE_OS::strcpy (hostname, name);
	  return 0;
	}
    } 
  else 
    {
      errno = error;
      return -1;
    }
#else
  int a_len = sizeof this->inet_addr_.sin_addr.s_addr;

  hostent hentry;
  int error;
  ACE_HOSTENT_DATA buf;
  hostent *hp;

  hp = ACE_OS::gethostbyaddr_r ((char *) &this->inet_addr_.sin_addr,
				a_len, this->addr_type_,
				&hentry, buf, &error);
  if (hp == 0)
    {
      errno = error;
      return -1;
    }
  else
    {
      if (ACE_OS::strlen (hp->h_name) >= len)
        return -1;
      else
	{
	  ACE_OS::strcpy (hostname, hp->h_name);
	  return 0;
	}
    }
#endif /* VXWORKS */
}

// Return the character representation of the hostname.

const char *
ACE_INET_Addr::get_host_name (void) const
{
  ACE_TRACE ("ACE_INET_Addr::get_host_name");

#if defined (VXWORKS)
  static char buf[MAXHOSTNAMELEN + 1];

  ::gethostname (buf, MAXHOSTNAMELEN + 1);
  return buf;
#else
  int a_len = sizeof this->inet_addr_.sin_addr.s_addr;

  hostent *hp = ACE_OS::gethostbyaddr ((char *) &this->inet_addr_.sin_addr,
				       a_len, this->addr_type_);

  if (hp == 0)
    return 0;
  else
    return hp->h_name;
#endif /* VXWORKS */
}

void
ACE_INET_Addr::set_port_number (u_short port_number, 
				int encode)
{
  ACE_TRACE ("ACE_INET_Addr::set_port_number");

  if (encode)
    port_number = htons (port_number);

  this->inet_addr_.sin_port = port_number;
}