Exemple #1
0
Fichier : udp.c Projet : luvit/luv
static int luv_udp_getsockname(lua_State* L) {
    uv_udp_t* handle = luv_check_udp(L, 1);
    struct sockaddr_storage address;
    int addrlen = sizeof(address);
    int ret = uv_udp_getsockname(handle, (struct sockaddr*)&address, &addrlen);
    if (ret < 0) return luv_error(L, ret);
    parse_sockaddr(L, &address, addrlen);
    return 1;
}
Exemple #2
0
static void luv_udp_recv_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags) {
  lua_State* L = luv_state(handle->loop);

  // err
  if (nread < 0) {
    luv_status(L, nread);
  }
  else {
    lua_pushnil(L);
  }

  // data
  if (nread == 0) {
    if (addr) {
      lua_pushstring(L, "");
    }
    else {
      lua_pushnil(L);
    }
  }
  else if (nread > 0) {
    lua_pushlstring(L, buf->base, nread);
  }
  else {
    lua_pushnil(L);
  }
  if (buf) free(buf->base);

  // address
  if (addr) {
    parse_sockaddr(L, (struct sockaddr_storage*)addr);
  }
  else {
    lua_pushnil(L);
  }

  // flags
  lua_newtable(L);
  if (flags & UV_UDP_PARTIAL) {
    lua_pushboolean(L, 1);
    lua_setfield(L, -2, "partial");
  }

  luv_call_callback(L, (luv_handle_t*)handle->data, LUV_RECV, 4);
}
Exemple #3
0
int main(int argc, char **argv, char **envp) {
  size_t buf_len = 4096;
  char *buf = malloc(buf_len);
  char *packed_saddr;
  unsigned char *s_inet;
  struct ip_port *ipp;

  memset(buf, 0, buf_len);
  while ((fgets(buf, buf_len, stdin)) != NULL) {
    if (!(packed_saddr = sockaddr_from_audit_log(buf)))
      continue;
    s_inet = bytes_from_packed_string(packed_saddr);
    if (!(ipp = parse_sockaddr(s_inet)))
      continue;

    printf("%s:%d\n", ipp->ip, ipp->port);
    free(ipp);
    memset(buf, 0, buf_len);
  }
  return 0;
}
/*
 * This code expects the path to be syslog:<priority>
 */
int
logfile_mod_udp_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
{
    l_udp_t *ll;
    struct sockaddr_in addr;
    char *strAddr;

    lf->f_close = logfile_mod_udp_close;
    lf->f_linewrite = logfile_mod_udp_writeline;
    lf->f_linestart = logfile_mod_udp_linestart;
    lf->f_lineend = logfile_mod_udp_lineend;
    lf->f_flush = logfile_mod_udp_flush;
    lf->f_rotate = logfile_mod_udp_rotate;

    ll = xcalloc(1, sizeof(*ll));
    lf->data = ll;

    if (strncmp(path, "//", 2) == 0) {
        path += 2;
    }
    strAddr = xstrdup(path);
    if (!parse_sockaddr(strAddr, &addr)) {
        if (lf->flags.fatal) {
            fatalf("Invalid UDP logging address '%s'\n", lf->path);
        } else {
            debug(50, 1) ("Invalid UDP logging address '%s'\n", lf->path);
            safe_free(strAddr);
            return FALSE;
        }
    }
    safe_free(strAddr);

    ll->fd = comm_open(SOCK_DGRAM,
                       IPPROTO_UDP,
                       no_addr,
                       0,
                       COMM_NONBLOCKING,
                       "UDP log socket");
    if (ll->fd < 0) {
        if (lf->flags.fatal) {
            fatalf("Unable to open UDP socket for logging\n");
        } else {
            debug(50, 1) ("Unable to open UDP socket for logging\n");
            return FALSE;
        }
    }
    if (comm_connect_addr(ll->fd, &addr)) {
        if (lf->flags.fatal) {
            fatalf("Unable to connect to %s for UDP log: %s\n", lf->path, xstrerror());
        } else {
            debug(50, 1) ("Unable to connect to %s for UDP log: %s\n", lf->path, xstrerror());
            return FALSE;
        }
    }
    if (ll->fd == -1) {
        if (ENOENT == errno && fatal_flag) {
            fatalf("Cannot open '%s' because\n"
                   "\tthe parent directory does not exist.\n"
                   "\tPlease create the directory.\n", path);
        } else if (EACCES == errno && fatal_flag) {
            fatalf("Cannot open '%s' for writing.\n"
                   "\tThe parent directory must be writeable by the\n"
                   "\tuser '%s', which is the cache_effective_user\n"
                   "\tset in squid.conf.", path, Config.effectiveUser);
        } else {
            debug(50, 1) ("logfileOpen (stdio): %s: %s\n", path, xstrerror());
            return 0;
        }
    }
    /* Force buffer size to something roughly fitting inside an MTU */
    /*
     * XXX note the receive side needs to receive the whole packet at once;
     * applications like netcat have a small default receive buffer and will
     * truncate!
     */
    bufsz = 1400;
    if (bufsz > 0) {
        ll->buf = (char *) xmalloc(bufsz);
        ll->bufsz = bufsz;
    }
    return 1;
}