Skip to content

Instantly share code, notes, and snippets.

@kimor79
Created May 2, 2013 17:14
Show Gist options
  • Save kimor79/5503747 to your computer and use it in GitHub Desktop.
Save kimor79/5503747 to your computer and use it in GitHub Desktop.
Testing apr methods and IP. Compiled on FreeBSD 9.0-RELEASE with: cc -I /usr/local/include/ -I /usr/local/include/apr-1/ -L /usr/local/lib -lapr-1
#include "stdio.h"
#include "stdlib.h"
#include "apr.h"
#include "apr_network_io.h"
#include "apr_strings.h"
#include "apr_tables.h"
int main(void) {
apr_file_t *err;
char *ipstr;
char hostname[APRMAXHOSTLEN + 1];
apr_file_t *out;
apr_pool_t *p;
apr_status_t rv;
apr_sockaddr_t *sockaddr;
apr_initialize();
// atexit(apr_terminate());
apr_pool_create(&p, NULL);
apr_file_open_stderr(&err, p);
apr_file_open_stdout(&out, p);
if((rv = apr_gethostname(hostname, APRMAXHOSTLEN, p)) != APR_SUCCESS) {
apr_file_printf(err, "Unable to determine hostname");
apr_pool_destroy(p);
return EXIT_FAILURE;
}
if((rv = apr_sockaddr_info_get(&sockaddr, hostname, APR_UNSPEC, 0,
APR_IPV4_ADDR_OK, p)) != APR_SUCCESS) {
apr_file_printf(err, "Unable to determine IP");
apr_pool_destroy(p);
return EXIT_FAILURE;
}
apr_sockaddr_ip_get(&ipstr, sockaddr);
apr_file_printf(out, "IP: %s\n", ipstr);
apr_pool_destroy(p);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment