Beispiel #1
0
int getnetorhostname(char *type, char *name, struct sockaddr_in *sin)
{
#ifndef EMBED

    if (strcmp(type, "net") == 0) {
        struct netent *np = getnetbyname(name);
        int n;

        if (np == 0)
            n = inet_network(name);
        else {
            if (np->n_addrtype != AF_INET)
                return (0);
            n = np->n_net;
            /*
             * getnetbyname returns right-adjusted value.
             */
            if (n < 128)
                n <<= IN_CLASSA_NSHIFT;
            else if (n < 65536)
                n <<= IN_CLASSB_NSHIFT;
            else
                n <<= IN_CLASSC_NSHIFT;
        }
        sin->sin_family = AF_INET;
        sin->sin_addr = inet_makeaddr(n, INADDR_ANY);
        return (1);
    }
    if (strcmp(type, "host") == 0)
        return (gethostnameornumber(name, sin));
#endif
    return (0);
}
Beispiel #2
0
int getStorageStatus(STORAGE_INFO_T *st)
{
	memset(st, sizeof(st), 0);

	if(nvram_get_int("sw_mode")!=SW_MODE_ROUTER) {
		return 0;
	}

	st->MagicWord = EXTEND_MAGIC;
	st->ExtendCap = EXTEND_CAP_WEBDAV;
	if(nvram_get_int("enable_webdav")) 	
		st->u.wt.EnableWebDav = 1;
	else
		st->u.wt.EnableWebDav = 0;

	st->u.wt.HttpType = nvram_get_int("st_webdav_mode");
	st->u.wt.HttpPort = htons(nvram_get_int("webdav_http_port"));
	st->u.wt.HttpsPort = htons(nvram_get_int("webdav_https_port"));

	if(nvram_get_int("ddns_enable_x")) {
		st->u.wt.EnableDDNS = 1;
		snprintf(st->u.wt.HostName, sizeof(st->u.wt.HostName), nvram_safe_get("ddns_hostname_x"));
	}
	else {
		st->u.wt.EnableDDNS = 0;
	}

	// setup st->u.WANIPAddr
	st->u.wt.WANIPAddr = inet_network(get_wanip());

	st->u.wt.WANState = get_wanstate(); 
	st->u.wt.isNotDefault = nvram_get_int("x_Setting");
	return 0;
}
Beispiel #3
0
static int
networks(int argc, char *argv[])
{
	int		i, rv = RV_OK;
	struct netent	*ne;
	in_addr_t	net;

	setnetent(1);
	if (argc == 2) {
		while ((ne = getnetent()) != NULL)
			networksprint(ne);
	} else {
		for (i = 2; i < argc; i++) {
			net = inet_network(argv[i]);
			if (net != INADDR_NONE)
				ne = getnetbyaddr(net, AF_INET);
			else
				ne = getnetbyname(argv[i]);
			if (ne != NULL)
				networksprint(ne);
			else {
				rv = RV_NOTFOUND;
				break;
			}
		}
	}
	endnetent();
	return rv;
}
Beispiel #4
0
struct netent *getnetent(void)
{
    char *p;
    register char *cp, **q;
    struct netent *rv = NULL;

    __UCLIBC_MUTEX_LOCK(mylock);
    if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
	goto DONE;
    }
again:

    if (!line) {
	line = malloc(BUFSIZ + 1);
	if (!line)
	    abort();
    }

    p = fgets(line, BUFSIZ, netf);
    if (p == NULL) {
	goto DONE;
    }
    if (*p == '#')
	goto again;
    cp = any(p, "#\n");
    if (cp == NULL)
	goto again;
    *cp = '\0';
    net.n_name = p;
    cp = any(p, " \t");
    if (cp == NULL)
	goto again;
    *cp++ = '\0';
    while (*cp == ' ' || *cp == '\t')
	cp++;
    p = any(cp, " \t");
    if (p != NULL)
	*p++ = '\0';
    net.n_net = inet_network(cp);
    net.n_addrtype = AF_INET;
    q = net.n_aliases = net_aliases;
    if (p != NULL)
	cp = p;
    while (cp && *cp) {
	if (*cp == ' ' || *cp == '\t') {
	    cp++;
	    continue;
	}
	if (q < &net_aliases[MAXALIASES - 1])
	    *q++ = cp;
	cp = any(cp, " \t");
	if (cp != NULL)
	    *cp++ = '\0';
    }
    *q = NULL;
    rv = &net;
DONE:
    __UCLIBC_MUTEX_UNLOCK(mylock);
    return rv;
}
Beispiel #5
0
static void
test_network (void)
{
  struct netent *nptr;
  u_int32_t ip;

  /*
     This test needs the following line in /etc/networks:
     loopback        127.0.0.0
  */
  nptr = getnetbyname ("loopback");
  output_netent ("getnetbyname (\"loopback\")",nptr);

  nptr = getnetbyname ("LoopBACK");
  output_netent ("getnetbyname (\"LoopBACK\")",nptr);

  ip = inet_network ("127.0.0.0");
  nptr = getnetbyaddr (ip, AF_INET);
  output_netent ("getnetbyaddr (inet_network (\"127.0.0.0\"), AF_INET)",nptr);

  setnetent (0);
  do
    {
      nptr = getnetent ();
      output_netent ("getnetent ()", nptr);
    }
  while (nptr != NULL);
  endnetent ();
}
Beispiel #6
0
/*
 * getnetbyname/addr - get entries from network database
 */
int
dogetnet(const char **list)
{
	struct netent *np;
	int rc = EXC_SUCCESS;

	if (list == NULL || *list == NULL) {
		while ((np = getnetent()) != NULL)
			(void) putnetent(np, stdout);
	} else {
		for (; *list != NULL; list++) {
			long addr = inet_network(*list);
			if (addr != -1)
				np = getnetbyaddr(addr, AF_INET);
			else
				np = getnetbyname(*list);
			if (np == NULL)
				rc = EXC_NAME_NOT_FOUND;
			else
				(void) putnetent(np, stdout);
		}
	}

	return (rc);
}
Beispiel #7
0
int
main (void)
{
  int errors = 0;
  size_t i;
  uint32_t res;

  for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
    {
      printf ("Testing: %s\n", tests[i].network);
      res = inet_network (tests[i].network);

      if (res != tests[i].number)
	{
	  ++errors;
	  printf ("Test failed for inet_network (\"%s\"):\n",
		  tests[i].network);
	  printf ("Expected return value %u (0x%x) but got %u (0x%x).\n",
		  tests[i].number, tests[i].number, res, res);
	}

    }

  return errors != 0;
}
Beispiel #8
0
// Hash Function
unsigned int IPFilter::Hash(char *str)
{
    unsigned int result;

    result = inet_network(str);

    return result;
}
void testValues() {
    f = 2;
    
    inet_network(anystring());

    //@ assert f == 2;
    //@ assert vacuous: \false;
}
Beispiel #10
0
int tun_alloc(const char *ip4, const char *prefix4, 
              const char *ip6, const char *prefix6, char *dev, int common) {
   struct ifreq ifr; 
   int fd, err;
   
   if (common) {
      if((fd = open("/dev/net/tun", O_RDWR)) < 0 ) 
         die("err opening tun fd\n");
   } else {
      char tunname[14];
      sprintf(tunname, "/dev/%s", dev);
      if((fd = open(tunname, O_RDWR)) < 0 ) 
         //      mknod /dev/tun1 c 10 200
         die("err opening tun fd\n");
      return fd;
   }

   memset(&ifr, 0, sizeof(ifr));
   ifr.ifr_flags = IFF_TUN | IFF_NO_PI; 
   if( *dev )
      strncpy(ifr.ifr_name, dev, IFNAMSIZ);

   if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) 
      die("ioctl\n");
   strcpy(dev, ifr.ifr_name);

   /* Create socket */
   int s;
   if ( (s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
      die("socket");

   /* Get interface flags */
   if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) 
      die("cannot get interface flags");

   /* Turn on interface */
   ifr.ifr_flags |= IFF_UP;
   if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) 
      die("ioctl ifup");

   /* Set interface address */
   struct sockaddr_in  tun_addr;
   memset((char *) &tun_addr, 0, sizeof(tun_addr));
   tun_addr.sin_family = AF_INET;
   tun_addr.sin_addr.s_addr = htonl(inet_network(ip4));
   memcpy(&ifr.ifr_addr, &tun_addr, sizeof(struct sockaddr));

   if (ioctl(s, SIOCSIFADDR, &ifr) < 0) 
      die("cannot set IP address. ");

   char net_prefix_cmd[128];
   sprintf(net_prefix_cmd, "ip addr add %s/%s dev %s", ip4, prefix4, dev);
   if (system(net_prefix_cmd) < 0) 
      die("tun prefix");

   close(s);
   return fd;
}                   
Beispiel #11
0
struct netent *
getnetent(void)
{
	char *p, *cp, **q;
	size_t len;

	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "re" )) == NULL)
		return (NULL);
again:
	if ((p = fgetln(netf, &len)) == NULL)
		return (NULL);
	if (p[len-1] == '\n')
		len--;
	if (len >= sizeof(line) || len == 0)
		goto again;
	p = memcpy(line, p, len);
	line[len] = '\0';
	if (*p == '#')
		goto again;
	if ((cp = strchr(p, '#')) != NULL)
		*cp = '\0';
	net.n_name = p;
	if (strlen(net.n_name) > HOST_NAME_MAX)
		net.n_name[HOST_NAME_MAX] = '\0';
	cp = strpbrk(p, " \t");
	if (cp == NULL)
		goto again;
	*cp++ = '\0';
	while (*cp == ' ' || *cp == '\t')
		cp++;
	p = strpbrk(cp, " \t");
	if (p != NULL)
		*p++ = '\0';
	net.n_net = inet_network(cp);
	net.n_addrtype = AF_INET;
	q = net.n_aliases = net_aliases;
	cp = p;
	while (cp && *cp) {
		if (*cp == ' ' || *cp == '\t') {
			cp++;
			continue;
		}
		if (q < &net_aliases[MAXALIASES - 1]) {
			*q++ = cp;
			if (strlen(cp) > HOST_NAME_MAX)
				cp[HOST_NAME_MAX] = '\0';
		}
		cp = strpbrk(cp, " \t");
		if (cp != NULL)
			*cp++ = '\0';
	}
	*q = NULL;
	return (&net);
}
Beispiel #12
0
struct netent *
getnetent(void)
{
	char *p;
	register char *cp, **q;

	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "re")) == NULL)
		return (NULL);
#if (defined(__sparc__) && defined(_LP64)) ||		\
    defined(__alpha__) ||				\
    (defined(__i386__) && defined(_LP64)) ||		\
    (defined(__sh__) && defined(_LP64))
	net.__n_pad0 = 0;
#endif
again:
	p = fgets(line, (int)sizeof line, netf);
	if (p == NULL)
		return (NULL);
	if (*p == '#')
		goto again;
	cp = strpbrk(p, "#\n");
	if (cp == NULL)
		goto again;
	*cp = '\0';
	net.n_name = p;
	cp = strpbrk(p, " \t");
	if (cp == NULL)
		goto again;
	*cp++ = '\0';
	while (*cp == ' ' || *cp == '\t')
		cp++;
	p = strpbrk(cp, " \t");
	if (p != NULL)
		*p++ = '\0';
	net.n_net = inet_network(cp);
	net.n_addrtype = AF_INET;
	q = net.n_aliases = net_aliases;
	if (p != NULL) {
		cp = p;
		while (cp && *cp) {
			if (*cp == ' ' || *cp == '\t') {
				cp++;
				continue;
			}
			if (q < &net_aliases[MAXALIASES - 1])
				*q++ = cp;
			cp = strpbrk(cp, " \t");
			if (cp != NULL)
				*cp++ = '\0';
		}
	}
	*q = NULL;
	return (&net);
}
static struct netent *
fakeaddr(const char *name, int af, struct net_data *net_data) {
	struct pvt *pvt;
	const char *cp;
	u_long tmp;

	if (af != AF_INET) {
		/* XXX should support IPv6 some day */
		errno = EAFNOSUPPORT;
		RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
		return (NULL);
	}
	if (!isascii((unsigned char)(name[0])) ||
	    !isdigit((unsigned char)(name[0])))
		return (NULL);
	for (cp = name; *cp; ++cp)
		if (!isascii(*cp) || (!isdigit((unsigned char)*cp) && *cp != '.'))
			return (NULL);
	if (*--cp == '.')
		return (NULL);

	/* All-numeric, no dot at the end. */

	tmp = inet_network(name);
	if (tmp == INADDR_NONE) {
		RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND);
		return (NULL);
	}

	/* Valid network number specified.
	 * Fake up a netent as if we'd actually
	 * done a lookup.
	 */
	freepvt(net_data);
	net_data->nw_data = malloc(sizeof (struct pvt));
	if (!net_data->nw_data) {
		errno = ENOMEM;
		RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL);
		return (NULL);
	}
	pvt = net_data->nw_data;

	strncpy(pvt->name, name, MAXDNAME);
	pvt->name[MAXDNAME] = '\0';
	pvt->netent.n_name = pvt->name;
	pvt->netent.n_addrtype = AF_INET;
	pvt->netent.n_aliases = pvt->aliases;
	pvt->aliases[0] = NULL;
	pvt->netent.n_net = tmp;

	return (&pvt->netent);
}
struct netent * getnetent(void)
{
    char *p;
    register char *cp, **q;

    LOCK;
    if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
	UNLOCK;
	return (NULL);
    }
again:
    p = fgets(line, BUFSIZ, netf);
    if (p == NULL) {
	UNLOCK;
	return (NULL);
    }
    if (*p == '#')
	goto again;
    cp = any(p, "#\n");
    if (cp == NULL)
	goto again;
    *cp = '\0';
    net.n_name = p;
    cp = any(p, " \t");
    if (cp == NULL)
	goto again;
    *cp++ = '\0';
    while (*cp == ' ' || *cp == '\t')
	cp++;
    p = any(cp, " \t");
    if (p != NULL)
	*p++ = '\0';
    net.n_net = inet_network(cp);
    net.n_addrtype = AF_INET;
    q = net.n_aliases = net_aliases;
    if (p != NULL) 
	cp = p;
    while (cp && *cp) {
	if (*cp == ' ' || *cp == '\t') {
	    cp++;
	    continue;
	}
	if (q < &net_aliases[MAXALIASES - 1])
	    *q++ = cp;
	cp = any(cp, " \t");
	if (cp != NULL)
	    *cp++ = '\0';
    }
    *q = NULL;
    UNLOCK;
    return (&net);
}
Beispiel #15
0
/*=========================FUNCTION===================================================== 
 *
 *      Name: dj_network_if_ipv4_addr_valid
 *      Description: 
 *	  Param: DJ_INT8 *pAddr
 *      Return: true , DJ_SUCCESS
 *			false , DJ_FAILURE
 *
 *======================================================================================
 */
DJ_INT32 dj_network_if_ipv4_addr_valid(DJ_INT8 *pAddr)
{ 
	in_addr_t ret;
 
	ret = inet_network(pAddr); 
	if (ret == -1) 
	{
		dj_printf("%s is not a valid IPv4 address.\n", pAddr); 
		return DJ_FAILURE;
	}

	return DJ_SUCCESS;
}
Beispiel #16
0
int main()
{	char buffer[32];
	int ret = 0;
	int host = 0;
	int network = 0;
	unsigned int address = 0;
	struct in_addr in;
	in.s_addr = 0;

	/*输入一个以.分隔的字符串形式的ip地址*/
	printf("please input your ip address:");
		fgets(buffer, 31, stdin);
	buffer[31] = '\0';
	
	/*示例使用inet_aton()函数*/
	if((ret = inet_aton(buffer, &in) == 0))
		printf("inet_aton:\t invalid address\n");
	else
		printf("inet_aton:\t0x%x\n", in.s_addr);

	/*示例使用inet_addr()函数*/
	if((address = inet_addr(buffer)) == INADDR_NONE)
		printf("inet_addr:\t invalid address\n");
	else
		printf("inet_addr:\t 0x%x\n", address);

	/*示例使用inet_network()函数*/
	if((address = inet_network(buffer)) == -1)
		printf("inet_work:\t invalid address\n");
	else
		printf("inet_work:\t 0x%x\n", address);

	/*示例使用inet_ntoa()函数*/
	if(inet_ntoa(in) == NULL)
		printf("inet_ntoa:\t invalid address\n");
	else
		printf("inet_ntoa:\t%s\n", inet_ntoa(in));

	/*示例使用inet_lnaof()与inet_netof()函数*/
	host = inet_lnaof(in);
	network = inet_netof(in);
	printf("inet_lnaof:\t 0x%x\n", host);
	printf("inet_netof:\t 0x%x\n", network);
	
	in = inet_makeaddr(network, host);
	printf("inet_makeaddr: 0x%x\n", in.s_addr);

	return 0;
	
}
Beispiel #17
0
int findWhitelist(int fd, int type) {
	socklen_t addr_len = sizeof(struct sockaddr_in);

	struct sockaddr sa;
	getpeername(fd,&sa,&addr_len);
	uint32_t ip_to_num = inet_network(inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr));
	
	int ret = 0;
	ret = binarySearch(ip_to_num, type);
	if(ret == -1) {
		redisLog(REDIS_NOTICE,"ip %s is forbidden.",(inet_ntoa(((struct sockaddr_in *)&sa)->sin_addr)));
	}
	return ret;
}
Beispiel #18
0
/*
 * Return the next (non-commented) line from the network-file
 * Format is:
 *   name [=] net [alias..] {\n | # ..}
 *
 * e.g.
 *   loopback     127
 *   arpanet      10   arpa
 */
struct netent * getnetent (void)
{
  static struct netent n;
  char  *name, *net, *alias;
  char   buf[100];

  if (!netdb_init())
     return (NULL);

  do
  {
    if (!fgets(buf,sizeof(buf)-1,networkFile))
       return (NULL);
  }
  while (buf[0] == '#' || buf[0] == ';' || buf[0] == '\n');

  if (networkClose)
     endnetent();

  name = strtok (buf, " \t");
  net  = strtok (NULL,"= \t\n");

  n.n_net  = inet_network (net);
  n.n_name = strdup (name);
  if (!n.n_name)
     return (NULL);

  n.n_addrtype = AF_INET;
  n.n_aliases  = NULL;
  alias        = strtok (NULL," \t\n");

  if (alias && *alias != '#' && *alias != ';')
  {
    char **alist = calloc ((1+MAX_NETW_ALIASES) * sizeof(char*), 1);
    int  i = 0;
    do
    {
      if (*alias == '#' || *alias == ';')
         break;
      if (!alist || (i == MAX_NETW_ALIASES) ||
          (alist[i++] = strdup(alias)) == NULL)
         break;
      alias = strtok (NULL," \t\n");
    }
    while (alias);
    n.n_aliases = alist;
  }
  return (&n);
}
Beispiel #19
0
static unsigned char netmask2prefixlen(const char *netmask)
{
	unsigned char bits = 0;
	in_addr_t mask = inet_network(netmask);
	in_addr_t host = ~mask;

	/* a valid netmask must be 2^n - 1 */
	if ((host & (host + 1)) != 0)
		return -1;

	for (; mask; mask <<= 1)
		++bits;

	return bits;
}
Beispiel #20
0
int
main(int argc, char *argv[])
{
	int			 i, ch, nflag = 0;
	struct netent		*n;
	char			*host;

	while((ch = getopt(argc, argv, "en")) !=  -1) {
		switch(ch) {
		case 'e':
			long_err += 1;
			break;
		case 'n':
			nflag = 1;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	for(i = 0; i < argc; i++) {

		if (i)
			printf("\n");
		printf("===> \"%s\"\n", argv[i]);
		host = gethostarg(argv[i]);

		errno = 0;
		h_errno = 0;
		gai_errno = 0;
		rrset_errno = 0;

		if (nflag)
			n = getnetbyname(host);
		else
			n = getnetbyaddr(inet_network(host), AF_INET);
		if (n)
			print_netent(n);
		print_errors();
	}

	return (0);
}
Beispiel #21
0
/*
 * Return the next (non-commented) line from the network-file
 * Format is:
 *   name [=] net [alias..] {\n | # ..}
 *
 * e.g.
 *   loopback     127
 *   arpanet      10   arpa
 */
struct netent * W32_CALL getnetent (void)
{
  struct _netent n;
  char  *name, *net, *alias;
  char   buf [2*MAX_NAMELEN], *tok;
  int    i;

  if (!netdb_init())
     return (NULL);

  while (1)
  {
    if (!fgets(buf,sizeof(buf),networkFile))
       return (NULL);

    tok = strltrim (buf);
    if (*tok == '#' || *tok == ';' || *tok == '\n')
       continue;

    name = strtok (tok, " \t");
    net  = strtok (NULL, "= \t\n");
    if (name && net)
       break;
  }

  if (networkClose)
     endnetent();

  memset (&n, 0, sizeof(n));
  n.n_net  = inet_network (net);
  n.n_name = name;
  alias    = strtok (NULL, " \t\n");

  for (i = 0; alias && i < MAX_NETENT_ALIASES; i++)
  {
    static char aliases [MAX_NETENT_ALIASES][MAX_NAMELEN];

    if (*alias == '#' || *alias == ';')
       break;

    n.n_aliases[i] = StrLcpy (aliases[i], alias, sizeof(aliases[i]));
    alias = strtok (NULL, " \t\n");
  }
  return fill_netent (&n);
}
Beispiel #22
0
Datei: acl.c Projekt: 274914765/C
static netdef_t *netdef_parse (char *str)
{
    unsigned int ipaddr, netmask;

    netdef_t *netdef;

    char ipbuf[DOTTED_QUAD_LEN + 1];

    if (strcmp (str, "any") == 0)
    {
        ipaddr = 0;
        netmask = 0;
    }
    else
    {
        read_address (&str, ipbuf);
        ipaddr = inet_addr (ipbuf);
        if (ipaddr == INADDR_NONE)
            return NULL;
        if (*str == 0)
            netmask = 0xfffffffful;
        else if (*str != '/')
            return NULL;
        else
        {
            str++;
            if (read_address (&str, ipbuf) == 0)
            {
                /* netmask length */
                unsigned int len = strtoul (ipbuf, NULL, 0);

                if (len > 32)
                    return NULL;
                netmask = 0xfffffffful >> (32 - len);
                netmask <<= (32 - len);
                /*FIXME: hostorder? */
            }
            else
                netmask = inet_network (ipbuf);
            netmask = htonl (netmask);
        }
Beispiel #23
0
// process ip with mask
void IPFilter::splitIP(char* raw, char* pch) {
    byte LEN = pch - raw;
    pch = pch + 1;
    unsigned int mask = atoi(pch);
    raw[LEN] = 0x00;
    

    if (32 == mask) {
        this-> buildHashChar(raw);
        return;
    }

    unsigned int MS = ( INTMX >> (32 - mask) ) << (32 - mask);
    unsigned int ME = (INTMX >> mask);

    unsigned int start = inet_network(raw) & MS;
    unsigned int end = start | ME;
    
    for (unsigned int i = start; i <= end; i++)
        this->buildHash(i);
}
Beispiel #24
0
unsigned char __connman_ipaddress_netmask_prefix_len(const char *netmask)
{
	unsigned char bits;
	in_addr_t mask;
	in_addr_t host;

	if (netmask == NULL)
		return 32;

	mask = inet_network(netmask);
	host = ~mask;

	/* a valid netmask must be 2^n - 1 */
	if ((host & (host + 1)) != 0)
		return -1;

	bits = 0;
	for (; mask; mask <<= 1)
		++bits;

	return bits;
}
Beispiel #25
0
int protocol_listen_open(int domain, int type, int protocal, char *ipstr, int port)
{
    int                 listenfd;
    int                 reuseaddr = 1;
    int                 servport = DEF_SERVER_PORT;
    uint32_t            ip = INADDR_ANY;
    struct sockaddr_in  servaddr;
    struct sockaddr_in  cliaddr;

    listenfd = socket(domain, type, protocal);

    bzero(&servaddr, sizeof(servaddr));

    servaddr.sin_family = domain;
    
    if (port > 0) {
        servport = port;
    }
    
    servaddr.sin_port = htons(servport);
    
    if (ipstr) {
        ip = inet_network(ipstr);
    }
    
    servaddr.sin_addr.s_addr = htons(ip);

    bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuseaddr, 
        sizeof(int));
    
    listen(listenfd, DEF_LSTEN_BACKLOG);

    // todo set recvbuf and sendbuf
    //fcntl(listenfd, F_SETFL, fcntl(listenfd, F_GETFL) | O_NONBLOCK);

    return listenfd;
}
Beispiel #26
0
void socket_open()
{
  struct sockaddr_in ownaddress;

  /* create an UDP socket */
  sockethandler = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);


  /* Initialize ownaddress with zeroes */
  memset( &ownaddress, 0, sizeof(ownaddress));

  ownaddress.sin_family = AF_INET;
  /* only accept connections from CLIENTADDRESS
     could be set to INADDR_ANY to accept all IPs */
  ownaddress.sin_addr.s_addr = htonl(inet_network(CLIENTADDRESS));
  ownaddress.sin_port = htons(PORTNUMBER);

  if (bind(sockethandler, (struct sockaddr *) &ownaddress, sizeof(ownaddress)) < 0)
    {
      error(__FUNCTION__, "Bind error.");
    }

  {
  /* set send and receive buffers */
  /* FIXME: is this necessary? */
    int tmp = MAXPACKETSIZE;
    if ( setsockopt(sockethandler, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0)
      {
	error(__FUNCTION__, "Error setting sendbuffer.");
      }
    if ( setsockopt(sockethandler, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0)
      {
	error(__FUNCTION__, "Error setting receivebuffer.");
      }
  }
}
Beispiel #27
0
void InitClientAccessList(struct ClientRightAscType *ptrClientRightAscList, 
			  struct ClientListType **pptrClientRightList) {
    
    
    struct ClientRightAscType *AsciiList;
    struct ClientListType *BinList;
    
    // assign some memory
    (*pptrClientRightList)=(struct ClientListType*) malloc(
	sizeof(struct ClientListType)*
	sizeof(ptrClientRightAscList)/
	sizeof(struct ClientRightAscType));
    if (!(*pptrClientRightList)) {
	perror("malloc rightlist");
	exit(EXIT_FAILURE);
    }
    
    AsciiList=ptrClientRightAscList;
    BinList=*pptrClientRightList;
    while (AsciiList->ClientIP) {
     	BinList->ClientID=0;
	BinList->ClientAddr.sin_addr.s_addr=htonl(inet_network(AsciiList->ClientIP));
	BinList->ClientRights.SetAll=1;	
	BinList->ClientRights.SetValve=1;
	BinList->ClientRights.SetLaser=1;
	strncpy(BinList->ClientName,AsciiList->ClientName,MAX_CLIENT_NAME);
	BinList++; AsciiList++;
    }
    BinList->ClientID=0;
    BinList->ClientAddr.sin_addr.s_addr=0;
    BinList->ClientRights.SetAll=0;	
    BinList->ClientRights.SetValve=0;
    BinList->ClientRights.SetLaser=0;
    strncpy(BinList->ClientName,"unvalid",MAX_CLIENT_NAME);
    
} //InitClientAccessList
Beispiel #28
0
/*
 * getnetmaskbyaddr - get entries from network database
 */
int
dogetnetmask(const char **list)
{
	int rc = EXC_SUCCESS;
	struct in_addr addr, netmask;

	if (list == NULL || *list == NULL)
		return (EXC_ENUM_NOT_SUPPORTED);

	for (; *list != NULL; list++) {
		addr.s_addr = htonl(inet_network(*list));
		if (addr.s_addr != -1) {
			if (getnetmaskbyaddr(addr, &netmask) == 0) {
				(void) putnetmask(addr, netmask, stdout);
			} else {
				rc = EXC_NAME_NOT_FOUND;
			}
		} else {
			rc = EXC_NAME_NOT_FOUND;
		}
	}

	return (rc);
}
Beispiel #29
0
Datei: main.c Projekt: lmatz/NAT
int main(int argc, char **argv) {

	struct nfq_handle *nfqHandle;
	struct nfq_q_handle *myQueue;
	struct nfnl_handle *netlinkHandle;

	int fd, res;
	char buf[BUFF_SIZE];

	if (argc!=4) {
		fprintf(stderr,"Error in number of arguments\n");
		exit(-1);
	}

	// get the public IP address. And the public IP address is host byte order.
	publicIP = inet_network(argv[1]);
	if (publicIP == 0) {
		fprintf(stderr,"Error in public IP\n");
		exit(-1);
	}
	fprintf(stdout,"publicIP: %u\n",publicIP);

	// get the subnet IP address. And the subnet IP address is host byte order.
	internalIP = inet_network(argv[2]);
	if (internalIP == 0) {
		fprintf(stderr,"Error in internal IP\n");
		exit(-1);
	}
	fprintf(stdout,"internalIP: %u\n",internalIP);
	mask = mask << (32-atoi(argv[3]));
	subnetIP = internalIP & mask;
	fprintf(stdout,"subnetIP: %u\n",subnetIP);

	initUdp( &udpHead );
	initTcp( &tcpHead );

	if ( !(nfqHandle = nfq_open())) {
		fprintf(stderr, "Error in nfq_open()\n");
		exit(-1);
	}

	if ( nfq_unbind_pf(nfqHandle, AF_INET) < 0 ) {
		fprintf(stderr, "Error in nfq_unbind_pf()\n");
		exit(-1);
	}

	if ( nfq_bind_pf(nfqHandle, AF_INET) < 0) {
		fprintf(stderr, "Error in nfq_bind_pf()\n");
		exit(-1);
	}

	if ( !(myQueue = nfq_create_queue(nfqHandle, 0, &Callback123, NULL)) ) {
		fprintf(stderr, "Error in nfq_create_queue()\n");
		exit(1);
	}

	if ( nfq_set_mode(myQueue, NFQNL_COPY_PACKET, 0xffff) <0 ) {
		fprintf(stderr, "Could not set packet copy mode\n");
		exit(1);
	}

	netlinkHandle = nfq_nfnlh(nfqHandle);
	fd = nfnl_fd(netlinkHandle);

	
	fprintf(stdout,"ready to receive packets\n");

	//Start to process the packet we receive.
	while ( (res = recv(fd, buf , sizeof(buf), 0))  && res>=0 ) {
		
		printf("\n\n\n*******NEW ONE*******\n");
		fprintf(stdout,"in the loop\n");
		nfq_handle_packet(nfqHandle, buf , res);
	}
	//End the process


	nfq_destroy_queue(myQueue);

	nfq_close(nfqHandle);

	return 0;

}
Beispiel #30
0
static int
_getnetbynis(const char *name, char *map, int af, struct netent *ne,
    struct netent_data *ned)
{
	char *p, *bp, *ep;
	char *cp, **q;
	char *result;
	int resultlen, len;
	char ypbuf[YPMAXRECORD + 2];

	switch(af) {
	case AF_INET:
		break;
	default:
	case AF_INET6:
		errno = EAFNOSUPPORT;
		return (-1);
	}

	if (ned->yp_domain == (char *)NULL)
		if (yp_get_default_domain (&ned->yp_domain))
			return (-1);

	if (yp_match(ned->yp_domain, map, name, strlen(name), &result,
	    &resultlen))
		return (-1);

	bcopy((char *)result, (char *)&ypbuf, resultlen);
	ypbuf[resultlen] = '\0';
	free(result);
	result = (char *)&ypbuf;

	if ((cp = index(result, '\n')))
		*cp = '\0';

	cp = strpbrk(result, " \t");
	*cp++ = '\0';
	bp = ned->netbuf;
	ep = ned->netbuf + sizeof ned->netbuf;
	len = strlen(result) + 1;
	if (ep - bp < len) {
		RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
		return (-1);
	}
	strlcpy(bp, result, ep - bp);
	ne->n_name = bp;
	bp += len;

	while (*cp == ' ' || *cp == '\t')
		cp++;

	ne->n_net = inet_network(cp);
	ne->n_addrtype = AF_INET;

	q = ne->n_aliases = ned->net_aliases;
	cp = strpbrk(cp, " \t");
	if (cp != NULL)
		*cp++ = '\0';
	while (cp && *cp) {
		if (*cp == ' ' || *cp == '\t') {
			cp++;
			continue;
		}
		if (q > &ned->net_aliases[_MAXALIASES - 1])
			break;
		p = strpbrk(cp, " \t");
		if (p != NULL)
			*p++ = '\0';
		len = strlen(cp) + 1;
		if (ep - bp < len)
			break;
		strlcpy(bp, cp, ep - bp);
		*q++ = bp;
		bp += len;
		cp = p;
	}
	*q = NULL;
	return (0);
}