Where is the source code implementation of gethostbyname() function?

2.3k Views Asked by At

From glibc tree, I can see only file gethstbynm.c (glibc-2.32/inet/gethstbynm.c), but there is no implementation code:

#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>


#define LOOKUP_TYPE struct hostent
#define FUNCTION_NAME   gethostbyname
#define DATABASE_NAME   hosts
#define ADD_PARAMS  const char *name
#define ADD_VARIABLES   name
#define BUFLEN      1024
#define NEED_H_ERRNO    1

#define HANDLE_DIGITS_DOTS  1

#include <nss/getXXbyYY.c>

I would like to find the implementation as https://www.amazon.com/Unix-Network-Programming-Sockets-Networking/dp/0131411551 suggest:

static struct hostent host ;
/* result stored here */
struct hostent *
gethostbyname (const char *hostname)
{
return (gethostbyname2 (hostname, family));
}

What file stores the implementation?

EDIT: uname -a:

Linux 5.8.0-40-generic #45-Ubuntu SMP Fri Jan 15 11:05:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

1

There are 1 best solutions below

2
Jacob Faib On

On linux gethostbyname() is declared in glibc/resolv/netdb.h (see here), pseudo defined via macros in inet/gethstbynm.c (see here), and finally defined in nss/getXXbyYY.c (see here).