File: ni_get_set.c

package info (click to toggle)
libnet-interface-perl 1.016-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,252 kB
  • ctags: 478
  • sloc: ansic: 3,397; sh: 2,736; perl: 888; makefile: 3
file content (153 lines) | stat: -rw-r--r-- 4,377 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* ********************************************************************	*
 *	ni_get_set.c	version 0.01	2-8-09				*
 *									*
 *     COPYRIGHT 2009 Michael Robinton <michael@bizsystems.com>		*
 *									*
 * This program is free software; you can redistribute it and/or modify	*
 * it under the terms of either:					*
 *									*
 *  a) the GNU General Public License as published by the Free		*
 *  Software Foundation; either version 2, or (at your option) any	*
 *  later version, or							*
 *									*
 *  b) the "Artistic License" which comes with this distribution.	*
 *									*
 * This program is distributed in the hope that it will be useful,	*
 * but WITHOUT ANY WARRANTY; without even the implied warranty of	*
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either	*
 * the GNU General Public License or the Artistic License for more 	*
 * details.								*
 *									*
 * You should have received a copy of the Artistic License with this	*
 * distribution, in the file named "Artistic".  If not, I'll be glad 	*
 * to provide one.							*
 *									*
 * You should also have received a copy of the GNU General Public 	*
 * License along with this program in the file named "Copying". If not, *
 * write to the 							*
 *									*
 *	Free Software Foundation, Inc.					*
 *	59 Temple Place, Suite 330					*
 *	Boston, MA  02111-1307, USA					*
 *									*
 * or visit their web page on the internet at:				*
 *									*
 *	http://www.gnu.org/copyleft/gpl.html.				*
 * ********************************************************************	*/


#include "localconf.h"

/* ****************************************************	*
 *	standard SIOCget functions			*
 *							*
 *	execute supported ioctl's			*
 *	where a value is requested, return it		*
 *	otherwise it is up to the user to retrieve	*
 *	information via their pointers			*
 *	returns -1 on error, sets errno			*
 * ****************************************************	*/

int32_t
ni_get_any(int fd, int cmd, void * ifr)
{
    switch (cmd) {
    case SIOCGIFMTU :
    case SIOCGIFMETRIC :
    case SIOCGIFFLAGS :
#ifdef SIOCGIFINDEX
    case SIOCGIFINDEX :
#endif
    case SIOCGIFADDR :
    case SIOCGIFNETMASK :
    case SIOCGIFBRDADDR :
    case SIOCGIFDSTADDR :
	break;
    default :
        errno = ENOSYS;
        return -1;
    }
    if (ioctl(fd,cmd,ifr) < 0)
	return -1;

    switch (cmd) {
    case SIOCGIFFLAGS :
    case SIOCGIFMETRIC :
    case SIOCGIFMTU :
#ifdef SIOCGIFINDEX
    case SIOCGIFINDEX :
#endif
	return ((struct nifreq *)ifr)->ni_int;
    }
    return 0;
}

/* ****************************************************	*
 *	standard SIOCset functions			*
 *							*
 *	execute supported ioctl's			*
 *	returns -1 on error, sets errno			*
 *							*
 *	struct nifreq ifr should be prepared, for	*
 *	add/modify of a Linux  ipV6 address, the 	*
 *	PREFIX/CIDR value should be stored at:		*
 *							*
 *		ifr->ni_sin6.sin6_port			*
 *							*
 * ****************************************************	*/

#if defined (__ni_Linux) && defined (LOCAL_SIZEOF_SOCKADDR_IN6)
struct lin6_ifreq {
    struct in6_addr	lin6_addr;
    u_int32_t		lin6_prfx;
    u_int		lin6_indx;
};
# endif


/*	This routine must have ALL possible set commands
 *	Remember that ifr may need offset as is the case with LIFREQ
 *	Use with care. See $if->flags in Interface.xs
 */
 
int
ni_set_any(int fd, int cmd, struct nifreq * ifr)
{
#if defined (__ni_Linux) && defined (LOCAL_SIZEOF_SOCKADDR_IN6)
    struct lin6_ifreq ifr6;
#endif

    switch (cmd) {
    case SIOCSIFFLAGS :
#ifdef SIOCSLIFFLAGS
    case SIOCSLIFFLAGS :
#endif
    case SIOCSIFMETRIC :
    case SIOCSIFMTU :
	break;
    case SIOCSIFADDR :
    case SIOCSIFDSTADDR :
    case SIOCSIFBRDADDR :
    case SIOCSIFNETMASK :
#ifdef SIOCDIFADDR
    case SIOCDIFADDR :
#endif
#if defined (__ni_Linux) && defined (LOCAL_SIZEOF_SOCKADDR_IN6)
	if (ifr->ni_saddr.sa_family == AF_INET6) {
	    memcpy(&ifr6.lin6_addr,&ifr->ni_sin6.sin6_addr,LOCAL_SIZEOF_SOCKADDR_IN6);
	    ifr6.lin6_prfx = ifr->ni_sin6.sin6_port;	/* temporarily stored here	*/
	    if (ioctl(fd,SIOCGIFINDEX,&ifr) < 0)
		return -1;
	    ifr6.lin6_indx = ifr->ni_ushort;
	    ifr = (struct nifreq *)&ifr6;		/* lie about cast	*/
	}
#endif
	break;
    default :
        errno = ENOSYS;
        return -1;
    }
    if (ioctl(fd,cmd,ifr) < 0)
	return -1;
    return 0;
}