本文整理汇总了C++中gethostbyname_r函数的典型用法代码示例。如果您正苦于以下问题:C++ gethostbyname_r函数的具体用法?C++ gethostbyname_r怎么用?C++ gethostbyname_r使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gethostbyname_r函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: enet_address_set_host
int
enet_address_set_host (ENetAddress * address, const char * name)
{
struct hostent * hostEntry = NULL;
#ifdef HAS_GETHOSTBYNAME_R
struct hostent hostData;
char buffer [2048];
int errnum;
#ifdef linux
gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
#else
hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
#endif
#else
hostEntry = gethostbyname (name);
#endif
if (hostEntry == NULL ||
hostEntry -> h_addrtype != AF_INET)
return -1;
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
return 0;
}
示例2: invoke_gethostbyname
PRIVATESTUFF void
invoke_gethostbyname(void *p)
{
struct t_data *td = (struct t_data *) p;
struct hostent hp, *h;
char buf[1000];
int h_errno;
u_long sip; /* IP address in network byte order */
fprintf(DEBUGIO, "invoke_gethostbyname: name = %s\r\n", td->name);
fprintf(DEBUGIO, "invoke_gethostbyname: 0x%lx sleeping for 2 seconds\r\n", (unsigned long) pthread_self());
sleep(2);
fprintf(DEBUGIO, "invoke_gethostbyname: 0x%lx done sleeping\r\n", (unsigned long) pthread_self());
#if defined(linux)
gethostbyname_r(td->name, &hp, buf, sizeof(buf), &h, &h_errno);
#else /* linux */
h = gethostbyname_r(td->name, &hp, buf, sizeof(buf), &h_errno);
#endif /* linux */
if (h == NULL) {
td->status = h_errno;
fprintf(DEBUGIO, "invoke_gethostbyname: error = %d\r\n", td->status);
} else {
td->status = 0;
fprintf(DEBUGIO, "invoke_gethostbyname: success = %d\r\n", td->status);
sip = *((u_long *) hp.h_addr_list[0]);
memcpy(td->resbuf, &sip, sizeof(sip));
td->resbuflen = sizeof(sip);
}
fprintf(DEBUGIO, "invoke_gethostbyname: done\r\n");
}
示例3: enet_address_set_host
int
enet_address_set_host (ENetAddress * address, const char * name)
{
struct hostent * hostEntry = NULL;
#ifdef HAS_GETHOSTBYNAME_R
struct hostent hostData;
char buffer [2048];
int errnum;
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
#else
hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
#endif
#else
hostEntry = gethostbyname (name);
#endif
if (hostEntry == NULL ||
hostEntry -> h_addrtype != AF_INET)
{
#ifdef HAS_INET_PTON
if (! inet_pton (AF_INET, name, & address -> host))
#else
if (! inet_aton (name, (struct in_addr *) & address -> host))
#endif
return -1;
return 0;
}
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
return 0;
}
示例4: safe_gethostbyname
unsigned long safe_gethostbyname(const char *host)
{
struct hostent *hp, hp_a, *hp_b;
char buf[1024];
int hp_errno;
u_long ip;
if (is_ipaddr(host))
return inet_addr(host);
#if HAVE_GETHOSTBYNAME_R
#if defined(linux)
if (!gethostbyname_r(host, &hp_a, buf, sizeof(buf), &hp_b, &hp_errno))
hp = &hp_a;
else
hp = NULL;
#else
hp = gethostbyname_r(host, &hp_a, buf, sizeof(buf), &hp_errno);
#endif
#else
pthread_mutex_lock(&nss_lock);
hp = gethostbyname(host);
#endif
if (!hp) {
_pam_log(LOG_ERR, "safe_gethostbyname: hostname '%s' not found", host);
#ifndef HAVE_GETHOSTBYNAME_R
pthread_mutex_unlock(&nss_lock);
#endif
return 0;
}
#ifndef HAVE_GETHOSTBYNAME_R
pthread_mutex_unlock(&nss_lock);
#endif
return *((unsigned int *) hp->h_addr);
}
示例5: malloc
struct hostent *gethostbyjosh (char *hostname)
{
/* A OS independant wrapper to gethostbyname */
struct hostent *host;
#ifndef FREEBSD
char hostbuf[HOSTBUF_LEN];
int errn;
#endif
host = (struct hostent *) malloc (sizeof (struct hostent));
#ifdef SOLARIS
gethostbyname_r (hostname, host, hostbuf, HOSTBUF_LEN, &errn);
#endif
#ifdef LINUX
gethostbyname_r (hostname, host, hostbuf, (size_t) HOSTBUF_LEN, &host, &errn);
#endif
#ifdef FREEBSD
host = gethostbyname (hostname);
#endif
if (host == NULL)
{
fprintf (stderr, "adding popserver '%s':\n", hostname);
perror ("gethostbyjosh");
exit (-1);
}
return (host);
}
示例6: gethostbyname
struct hostent *ei_gethostbyname_r(const char *name,
struct hostent *hostp,
char *buffer,
int buflen,
int *h_errnop)
{
#ifndef _REENTRANT
/* threads disabled, no need to call reentrant function */
return gethostbyname(name);
#else
#ifndef HAVE_GETHOSTBYNAME_R
return my_gethostbyname_r(name,hostp,buffer,buflen,h_errnop);
#else
#if (defined(__GLIBC__) || (__FreeBSD_version >= 602000) || defined(__DragonFly__))
struct hostent *result;
gethostbyname_r(name, hostp, buffer, buflen, &result, h_errnop);
return result;
#else
return gethostbyname_r(name,hostp,buffer,buflen,h_errnop);
#endif
#endif
#endif
}
示例7: gethostbyname
struct hostent *ei_gethostbyname_r(const char *name,
struct hostent *hostp,
char *buffer,
int buflen,
int *h_errnop)
{
#ifndef _REENTRANT
/* threads disabled, no need to call reentrant function */
return gethostbyname(name);
#else
#ifndef HAVE_GETHOSTBYNAME_R
return my_gethostbyname_r(name,hostp,buffer,buflen,h_errnop);
#else
#ifdef __GLIBC__
struct hostent *result;
gethostbyname_r(name, hostp, buffer, buflen, &result, h_errnop);
return result;
#else
return gethostbyname_r(name,hostp,buffer,buflen,h_errnop);
#endif
#endif
#endif
}
示例8: SocketStateException
void TCPSocket::connect(const char *name, unsigned short port) {
if (this->_state != CREATED) {
throw SocketStateException("Wrong state for operation");
}
struct sockaddr_in remote_addr;
const int GETHOSTBYNAME_R_BUFSIZE = 4096;
char tmp_buf[GETHOSTBYNAME_R_BUFSIZE];
struct hostent *result;
#if defined( linux ) || defined( __linux )
struct hostent ret;
int h_errnop;
if (gethostbyname_r(name, &ret, tmp_buf, GETHOSTBYNAME_R_BUFSIZE,
&result, &h_errnop) == -1) {
throw DNSException(h_errno);
}
if (result == NULL) {
throw DNSException(h_errno);
}
#endif
#if defined( sun ) || defined( __sun )
struct hostent entry;
int h_errnop;
if ((result = gethostbyname_r(name, &entry, tmp_buf, GETHOSTBYNAME_R_BUFSIZE,
&h_errnop)) == NULL) {
throw DNSException(h_errno);
}
#endif
memset(&remote_addr, 0, sizeof (remote_addr));
remote_addr.sin_addr = *((in_addr *) result->h_addr_list[0]);
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(port);
if (this->_nonblocking == false) {
if (::connect(this->_b->_sock, (struct sockaddr*) & remote_addr, sizeof (remote_addr)) == -1) {
if (errno == EAGAIN)
throw EAGAINException();
else
throw ConnectException(errno);
}
this->_state = CONNECTED;
}
if (this->_nonblocking == true) {
::connect(this->_b->_sock, (struct sockaddr*) & remote_addr, sizeof (remote_addr));
this->_state = CONNECTING;
}
#ifdef DEBUG
fprintf(stderr, "connected to %s port %d\n", name, port);
#endif
}
示例9: defined
struct hostent *sigar_gethostbyname(const char *name,
sigar_hostent_t *data)
{
struct hostent *hp = NULL;
#if defined(__linux__)
gethostbyname_r(name, &data->hs,
data->buffer, sizeof(data->buffer),
&hp, &data->error);
#elif defined(__sun)
hp = gethostbyname_r(name, &data->hs,
data->buffer, sizeof(data->buffer),
&data->error);
#elif defined(SIGAR_HAS_HOSTENT_DATA)
if (gethostbyname_r(name, &data->hs, &data->hd) == 0) {
hp = &data->hs;
}
else {
data->error = h_errno;
}
#else
hp = gethostbyname(name);
#endif
return hp;
}
示例10: enet_address_set_host
int
enet_address_set_host (ENetAddress * address, const char * name)
{
#ifdef HAS_GETADDRINFO
struct addrinfo hints, * resultList = NULL, * result = NULL;
memset (& hints, 0, sizeof (hints));
hints.ai_family = AF_INET;
if (getaddrinfo (name, NULL, NULL, & resultList) != 0)
return -1;
for (result = resultList; result != NULL; result = result -> ai_next)
{
if (result -> ai_family == AF_INET && result -> ai_addr != NULL && result -> ai_addrlen >= sizeof (struct sockaddr_in))
{
struct sockaddr_in * sin = (struct sockaddr_in *) result -> ai_addr;
address -> host = sin -> sin_addr.s_addr;
freeaddrinfo (resultList);
return 0;
}
}
if (resultList != NULL)
freeaddrinfo (resultList);
#else
struct hostent * hostEntry = NULL;
#ifdef HAS_GETHOSTBYNAME_R
struct hostent hostData;
char buffer [2048];
int errnum;
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
#else
hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
#endif
#else
hostEntry = gethostbyname (name);
#endif
if (hostEntry != NULL && hostEntry -> h_addrtype == AF_INET)
{
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
return 0;
}
#endif
return enet_address_set_host_ip (address, name);
}
示例11: hton32
SocketAddress
SocketAddress::getAnyLocalHost(UInt16 port)
{
struct in_addr addr;
addr.s_addr = hton32(INADDR_ANY);
SocketAddress rval = getFromNativeForm(addr, port, "localhost");
char buf[256];
gethostname(buf, sizeof(buf));
String hname(buf);
if (hname.indexOf('.') == String::npos)
{
#if defined(OW_HAVE_GETHOSTBYNAME_R) && defined(OW_GETHOSTBYNAME_R_ARGUMENTS)
hostent hostbuf;
hostent* hent = &hostbuf;
#if (OW_GETHOSTBYNAME_R_ARGUMENTS == 6)
char local_buf[2048];
int h_err = 0;
if (gethostbyname_r(buf, &hostbuf, local_buf, sizeof(local_buf),
&hent, &h_err) == -1)
{
hent = NULL;
}
#elif (OW_GETHOSTBYNAME_R_ARGUMENTS == 5)
char local_buf[2048];
int h_err(0);
// returns NULL if not successful
hent = gethostbyname_r(buf, &hostbuf, local_buf, sizeof(local_buf), &h_err);
#elif (OW_GETHOSTBYNAME_R_ARGUMENTS == 3)
hostent_data hostdata;
if (gethostbyname_r(buf, &hostbuf, &hostdata) != 0)
{
hent = NULL;
}
#else
#error Not yet supported: gethostbyname_r() with other argument counts.
#endif /* OW_GETHOSTBYNAME_R_ARGUMENTS */
#else
MutexLock mlock(gethostbynameMutex);
hostent* hent = gethostbyname(buf);
#endif
if (hent && hent->h_name && (strlen(hent->h_name) > 0))
{
hname = String(hent->h_name);
}
}
rval.m_name = hname;
return rval;
}
示例12: unix_gethostbyname
CAMLprim value unix_gethostbyname(value name)
{
struct hostent * hp;
char * hostname;
#if HAS_GETHOSTBYNAME_R
struct hostent h;
char buffer[NETDB_BUFFER_SIZE];
int err;
#endif
if (! caml_string_is_c_safe(name)) raise_not_found();
#if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT
hostname = caml_strdup(String_val(name));
#else
hostname = String_val(name);
#endif
#if HAS_GETHOSTBYNAME_R == 5
{
enter_blocking_section();
hp = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &err);
leave_blocking_section();
}
#elif HAS_GETHOSTBYNAME_R == 6
{
int rc;
enter_blocking_section();
rc = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &hp, &err);
leave_blocking_section();
if (rc != 0) hp = NULL;
}
#else
#ifdef GETHOSTBYNAME_IS_REENTRANT
enter_blocking_section();
#endif
hp = gethostbyname(hostname);
#ifdef GETHOSTBYNAME_IS_REENTRANT
leave_blocking_section();
#endif
#endif
#if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT
stat_free(hostname);
#endif
if (hp == (struct hostent *) NULL) raise_not_found();
return alloc_host_entry(hp);
}
示例13: unix_gethostbyname_r
CAMLprim value unix_gethostbyname_r(CAML_R, value name)
{
struct hostent * hp;
char * hostname;
#if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT
hostname = stat_alloc(string_length(name) + 1);
strcpy(hostname, String_val(name));
#else
hostname = String_val(name);
#endif
#if HAS_GETHOSTBYNAME_R == 5
{
struct hostent h;
char buffer[NETDB_BUFFER_SIZE];
int h_errno;
caml_enter_blocking_section_r(ctx);
hp = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &h_errno);
caml_leave_blocking_section_r(ctx);
}
#elif HAS_GETHOSTBYNAME_R == 6
{
struct hostent h;
char buffer[NETDB_BUFFER_SIZE];
int h_errno, rc;
caml_enter_blocking_section_r(ctx);
rc = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &hp, &h_errno);
caml_leave_blocking_section_r(ctx);
if (rc != 0) hp = NULL;
}
#else
#ifdef GETHOSTBYNAME_IS_REENTRANT
caml_enter_blocking_section_r(ctx);
#endif
hp = gethostbyname(hostname);
#ifdef GETHOSTBYNAME_IS_REENTRANT
caml_leave_blocking_section_r(ctx);
#endif
#endif
#if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT
stat_free(hostname);
#endif
if (hp == (struct hostent *) NULL) caml_raise_not_found_r(ctx);
return alloc_host_entry_r(ctx, hp);
}
示例14: check_vulnerable
static void check_vulnerable(void)
{
struct hostent resbuf;
struct hostent *result;
int herrno;
int retval;
char name[sizeof(temp.buffer)];
size_t len;
/*
* <glibc>/nss/digits_dots.c:
* strlen(name) = size_needed - sizeof(*host_addr) -
* sizeof(*h_addr_ptrs) - 1;
*/
len = sizeof(temp.buffer) - 16 - 2 * sizeof(char *) - 1;
memset(name, '0', len);
name[len] = '\0';
retval = gethostbyname_r(name, &resbuf, temp.buffer,
sizeof(temp.buffer), &result, &herrno);
if (strcmp(temp.canary, CANARY) != 0) {
tst_resm(TFAIL, "vulnerable");
return;
}
if (retval == ERANGE) {
tst_resm(TPASS, "not vulnerable");
return;
}
tst_resm(TFAIL, "gethostbyname_r() returned %s, expected ERANGE",
tst_strerrno(retval));
}
示例15: gethostbyname_r
/**
* initialize primarily functions to initialize static global variables
* that will not be changed frequently. These variables are currently
* _hostName.
*
*/
void ComputerSystem::initialize()
{
char hostName[PEGASUS_MAXHOSTNAMELEN + 1];
struct hostent *hostEntry;
if (gethostname(hostName, sizeof(hostName)) != 0)
{
_hostName.assign("Not initialized");
}
hostName[sizeof(hostName)-1] = 0;
// Now get the official hostname. If this call fails then return
// the value from gethostname().
char hostEntryBuffer[8192];
struct hostent hostEntryStruct;
int hostEntryErrno;
gethostbyname_r(
hostName,
&hostEntryStruct,
hostEntryBuffer,
sizeof(hostEntryBuffer),
&hostEntry,
&hostEntryErrno);
if (hostEntry)
{
_hostName.assign(hostEntry->h_name);
}
else
{
_hostName.assign(hostName);
}
}