Beispiel #1
0
int scamper_ip4_openraw(void)
{
    int fd = -1;

#if defined(WITHOUT_PRIVSEP)
    int hdr = 1;
    if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1)
    {
        printerror(errno, strerror, __func__, "could not open socket");
        goto err;
    }

    if(setsockopt(fd, IPPROTO_IP, IP_HDRINCL, (void *)&hdr, sizeof(hdr)) == -1)
    {
        printerror(errno, strerror, __func__, "could not IP_HDRINCL");
        goto err;
    }
#else
    if((fd = scamper_privsep_open_rawip()) == -1)
    {
        printerror(errno, strerror, __func__, "could not open socket");
        goto err;
    }
#endif

    return fd;

err:
    if(fd != -1) scamper_ip4_close(fd);
    return -1;
}
Beispiel #2
0
static scamper_fd_t *fd_null(int type)
{
  scamper_fd_t *fdn = NULL, findme;
  int fd = -1;

  /* first check if a sharable fd exists for this type */
  findme.type = type;
  if((fdn = fd_find(&findme)) != NULL)
    {
      return fdn;
    }

  if(type == SCAMPER_FD_TYPE_RTSOCK) fd = scamper_rtsock_open();
  else if(type == SCAMPER_FD_TYPE_IFSOCK) fd = socket(AF_INET, SOCK_DGRAM, 0);
  else if(type == SCAMPER_FD_TYPE_IP4) fd = scamper_ip4_openraw();

  if(fd == -1 || (fdn = fd_alloc(type, fd)) == NULL ||
     (fdn->fd_tree_node = splaytree_insert(fd_tree, fdn)) == NULL ||
     (fdn->fd_list_node = dlist_tail_push(fd_list, fdn)) == NULL)
    {
      goto err;
    }

  return fdn;

 err:
  if(fd != -1)
    {
      if(type == SCAMPER_FD_TYPE_RTSOCK)
	scamper_rtsock_close(fd);
      else if(type == SCAMPER_FD_TYPE_IFSOCK)
	close(fd);
      else if(type == SCAMPER_FD_TYPE_IP4)
	scamper_ip4_close(fd);
    }
  if(fdn != NULL) fd_free(fdn);
  return NULL;
}