From e6ec23d7cc069197356224b7ca9ea6dad37bad31 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Fri, 16 Nov 2007 17:29:30 +0000 Subject: [PATCH] Added sequential (socket API) function gethostbyname and the struct hostent it uses --- CHANGELOG | 5 +++-- src/api/sockets.c | 42 ++++++++++++++++++++++++++++++++++++++ src/include/lwip/sockets.h | 17 +++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ecde64ad..db65b0fd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -20,8 +20,9 @@ HISTORY ++ New features: 2007-11-16 Simon Goldschmidt - * api.h, api_msg.h, api_lib.c, api_msg.c: Added sequential dns resolver - function for netconn api (netconn_gethostbyname). + * api.h, api_msg.h, api_lib.c, api_msg.c, socket.h, socket.c: Added sequential + dns resolver function for netconn api (netconn_gethostbyname) and socket api + (gethostbyname). 2007-11-15 Jim Pettinato, Frédéric Bernon * opt.h, init.c, tcpip.c, dhcp.c, dns.h, dns.c: add DNS client for simple name diff --git a/src/api/sockets.c b/src/api/sockets.c index cca3b932..bb49f79e 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -96,6 +96,13 @@ static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len static void lwip_getsockopt_internal(void *arg); static void lwip_setsockopt_internal(void *arg); +#if LWIP_DNS +static struct hostent s_hostent; +static char *s_aliases; +static struct ip_addr s_hostent_addr; +static struct ip_addr *s_phostent_addr; +#endif /* LWIP_DNS */ + static const int err_to_errno_table[] = { 0, /* ERR_OK 0 No error, everything OK. */ ENOMEM, /* ERR_MEM -1 Out of memory error. */ @@ -1795,4 +1802,39 @@ lwip_ioctl(int s, long cmd, void *argp) } /* switch (cmd) */ } +#if LWIP_DNS +static void +lwip_fill_hostent(struct hostent *he, char *name, struct ip_addr **addr, char **aliases) +{ + he->h_name = name; + he->h_aliases = aliases; + he->h_addrtype = AF_INET; + he->h_length = sizeof(struct ip_addr); + he->h_addr_list = (char**)addr; +} + + +struct hostent* +gethostbyname(const char *name) +{ + err_t err; + struct ip_addr addr; + + err = netconn_gethostbyname(name, &addr); + if (err != ERR_OK) { + return NULL; + } + + s_hostent_addr = addr; + s_phostent_addr = &s_hostent_addr; + s_hostent.h_name = (char*)name; + s_hostent.h_aliases = &s_aliases; + s_hostent.h_addrtype = AF_INET; + s_hostent.h_length = sizeof(struct ip_addr); + s_hostent.h_addr_list = (char**)&s_phostent_addr; + + return &s_hostent; +} +#endif /* LWIP_DNS*/ + #endif /* LWIP_SOCKET */ diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h index 6d45fa44..08e142fd 100644 --- a/src/include/lwip/sockets.h +++ b/src/include/lwip/sockets.h @@ -60,6 +60,19 @@ struct sockaddr { char sa_data[14]; }; +#if LWIP_DNS +struct hostent { + char *h_name; /* Official name of the host. */ + char **h_aliases; /* A pointer to an array of pointers to alternative host names, + terminated by a null pointer. */ + int h_addrtype; /* Address type. */ + int h_length; /* The length, in bytes, of the address. */ + char **h_addr_list; /* A pointer to an array of pointers to network addresses (in + network byte order) for the host, terminated by a null pointer. */ +#define h_addr h_addr_list[0] /* for backward compatibility */ +}; +#endif /* LWIP_DNS */ + #ifndef socklen_t # define socklen_t u32_t #endif @@ -300,6 +313,10 @@ int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptse struct timeval *timeout); int lwip_ioctl(int s, long cmd, void *argp); +#if LWIP_DNS +struct hostent *gethostbyname(const char *name); +#endif /* LWIP_DNS */ + #if LWIP_COMPAT_SOCKETS #define accept(a,b,c) lwip_accept(a,b,c) #define bind(a,b,c) lwip_bind(a,b,c)