]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/iffake.c
network_cmds-606.100.3.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / iffake.c
1 /*
2 * Copyright (c) 2017 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 /*
24 * iffake.c
25 * - manage fake interfaces that pretend to be e.g. ethernet
26 */
27
28 /*
29 * Modification History:
30 *
31 * January 17, 2017 Dieter Siegmund (dieter@apple.com)
32 * - created
33 */
34
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include <net/ethernet.h>
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_fake_var.h>
46
47 #include <net/route.h>
48
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <err.h>
55 #include <errno.h>
56
57 #include "ifconfig.h"
58
59 static void
60 fake_status(int s)
61 {
62 struct ifdrv ifd;
63 struct if_fake_request iffr;
64
65 bzero((char *)&ifd, sizeof(ifd));
66 bzero((char *)&iffr, sizeof(iffr));
67 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
68 ifd.ifd_cmd = IF_FAKE_G_CMD_GET_PEER;
69 ifd.ifd_len = sizeof(iffr);
70 ifd.ifd_data = &iffr;
71 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0) {
72 return;
73 }
74 if (iffr.iffr_peer_name[0] == '\0') {
75 printf("\tpeer: <none>\n");
76 } else {
77 printf("\tpeer: %s\n", iffr.iffr_peer_name);
78 }
79 return;
80 }
81
82 static void
83 set_peer(int s, const char * operation, const char * val)
84 {
85 struct ifdrv ifd;
86 struct if_fake_request iffr;
87
88 bzero((char *)&ifd, sizeof(ifd));
89 bzero((char *)&iffr, sizeof(iffr));
90 strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
91 ifd.ifd_cmd = IF_FAKE_S_CMD_SET_PEER;
92 ifd.ifd_len = sizeof(iffr);
93 ifd.ifd_data = &iffr;
94 if (val != NULL) {
95 strlcpy(iffr.iffr_peer_name, val, sizeof(iffr.iffr_peer_name));
96 }
97 if (ioctl(s, SIOCSDRVSPEC, &ifd) < 0) {
98 err(1, "SIOCDRVSPEC %s peer", operation);
99 }
100 return;
101 }
102
103 static
104 DECL_CMD_FUNC(setpeer, val, d)
105 {
106 set_peer(s, "set", val);
107 return;
108 }
109
110 static
111 DECL_CMD_FUNC(unsetpeer, val, d)
112 {
113 set_peer(s, "unset", NULL);
114 return;
115 }
116
117 static struct cmd fake_cmds[] = {
118 DEF_CLONE_CMD_ARG("peer", setpeer),
119 DEF_CMD_OPTARG("-peer", unsetpeer),
120 };
121 static struct afswtch af_fake = {
122 .af_name = "af_fake",
123 .af_af = AF_UNSPEC,
124 .af_other_status = fake_status,
125 };
126
127 static __constructor void
128 fake_ctor(void)
129 {
130 #define N(a) (sizeof(a) / sizeof(a[0]))
131 int i;
132
133 for (i = 0; i < N(fake_cmds); i++)
134 cmd_register(&fake_cmds[i]);
135 af_register(&af_fake);
136 #undef N
137 }
138