contrib/hostapd/hostapd/vlan_init.c
static int vlan_add(const char *if_name, int vid)
{
	int fd;
	struct vlan_ioctl_args if_request;

	ifconfig_up(if_name);

	if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
		fprintf(stderr, "Interface name to long.\n");
		return -1;
	}

	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket[AF_INET,SOCK_STREAM]");
		return -1;
	}

	os_memset(&if_request, 0, sizeof(if_request));

	/* Determine if a suitable vlan device already exists. */

	os_snprintf(if_request.device1, sizeof(if_request.device1), "vlan%d",
		    vid);

	if_request.cmd = _GET_VLAN_VID_CMD;

	if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0) {

		if (if_request.u.VID == vid) {
			if_request.cmd = _GET_VLAN_REALDEV_NAME_CMD;

			if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0 &&
			    os_strncmp(if_request.u.device2, if_name,
				       sizeof(if_request.u.device2)) == 0) {
				close(fd);
				return 1;
			}
		}
	}

	/* A suitable vlan device does not already exist, add one. */

	os_memset(&if_request, 0, sizeof(if_request));
	os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
	if_request.u.VID = vid;
	if_request.cmd = ADD_VLAN_CMD;

	if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
		perror("ioctl[SIOCSIFVLAN,ADD_VLAN_CMD]");
		close(fd);
		return -1;
	}

	close(fd);
	return 0;
}
/*
	Add a vlan interface with VLAN ID 'vid' and tagged interface
	'if_name'.

	returns -1 on error
	returns 1 if the interface already exists
	returns 0 otherwise
*/
Add a vlan interface with VLAN ID 'vid' and tagged interface
'if_name'.

1 on error
returns 1 if the interface already exists
returns 0 otherwise
Add a vlan interface with VLAN ID ' vid ' and tagged interface ' if_name ' . 1 on error returns 1 if the interface already exists returns 0 otherwise
static int vlan_add(const char *if_name, int vid)
{
	int fd;
	struct vlan_ioctl_args if_request;
	ifconfig_up(if_name);
	if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
		fprintf(stderr, "Interface name to long.\n");
		return -1;
	}
	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket[AF_INET,SOCK_STREAM]");
		return -1;
	}
	os_memset(&if_request, 0, sizeof(if_request));
	os_snprintf(if_request.device1, sizeof(if_request.device1), "vlan%d",
		    vid);
	if_request.cmd = _GET_VLAN_VID_CMD;
	if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0) {
		if (if_request.u.VID == vid) {
			if_request.cmd = _GET_VLAN_REALDEV_NAME_CMD;
			if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0 &&
			    os_strncmp(if_request.u.device2, if_name,
				       sizeof(if_request.u.device2)) == 0) {
				close(fd);
				return 1;
			}
		}
	}
	os_memset(&if_request, 0, sizeof(if_request));
	os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
	if_request.u.VID = vid;
	if_request.cmd = ADD_VLAN_CMD;
	if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
		perror("ioctl[SIOCSIFVLAN,ADD_VLAN_CMD]");
		close(fd);
		return -1;
	}
	close(fd);
	return 0;
}
static int vlan_add ( const char * if_name , int vid ) { int fd ; struct vlan_ioctl_args if_request ; ifconfig_up ( if_name ) ; if ( ( os_strlen ( if_name ) + 1 ) > sizeof ( if_request . device1 ) ) { fprintf ( stderr , " \n " ) ; return -1 ; } if ( ( fd = socket ( AF_INET , SOCK_STREAM , 0 ) ) < 0 ) { perror ( " " ) ; return -1 ; } os_memset ( & if_request , 0 , sizeof ( if_request ) ) ; os_snprintf ( if_request . device1 , sizeof ( if_request . device1 ) , " " , vid ) ; if_request . cmd = _GET_VLAN_VID_CMD ; if ( ioctl ( fd , SIOCSIFVLAN , & if_request ) == 0 ) { if ( if_request . u . VID == vid ) { if_request . cmd = _GET_VLAN_REALDEV_NAME_CMD ; if ( ioctl ( fd , SIOCSIFVLAN , & if_request ) == 0 && os_strncmp ( if_request . u . device2 , if_name , sizeof ( if_request . u . device2 ) ) == 0 ) { close ( fd ) ; return 1 ; } } } os_memset ( & if_request , 0 , sizeof ( if_request ) ) ; os_strlcpy ( if_request . device1 , if_name , sizeof ( if_request . device1 ) ) ; if_request . u . VID = vid ; if_request . cmd = ADD_VLAN_CMD ; if ( ioctl ( fd , SIOCSIFVLAN , & if_request ) < 0 ) { perror ( " " ) ; close ( fd ) ; return -1 ; } close ( fd ) ; return 0 ; }
Add a vlan interface with VLAN ID 'vid' and tagged interface
'if_name'.
Add a vlan interface with VLAN ID ' vid ' and tagged interface ' if_name ' .
/* Determine if a suitable vlan device already exists. */ /* A suitable vlan device does not already exist, add one. */