Add "iface name" and "iface description" commands.

PR:		151400
Submitted by:	Aragon Gouveia - aragon at phat dot za dot net with minor fixes
MFC after:	3 weeks
This commit is contained in:
Brian Somers 2011-02-07 11:18:18 +00:00
parent f8140f7291
commit f2f5156f21
8 changed files with 177 additions and 24 deletions

View File

@ -758,7 +758,7 @@ bundle_Create(const char *prefix, int type, int unit)
return NULL;
}
log_SetTun(bundle.unit);
log_SetTun(bundle.unit, NULL);
ifname = strrchr(bundle.dev.Name, '/');
if (ifname == NULL)
@ -849,7 +849,7 @@ bundle_Create(const char *prefix, int type, int unit)
bundle.links = datalink_Create("deflink", &bundle, type);
if (bundle.links == NULL) {
log_Printf(LogALERT, "Cannot create data link: %s\n", strerror(errno));
iface_Destroy(bundle.iface);
iface_Free(bundle.iface);
bundle.iface = NULL;
close(bundle.dev.fd);
return NULL;

View File

@ -184,6 +184,7 @@ static int DeleteCommand(struct cmdargs const *);
static int NegotiateCommand(struct cmdargs const *);
static int ClearCommand(struct cmdargs const *);
static int RunListCommand(struct cmdargs const *);
static int IfaceNameCommand(struct cmdargs const *arg);
static int IfaceAddCommand(struct cmdargs const *);
static int IfaceDeleteCommand(struct cmdargs const *);
static int IfaceClearCommand(struct cmdargs const *);
@ -823,6 +824,10 @@ static struct cmdtab const IfaceCommands[] =
"Delete iface address", "iface delete addr", (void *)1},
{NULL, "delete!", IfaceDeleteCommand, LOCAL_AUTH,
"Delete iface address", "iface delete addr", (void *)1},
{"name", NULL, IfaceNameCommand, LOCAL_AUTH,
"Set iface name", "iface name name", NULL},
{"description", NULL, iface_Descr, LOCAL_AUTH,
"Set iface description", "iface description text", NULL},
{"show", NULL, iface_Show, LOCAL_AUTH,
"Show iface address(es)", "iface show", NULL},
{"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
@ -3175,6 +3180,21 @@ RunListCommand(struct cmdargs const *arg)
return 0;
}
static int
IfaceNameCommand(struct cmdargs const *arg)
{
int n = arg->argn;
if (arg->argc != n + 1)
return -1;
if (!iface_Name(arg->bundle->iface, arg->argv[n]))
return 1;
log_SetTun(arg->bundle->unit, arg->bundle->iface->name);
return 0;
}
static int
IfaceAddCommand(struct cmdargs const *arg)
{

View File

@ -151,6 +151,7 @@ iface_Create(const char *name)
return NULL;
}
iface->name = strdup(name);
iface->descr = NULL;
iface->index = ifm->ifm_index;
iface->flags = ifm->ifm_flags;
iface->mtu = 0;
@ -369,6 +370,103 @@ iface_addr_Add(const char *name, struct iface_addr *addr, int s)
return res != -1;
}
int
iface_Name(struct iface *iface, const char *name)
{
struct ifreq ifr;
int s;
char *newname;
if ((newname = strdup(name)) == NULL) {
log_Printf(LogWARN, "iface name: strdup failed: %s\n", strerror(errno));
return 0;
}
if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
log_Printf(LogERROR, "iface name: socket(): %s\n", strerror(errno));
free(newname);
return 0;
}
strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
ifr.ifr_data = newname;
if (ID0ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
log_Printf(LogWARN, "iface name: ioctl(SIOCSIFNAME, %s -> %s): %s\n",
name, newname, strerror(errno));
free(newname);
return 0;
}
free(iface->name);
iface->name = newname;
return 1;
}
int
iface_Descr(struct cmdargs const *arg)
{
struct ifreq ifr;
struct iface *iface;
size_t sz, len;
int s, n, ifdescr_maxlen;
char *descr;
sz = sizeof(int);
if (sysctlbyname("net.ifdescr_maxlen", &ifdescr_maxlen, &sz, NULL, 0) < 0) {
log_Printf(LogERROR, "iface descr: sysctl failed: %s\n", strerror(errno));
return 1;
}
if (ifdescr_maxlen < 1) {
log_Printf(LogERROR, "iface descr: sysctl net.ifdescr_maxlen < 1\n");
return 1;
}
sz = sizeof(char) * ifdescr_maxlen;
if ((descr = malloc(sz)) == NULL) {
log_Printf(LogERROR, "iface descr: malloc failed: %s\n", strerror(errno));
return 1;
}
*descr = '\0';
n = arg->argn;
while (n < arg->argc) {
if (n > arg->argn && (len = strlcat(descr, " ", sz)) >= sz)
break;
if ((len = strlcat(descr, arg->argv[n], sz)) >= sz)
break;
++n;
}
if (len >= sz) {
log_Printf(LogERROR, "iface descr: description exceeds maximum (%d)\n",
ifdescr_maxlen-1);
free(descr);
return 1;
}
if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
log_Printf(LogERROR, "iface descr: socket(): %s\n", strerror(errno));
free(descr);
return 1;
}
iface = arg->bundle->iface;
strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
ifr.ifr_buffer.length = strlen(descr) + 1;
ifr.ifr_buffer.buffer = descr;
if (ID0ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
log_Printf(LogWARN, "iface descr: ioctl(SIOCSIFDESCR, %s): %s\n",
descr, strerror(errno));
free(descr);
return 1;
}
free(iface->descr);
iface->descr = descr;
return 0;
}
void
iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
@ -608,18 +706,30 @@ iface_ClearFlags(const char *ifname, int flags)
}
void
iface_Destroy(struct iface *iface)
iface_Free(struct iface *iface)
{
/*
* iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually
* if that's what the user wants. It's better to leave the interface
* allocated so that existing connections can continue to work.
*/
if (iface != NULL) {
free(iface->name);
free(iface->descr);
free(iface->addr);
free(iface);
}
void
iface_Destroy(struct iface *iface)
{
struct ifreq ifr;
int s;
if (iface != NULL) {
if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
log_Printf(LogERROR, "iface_Destroy: socket(): %s\n", strerror(errno));
} else {
strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
if (ID0ioctl(s, SIOCIFDESTROY, (caddr_t)&ifr) < 0)
log_Printf(LogWARN, "iface_Destroy: ioctl(SIOCIFDESTROY, %s): %s\n",
iface->name, strerror(errno));
}
iface_Free(iface);
}
}
@ -661,7 +771,7 @@ iface_Show(struct cmdargs const *arg)
current = iface_Create(iface->name);
flags = iface->flags = current->flags;
iface_Destroy(current);
iface_Free(current);
prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)

View File

@ -36,6 +36,7 @@ struct iface_addr {
struct iface {
char *name; /* Interface name (malloc'd) */
char *descr; /* Interface description (malloc'd) */
int index; /* Interface index */
int flags; /* Interface flags (IFF_*) */
unsigned long mtu; /* struct tuninfo MTU */
@ -55,11 +56,14 @@ struct iface {
extern struct iface *iface_Create(const char *name);
extern void iface_Clear(struct iface *, struct ncp *, int, int);
extern int iface_Name(struct iface *, const char *);
extern int iface_Descr(struct cmdargs const *);
extern int iface_Add(struct iface *, struct ncp *, const struct ncprange *,
const struct ncpaddr *, int);
extern int iface_Delete(struct iface *, struct ncp *, const struct ncpaddr *);
extern int iface_Show(struct cmdargs const *);
extern int iface_SetFlags(const char *, int);
extern int iface_ClearFlags(const char *, int);
extern void iface_Free(struct iface *);
extern void iface_Destroy(struct iface *);
extern void iface_ParseHdr(struct ifa_msghdr *, struct sockaddr *[RTAX_MAX]);

View File

@ -75,6 +75,7 @@ static const char *const LogNames[] = {
static u_long LogMask = MSK(LogPHASE);
static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
static int LogTunno = -1;
static const char *LogIfaceName;
static struct prompt *promptlist; /* Where to log local stuff */
struct prompt *log_PromptContext;
int log_PromptListChanged;
@ -296,9 +297,10 @@ log_Open(const char *Name)
}
void
log_SetTun(int tunno)
log_SetTun(int tunno, const char *ifaceName)
{
LogTunno = tunno;
LogIfaceName = ifaceName;
}
void
@ -306,6 +308,7 @@ log_Close()
{
closelog();
LogTunno = -1;
LogIfaceName = NULL;
}
void
@ -319,10 +322,14 @@ log_Printf(int lev, const char *fmt,...)
va_start(ap, fmt);
if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1)
snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1) {
if (LogIfaceName)
snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
LogTunno, LogIfaceName, log_Name(lev), fmt);
else
snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
LogTunno, log_Name(lev), fmt);
else
} else
snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
if (log_PromptContext && lev == LogWARN)
@ -337,10 +344,14 @@ log_Printf(int lev, const char *fmt,...)
va_start(ap, fmt);
if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
(lev != LogWARN || !log_PromptContext)) {
if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1)
snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1) {
if (LogIfaceName)
snprintf(nfmt, sizeof nfmt, "%s%d(%s): %s: %s", TUN_NAME,
LogTunno, LogIfaceName, log_Name(lev), fmt);
else
snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
LogTunno, log_Name(lev), fmt);
else
} else
snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
vsyslog(syslogLevel(lev), nfmt, ap);
}

View File

@ -76,7 +76,7 @@ extern void log_DiscardAllLocal(u_long *);
extern int log_IsKept(int);
extern int log_IsKeptLocal(int, u_long);
extern void log_Open(const char *);
extern void log_SetTun(int);
extern void log_SetTun(int, const char *);
extern void log_Close(void);
#ifdef __GNUC__
extern void log_Printf(int, const char *,...)

View File

@ -386,11 +386,6 @@ main(int argc, char **argv)
/* NOTE: We may now have changed argv[1] via a ``set proctitle'' */
if (prompt) {
prompt->bundle = bundle; /* couldn't do it earlier */
if (!sw.quiet)
prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
}
SignalBundle = bundle;
bundle->NatEnabled = sw.nat;
if (sw.nat)
@ -430,6 +425,12 @@ main(int argc, char **argv)
AbortProgram(EX_START);
}
if (prompt) {
prompt->bundle = bundle; /* couldn't do it earlier */
if (!sw.quiet)
prompt_Printf(prompt, "Using interface: %s\n", bundle->iface->name);
}
if (sw.mode != PHYS_INTERACTIVE) {
if (sw.mode != PHYS_DIRECT) {
if (!sw.fg) {

View File

@ -3924,6 +3924,13 @@ If the
.Dq !\&
is used, no error is given if the address is not currently assigned to
the interface (and no deletion takes place).
.It iface name Ar name
Renames the interface to
.Ar name .
.It iface description Ar description
Sets the interface description to
.Ar description .
Useful if you have many interfaces on your system.
.It iface show
Shows the current state and current addresses for the interface.
It is much the same as running