Пример #1
0
void serf__log_skt(int verbose_flag, const char *filename, apr_socket_t *skt,
                   const char *fmt, ...)
{
    va_list argp;

    if (verbose_flag) {
        apr_sockaddr_t *sa;
        log_time();

        if (skt) {
            /* Log local and remote ip address:port */
            fprintf(stderr, "[l:");
            if (apr_socket_addr_get(&sa, APR_LOCAL, skt) == APR_SUCCESS) {
                char buf[32];
                apr_sockaddr_ip_getbuf(buf, 32, sa);
                fprintf(stderr, "%s:%d", buf, sa->port);
            }
            fprintf(stderr, " r:");
            if (apr_socket_addr_get(&sa, APR_REMOTE, skt) == APR_SUCCESS) {
                char buf[32];
                apr_sockaddr_ip_getbuf(buf, 32, sa);
                fprintf(stderr, "%s:%d", buf, sa->port);
            }
            fprintf(stderr, "] ");
        }

        if (filename)
            fprintf(stderr, "%s: ", filename);

        va_start(argp, fmt);
        vfprintf(stderr, fmt, argp);
        va_end(argp);
    }
}
Пример #2
0
static void test_print_addr(abts_case *tc, void *data)
{
    apr_sockaddr_t *sa;
    apr_status_t rv;
    char *s;

    rv = apr_sockaddr_info_get(&sa, "0.0.0.0", APR_INET, 80, 0, p);
    APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);

    s = apr_psprintf(p, "foo %pI bar", sa);

    ABTS_STR_EQUAL(tc, "foo 0.0.0.0:80 bar", s);

#if APR_HAVE_IPV6
    rv = apr_sockaddr_info_get(&sa, "::ffff:0.0.0.0", APR_INET6, 80, 0, p);
    APR_ASSERT_SUCCESS(tc, "Problem generating sockaddr", rv);
    if (rv == APR_SUCCESS)
        ABTS_TRUE(tc, sa != NULL);
    if (rv == APR_SUCCESS && sa) {
        /* sa should now be a v4-mapped IPv6 address. */
        char buf[128];
        int rc;

        rc = apr_sockaddr_is_wildcard(sa);
        ABTS_INT_NEQUAL(tc, 0, rc);

        memset(buf, 'z', sizeof buf);
        
        APR_ASSERT_SUCCESS(tc, "could not get IP address",
                           apr_sockaddr_ip_getbuf(buf, 22, sa));
        
        ABTS_STR_EQUAL(tc, "0.0.0.0", buf);
    }
#endif
}
Пример #3
0
void sock_set_peer(net_sock_t *nsock, char *address, int add_size)
{
    network_sock_t *sock = (network_sock_t *)nsock;
    apr_sockaddr_t *sa;

    address[0] = '\0';
    if (sock == NULL) return;
    if (sock->fd == NULL) return;

    if (apr_socket_addr_get(&sa, APR_REMOTE, sock->fd) != APR_SUCCESS) return;
    apr_sockaddr_ip_getbuf(address, add_size, sa);

    return;
}
Пример #4
0
static int socket_recvfrom(lua_State *L)
{
  lua_apr_socket *object;
  apr_status_t status;
  apr_sockaddr_t address = { 0 };
  apr_size_t buflen;
  apr_int32_t flags;
  char buffer[1024], ip_addr[APRMAXHOSTLEN];
  char *bufptr;
  int i;

  /* Validate arguments. */
  object = socket_check(L, 1, 1);
  buflen = luaL_optint(L, 2, sizeof buffer);

  /* Use dynamically allocated buffer only when necessary. */
  bufptr = (buflen > sizeof buffer) ? lua_newuserdata(L, buflen) : &buffer[0];

  flags = 0;
  status = apr_socket_recvfrom(&address, object->handle, flags, bufptr, &buflen);
  if (status != APR_SUCCESS)
    return push_error_status(L, status);

  /* Convert the socket address to a Lua table. */
  lua_newtable(L);

  /* Get the IP address in numeric format. */
  status = apr_sockaddr_ip_getbuf(ip_addr, sizeof ip_addr, &address);
  if (status == APR_SUCCESS)
    lua_pushstring(L, ip_addr), lua_setfield(L, -2, "address");

  /* Get the port number. */
  lua_pushnumber(L, address.port), lua_setfield(L, -2, "port");

  /* Get the address family. */
  for (i = 0; i < count(family_values); i++)
    if (family_values[i] == address.family) {
      lua_pushstring(L, family_options[i]);
      lua_setfield(L, -2, "family");
      break;
    }

  /* Push the received data. */
  lua_pushlstring(L, bufptr, buflen);

  /* Return address table and received data. */
  return 2;
}
Пример #5
0
/* Must be passed a buffer of size NUM_BUF_SIZE where buf_end points
 * to 1 byte past the end of the buffer. */
static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, apr_size_t *len)
{
    char *p = buf_end;
    int is_negative;
    apr_size_t sub_len;
    char *ipaddr_str;

    p = conv_10(sa->port, TRUE, &is_negative, p, &sub_len);
    *--p = ':';
    ipaddr_str = buf_end - NUM_BUF_SIZE;
    if (apr_sockaddr_ip_getbuf(ipaddr_str, sa->addr_str_len, sa)) {
        /* Should only fail if the buffer is too small, which it
         * should not be; but fail safe anyway: */
        *--p = '?';
        *len = buf_end - p;
        return p;
    }
    sub_len = strlen(ipaddr_str);
#if APR_HAVE_IPV6
    if (sa->family == APR_INET6 &&
        !IN6_IS_ADDR_V4MAPPED(&sa->sa.sin6.sin6_addr)) {
        *(p - 1) = ']';
        p -= sub_len + 2;
        *p = '[';
        memcpy(p + 1, ipaddr_str, sub_len);
    }
    else
#endif
    {
        p -= sub_len;
        memcpy(p, ipaddr_str, sub_len);
    }

    *len = buf_end - p;
    return (p);
}
Пример #6
0
int lookup_host(const char *name, char *byte_addr, char *ip_addr) {
  char ip_buffer[256];
//  char byte_buffer[256];
  char *s, *bstate;
  int err, i;
  DNS_entry_t *h;
  apr_sockaddr_t *sa;
  int family;

log_printf(20, "lookup_host: start time=" TT " name=%s\n", time(NULL), name);
if (_cache == NULL) log_printf(20, "lookup_host: _cache == NULL\n");

  if (name[0] == '\0') return(1);  //** Return early if name is NULL

//  ipaddr = (ip_addr == NULL) ? ip_buffer : ip_addr;
//  addr = (byte_addr == NULL) ? byte_buffer : byte_addr;
  
//log_printf(20, "lookup_host: before lock\n");
  apr_thread_mutex_lock(_cache->lock);
//log_printf(20, "lookup_host: after lock\n");
 
  if ((time(NULL) > _cache->restart_time) || (apr_hash_count(_cache->table) > _cache->size)) wipe_entries(_cache);
  
  h = (DNS_entry_t *)apr_hash_get(_cache->table, name, APR_HASH_KEY_STRING);

  if (h != NULL) {  //** Got a hit!!
     if (ip_addr != NULL) strcpy(ip_addr, h->ip_addr);
     if (byte_addr != NULL) memcpy(byte_addr, h->addr, DNS_ADDR_MAX);
     family = h->family;
     apr_thread_mutex_unlock(_cache->lock);
     return(0);
  }
  
  //** If we made it here that means we have to look it up
  err = apr_sockaddr_info_get(&sa, name, APR_INET, 80, 0, _cache->mpool);
//log_printf(20, "lookup_host: apr_sockaddr_info_get=%d\n", err);

  if (err != APR_SUCCESS) {
    apr_thread_mutex_unlock(_cache->lock);
    return(-1);
  }
  h = (DNS_entry_t *)apr_palloc(_cache->mpool, sizeof(DNS_entry_t)); //** This is created withthe pool for easy cleanup
  memset(h, 0, sizeof(DNS_entry_t));

  strncpy(h->name, name, sizeof(h->name));  h->name[sizeof(h->name)-1] = '\0';

  apr_sockaddr_ip_getbuf(ip_buffer, sizeof(ip_buffer), sa);
  strcpy(h->ip_addr, ip_buffer);
  
log_printf(20, "lookup_host: start host=%s address=%s\n", name, ip_buffer);

  h->family = DNS_IPV4;
  i = 0;
  for (s = string_token(ip_buffer, ".", &bstate, &err); err == 0; s = string_token(NULL, ".", &bstate, &err)) {
      h->addr[i] = atoi(s);
//n = h->addr[i];
//log_printf(20, "lookup_host: err=%d i=%d n=%d s=%s\n", err, i, n, s);

      i++;
  }
  if (i>4) h->family = DNS_IPV6;

  //** Add the enry to the table
  apr_hash_set(_cache->table, h->name, APR_HASH_KEY_STRING, h);

  //** Return the address
  if (ip_addr != NULL) strcpy(ip_addr, h->ip_addr);
  if (byte_addr != NULL) memcpy(byte_addr, h->addr, DNS_ADDR_MAX);
  family = h->family;

  apr_thread_mutex_unlock(_cache->lock);

  return(0);
}