Esempio n. 1
0
int
main(int argc, char *argv[])
{
	char *ifname, *ptr;
	int mtu;
	ifconfig_handle_t *lifh;

	if (argc != 3) {
		errx(EINVAL, "Invalid number of arguments."
		    " First argument should be interface name, second argument"
		    " should be the MTU to set.");
	}

	/* We have a static number of arguments. Therefore we can do it simple. */
	ifname = strdup(argv[1]);
	mtu = (int)strtol(argv[2], &ptr, 10);

	printf("Interface name: %s\n", ifname);
	printf("New MTU: %d", mtu);

	lifh = ifconfig_open();
	if (lifh == NULL) {
		errx("Failed to open libifconfig handle.");
		return (-1);
	}

	if (ifconfig_set_mtu(lifh, ifname, mtu) == 0) {
		printf("Successfully changed MTU of %s to %d\n", ifname, mtu);
		ifconfig_close(lifh);
		lifh = NULL;
		free(ifname);
		return (0);
	}

	switch (ifconfig_err_errtype(lifh)) {
	case SOCKET:
		warnx("couldn't create socket. This shouldn't happen.\n");
		break;
	case IOCTL:
		if (ifconfig_err_ioctlreq(lifh) == SIOCSIFMTU) {
			warnx("Failed to set MTU (SIOCSIFMTU)\n");
		} else {
			warnx(
				"Failed to set MTU due to error in unexpected ioctl() call %lu. Error code: %i.\n",
				ifconfig_err_ioctlreq(lifh),
				ifconfig_err_errno(lifh));
		}
		break;
	default:
		warnx(
			"Should basically never end up here in this example.\n");
		break;
	}

	ifconfig_close(lifh);
	lifh = NULL;
	free(ifname);
	return (-1);
}
Esempio n. 2
0
int
main(int argc, char *argv[])
{
	char *ifname, *ifactualname;
	ifconfig_handle_t *lifh;

	if (argc != 2) {
		errx(EINVAL, "Invalid number of arguments."
		    " Only one argument is accepted, and it should be the name"
		    " of the interface to be created.");
	}

	/* We have a static number of arguments. Therefore we can do it simple. */
	ifname = strdup(argv[1]);

	printf("Requested interface name: %s\n", ifname);

	lifh = ifconfig_open();
	if (lifh == NULL) {
		errx("Failed to open libifconfig handle.");
		return (-1);
	}

	if (ifconfig_create_interface(lifh, ifname, &ifactualname) == 0) {
		printf("Successfully created interface '%s'\n", ifactualname);
		ifconfig_close(lifh);
		lifh = NULL;
		free(ifname);
		free(ifactualname);
		return (0);
	}

	switch (ifconfig_err_errtype(lifh)) {
	case SOCKET:
		warnx("couldn't create socket. This shouldn't happen.\n");
		break;
	case IOCTL:
		if (ifconfig_err_ioctlreq(lifh) == SIOCIFCREATE2) {
			warnx(
				"Failed to create interface (SIOCIFCREATE2)\n");
		}
		break;
	default:
		warnx(
			"This is a thorough example accommodating for temporary"
			" 'not implemented yet' errors. That's likely what happened"
			" now. If not, your guess is as good as mine. ;)"
			" Error code: %d\n", ifconfig_err_errno(
				lifh));
		break;
	}

	ifconfig_close(lifh);
	lifh = NULL;
	free(ifname);
	free(ifactualname);
	return (-1);
}
Esempio n. 3
0
int
main(int argc, char *argv[])
{
	char *ifname;

	if (argc != 2) {
		errx(EINVAL, "Invalid number of arguments."
		    " Only one argument is accepted, and it should be the name"
		    " of the interface to be destroyed.");
	}

	/* We have a static number of arguments. Therefore we can do it simple. */
	ifname = strdup(argv[1]);

	printf("Interface name: %s\n", ifname);

	ifconfig_handle_t *lifh = ifconfig_open();
	if (ifconfig_destroy_interface(lifh, ifname) == 0) {
		printf("Successfully destroyed interface '%s'.", ifname);
		ifconfig_close(lifh);
		lifh = NULL;
		free(ifname);
		return (0);
	}

	switch (ifconfig_err_errtype(lifh)) {
	case SOCKET:
		warnx("couldn't create socket. This shouldn't happen.\n");
		break;
	case IOCTL:
		if (ifconfig_err_ioctlreq(lifh) == SIOCIFDESTROY) {
			warnx(
				"Failed to destroy interface (SIOCIFDESTROY)\n");
		}
		break;
	default:
		warnx(
			"Should basically never end up here in this example.\n");
		break;
	}

	ifconfig_close(lifh);
	lifh = NULL;
	free(ifname);
	return (-1);
}