mirror of https://gerrit.osmocom.org/libosmocore
add missing .c files for ares integration
This commit is contained in:
parent
6ca09c230d
commit
0a3bf76ac0
|
@ -0,0 +1,62 @@
|
|||
#include <string.h>
|
||||
#include <ares.h>
|
||||
|
||||
#include <osmocom/core/logging.h>
|
||||
#include <osmocom/core/utils.h>
|
||||
|
||||
ares_channel osmo_ares_channel;
|
||||
|
||||
struct value_string ares_status_strs[25] = {
|
||||
/* Server error codes */
|
||||
{ ARES_SUCCESS, "Success" },
|
||||
{ ARES_ENODATA, "Server: No relevant answer" },
|
||||
{ ARES_ESERVFAIL, "Server: Server failure" },
|
||||
{ ARES_ENOTFOUND, "Server: Not found" },
|
||||
{ ARES_ENOTIMP, "Server: Not implemented" },
|
||||
{ ARES_EREFUSED, "Server: Refused" },
|
||||
/* Locally generated error codes */
|
||||
{ ARES_EBADQUERY, "Local: Bad Query" },
|
||||
{ ARES_EBADNAME, "Local: Bad Name" },
|
||||
{ ARES_EBADFAMILY, "Local: Bad Family" },
|
||||
{ ARES_EBADRESP, "Local: Bad Response" },
|
||||
{ ARES_ECONNREFUSED, "Local: Connection refused" },
|
||||
{ ARES_ETIMEOUT, "Local: Timeout" },
|
||||
{ ARES_EOF, "Local: End of file" },
|
||||
{ ARES_EFILE, "Local: EFILE?" },
|
||||
{ ARES_ENOMEM, "Local: Out of memory" },
|
||||
{ ARES_EDESTRUCTION, "Local: Destruction?" },
|
||||
{ ARES_EBADSTR, "Local: Bad String?" },
|
||||
{ ARES_EBADFLAGS, "genameinfo: Bad flags" },
|
||||
{ ARES_ENONAME, "getaddrinfo: No name" },
|
||||
{ ARES_EBADHINTS, "getaddrinfo: Bad Hints" },
|
||||
{ ARES_ENOTINITIALIZED, "Library not initialized" },
|
||||
{ ARES_ELOADIPHLPAPI, "?" },
|
||||
{ ARES_EADDRGETNETWORKPARAMS, "?" },
|
||||
{ ARES_ECANCELLED, "Cancelled" },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
int osmo_ares_init()
|
||||
{
|
||||
struct ares_options options;
|
||||
unsigned int optmask = 0;
|
||||
int rc;
|
||||
|
||||
memset(&options, 0, sizeof(options));
|
||||
|
||||
rc = ares_library_init(ARES_LIB_INIT_ALL);
|
||||
if (rc != ARES_SUCCESS) {
|
||||
LOGP(DLGLOBAL, LOGL_ERROR, "ares_library_init(): %s\n",
|
||||
ares_strerror(rc));
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = ares_init_options(&osmo_ares_channel, &options, optmask);
|
||||
if (rc != ARES_SUCCESS) {
|
||||
LOGP(DLGLOBAL, LOGL_ERROR, "ares_init_options(): %s\n",
|
||||
ares_strerror(rc));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <netdb.h>
|
||||
#include <ares.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <osmocom/core/select.h>
|
||||
|
||||
extern ares_channel osmo_ares_channel;
|
||||
|
||||
|
||||
static void ares_cb(void *arg, int status, int timeouts, struct hostent *hostent)
|
||||
{
|
||||
struct in_addr *ia;
|
||||
int i;
|
||||
|
||||
printf("Callback called: status=%d, timeouts=%d\n", status, timeouts);
|
||||
|
||||
if (status != ARES_SUCCESS)
|
||||
return;
|
||||
|
||||
if (hostent->h_length != sizeof(struct in_addr))
|
||||
return;
|
||||
|
||||
for (i = 0, ia = (struct in_addr *) hostent->h_addr_list[i]; ia;
|
||||
i++, ia = (struct in_addr *) hostent->h_addr_list[i]) {
|
||||
printf("%s -> %s\n", hostent->h_name, inet_ntoa(*ia));
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
osmo_ares_init();
|
||||
|
||||
ares_gethostbyname(osmo_ares_channel, "localhost", AF_INET,
|
||||
ares_cb, NULL);
|
||||
|
||||
while (1) {
|
||||
osmo_select_main(0);
|
||||
}
|
||||
|
||||
exit(1);
|
||||
}
|
Loading…
Reference in New Issue