mirror of
https://github.com/freebsd/freebsd-src
synced 2024-10-15 12:54:27 +00:00
ifconfig: start ifconfig context from main()
Differential Revision: https://reviews.freebsd.org/D40440 MFC after: 2 weeks
This commit is contained in:
parent
d61064229f
commit
bbad5525fa
|
@ -111,8 +111,8 @@ static char ifname_to_print[IFNAMSIZ]; /* Helper for printifnamemaybe() */
|
||||||
char *f_inet, *f_inet6, *f_ether, *f_addr;
|
char *f_inet, *f_inet6, *f_ether, *f_addr;
|
||||||
|
|
||||||
#ifdef WITHOUT_NETLINK
|
#ifdef WITHOUT_NETLINK
|
||||||
static void list_interfaces_ioctl(struct ifconfig_args *args);
|
static void list_interfaces_ioctl(if_ctx *ctx);
|
||||||
static void status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
|
static void status(if_ctx *ctx, const struct sockaddr_dl *sdl,
|
||||||
struct ifaddrs *ifa);
|
struct ifaddrs *ifa);
|
||||||
#endif
|
#endif
|
||||||
static _Noreturn void usage(void);
|
static _Noreturn void usage(void);
|
||||||
|
@ -422,12 +422,12 @@ printifnamemaybe(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
list_interfaces(struct ifconfig_args *args)
|
list_interfaces(if_ctx *ctx)
|
||||||
{
|
{
|
||||||
#ifdef WITHOUT_NETLINK
|
#ifdef WITHOUT_NETLINK
|
||||||
list_interfaces_ioctl(args);
|
list_interfaces_ioctl(ctx);
|
||||||
#else
|
#else
|
||||||
list_interfaces_nl(args);
|
list_interfaces_nl(ctx->args);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -571,19 +571,12 @@ args_parse(struct ifconfig_args *args, int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ifconfig_wrapper(struct ifconfig_args *args, int iscreate,
|
ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp)
|
||||||
const struct afswtch *uafp)
|
|
||||||
{
|
{
|
||||||
struct ifconfig_context ctx = {
|
|
||||||
.args = args,
|
|
||||||
.io_s = -1,
|
|
||||||
.ifname = args->ifname,
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef WITHOUT_NETLINK
|
#ifdef WITHOUT_NETLINK
|
||||||
return (ifconfig(&ctx, iscreate, uafp));
|
return (ifconfig_ioctl(ctx, iscreate, uafp));
|
||||||
#else
|
#else
|
||||||
return (ifconfig_wrapper_nl(&ctx, iscreate, uafp));
|
return (ifconfig_nl(ctx, iscreate, uafp));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -616,6 +609,11 @@ main(int ac, char *av[])
|
||||||
struct ifconfig_args _args = {};
|
struct ifconfig_args _args = {};
|
||||||
struct ifconfig_args *args = &_args;
|
struct ifconfig_args *args = &_args;
|
||||||
|
|
||||||
|
struct ifconfig_context ctx = {
|
||||||
|
.args = args,
|
||||||
|
.io_s = -1,
|
||||||
|
};
|
||||||
|
|
||||||
f_inet = f_inet6 = f_ether = f_addr = NULL;
|
f_inet = f_inet6 = f_ether = f_addr = NULL;
|
||||||
|
|
||||||
lifh = ifconfig_open();
|
lifh = ifconfig_open();
|
||||||
|
@ -647,6 +645,7 @@ main(int ac, char *av[])
|
||||||
if (!args->all && !args->namesonly) {
|
if (!args->all && !args->namesonly) {
|
||||||
/* not listing, need an argument */
|
/* not listing, need an argument */
|
||||||
args->ifname = args_pop(args);
|
args->ifname = args_pop(args);
|
||||||
|
ctx.ifname = args->ifname;
|
||||||
|
|
||||||
/* check and maybe load support for this interface */
|
/* check and maybe load support for this interface */
|
||||||
ifmaybeload(args, args->ifname);
|
ifmaybeload(args, args->ifname);
|
||||||
|
@ -662,7 +661,7 @@ main(int ac, char *av[])
|
||||||
if (isnametoolong(args->ifname))
|
if (isnametoolong(args->ifname))
|
||||||
errx(1, "%s: cloning name too long",
|
errx(1, "%s: cloning name too long",
|
||||||
args->ifname);
|
args->ifname);
|
||||||
ifconfig_wrapper(args, 1, NULL);
|
ifconfig(&ctx, 1, NULL);
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
#ifdef JAIL
|
#ifdef JAIL
|
||||||
|
@ -675,7 +674,7 @@ main(int ac, char *av[])
|
||||||
if (isnametoolong(args->ifname))
|
if (isnametoolong(args->ifname))
|
||||||
errx(1, "%s: interface name too long",
|
errx(1, "%s: interface name too long",
|
||||||
args->ifname);
|
args->ifname);
|
||||||
ifconfig_wrapper(args, 0, NULL);
|
ifconfig(&ctx, 0, NULL);
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -714,14 +713,14 @@ main(int ac, char *av[])
|
||||||
if (!(((flags & IFF_CANTCONFIG) != 0) ||
|
if (!(((flags & IFF_CANTCONFIG) != 0) ||
|
||||||
(args->downonly && (flags & IFF_UP) != 0) ||
|
(args->downonly && (flags & IFF_UP) != 0) ||
|
||||||
(args->uponly && (flags & IFF_UP) == 0)))
|
(args->uponly && (flags & IFF_UP) == 0)))
|
||||||
ifconfig_wrapper(args, 0, args->afp);
|
ifconfig(&ctx, 0, args->afp);
|
||||||
}
|
}
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
args->allfamilies = args->afp == NULL;
|
args->allfamilies = args->afp == NULL;
|
||||||
|
|
||||||
list_interfaces(args);
|
list_interfaces(&ctx);
|
||||||
|
|
||||||
done:
|
done:
|
||||||
freeformat();
|
freeformat();
|
||||||
|
@ -771,13 +770,14 @@ match_afp(const struct afswtch *afp, int sa_family, const struct sockaddr_dl *sd
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
list_interfaces_ioctl(struct ifconfig_args *args)
|
list_interfaces_ioctl(if_ctx *ctx)
|
||||||
{
|
{
|
||||||
struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
|
struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
|
||||||
struct ifaddrs *ifap, *sifap, *ifa;
|
struct ifaddrs *ifap, *sifap, *ifa;
|
||||||
struct ifa_order_elt *cur, *tmp;
|
struct ifa_order_elt *cur, *tmp;
|
||||||
char *namecp = NULL;
|
char *namecp = NULL;
|
||||||
int ifindex;
|
int ifindex;
|
||||||
|
struct ifconfig_args *args = ctx->args;
|
||||||
|
|
||||||
if (getifaddrs(&ifap) != 0)
|
if (getifaddrs(&ifap) != 0)
|
||||||
err(EXIT_FAILURE, "getifaddrs");
|
err(EXIT_FAILURE, "getifaddrs");
|
||||||
|
@ -840,9 +840,9 @@ list_interfaces_ioctl(struct ifconfig_args *args)
|
||||||
ifindex++;
|
ifindex++;
|
||||||
|
|
||||||
if (args->argc > 0)
|
if (args->argc > 0)
|
||||||
ifconfig_wrapper(args, 0, args->afp);
|
ifconfig(ctx, 0, args->afp);
|
||||||
else
|
else
|
||||||
status(args, sdl, ifa);
|
status(ctx, sdl, ifa);
|
||||||
}
|
}
|
||||||
if (args->namesonly)
|
if (args->namesonly)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
@ -1089,7 +1089,7 @@ addifaddr(if_ctx *ctx, const struct afswtch *afp)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
ifconfig(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp)
|
ifconfig_ioctl(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp)
|
||||||
{
|
{
|
||||||
const struct afswtch *afp, *nafp;
|
const struct afswtch *afp, *nafp;
|
||||||
const struct cmd *p;
|
const struct cmd *p;
|
||||||
|
@ -1717,11 +1717,12 @@ print_description(int s)
|
||||||
* specified, show only it; otherwise, show them all.
|
* specified, show only it; otherwise, show them all.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
|
status(if_ctx *ctx, const struct sockaddr_dl *sdl,
|
||||||
struct ifaddrs *ifa)
|
struct ifaddrs *ifa)
|
||||||
{
|
{
|
||||||
struct ifaddrs *ift;
|
struct ifaddrs *ift;
|
||||||
int s;
|
int s, old_s;
|
||||||
|
struct ifconfig_args *args = ctx->args;
|
||||||
bool allfamilies = args->afp == NULL;
|
bool allfamilies = args->afp == NULL;
|
||||||
char *ifname = ifa->ifa_name;
|
char *ifname = ifa->ifa_name;
|
||||||
|
|
||||||
|
@ -1735,12 +1736,8 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
|
||||||
s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
|
s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
|
||||||
if (s < 0)
|
if (s < 0)
|
||||||
err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
|
err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
|
||||||
|
old_s = ctx->io_s;
|
||||||
struct ifconfig_context _ctx = {
|
ctx->io_s = s;
|
||||||
.io_s = s,
|
|
||||||
.ifname = ifname,
|
|
||||||
};
|
|
||||||
struct ifconfig_context *ctx = &_ctx;
|
|
||||||
|
|
||||||
printf("%s: ", ifname);
|
printf("%s: ", ifname);
|
||||||
printb("flags", ifa->ifa_flags, IFFBITS);
|
printb("flags", ifa->ifa_flags, IFFBITS);
|
||||||
|
@ -1794,6 +1791,7 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
|
||||||
sfp_status(ctx);
|
sfp_status(ctx);
|
||||||
|
|
||||||
close(s);
|
close(s);
|
||||||
|
ctx->io_s = old_s;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -274,7 +274,7 @@ void sfp_status(if_ctx *ctx);
|
||||||
struct sockaddr_dl;
|
struct sockaddr_dl;
|
||||||
bool match_ether(const struct sockaddr_dl *sdl);
|
bool match_ether(const struct sockaddr_dl *sdl);
|
||||||
bool match_if_flags(struct ifconfig_args *args, int if_flags);
|
bool match_if_flags(struct ifconfig_args *args, int if_flags);
|
||||||
int ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp);
|
int ifconfig_ioctl(if_ctx *ctx, int iscreate, const struct afswtch *uafp);
|
||||||
bool group_member(const char *ifname, const char *match, const char *nomatch);
|
bool group_member(const char *ifname, const char *match, const char *nomatch);
|
||||||
void print_ifcap(struct ifconfig_args *args, int s);
|
void print_ifcap(struct ifconfig_args *args, int s);
|
||||||
void tunnel_status(if_ctx *ctx);
|
void tunnel_status(if_ctx *ctx);
|
||||||
|
@ -285,7 +285,7 @@ void print_metric(int s);
|
||||||
|
|
||||||
/* Netlink-related functions */
|
/* Netlink-related functions */
|
||||||
void list_interfaces_nl(struct ifconfig_args *args);
|
void list_interfaces_nl(struct ifconfig_args *args);
|
||||||
int ifconfig_wrapper_nl(if_ctx *ctx, int iscreate,
|
int ifconfig_nl(if_ctx *ctx, int iscreate,
|
||||||
const struct afswtch *uafp);
|
const struct afswtch *uafp);
|
||||||
uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname);
|
uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname);
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ nl_init_socket(struct snl_state *ss)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
ifconfig_wrapper_nl(if_ctx *ctx, int iscreate,
|
ifconfig_nl(if_ctx *ctx, int iscreate,
|
||||||
const struct afswtch *uafp)
|
const struct afswtch *uafp)
|
||||||
{
|
{
|
||||||
struct snl_state ss = {};
|
struct snl_state ss = {};
|
||||||
|
@ -131,9 +131,10 @@ ifconfig_wrapper_nl(if_ctx *ctx, int iscreate,
|
||||||
nl_init_socket(&ss);
|
nl_init_socket(&ss);
|
||||||
ctx->io_ss = &ss;
|
ctx->io_ss = &ss;
|
||||||
|
|
||||||
int error = ifconfig(ctx, iscreate, uafp);
|
int error = ifconfig_ioctl(ctx, iscreate, uafp);
|
||||||
|
|
||||||
snl_free(&ss);
|
snl_free(&ss);
|
||||||
|
ctx->io_ss = NULL;
|
||||||
|
|
||||||
return (error);
|
return (error);
|
||||||
}
|
}
|
||||||
|
@ -447,7 +448,7 @@ list_interfaces_nl(struct ifconfig_args *args)
|
||||||
} else if (args->argc == 0)
|
} else if (args->argc == 0)
|
||||||
status_nl(ctx, iface);
|
status_nl(ctx, iface);
|
||||||
else
|
else
|
||||||
ifconfig(ctx, 0, args->afp);
|
ifconfig_ioctl(ctx, 0, args->afp);
|
||||||
}
|
}
|
||||||
if (args->namesonly)
|
if (args->namesonly)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
Loading…
Reference in a new issue