예제 #1
0
int testDAD_init_suite() {
    char *macdata[] = {
        "ec:ac:24:70:4c:f6", "EC:AC:24:70:4C:F7",
        "Ec:aC:24:70:4c:F8", "38:45:63:f6:8e:83",
        "f7:d5:9c:38:8f:db", "bf:de:9e:0e:6e:eb",
        "b6:f5:bf:ff:c2:32", "83:23:3d:f2:17:31",
        "26:3e:f2:4e:0d:ff", "d4:fc:84:dc:81:a9",
        NULL
    };
    char *ipdata[] = {
        "ffe5:1838:afd7:2472:b3e7:3ae6:a228:12b4", "0E51:A030:C113:3838:C080:DD09:4D6C:189D",
        "9af2:2354:ecd1:f412:b9e3:648C:519D:DDDF", "69df:0c97:aaff:ef86:7cc0:ede5:2a7b:6cc0",
        "4605:da5a:9f0f:8a36:f63a:e40a:1614:0554", "6956:2caa:6abf:f6b2:3c57:a99c:d88d:ff7d",
        "4a7d:e4d7:54ea:48ed:8467:6b59:3670:a941", "1d24:c623:219a:0aa1:7628:51ce:870a:898f",
        "5e0f:1e4b:e619:d99f:dd65:caf1:12a5:01fc", "8693:39c0:36de:d326:cb63:a585:c63e:2f04",
        NULL
    };
    char *ipstr = "5338:41ab:64f7:a598:39b6:e0f8:5a6e:5ec8";
    IP_t ip;
    int i;
    time_t ts = 1234567890;
    
    ip_parse(&ip, ipstr);

    for (i = 0; i < EXAMPLE_LEN; i++) {
        host_set(&h[i], mac_parse(NULL, macdata[i]), ip_parse(NULL, ipdata[i]), ts++);
    }
    for (i = 0; i < EXAMPLE_LEN; i++) {
        host_set(&g[i], mac_parse(NULL, macdata[i]), &ip, ts++);
    }
    return 0;
}
예제 #2
0
/* Translates 'host_name', which must be a string representation of an IP
 * address, into a numeric IP address in '*addr'.  Returns 0 if successful,
 * otherwise a positive errno value. */
int
lookup_ip(const char *host_name, struct in_addr *addr)
{
    if (!ip_parse(host_name, &addr->s_addr)) {
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
        VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
        return ENOENT;
    }
    return 0;
}
예제 #3
0
/* Translates 'host_name', which must be a host name or a string representation
 * of an IP address, into a numeric IP address in '*addr'.  Returns 0 if
 * successful, otherwise a positive errno value.
 *
 * Most Open vSwitch code should not use this because it causes deadlocks:
 * getaddrinfo() sends out a DNS request but that starts a new flow for which
 * OVS must set up a flow, but it can't because it's waiting for a DNS reply.
 * The synchronous lookup also delays other activity.  (Of course we can solve
 * this but it doesn't seem worthwhile quite yet.)  */
int
lookup_hostname(const char *host_name, struct in_addr *addr)
{
    struct addrinfo *result;
    struct addrinfo hints;

    if (ip_parse(host_name, &addr->s_addr)) {
        return 0;
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;

    switch (getaddrinfo(host_name, NULL, &hints, &result)) {
    case 0:
        *addr = ALIGNED_CAST(struct sockaddr_in *,
                             result->ai_addr)->sin_addr;
        freeaddrinfo(result);
        return 0;

#ifdef EAI_ADDRFAMILY
    case EAI_ADDRFAMILY:
#endif
    case EAI_NONAME:
    case EAI_SERVICE:
        return ENOENT;

    case EAI_AGAIN:
        return EAGAIN;

    case EAI_BADFLAGS:
    case EAI_FAMILY:
    case EAI_SOCKTYPE:
        return EINVAL;

    case EAI_FAIL:
        return EIO;

    case EAI_MEMORY:
        return ENOMEM;

#if defined (EAI_NODATA) && EAI_NODATA != EAI_NONAME
    case EAI_NODATA:
        return ENXIO;
#endif

#ifdef EAI_SYSTEM
    case EAI_SYSTEM:
        return sock_errno();
#endif

    default:
        return EPROTO;
    }
}
예제 #4
0
파일: udpserver.c 프로젝트: agl/dnscurve
int
main(int argc, char **argv) {
  if (argc < 4)
    return usage(argv[0]);

  uint32_t bind_ip;
  if (!ip_parse(&bind_ip, argv[1]))
    return usage(argv[0]);

  char *endptr;
  unsigned long port = strtoul(argv[2], &endptr, 10);
  if (*endptr)
    return usage(argv[0]);

  if (port == 0 || port > 65535) {
    fprintf(stderr, "Port number out of range (1..65535)\n");
    return 1;
  }

  const int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0) {
    perror("socket");
    return 1;
  }

  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = bind_ip;
  sin.sin_port = htons(port);

  int n;
  do {
    n = bind(sock, (struct sockaddr *) &sin, sizeof(sin));
    if (n) {
      if (errno == EADDRINUSE || errno == ENOMEM) {
        sleep(1);
        continue;
      }
      perror("bind");
      return 1;
    }
  } while (n);

  if (sock != 3) {
    dup2(sock, 3);
    close(sock);
  }

  execvp(argv[3], &argv[3]);
  perror("execvp");

  return 1;
}
예제 #5
0
/* Extracts and returns the server name from 'suffix'.  The caller must
 * eventually free it.
 *
 * Returns NULL if there is no server name, and particularly if it is an IP
 * address rather than a host name, since RFC 3546 is explicit that IP
 * addresses are unsuitable as server name indication (SNI). */
static char *
get_server_name(const char *suffix_)
{
    char *suffix = xstrdup(suffix_);

    char *host, *port;
    inet_parse_host_port_tokens(suffix, &host, &port);

    ovs_be32 ipv4;
    struct in6_addr ipv6;
    char *server_name = (ip_parse(host, &ipv4) || ipv6_parse(host, &ipv6)
                         ? NULL : xstrdup(host));

    free(suffix);

    return server_name;
}
예제 #6
0
/**
*	Brain 부분에서 ARP 테이블 갱신 
*/
int main(){

	int readn=0;
	int pipeFd=0;
	int isOn=0, returnbyte=0;
	char buffer[BUFFER_SIZE];
	char query_body[2000];
	if((pipeFd = open("/tmp/write_arp", O_RDWR))<0){
		perror("fail to call");	
		exit(1);
	}
	memset(buffer, 0x00, BUFFER_SIZE);
	while(1){
		if(isOn==1){
			MYSQL *connection=NULL, conn;
			int query_stat =0;
			mysql_init(&conn);
			connection = mysql_real_connect(&conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 3306, (char*)NULL, 0);
			sprintf(query_result, "%s WHEN pid= 252 THEN 1 %s %s", query_head, query_body, query_leg);
			query_stat = mysql_query(connection, query_result);
			memset(query_result, 0x00, strlen(query_result));
			memset(query_body, 0x00, strlen(query_body));
			returnbyte = 0;
			isOn=0;
			mysql_close(connection);
		}
		readn = read(pipeFd, buffer, BUFFER_SIZE);
		if(readn>0){
			char temp[20],ptr;
			int ip=0;	
			memset(temp, 0x00, BUFFER_SIZE);
			sprintf(temp, "%s", buffer+2);
			ip= ip_parse(temp);	
			if(ip==250){
				isOn=1;
			}
			returnbyte += sprintf(query_body+returnbyte, "WHEN pid =%d THEN 1 ", ip);
		}
		memset(buffer, 0x00, BUFFER_SIZE);

	}
	return 0;
}
예제 #7
0
파일: ip.c 프로젝트: vstakhov/curvedns
// outcount		The number of anysin_t objects that are returned
// inip			The IP string (like '127.0.0.1,10.0.0.1')
// inport		The port string (like '53' or '1053')
// return		Array of anysin_t objects, for each IP in the IP string one
anysin_t *ip_multiple_parse(int *outcount, const char *inip, const char *inport) {
	anysin_t *result = NULL;
	char *prev, *p;
	int i, len, found, wasnull;

	*outcount = 1;
	len = strlen(inip);
	for (p = (char *)inip; *p; p++) {
		if (*p == ',') (*outcount)++;
	}

	result = (anysin_t *) calloc(*outcount, sizeof(anysin_t));
	if (!result)
		goto wrong;

	p = prev = (char *)inip;
	found = wasnull = 0;
	for (i = 0; (i <= len) && (found < *outcount); i++, p++) {
		wasnull = 0;
		if (*p == '\0') wasnull = 1;
		if (wasnull || (*p == ',')) {
			*p = '\0';
			if (!ip_parse(&result[found], prev, inport))
				goto wrong;
			if (!wasnull)	// make sure prev only points to okay memory
				prev = p + 1;
			found++;
		}
	}
	return result;

wrong:
	debug_log(DEBUG_FATAL, "ip_multiple_parse(): failed to parse IP addresses\n");
	if (result)
		free(result);
	return NULL;
}
예제 #8
0
int
main(int argc, char **argv) {
  global_urandom_fd = open("/dev/urandom", O_RDONLY);
  if (global_urandom_fd < 0) {
    perror("Opening /dev/urandom");
    return 1;
  }

  if (argc != 4)
    return usage(argv[0]);

  uint32_t target_ip;

  if (!ip_parse(&target_ip, argv[1]))
    return usage(argv[0]);

  const unsigned portnum = strtoul(argv[2], NULL, 10);

  uint8_t server_pk[32];
  unsigned server_pk_len = sizeof(server_pk);
  if (!base32_decode(server_pk, &server_pk_len, (const uint8_t *) argv[3], strlen(argv[3]), 1)) {
    perror("base32_decode");
    return 1;
  }
  if (server_pk_len != 32) {
    fprintf(stderr, "Invalid server public key\n");
    return 1;
  }

  static const uint8_t query[] =
    "\xab\xcd\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03org\x00\x00\x02\x00\x01";

  uint8_t pk[32];
  crypto_box_curve25519xsalsa20poly1305_keypair(pk, global_secret_key);

  uint8_t nonce[24];
  uint8_t nonce_and_box[4096];

  randombytes(nonce, 12);
  memset(nonce + 12, 0, 12);

  memset(nonce_and_box, 0, 32);
  memcpy(nonce_and_box + 32, query, sizeof(query) - 1);

  crypto_box_curve25519xsalsa20poly1305
    (nonce_and_box, nonce_and_box, 32 + sizeof(query) - 1, nonce,
     server_pk, global_secret_key);

  memcpy(nonce_and_box + 4, nonce, 12);

  write(1, pk, 32);
  write(1, nonce_and_box + 4, 12 + 16 + sizeof(query) - 1);

  uint8_t request[4096];
  unsigned requestlen = sizeof(request) - 2;

  if (!dns_curve_request_build(request + 2, &requestlen,
                               nonce_and_box + 4, 12 + 16 + sizeof(query) - 1,
                               pk, (unsigned char *) "\x06google\x03org\x00")) {
    perror("dns_curve_request_build");
    return 1;
  }

  requestlen += 2;

  const int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (fd < 0) {
    perror("socket");
    return 1;
  }

  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = target_ip;
  sin.sin_port = htons(portnum);

  ssize_t n;
  do {
    n = sendto(fd, request, requestlen, 0, (struct sockaddr *) &sin, sizeof(sin));
  } while (n == -1 && errno == EINTR);

  if (n < 0) {
    perror("sendto");
    return 1;
  }

  return 0;
}
예제 #9
0
int main(int argc, char *argv[]) {
	int uid = -1, gid = -1, tmp;

	if (argc != 5)
		return usage(argv[0]);

	// First determine debug level:
	if (misc_getenv_int("CURVEDNS_DEBUG", 0, &tmp)) {
		if ((tmp > 0) && (tmp < 6))
			debug_level = tmp;
	}
	debug_log(DEBUG_FATAL, "starting %s version %s (debug level %d)\n", argv[0], CURVEDNS_VERSION, debug_level);

	// Parse the listening IP addresses:
	local_addresses = ip_multiple_parse(&local_addresses_count, argv[1], argv[2]);
	if (!local_addresses) {
		debug_log(DEBUG_FATAL, "listening IPs or port malformed\n");
		return 1;
	}

	// Parse target IP:
	if (!ip_parse(&global_target_address, argv[3], argv[4]))
		return usage(argv[0]);
	
	// XXX: should be handled by ip_parse() :/?
	if (global_target_address.sa.sa_family == AF_INET) {
		global_target_address_len = sizeof(struct sockaddr_in);
	} else {
		global_target_address_len = sizeof(struct sockaddr_in6);
	}

	// Open urandom for randomness during run:
	if (!misc_crypto_random_init()) {
		debug_log(DEBUG_FATAL, "unable to open /dev/urandom for randomness\n");
		return 1;
	}

	// Fetch the secret key from environment and setup:
	if (!misc_getenv_key("CURVEDNS_PRIVATE_KEY", 1, global_secret_key))
		return 1;

	// Fetch group id:
	misc_getenv_int("GID", 0, &gid);

	// Fetch user id:
	misc_getenv_int("UID", 0, &uid);
	// Open UDP and TCP sockets on local address(es):
	if (!ip_init(local_addresses, local_addresses_count)) {
		debug_log(DEBUG_FATAL, "ip_init(): failed, are you root?\n");
		return 1;
	}

	// Do exactly this ;]
	debug_log(DEBUG_INFO, "main(): throwing away root privileges\n");

	if (gid != -1 && setgid(gid) != 0) {
		debug_log(DEBUG_FATAL, "main(): unable to set gid\n");
		return 1;
	}
	if (uid != -1 && setuid(uid) != 0) {
		debug_log(DEBUG_FATAL, "main(): unable to set uid\n");
		return 1;
	}
	// Fetch all optional options from the environment:
	if (!getenvoptions())
		return 1;

	// Initialize the event handler, the core of CurveDNS:
	if (!event_init()) {
		debug_log(DEBUG_FATAL, "event_init(): failed\n");
		return 1;
	}

	// Initialize the DNSCurve part (such as the shared secret cache):
	if (!dnscurve_init()) {
		debug_log(DEBUG_FATAL, "dnscurve_init(): failed\n");
		return 1;
	}

	// Start the event worker:
	event_worker();

	// Should only be reached when loop is destroyed (at SIGINT and SIGTERM):
	return 0;
}
예제 #10
0
파일: ipobj.c 프로젝트: sdnnfv/snort
int ipset_parse(IPSET *ipset, char *ipstr)
{
    char *copy, *startIP, *endIP;
    int parse_count = 0;
    char set_not_flag = 0;
    char item_not_flag;
    char open_bracket = 0;
    sfip_t ip;
    PORTSET portset;

    copy = strdup(ipstr);

    if(!copy)
        return -2;

    startIP = copy;

    if (*startIP == '!')
    {
        set_not_flag = 1;
        startIP++;
    }

    while (startIP)
    {
        if (*startIP == '[')
        {
            open_bracket++;
            startIP++;
            if (!*startIP)
                break;
        }

        if ((*startIP == ']') || (*startIP == '\0'))
        {
            open_bracket--;
            break;
        }

        portset_init(&portset);

        if(ip_parse(startIP, &ip, &item_not_flag, &portset, &endIP) != 0)
        {
            free(copy);
            return -5;
        }

        if(ipset_add(ipset, &ip, &portset, (item_not_flag ^ set_not_flag)) != 0)
        {
            free(copy);
            return -6;
        }

        parse_count++;

        if (endIP && (*endIP != ']'))
        {
            endIP++;
        }

        startIP = endIP;
    }

    free(copy);

    if (!parse_count)
        return -7;

    if (open_bracket)
        return -8;

    return 0;
}
예제 #11
0
파일: lex.c 프로젝트: l8huang/ovs
static const char *
lex_parse_integer__(const char *p, struct lex_token *token)
{
    lex_token_init(token);
    token->type = LEX_T_INTEGER;
    memset(&token->value, 0, sizeof token->value);
    const char *start = p;
    const char *end = start;
    while (isalnum((unsigned char) *end) || *end == ':'
           || (*end == '.' && end[1] != '.')) {
        end++;
    }
    size_t len = end - start;

    int n;
    struct eth_addr mac;

    if (!len) {
        lex_error(token, "Integer constant expected.");
    } else if (len == 17
               && ovs_scan(start, ETH_ADDR_SCAN_FMT"%n",
                           ETH_ADDR_SCAN_ARGS(mac), &n)
               && n == len) {
        token->value.mac = mac;
        token->format = LEX_F_ETHERNET;
    } else if (start + strspn(start, "0123456789") == end) {
        if (p[0] == '0' && len > 1) {
            lex_error(token, "Decimal constants must not have leading zeros.");
        } else {
            unsigned long long int integer;
            char *tail;

            errno = 0;
            integer = strtoull(p, &tail, 10);
            if (tail != end || errno == ERANGE) {
                lex_error(token, "Decimal constants must be less than 2**64.");
            } else {
                token->value.integer = htonll(integer);
                token->format = LEX_F_DECIMAL;
            }
        }
    } else if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
        if (len > 2) {
            lex_parse_hex_integer(start + 2, len - 2, token);
        } else {
            lex_error(token, "Hex digits expected following 0%c.", p[1]);
        }
    } else if (len < INET6_ADDRSTRLEN) {
        char copy[INET6_ADDRSTRLEN];
        memcpy(copy, p, len);
        copy[len] = '\0';

        if (ip_parse(copy, &token->value.ipv4)) {
            token->format = LEX_F_IPV4;
        } else if (ipv6_parse(copy, &token->value.ipv6)) {
            token->format = LEX_F_IPV6;
        } else {
            lex_error(token, "Invalid numeric constant.");
        }
    } else {
        lex_error(token, "Invalid numeric constant.");
    }

    ovs_assert(token->type == LEX_T_INTEGER || token->type == LEX_T_ERROR);
    return end;
}
예제 #12
0
파일: lex.c 프로젝트: ALutzG/ovs
static const char *
lex_parse_integer__(const char *p, struct lex_token *token)
{
    lex_token_init(token);
    token->type = LEX_T_INTEGER;
    memset(&token->value, 0, sizeof token->value);

    /* Find the extent of an "integer" token, which can be in decimal or
     * hexadecimal, or an Ethernet address or IPv4 or IPv6 address, as 'start'
     * through 'end'.
     *
     * Special cases we handle here are:
     *
     *     - The ellipsis token "..", used as e.g. 123..456.  A doubled dot
     *       is never valid syntax as part of an "integer", so we stop if
     *       we encounter two dots in a row.
     *
     *     - Syntax like 1.2.3.4:1234 to indicate an IPv4 address followed by a
     *       port number should be considered three tokens: 1.2.3.4 : 1234.
     *       The obvious approach is to allow just dots or just colons within a
     *       given integer, but that would disallow IPv4-mapped IPv6 addresses,
     *       e.g. ::ffff:192.0.2.128.  However, even in those addresses, a
     *       colon never follows a dot, so we stop if we encounter a colon
     *       after a dot.
     *
     *       (There is no corresponding way to parse an IPv6 address followed
     *       by a port number: ::1:2:3:4:1234 is unavoidably ambiguous.)
     */
    const char *start = p;
    const char *end = start;
    bool saw_dot = false;
    while (isalnum((unsigned char) *end)
           || (*end == ':' && !saw_dot)
           || (*end == '.' && end[1] != '.')) {
        if (*end == '.') {
            saw_dot = true;
        }
        end++;
    }
    size_t len = end - start;

    int n;
    struct eth_addr mac;

    if (!len) {
        lex_error(token, "Integer constant expected.");
    } else if (len == 17
               && ovs_scan(start, ETH_ADDR_SCAN_FMT"%n",
                           ETH_ADDR_SCAN_ARGS(mac), &n)
               && n == len) {
        token->value.mac = mac;
        token->format = LEX_F_ETHERNET;
    } else if (start + strspn(start, "0123456789") == end) {
        if (p[0] == '0' && len > 1) {
            lex_error(token, "Decimal constants must not have leading zeros.");
        } else {
            unsigned long long int integer;
            char *tail;

            errno = 0;
            integer = strtoull(p, &tail, 10);
            if (tail != end || errno == ERANGE) {
                lex_error(token, "Decimal constants must be less than 2**64.");
            } else {
                token->value.integer = htonll(integer);
                token->format = LEX_F_DECIMAL;
            }
        }
    } else if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
        if (len > 2) {
            lex_parse_hex_integer(start + 2, len - 2, token);
        } else {
            lex_error(token, "Hex digits expected following 0%c.", p[1]);
        }
    } else if (len < INET6_ADDRSTRLEN) {
        char copy[INET6_ADDRSTRLEN];
        memcpy(copy, p, len);
        copy[len] = '\0';

        if (ip_parse(copy, &token->value.ipv4)) {
            token->format = LEX_F_IPV4;
        } else if (ipv6_parse(copy, &token->value.ipv6)) {
            token->format = LEX_F_IPV6;
        } else {
            lex_error(token, "Invalid numeric constant.");
        }
    } else {
        lex_error(token, "Invalid numeric constant.");
    }

    ovs_assert(token->type == LEX_T_INTEGER || token->type == LEX_T_ERROR);
    return end;
}