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"
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)
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
)
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.
47 // Might not be official: just echo input arg.
48 hostaddr
[0] = (char *) addr
;
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
;
61 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYADDR */
63 #if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYADDR)
66 ACE_OS::gethostbyaddr_r (const char *addr
, int length
, int type
,
67 hostent
*result
, ACE_HOSTENT_DATA buffer
,
70 ACE_OS_TRACE ("ACE_OS::gethostbyaddr_r");
71 if (length
!= 4 || type
!= AF_INET
)
77 if (ACE_OS::netdb_acquire ())
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
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];
105 // errno will have been set to S_hostLib_UNKNOWN_HOST.
110 ACE_OS::netdb_release ();
115 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYADDR */
117 #if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYNAME)
120 ACE_OS::gethostbyname (const char *name
)
122 ACE_OS_TRACE ("ACE_OS::gethostbyname");
126 static int first_addr
;
127 static char *hostaddr
[2];
128 static char *aliases
[1];
130 if (0 == name
|| '\0' == name
[0])
133 ACE_OSCALL (::hostGetByName (const_cast <char *> (name
)), int, -1, first_addr
);
134 if (first_addr
== -1)
137 hostaddr
[0] = (char *) &first_addr
;
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
;
151 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYNAME */
153 #if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYNAME)
156 ACE_OS::gethostbyname_r (const char *name
, hostent
*result
,
157 ACE_HOSTENT_DATA buffer
,
160 ACE_OS_TRACE ("ACE_OS::gethostbyname_r");
162 if (0 == name
|| '\0' == name
[0])
165 if (ACE_OS::netdb_acquire ())
170 ACE_OSCALL (::hostGetByName (const_cast <char *> (name
)), int, -1, addr
);
174 // errno will have been set to S_hostLib_UNKNOWN_HOST
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;
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 ();
206 #endif /* ACE_VXWORKS && ACE_LACKS_GETHOSTBYNAME*/
208 ACE_END_VERSIONED_NAMESPACE_DECL
210 // Include if_arp so that getmacaddr can use the
213 # include /**/ <net/if_arp.h>
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 */
228 ADAPTER_STATUS adapt
;
229 NAME_BUFFER NameBuff
[30];
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
)
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
);
269 ACE_OS::memcpy (node
->node
,
270 adapter
.adapt
.adapter_address
,
277 # if defined (ACE_HAS_PHARLAP_RT)
278 DEVHANDLE ip_dev
= (DEVHANDLE
)0;
281 ACE_TCHAR dev_name
[16];
283 for (i
= 0; i
< 10; i
++)
286 ACE_OS::sprintf (dev_name
,
289 ip_dev
= EtsTCPGetDeviceHandle (dev_name
);
295 devp
= EtsTCPGetDeviceCfg (ip_dev
);
298 ACE_OS::memcpy (node
->node
,
299 &devp
->EthernetAddress
[0],
303 ACE_UNUSED_ARG (node
);
304 ACE_NOTSUP_RETURN (-1);
305 # endif /* ACE_HAS_PHARLAP_RT */
306 # endif /* ACE_HAS_PHARLAP */
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
);
321 ACE_OS::socket (PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
323 if (handle
== ACE_INVALID_HANDLE
)
326 char **paddrs
= phost
->h_addr_list
;
330 struct sockaddr_in
*psa
=
331 (struct sockaddr_in
*)&(ar
.arp_pa
);
335 sizeof (struct arpreq
));
337 psa
->sin_family
= AF_INET
;
339 ACE_OS::memcpy (&(psa
->sin_addr
),
341 sizeof (struct in_addr
));
343 if (ACE_OS::ioctl (handle
,
347 ACE_OS::close (handle
);
351 ACE_OS::close (handle
);
353 ACE_OS::memcpy (node
->node
,
359 #elif defined (linux) && !defined (ACE_LACKS_NETWORKING)
364 ACE_OS::socket (PF_INET
, SOCK_DGRAM
, 0);
366 if (handle
== ACE_INVALID_HANDLE
)
369 ACE_OS::strcpy (ifr
.ifr_name
, "eth0");
371 if (ACE_OS::ioctl (handle
/*s*/, SIOCGIFHWADDR
, &ifr
) < 0)
373 ACE_OS::close (handle
);
377 struct sockaddr
* sa
=
378 (struct sockaddr
*) &ifr
.ifr_addr
;
380 ACE_OS::close (handle
);
382 ACE_OS::memcpy (node
->node
,
389 ACE_UNUSED_ARG (node
);
390 ACE_NOTSUP_RETURN (-1);
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) */