Restore build on FreeBSD.
[getmangos.git] / dep / ACE_wrappers / ace / OS_NS_netdb.cpp
blob8cee46be2090824ef6098cde5528cd313e58eb6b
1 // -*- C++ -*-
2 // $Id: OS_NS_netdb.cpp 80826 2008-03-04 14:51:23Z wotte $
4 #include "ace/OS_NS_netdb.h"
6 ACE_RCSID(ace, OS_NS_netdb, "$Id: OS_NS_netdb.cpp 80826 2008-03-04 14:51:23Z wotte $")
8 #if !defined (ACE_HAS_INLINED_OSCALLS)
9 # include "ace/OS_NS_netdb.inl"
10 #endif /* ACE_HAS_INLINED_OSCALLS */
12 #include "ace/os_include/net/os_if.h"
13 #include "ace/OS_NS_unistd.h"
14 #if defined (ACE_WIN32) && defined (ACE_HAS_PHARLAP)
15 #include "ace/OS_NS_stdio.h"
16 #endif
17 #include "ace/OS_NS_stropts.h"
18 #include "ace/OS_NS_sys_socket.h"
20 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
22 #if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYADDR)
24 struct hostent *
25 ACE_OS::gethostbyaddr (const char *addr, int length, int type)
27 ACE_OS_TRACE ("ACE_OS::gethostbyaddr");
29 if (length != 4 || type != AF_INET)
31 errno = EINVAL;
32 return 0;
35 // not thread safe!
36 static hostent ret;
37 static char name [MAXNAMELEN + 1];
38 static char *hostaddr[2];
39 static char *aliases[1];
41 if (::hostGetByAddr (*(int *) addr, name) != 0)
43 // errno will have been set to S_hostLib_UNKNOWN_HOST.
44 return 0;
47 // Might not be official: just echo input arg.
48 hostaddr[0] = (char *) addr;
49 hostaddr[1] = 0;
50 aliases[0] = 0;
52 ret.h_name = name;
53 ret.h_addrtype = AF_INET;
54 ret.h_length = 4; // VxWorks 5.2/3 doesn't define IP_ADDR_LEN;
55 ret.h_addr_list = hostaddr;
56 ret.h_aliases = aliases;
58 return &ret;
61 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYADDR */
63 #if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYADDR)
65 struct hostent *
66 ACE_OS::gethostbyaddr_r (const char *addr, int length, int type,
67 hostent *result, ACE_HOSTENT_DATA buffer,
68 int *h_errnop)
70 ACE_OS_TRACE ("ACE_OS::gethostbyaddr_r");
71 if (length != 4 || type != AF_INET)
73 errno = EINVAL;
74 return 0;
77 if (ACE_OS::netdb_acquire ())
78 return 0;
79 else
81 // buffer layout:
82 // buffer[0-3]: h_addr_list[0], the first (and only) addr.
83 // buffer[4-7]: h_addr_list[1], the null terminator for the h_addr_list.
84 // buffer[8]: the name of the host, null terminated.
86 // Call ::hostGetByAddr (), which puts the (one) hostname into
87 // buffer.
88 if (::hostGetByAddr (*(int *) addr, &buffer[8]) == 0)
90 // Store the return values in result.
91 result->h_name = &buffer[8]; // null-terminated host name
92 result->h_addrtype = AF_INET;
93 result->h_length = 4; // VxWorks 5.2/3 doesn't define IP_ADDR_LEN.
95 result->h_addr_list = (char **) buffer;
96 // Might not be official: just echo input arg.
97 result->h_addr_list[0] = (char *) addr;
98 // Null-terminate the list of addresses.
99 result->h_addr_list[1] = 0;
100 // And no aliases, so null-terminate h_aliases.
101 result->h_aliases = &result->h_addr_list[1];
103 else
105 // errno will have been set to S_hostLib_UNKNOWN_HOST.
106 result = 0;
110 ACE_OS::netdb_release ();
111 *h_errnop = errno;
112 return result;
115 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYADDR */
117 #if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYNAME)
119 struct hostent *
120 ACE_OS::gethostbyname (const char *name)
122 ACE_OS_TRACE ("ACE_OS::gethostbyname");
124 // not thread safe!
125 static hostent ret;
126 static int first_addr;
127 static char *hostaddr[2];
128 static char *aliases[1];
130 if (0 == name || '\0' == name[0])
131 return 0;
133 ACE_OSCALL (::hostGetByName (const_cast <char *> (name)), int, -1, first_addr);
134 if (first_addr == -1)
135 return 0;
137 hostaddr[0] = (char *) &first_addr;
138 hostaddr[1] = 0;
139 aliases[0] = 0;
141 // Might not be official: just echo input arg.
142 ret.h_name = (char *) name;
143 ret.h_addrtype = AF_INET;
144 ret.h_length = 4; // VxWorks 5.2/3 doesn't define IP_ADDR_LEN;
145 ret.h_addr_list = hostaddr;
146 ret.h_aliases = aliases;
148 return &ret;
151 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYNAME */
153 #if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYNAME)
155 struct hostent *
156 ACE_OS::gethostbyname_r (const char *name, hostent *result,
157 ACE_HOSTENT_DATA buffer,
158 int *h_errnop)
160 ACE_OS_TRACE ("ACE_OS::gethostbyname_r");
162 if (0 == name || '\0' == name[0])
163 return 0;
165 if (ACE_OS::netdb_acquire ())
166 return 0;
167 else
169 int addr;
170 ACE_OSCALL (::hostGetByName (const_cast <char *> (name)), int, -1, addr);
172 if (addr == -1)
174 // errno will have been set to S_hostLib_UNKNOWN_HOST
175 result = 0;
177 else
179 // Might not be official: just echo input arg.
180 result->h_name = (char *) name;
181 result->h_addrtype = AF_INET;
182 result->h_length = 4; // VxWorks 5.2/3 doesn't define IP_ADDR_LEN;
184 // buffer layout:
185 // buffer[0-3]: h_addr_list[0], pointer to the addr.
186 // buffer[4-7]: h_addr_list[1], null terminator for the h_addr_list.
187 // buffer[8-11]: the first (and only) addr.
189 // Store the address list in buffer.
190 result->h_addr_list = (char **) buffer;
191 // Store the actual address _after_ the address list.
192 result->h_addr_list[0] = (char *) &result->h_addr_list[2];
193 result->h_addr_list[2] = (char *) addr;
194 // Null-terminate the list of addresses.
195 result->h_addr_list[1] = 0;
196 // And no aliases, so null-terminate h_aliases.
197 result->h_aliases = &result->h_addr_list[1];
201 ACE_OS::netdb_release ();
202 *h_errnop = errno;
203 return result;
206 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYNAME*/
208 ACE_END_VERSIONED_NAMESPACE_DECL
210 // Include if_arp so that getmacaddr can use the
211 // arp structure.
212 #if defined (sun)
213 # include /**/ <net/if_arp.h>
214 #endif
216 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
219 ACE_OS::getmacaddress (struct macaddr_node_t *node)
221 ACE_OS_TRACE ("ACE_OS::getmacaddress");
223 #if defined (ACE_WIN32) && !defined (ACE_HAS_WINCE)
224 # if !defined (ACE_HAS_PHARLAP)
225 /** Define a structure for use with the netbios routine */
226 struct ADAPTERSTAT
228 ADAPTER_STATUS adapt;
229 NAME_BUFFER NameBuff [30];
232 NCB ncb;
233 LANA_ENUM lenum;
234 unsigned char result;
236 ACE_OS::memset (&ncb, 0, sizeof(ncb));
237 ncb.ncb_command = NCBENUM;
238 ncb.ncb_buffer = reinterpret_cast<unsigned char*> (&lenum);
239 ncb.ncb_length = sizeof(lenum);
241 result = Netbios (&ncb);
243 for(int i = 0; i < lenum.length; i++)
245 ACE_OS::memset (&ncb, 0, sizeof(ncb));
246 ncb.ncb_command = NCBRESET;
247 ncb.ncb_lana_num = lenum.lana [i];
249 /** Reset the netbios */
250 result = Netbios (&ncb);
252 if (ncb.ncb_retcode != NRC_GOODRET)
254 return -1;
257 ADAPTERSTAT adapter;
258 ACE_OS::memset (&ncb, 0, sizeof (ncb));
259 ACE_OS::strcpy (reinterpret_cast<char*> (ncb.ncb_callname), "*");
260 ncb.ncb_command = NCBASTAT;
261 ncb.ncb_lana_num = lenum.lana[i];
262 ncb.ncb_buffer = reinterpret_cast<unsigned char*> (&adapter);
263 ncb.ncb_length = sizeof (adapter);
265 result = Netbios (&ncb);
267 if (result == 0)
269 ACE_OS::memcpy (node->node,
270 adapter.adapt.adapter_address,
272 return 0;
275 return 0;
276 # else
277 # if defined (ACE_HAS_PHARLAP_RT)
278 DEVHANDLE ip_dev = (DEVHANDLE)0;
279 EK_TCPIPCFG *devp;
280 size_t i;
281 ACE_TCHAR dev_name[16];
283 for (i = 0; i < 10; i++)
285 // Ethernet.
286 ACE_OS::sprintf (dev_name,
287 "ether%d",
289 ip_dev = EtsTCPGetDeviceHandle (dev_name);
290 if (ip_dev != 0)
291 break;
293 if (ip_dev == 0)
294 return -1;
295 devp = EtsTCPGetDeviceCfg (ip_dev);
296 if (devp == 0)
297 return -1;
298 ACE_OS::memcpy (node->node,
299 &devp->EthernetAddress[0],
301 return 0;
302 # else
303 ACE_UNUSED_ARG (node);
304 ACE_NOTSUP_RETURN (-1);
305 # endif /* ACE_HAS_PHARLAP_RT */
306 # endif /* ACE_HAS_PHARLAP */
307 #elif defined (sun)
309 /** obtain the local host name */
310 char hostname [MAXHOSTNAMELEN];
311 ACE_OS::hostname (hostname, sizeof (hostname));
313 /** Get the hostent to use with ioctl */
314 struct hostent *phost =
315 ACE_OS::gethostbyname (hostname);
317 if (phost == 0)
318 return -1;
320 ACE_HANDLE handle =
321 ACE_OS::socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
323 if (handle == ACE_INVALID_HANDLE)
324 return -1;
326 char **paddrs = phost->h_addr_list;
328 struct arpreq ar;
330 struct sockaddr_in *psa =
331 (struct sockaddr_in *)&(ar.arp_pa);
333 ACE_OS::memset (&ar,
335 sizeof (struct arpreq));
337 psa->sin_family = AF_INET;
339 ACE_OS::memcpy (&(psa->sin_addr),
340 *paddrs,
341 sizeof (struct in_addr));
343 if (ACE_OS::ioctl (handle,
344 SIOCGARP,
345 &ar) == -1)
347 ACE_OS::close (handle);
348 return -1;
351 ACE_OS::close (handle);
353 ACE_OS::memcpy (node->node,
354 ar.arp_ha.sa_data,
357 return 0;
359 #elif defined (linux) && !defined (ACE_LACKS_NETWORKING)
361 struct ifreq ifr;
363 ACE_HANDLE handle =
364 ACE_OS::socket (PF_INET, SOCK_DGRAM, 0);
366 if (handle == ACE_INVALID_HANDLE)
367 return -1;
369 ACE_OS::strcpy (ifr.ifr_name, "eth0");
371 if (ACE_OS::ioctl (handle/*s*/, SIOCGIFHWADDR, &ifr) < 0)
373 ACE_OS::close (handle);
374 return -1;
377 struct sockaddr* sa =
378 (struct sockaddr *) &ifr.ifr_addr;
380 ACE_OS::close (handle);
382 ACE_OS::memcpy (node->node,
383 sa->sa_data,
386 return 0;
388 #else
389 ACE_UNUSED_ARG (node);
390 ACE_NOTSUP_RETURN (-1);
391 #endif
394 ACE_END_VERSIONED_NAMESPACE_DECL
396 # if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0) && defined (ACE_LACKS_NETDB_REENTRANT_FUNCTIONS)
397 # include "ace/OS_NS_Thread.h"
398 # include "ace/Object_Manager_Base.h"
400 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
403 ACE_OS::netdb_acquire (void)
405 return ACE_OS::thread_mutex_lock ((ACE_thread_mutex_t *)
406 ACE_OS_Object_Manager::preallocated_object[
407 ACE_OS_Object_Manager::ACE_OS_MONITOR_LOCK]);
411 ACE_OS::netdb_release (void)
413 return ACE_OS::thread_mutex_unlock ((ACE_thread_mutex_t *)
414 ACE_OS_Object_Manager::preallocated_object[
415 ACE_OS_Object_Manager::ACE_OS_MONITOR_LOCK]);
418 ACE_END_VERSIONED_NAMESPACE_DECL
420 # endif /* defined (ACE_LACKS_NETDB_REENTRANT_FUNCTIONS) */