<commit_before>/*
 * intf.c
 *
 * Copyright (c) 2001 <NAME> <<EMAIL>>
 *
 * $Id$
 */

#include "config.h"

#include <sys/types.h>

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "dnet.h"

static intf_t	*intf;

static void
usage(void)
{
	fprintf(stderr, "Usage: intf show\n"
	                "Usage: intf any\n"
			"Usage: intf get device [ip|eth]\n"
			"Usage: intf set device addr "
	    "[[up|down|arp|noarp] ...]\n");
	exit(1);
}

static char *
flags2string(int flags)
{
	static char buf[256];

	buf[0] = '\0';
	
	if (flags & INTF_FLAG_UP)
		strlcat(buf, ",UP", sizeof(buf));
	if (flags & INTF_FLAG_LOOPBACK)
		strlcat(buf, ",LOOP", sizeof(buf));
	if (flags & INTF_FLAG_POINTOPOINT)
		strlcat(buf, ",P2P", sizeof(buf));
	if (flags & INTF_FLAG_NOARP)
		strlcat(buf, ",NOARP", sizeof(buf));
	if (flags & INTF_FLAG_MULTICAST)
		strlcat(buf, ",MCAST", sizeof(buf));
	
	if (buf[0] != '\0')
		return (buf + 1);

	return (buf);
}

static int
print_intf(char *device, struct addr *addr, int flags, void *arg)
{
	printf("%s: flags=%x<%s>\n", device, flags, flags2string(flags));

	if (addr != NULL) {
		if (addr->addr_type == ADDR_TYPE_IP)
			printf("\tinet %s\n", addr_ntoa(addr));
		else if (addr->addr_type == ADDR_TYPE_ETH)
			printf("\teth %s\n", addr_ntoa(addr));
	}
	return (0);
}

static int
lookup_intf(char *device, struct addr *addr, int flags, void *arg)
{
	struct addr ea;
	
	ea.addr_type = ADDR_TYPE_ETH;
	
	if (intf_get(intf, device, &ea, NULL) == 0) {
		print_intf(device, &ea, flags, NULL);
		return (1);
	}
	return (0);
}

int
main(int argc, char *argv[])
{
	struct addr addr;
	char *cmd, *device;
	int i, flags;

	if (argc < 2)
		usage();

	cmd = argv[1];

	if ((intf = intf_open()) == NULL)
		err(1, "intf_open");

	if (strcmp(cmd, "show") == 0) {
		if (intf_loop(intf, print_intf, NULL) < 0)
			err(1, "intf_loop");
	} else if (strcmp(cmd, "any") == 0) {
		if (intf_loop(intf, lookup_intf, &device) < 0)
			err(1, "intf_loop");
	} else if (strcmp(cmd, "get") == 0) {
		device = argv[2];
		    
		if (argc > 3) {
			if (strcmp(argv[3], "ip") == 0)
				addr.addr_type = ADDR_TYPE_IP;
			else if (strcmp(argv[3], "eth") == 0)
				addr.addr_type = ADDR_TYPE_ETH;
			else
				usage();
		} else
			addr.addr_type = ADDR_TYPE_IP;
		
		if (intf_get(intf, device, &addr, &flags) < 0) {
			if (errno == EADDRNOTAVAIL)
				print_intf(device, NULL, flags, NULL);
			else
				err(1, "intf_get");
		} else
			print_intf(device, &addr, flags, NULL);
	} else if (strcmp(cmd, "set") == 0) {
		device = argv[2];
		
		if (intf_get(intf, device, NULL, &flags) < 0)
			err(1, "intf_get");

		if (addr_pton(argv[3], &addr) < 0)
			err(1, "addr_pton");

		for (i = 4; i < argc; i++) {
			if (strcmp(argv[i], "up") == 0)
				flags |= INTF_FLAG_UP;
			else if (strcmp(argv[i], "down") == 0)
				flags &= ~INTF_FLAG_UP;
			else if (strcmp(argv[i], "arp") == 0)
				flags &= ~INTF_FLAG_NOARP;
			else if (strcmp(argv[i], "noarp") == 0)
				flags |= INTF_FLAG_NOARP;
		}
		if (intf_set(intf, device, &addr, &flags) < 0)
			err(1, "intf_set");
	} else
		usage();
	
	intf_close(intf);

	exit(0);
}<commit_msg>update to new intf interface<commit_after>/*
 * intf.c
 *
 * Copyright (c) 2001 <NAME> <<EMAIL>>
 *
 * $Id$
 */

#include "config.h"

#include <sys/types.h>

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "dnet.h"

static intf_t	*intf;

static void
usage(void)
{
	fprintf(stderr, "Usage: intf show\n"
			"Usage: intf get device\n"
			"Usage: intf set device addr "
	    "[[up|down|arp|noarp] ...]\n");
	exit(1);
}

static char *
flags2string(u_int flags)
{
	static char buf[256];

	buf[0] = '\0';
	
	if (flags & INTF_FLAG_UP)
		strlcat(buf, ",UP", sizeof(buf));
	if (flags & INTF_FLAG_LOOPBACK)
		strlcat(buf, ",LOOP", sizeof(buf));
	if (flags & INTF_FLAG_POINTOPOINT)
		strlcat(buf, ",P2P", sizeof(buf));
	if (flags & INTF_FLAG_NOARP)
		strlcat(buf, ",NOARP", sizeof(buf));
	if (flags & INTF_FLAG_MULTICAST)
		strlcat(buf, ",MCAST", sizeof(buf));
	
	if (buf[0] != '\0')
		return (buf + 1);

	return (buf);
}

static int
print_intf(char *device, struct intf_info *info, void *arg)
{
	struct addr bcast;
	u_int32_t mask;
	
	printf("%s:", device);
	
	if ((info->intf_info & INTF_INFO_FLAGS) != 0)
		printf(" flags=%x<%s>", info->intf_flags,
		    flags2string(info->intf_flags));

	if ((info->intf_info & INTF_INFO_MTU) != 0)
		printf(" mtu %d", info->intf_mtu);

	printf("\n");

	if ((info->intf_info & INTF_INFO_ADDR) != 0) {
		addr_btom(info->intf_addr.addr_bits, &mask, IP_ADDR_LEN);
		mask = ntohl(mask);
		addr_bcast(&info->intf_addr, &bcast);

		printf("\tinet %s netmask 0x%x broadcast %s\n",
		    addr_ntoa(&info->intf_addr), mask, addr_ntoa(&bcast));
	}
	return (0);
}

int
main(int argc, char *argv[])
{
	struct intf_info info;
	char *cmd, *device;
	int i;

	if (argc < 2)
		usage();

	cmd = argv[1];

	if ((intf = intf_open()) == NULL)
		err(1, "intf_open");

	if (strcmp(cmd, "show") == 0) {
		if (intf_loop(intf, print_intf, NULL) < 0)
			err(1, "intf_loop");
	} else if (strcmp(cmd, "get") == 0) {
		device = argv[2];

		if (intf_get(intf, device, &info) < 0)
			err(1, "intf_get");

		print_intf(device, &info, NULL);
	} else if (strcmp(cmd, "set") == 0) {
		device = argv[2];

		if (intf_get(intf, device, &info) < 0)
			err(1, "intf_get");

		if (addr_pton(argv[3], &info.intf_addr) < 0)
			err(1, "addr_pton");
		
		info.intf_info |= INTF_INFO_ADDR;
		
		for (i = 4; i < argc; i++) {
			if (strcmp(argv[i], "up") == 0)
				info.intf_flags |= INTF_FLAG_UP;
			else if (strcmp(argv[i], "down") == 0)
				info.intf_flags &= ~INTF_FLAG_UP;
			else if (strcmp(argv[i], "arp") == 0)
				info.intf_flags &= ~INTF_FLAG_NOARP;
			else if (strcmp(argv[i], "noarp") == 0)
				info.intf_flags |= INTF_FLAG_NOARP;
		}
		if (intf_set(intf, device, &info) < 0)
			err(1, "intf_set");
	} else
		usage();
	
	intf_close(intf);

	exit(0);
}
{'fraction_non_alphanumeric': 0.19819669393888797, 'fraction_numerical': 0.01686425112706629, 'mean_word_length': 3.72397476340694, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3247605', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2558, 'n_words': 600}