#include <errno.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <zone.h>
#include "netinet/ip_fil.h"
#include "ipfzone.h"
static ipfzoneobj_t ipzo;
static boolean_t do_setzone = 0;
static int num_setzones = 0;
extern int errno;
extern int opterr;
extern int optind;
extern char *optarg;
void
getzonearg(int argc, char *argv[], const char *optstr)
{
int c;
opterr = 0;
while ((c = getopt(argc, argv, optstr)) != -1) {
if (c == 'G')
ipzo.ipfz_gz = 1;
}
if (optind < argc)
setzonename(argv[optind]);
optind = 1;
opterr = 1;
}
void
getzoneopt(int argc, char *argv[], const char *optstr)
{
int c;
opterr = 0;
while ((c = getopt(argc, argv, optstr)) != -1) {
if (c == 'G')
setzonename_global(optarg);
if (c == 'z')
setzonename(optarg);
}
optind = 1;
opterr = 1;
}
void
setzonename(const char *zonename)
{
memcpy(ipzo.ipfz_zonename, zonename, sizeof (ipzo.ipfz_zonename));
do_setzone = B_TRUE;
num_setzones++;
}
void
setzonename_global(const char *zonename)
{
setzonename(zonename);
ipzo.ipfz_gz = 1;
}
int
setzone(int fd)
{
if (!do_setzone)
return (0);
if (num_setzones > 1) {
(void) fprintf(stderr,
"Only one of -G and -z may be set\n");
return (-1);
}
if (ipzo.ipfz_gz == 1 &&
getzoneidbyname(ipzo.ipfz_zonename) == GLOBAL_ZONEID) {
(void) fprintf(stderr,
"-G cannot be used with the global zone\n");
return (-1);
}
if (ioctl(fd, SIOCIPFZONESET, &ipzo) == -1) {
switch (errno) {
case ENODEV:
(void) fprintf(stderr,
"Could not find running zone: %s\n",
ipzo.ipfz_zonename);
break;
case EACCES:
(void) fprintf(stderr,
"Permission denied setting zone: %s\n",
ipzo.ipfz_zonename);
break;
default:
perror("Error setting zone");
}
return (-1);
}
return (0);
}