示例#1
0
int
EncryptType(char *type, char *mode)
{
	Encryptions *ep;
	int ret = 0;

	if (isprefix(type, "help") || isprefix(type, "?")) {
		printf("Usage: encrypt type <type> [input|output]\n");
		encrypt_list_types();
	} else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
						sizeof(Encryptions))) == 0) {
		printf("%s: invalid encryption type\n", type);
	} else if (Ambiguous((char **)ep)) {
		printf("Ambiguous type '%s'\n", type);
	} else {
		if ((mode == 0) || isprefix(mode, "input")) {
			decrypt_mode = ep->type;
			i_wont_support_decrypt &= ~typemask(ep->type);
			ret = 1;
		}
		if ((mode == 0) || isprefix(mode, "output")) {
			encrypt_mode = ep->type;
			i_wont_support_encrypt &= ~typemask(ep->type);
			ret = 1;
		}
		if (ret == 0)
			printf("%s: invalid encryption mode\n", mode);
	}
	return(ret);
}
示例#2
0
int
EncryptDisable(char *type, char *mode)
{
	Encryptions *ep;
	int ret = 0;

	if (isprefix(type, "help") || isprefix(type, "?")) {
		printf("Usage: encrypt disable <type> [input|output]\n");
		encrypt_list_types();
	} else if ((ep = (Encryptions *)genget(type, (char **)encryptions,
						sizeof(Encryptions))) == NULL) {
		printf("%s: invalid encryption type\n", type);
	} else if (Ambiguous((char **)ep)) {
		printf("Ambiguous type '%s'\n", type);
	} else {
		if ((mode == NULL) || (isprefix(mode, "input") ? 1 : 0)) {
			if (decrypt_mode == ep->type)
				EncryptStopInput();
			i_wont_support_decrypt |= typemask(ep->type);
			ret = 1;
		}
		if ((mode == NULL) || (isprefix(mode, "output"))) {
			if (encrypt_mode == ep->type)
				EncryptStopOutput();
			i_wont_support_encrypt |= typemask(ep->type);
			ret = 1;
		}
		if (ret == 0)
			printf("%s: invalid encryption mode\n", mode);
	}
	return(ret);
}
示例#3
0
int
EncryptEnable(char *type, char *mode)
{
	if (isprefix(type, "help") || isprefix(type, "?")) {
		printf("Usage: encrypt enable <type> [input|output]\n");
		encrypt_list_types();
		return(0);
	}
	if (EncryptType(type, mode))
		return(EncryptStart(mode));
	return(0);
}
示例#4
0
int
intflags(char *ifname, int ifs, int argc, char **argv)
{
	int set, value, flags;

	if (NO_ARG(argv[0])) {
		set = 0;
		argv++;
		argc--;
	} else
		set = 1;

	if (isprefix(argv[0], "debug")) {
		/* debug */
		value = IFF_DEBUG;
	} else if (isprefix(argv[0], "shutdown")) {
		/* shutdown */
		value = -IFF_UP;
	} else if (isprefix(argv[0], "arp")) {
		/* arp */
		value = -IFF_NOARP;
	} else {
		printf("%% intflags: Internal error\n");
		return(0);
	}

	flags = get_ifflags(ifname, ifs);
	if (value < 0) {
		/*
		 * Idea from ifconfig.  If value is negative then
		 * we just reverse the operation. (e.g. 'shutdown' is
		 * the opposite of the IFF_UP flag)
		 */
		if (set) {
			value = -value;
			flags &= ~value;
		} else {
			value = -value;
			flags |= value;
		}
	} else if (value > 0) {
		if (set)
			flags |= value;
		else
			flags &= ~value;
	} else {
		printf("%% intflags: value internal error\n");
	}
	set_ifflags(ifname, ifs, flags);
	return(0);
}
示例#5
0
文件: if.c 项目: yellowman/nsh
int
intaf(char *ifname, int ifs, int argc, char **argv)
{
	int set;

	if (NO_ARG(argv[0])) {
		set = 0;
		argv++;
		argc--;
	} else
		set = 1;

	if (argc > 1) {
		printf("%% Invalid argument\n");
		return(1);
	}

	if (isprefix(argv[0], "inet6")) {
		if (set)
			addaf(ifname, AF_INET6, ifs);
		else
			removeaf(ifname, AF_INET6, ifs);
	} else {
		printf("%% intaf: prefix internal error\n");
	}
	return(0);
}
示例#6
0
/*
 * Test driver for isprefix.  Compares the two parameters; returns
 * true (0) if a prefix, false (1) otherwise.
 */
int main(int argc, char **argv)
{
    if (argc < 3) {
	usage();
    }

    if (strcmp(argv[1], "!")) { 
	return !isprefix(argv[1], argv[2]);
    } else {
	/* inverted */
	if (argc < 4) {
	    usage();
	}
	return isprefix(argv[2], argv[3]);
    }
}
示例#7
0
文件: tunnel.c 项目: arnobroekhof/nsh
int
inttunnel(char *ifname, int ifs, int argc, char **argv)
{
	int set;

	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	argc--;
	argv++;

	if (((set && argc !=2 && argc != 4) || (set && argc == 4 &&
	    !isprefix(argv[2], "rdomain"))) || (!set && argc > 4)) {
		printf("%% tunnel <src ip> <dest ip> [rdomain <domain>]\n");
		printf("%% no tunnel [src ip] [dest ip] [rdomain <domain>]\n");
		return(0);
	}

	if(set && argc == 2)
		settunnel(ifs, ifname, argv[0], argv[1], NULL);
	else if(set && argc == 4)
		settunnel(ifs, ifname, argv[0], argv[1], argv[3]);
	else
		deletetunnel(ifs, ifname);
	return(0);
}
示例#8
0
文件: conf.c 项目: sthen/nsh
void conf_ifflags(FILE *output, int flags, char *ifname, int ippntd)
{
	if (flags & IFF_DEBUG)
		fprintf(output, " debug\n");
	if (flags & (IFF_LINK0|IFF_LINK1|IFF_LINK2)) {
		fprintf(output, " link ");
		if(flags & IFF_LINK0)
			fprintf(output, "0 ");
		if(flags & IFF_LINK1)
			fprintf(output, "1 ");
		if(flags & IFF_LINK2)
			fprintf(output, "2");
		fprintf(output, "\n");
	}
	if (flags & IFF_NOARP)
		fprintf(output, " no arp\n");

	if (isprefix("pppoe", ifname)) {		/* XXX */
		fprintf(output, " no shutdown\n");
	} else {
		/*
		 * ip X/Y turns the interface up (just like 'no shutdown')
		 * ...but if we never had an ip address set and the interface
		 * is up, we need to save this state explicitly.
		 */
		if (!ippntd && (flags & IFF_UP))
			fprintf(output, " no shutdown\n");
		else if (!(flags & IFF_UP))
			fprintf(output, " shutdown\n");
	}
	fprintf(output, "!\n");
}
示例#9
0
static rs_result rdiff_action(poptContext opcon)
{
    const char      *action;

    action = poptGetArg(opcon);
    if (!action) 
        ;
    else if (isprefix(action, "signature")) 
        return rdiff_sig(opcon);
    else if (isprefix(action, "delta")) 
        return rdiff_delta(opcon);
    else if (isprefix(action, "patch"))
        return rdiff_patch(opcon);
    
    rdiff_usage("rdiff: You must specify an action: `signature', `delta', or `patch'.");
    return RS_SYNTAX_ERROR;
}
示例#10
0
文件: if.c 项目: yellowman/nsh
int
intxflags(char *ifname, int ifs, int argc, char **argv)
{
	int set, value, flags;

	if (NO_ARG(argv[0])) {
		set = 0;
		argv++;
		argc--;
	} else
		set = 1;

	if (isprefix(argv[0], "autoconfprivacy")) {
		value = -IFXF_INET6_NOPRIVACY;
	} else if (isprefix(argv[0], "autoconf6")) {
		value = IFXF_AUTOCONF6;
	} else if (isprefix(argv[0], "mpls")) {
		value = IFXF_MPLS;
	} else if (isprefix(argv[0], "wol")) {
		value = IFXF_WOL;
	} else {
		printf("%% intxflags: Internal error\n");
		return(0);
	}

	flags = get_ifxflags(ifname, ifs);
	if (value < 0) {
		if (set) {
			value = -value;
			flags &= ~value;
		} else {
			value = -value;
			flags |= value;
		}
	} else if (value > 0) {
		if (set)
			flags |= value;
		else
			flags &= ~value;
	} else {
		printf("%% intxflags: value internal error\n");
	}
	set_ifxflags(ifname, ifs, flags);
	return(0);
}
示例#11
0
文件: pfsync.c 项目: bklang/nsh
int
intmaxupd(char *ifname, int ifs, int argc, char **argv)
{
	struct ifreq ifr;
	struct pfsyncreq preq;
	u_int32_t val;
	int set;
	const char *errmsg = NULL;

	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	argc--;
	argv++;

	if ((!set && argc > 2) || (set && (argc < 1 || argc > 2)) ||
	    (set && argc == 2 && !isprefix(argv[1], "defer"))) {
		printf("%% maxupd <max pfsync updates> [defer]\n");
		printf("%% no maxupd [max pfsync updates] [defer]\n");
		return (0);
	}
	bzero((char *) &preq, sizeof(struct pfsyncreq));
	ifr.ifr_data = (caddr_t) & preq;
	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));

	if (ioctl(ifs, SIOCGETPFSYNC, (caddr_t) & ifr) == -1) {
		printf("%% intmaxupd: SIOCGETPFSYNC: %s\n", strerror(errno));
		return (0);
	}
	if (set) {
		errno = 0;
		val = strtonum(argv[0], 0, INT_MAX, &errmsg);
		if (errmsg) {
			printf("%% maxupd value out of range %s: %s\n", argv[0],
			    errmsg);
			return (0);
		}
		preq.pfsyncr_maxupdates = (int)val;
		if (argc == 2)
			preq.pfsyncr_defer = 1;
	} else
		preq.pfsyncr_maxupdates = PFSYNC_MAXUPDATES;

	if (ioctl(ifs, SIOCSETPFSYNC, (caddr_t) & ifr) == -1) {
		if (errno == EINVAL)
			printf("%% maxupd value out of range\n");
		else
			printf("%% intmaxupd: SIOCSETPFSYNC: %s\n",
			    strerror(errno));
	}
	return (0);
}
示例#12
0
文件: conf.c 项目: gonzopancho/nsh
int islateif(char *ifname)
{
	int i;

	for (i = 0; i < sizeof(latestartifs) / sizeof(latestartifs[0]); i++)
		if (isprefix(latestartifs[i].name, ifname))  
			return(1);

	return(0);
}
int main(int argc, char *argv[])
{
	static entry_t entry[MAXENTRIES];
	int nentry,i,j,k;
	int nprefs = 0, nbases = 0;
	int nextfree = 1;
	base_t *b, btemp;
	pre_t *p, ptemp;

	if ((nentry = readlist(argv[1], entry, MAXENTRIES)) < 0)
	{
		fprintf(stderr, "Input file too large.\n");
		return 1;
	}

	xidentrysort(entry, nentry, sizeof(entry_t), compareentries);
	b = (base_t *) malloc(nentry * sizeof(base_t));
	p = (pre_t *) malloc(nentry * sizeof(pre_t));
	for (i = 0; i < nentry; i++)
	{
		if (i < nentry-1 && isprefix(entry[i], entry[i+1]))
		{
			ptemp = (pre_t) malloc(sizeof(struct prerec));
			ptemp->len = entry[i]->len;
			ptemp->pre = entry[i]->pre;
			/* Update 'pre' for all entries that have this prefix */
			for (j = i + 1; j < nentry && isprefix(entry[i], entry[j]); j++)
				entry[j]->pre = nprefs;
			p[nprefs++] = ptemp;
		}
		else
		{
			btemp = (base_t) malloc(sizeof(struct baserec));
			btemp->len = entry[i]->len;
			btemp->str = entry[i]->data;
			btemp->pre = entry[i]->pre;
			b[nbases++] = btemp;
		}
	}
	node_patric *root;
	root = buildpatricia(b, 0, 0, nbases);
	print_trie(root, 0);
}
示例#14
0
文件: conf.c 项目: sthen/nsh
int islateif(char *ifname)
{
	int i;

	for (i = 0; i < nitems(latestartifs); i++)
		if (isprefix(latestartifs[i].name, ifname))  
			return(1);

	return(0);
}
示例#15
0
文件: trunk.c 项目: arnobroekhof/nsh
int
inttrunkproto(char *ifname, int ifs, int argc, char **argv)
{
	int i, set;
	struct trunk_reqall ra;

	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	argc--;
	argv++;

	if ((!set && argc != 1) || (set && argc != 1)) {
                printf("%% trunkproto <proto>\n");
                printf("%% no trunkproto\n");
                return(0);
        }

	bzero(&ra, sizeof(ra));
	strlcpy(ra.ra_ifname, ifname, sizeof(ra.ra_ifname));

	if (set) {
		for (i = 0; i < TRUNK_PROTO_MAX; ++i) {
			if (isprefix(argv[0], (char *)tpr[i].tpr_name)) {
				ra.ra_proto = tpr[i].tpr_proto;
				break;
			}
		}
		if (i == TRUNK_PROTO_MAX) {
			printf("%% trunkproto <");
			for (i = 0; i < TRUNK_PROTO_MAX; ++i)
				printf("%s%s", i == 0 ? "" : "|", tpr[i].tpr_name);
			printf(">\n");
			return(0);
		}
	} else
		ra.ra_proto = TRUNK_PROTO_DEFAULT;

	if (ioctl(ifs, SIOCSTRUNK, &ra) != 0) {
		switch(errno) {
		case ENOTTY:
			printf("%% %s not trunk\n", ifname);
			break;
		default:
			printf("%% inttrunkproto: SIOCSTRUNK: %s\n", strerror(errno));
		}
		return 1;
	}

	return 0;
}
示例#16
0
int
EncryptStop(char *mode)
{
	int ret = 0;
	if (mode) {
		if (isprefix(mode, "input"))
			return(EncryptStopInput());
		if (isprefix(mode, "output"))
			return(EncryptStopOutput());
		if (isprefix(mode, "help") || isprefix(mode, "?")) {
			printf("Usage: encrypt stop [input|output]\n");
			return(0);
		}
		printf("%s: invalid encryption mode 'encrypt stop ?' for help\n", mode);
		return(0);
	}
	ret += EncryptStopInput();
	ret += EncryptStopOutput();
	return(ret);
}
示例#17
0
char* readline_auto_completion(const char* s, int state)
{
  /*
   * Regarding GC, see comment in auto_complete()
   */

  static char** commands = NULL;
  static int idx = 0;

  // Start-state; build list of completion hits
  if ( state == 0 ) {
    // get all commands
    cons_t *all_commands = proc_list_globals(NULL, global_env);
    size_t count = 0;

    // count number of hits
    for ( cons_t *p = all_commands; !nullp(p); p = cdr(p) )
      count += (symbolp(car(p)) &&
                 isprefix(s, car(p)->symbol->c_str()));

    // build actual hits; readline will (hopefully!) free for us
    // (but TODO don't count on it)
    commands = (char**) malloc((1+count)*sizeof(char*));
    char** command = commands;

    for ( cons_t *p = all_commands; !nullp(p); p = cdr(p) ) {
      if ( symbolp(car(p)) && isprefix(s, car(p)->symbol->c_str()) )
          *command++ = strdup(car(p)->symbol->c_str());
    }

    *command = NULL;
    idx = 0;
  } else {
    // bring up next hit; don't know if this is right
    if ( commands[idx] != NULL )
      ++idx;
  }

  return commands[idx];
}
示例#18
0
  Cmap *parse(char *cmd, Cmap *cmds) throw(Error) {
    Cmap *f=0;
    while (cmds->foo) {
      if (isprefix(cmd,cmds->cmd)) {
	if (f)
	  throw Error("Cmdr",string("Ambiguous command: ")+cmd);
	else
	  f = cmds;
      }
      cmds++;
    }
    if (f)
      return f;
    throw Error("Cmdr",string("Unknown command: ")+cmd);
  }
示例#19
0
文件: lex.c 项目: coyizumi/cs111
const struct cmd *
lex(char word[])
{
	const struct cmd *cp;

	/*
	 * ignore trailing chars after `#'
	 *
	 * lines with beginning `#' are comments
	 * spaces before `#' are ignored in execute()
	 */

	if (*word == '#')
	    *(word+1) = '\0';


	for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
		if (isprefix(word, cp->c_name))
			return (cp);
	return (NULL);
}
示例#20
0
// TODO: Can we use SeekableReadStream::readLine instead?
char *bdf_getline(Common::SeekableReadStream &fp, char *buf, int len) {
	int c;
	char *b;

	for (;;) {
		b = buf;
		while (!fp.eos()) {
			c = fp.readByte();
			if (c == '\r')
				continue;
			if (c == '\n')
				break;
			if (b - buf >= (len - 1))
				break;
			*b++ = c;
		}
		*b = '\0';
		if (fp.eos() && b == buf)
			return NULL;
		if (b != buf && !isprefix(buf, "COMMENT"))
			break;
	}
	return buf;
}
示例#21
0
文件: if.c 项目: yellowman/nsh
/*
 * addr/port parsing lifted from sbin/ifconfig/ifconfig.c
 */
int
intpflow(char *ifname, int ifs, int argc, char **argv)
{
	struct ifreq ifr;
	struct pflowreq preq;
	int set;
	const char *errmsg = NULL;

	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	argc--;
	argv++;

	/* XXX f*****g makes my eyes bleed. learn how to use yacc ? */
	if ((set && argc < 4) || (set && argc == 5) || (set &&
	    (argc == 4 || argc == 6) && (!isprefix(argv[0], "sender") ||
	    !isprefix(argv[2], "receiver") ||
	    (argc == 6 && !isprefix(argv[4], "version"))))) {
		printf("%% pflow sender <x.x.x.x> receiver <x.x.x.x:port> "
		    "[version 5|9|10]\n"
		    "%% no pflow [sender x.x.x.x receiver x.x.x.x:port "
		    "version 5|9|10]\n");
		return(0);
	}

	if (set) {
	        if (strchr(argv[3], ':') == NULL) {
			printf("%% Receiver has no port specified\n");
			return(0);
		}
	}

	bzero(&ifr, sizeof(ifr));     
	strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);

	bzero(&preq, sizeof(struct pflowreq));
	ifr.ifr_data = (caddr_t)&preq;

	preq.addrmask = PFLOW_MASK_SRCIP | PFLOW_MASK_DSTIP;
	if (set) {
		pflow_addr(argv[1], &preq.flowsrc);
		pflow_addr(argv[3], &preq.flowdst);
		if (argc == 6) {
			preq.version = strtonum(argv[5], 5, PFLOW_PROTO_MAX,
			    &errmsg);
			preq.addrmask |= PFLOW_MASK_VERSION;
	                if (errmsg) {
				printf("%% Invalid pflow version %s: %s\n",
				    argv[0], errmsg);
				return(0);
			}
                }
	}

	if (ioctl(ifs, SIOCSETPFLOW, (caddr_t)&ifr) == -1)
		printf("%% Unable to set pflow parameters: %s\n",
		    strerror(errno));

	return(0);
}
示例#22
0
文件: if.c 项目: yellowman/nsh
int
intip(char *ifname, int ifs, int argc, char **argv)
{
	int s, set, flags, argcmax;
	char *msg, *cmdname;
	ip_t ip;
	/* ipv4 structures */
	struct in_addr in4dest;
	struct ifaliasreq ip4req;
	/* ipv6 structures */
	struct in6_addr in6dest;
	struct in6_aliasreq ip6req;

	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	/*
	 * We use this function for ip and alias setup since they are
	 * the same thing.
	 */
	if (isprefix(argv[0], "alias")) {
		cmdname = "alias";
	} else if (isprefix(argv[0], "ip")) {
		cmdname = "ip";
	} else {
		printf("%% intip: Internal error\n");
		return 0;
	}

	argc--;
	argv++;

	flags = get_ifflags(ifname, ifs);
	if (flags & IFF_POINTOPOINT) {
		argcmax = 2;
		msg = "destination";
	} else if (flags & IFF_BROADCAST) {
		argcmax = 2;
		msg = "broadcast";
	} else {
		argcmax = 1;
		msg = NULL;
	}

	if (argc < 1 || argc > argcmax) {
		printf("%% %s <address>/<bits> %s%s%s\n", cmdname,
		    msg ? "[" : "", msg ? msg : "", msg ? "]" : "");
		printf("%% %s <address>/<netmask> %s%s%s\n", cmdname,
		    msg ? "[" : "", msg ? msg : "", msg ? "]" : "");
		printf("%% no %s <address>[/bits]\n", cmdname);
		printf("%% no %s <address>[/netmask]\n", cmdname);
		return(0);
	}

	/* ignore 'address' keyword, don't print error */
	if (isprefix(argv[0], "address")) {
		argc--;
		argv++;
	}

	if (isprefix(argv[0], "dhcp")) {
		char *args[] = { PKILL, "dhclient", ifname, '\0' };
		char *args_set[] = { DHCLIENT, ifname, '\0' };
		char leasefile[sizeof(LEASEPREFIX)+1+IFNAMSIZ];

		if (set)
			cmdargs(DHCLIENT, args_set);
		else {
			cmdargs(PKILL, args);
			snprintf(leasefile, sizeof(leasefile), "%s.%s",
			    LEASEPREFIX, ifname);
			rmtemp(leasefile);
		}
		return(0);
	}

	memset(&ip, 0, sizeof(ip));
	parse_ip_pfx(argv[0], NO_NETMASK, &ip);

	if (ip.family == 0)
		/* bad IP specified */
		return(0);

	if (set && !(flags & IFF_POINTOPOINT) && ip.bitlen == -1) {
		printf("%% Netmask not specified\n");
		return(0);
	}
	
	if (ip.bitlen == -1) {
		/*
		 * No netmask specified, set the field at 0.
		 * The kernel mostly ignores netmask for PTP interfaces,
		 * but won't allow anything less than a /128 for an IPv6
		 * PTP interface.
		 */
		if (!(flags & IFF_POINTOPOINT))
			ip.bitlen = 0;
		else if (ip.family == AF_INET)
			ip.bitlen = 32;
		else if (ip.family == AF_INET6)
			ip.bitlen = 128;
	}

	switch(ip.family) {
	case AF_INET:
		memset(&in4dest, 0, sizeof(in4dest));
		if (argc == 2 && !inet_pton(AF_INET, argv[1], &in4dest)) {
			printf("%% Invalid %s address\n", msg);
			return(0);
		}
		memset(&ip4req, 0, sizeof(ip4req));
		pack_ifaliasreq(&ip4req, &ip, &in4dest, ifname);
		/* do it */
		if (ioctl(ifs, set ? SIOCAIFADDR : SIOCDIFADDR, &ip4req) < 0)
			printf("%% intip: SIOC%sIFADDR: %s\n", set ? "A" : "D",
			    strerror(errno));
		break;
	case AF_INET6:
		memset(&in6dest, 0, sizeof(in6dest));
		if (argc == 2 && parse_ipv6(argv[1], &in6dest) != 0) {
			printf("%% Invalid destination address %s\n", argv[1]);
			return(0);
		}
		memset(&ip6req, 0, sizeof(ip6req));
		pack_in6aliasreq(&ip6req, &ip, &in6dest, ifname);
		/* get inet6 socket */
		s = socket(PF_INET6, SOCK_DGRAM, 0);
		if (s < 0) {
			printf("%% socket failed: %s\n", strerror(errno));
			return(0);
		}
		/* turn on inet6 */
		addaf(ifname, AF_INET6, ifs);
		/* do it */
		if (ioctl(s, set ? SIOCAIFADDR_IN6 : SIOCDIFADDR_IN6, &ip6req)
		    < 0) {
			if (!set && errno == EADDRNOTAVAIL)
				printf("%% IP address not found on %s\n",
				    ifname);
			else
				printf("%% intip: SIOC%sIFADDR_IN6: %s\n",
				    set ? "A" : "D", strerror(errno));
		} else {
			ipv6ll_db_store(
			    (struct sockaddr_in6 *)&ip6req.ifra_addr,
			    (struct sockaddr_in6 *)&ip6req.ifra_prefixmask,
			    set ? DB_X_ENABLE : DB_X_REMOVE, ifname);
		}
		close(s);
		break;
	default:
		printf("%% unknown address family: %d\n", ip.family);
		break;
	}
	return(0);
}
示例#23
0
文件: if.c 项目: yellowman/nsh
int
intrtd(char *ifname, int ifs, int argc, char **argv)
{
	StringList *dbreturn;
	char *cmdpath, *cmdname;
	int set;

	if (NO_ARG(argv[0])) {
		argv++;
		argc--;
		set = 0;
	} else
		set = 1;

	if (isprefix(argv[0], "rtadvd")) {
		cmdname = "rtadvd";
		cmdpath = RTADVD;
	} else {
		printf("%% intrtd: Internal error\n");
		return 0;
	}

	if (argc > 1) {
		printf ("%% %s\n", cmdname);
		printf ("%% no %s\n", cmdname);
		return(0);
	}

	dbreturn = sl_init();
	if (db_select_flag_x_ctl(dbreturn, cmdname, ifname) < 0) {
		printf("%% database failure select flag x ctl\n");
		sl_free(dbreturn, 1);
		return(1);
	}
	if (dbreturn->sl_cur > 0) {
		/* already found in db for ifname */
		if (!set) {
			if (db_delete_flag_x_ctl(cmdname, ifname) < 0)
				printf("%% database delete failure\n");
		} else {
			printf("%% %s already running\n", cmdname);
		}
		if (!set && strcmp(cmdname, "rtadvd") == 0) {
			char *args[] = { PKILL, cmdpath, "-c",
			    "/var/run/rtadvd.0", ifname, '\0' };

			cmdargs(PKILL, args);
		}
	} else {
		/* not found in db for ifname */
		if (set) {
			if(db_insert_flag_x(cmdname, ifname, 0, DB_X_ENABLE,
			    NULL) < 0) {
				printf("%% database insert failure\n");
				sl_free(dbreturn, 1);
				return(1);
			}
		} else {
			printf("%% %s not running\n", cmdname);
		}
		if (set && strcmp(cmdname, "rtadvd") == 0) {
			char *args[] = { cmdpath, "-c", "/var/run/rtadvd.0",
			    ifname, '\0' };

			cmdargs(cmdpath, args);
		}
	}
	sl_free(dbreturn, 1);
	return(0);
}
示例#24
0
void conf_interfaces(FILE *output, char *only)
{
	FILE *dhcpif, *llfile;
	int ifs, flags, ippntd, br;
#define	LLPREFIX	"/var/run/lladdr"
	char leasefile[sizeof(LEASEPREFIX)+1+IFNAMSIZ];
	char *lladdr, llorig[IFNAMSIZ];
	char llfn[sizeof(LLPREFIX)+IFNAMSIZ];
	char ifdescr[IFDESCRSIZE];

	struct if_nameindex *ifn_list, *ifnp;
	struct ifreq ifr, ifrdesc;
	struct if_data if_data;
	struct vlanreq vreq;

	if ((ifn_list = if_nameindex()) == NULL) {
		printf("%% conf_interfaces: if_nameindex failed\n");
		return;
	}

	if ((ifs = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		printf("%% conf_interfaces socket: %s\n", strerror(errno));
		if_freenameindex(ifn_list);
		return;
	}

	for (ifnp = ifn_list; ifnp->if_name != NULL; ifnp++) {
		strlcpy(ifr.ifr_name, ifnp->if_name, sizeof(ifr.ifr_name));

		if (only && !isprefix(only, ifnp->if_name))
			/* only display interfaces which start with ... */
			continue;
		if (!only) {
			/* interface prefixes to exclude on generic run */
			if (isprefix("pfsync", ifnp->if_name))
				continue;
		}

		if (ioctl(ifs, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
			printf("%% conf: SIOCGIFFLAGS: %s\n", strerror(errno));
			continue;
		}
		flags = ifr.ifr_flags;

		ifr.ifr_data = (caddr_t)&if_data;
		if (ioctl(ifs, SIOCGIFDATA, (caddr_t)&ifr) < 0) {
			printf("%% conf: SIOCGIFDATA: %s\n", strerror(errno));
			continue;
		}

		/*
		 * Keep in mind that the order in which things are displayed
		 * here is important.  For instance, we want to setup the
		 * vlan tag before setting the IP address since the vlan
		 * must know what parent to inherit the parent interface
		 * flags from before it is brought up.  Another example of
		 * this would be that we need to setup the members on a
		 * bridge before we setup flags on them.
		 */

		/*
		 * set interface/bridge mode
		 */
		if (!(br = is_bridge(ifs, ifnp->if_name)))
			br = 0;
		fprintf(output, "%s %s\n", br ? "bridge" : "interface",
		    ifnp->if_name);

		/*
		 * description, if available
		 * copied straight from ifconfig.c
		 */
		memset(&ifrdesc, 0, sizeof(ifrdesc));
		strlcpy(ifrdesc.ifr_name, ifnp->if_name,
		    sizeof(ifrdesc.ifr_name));
		ifrdesc.ifr_data = (caddr_t)&ifdescr;
		if (ioctl(ifs, SIOCGIFDESCR, &ifrdesc) == 0 &&
		    strlen(ifrdesc.ifr_data))
			fprintf(output, " description %s\n", ifrdesc.ifr_data);

		/*
		 * print lladdr if necessary
		 */
		if ((lladdr = get_hwdaddr(ifnp->if_name)) != NULL) {
			/* We assume lladdr only useful if we can get_hwdaddr */
			snprintf(llfn, sizeof(llfn), "%s.%s", LLPREFIX,
			    ifnp->if_name);
			if ((llfile = fopen(llfn, "r"))) {
				fgets(llorig, sizeof(llorig), llfile);
				if (strcmp(llorig, lladdr) != 0) {
					fprintf(output, " lladdr %s\n",
					    lladdr);
				}
				fclose(llfile);
			}
		}
		 
		/*
		 * print vlan tag, parent if available.  if a tag is set
		 * but there is no parent, discard.
		 */
		bzero(&vreq, sizeof(struct vlanreq));
		ifr.ifr_data = (caddr_t)&vreq;  

		if (ioctl(ifs, SIOCGETVLAN, (caddr_t)&ifr) != -1) {
			struct vlanreq preq;

			bzero(&preq, sizeof(struct vlanreq));

			ifr.ifr_data = (caddr_t)&preq;
			ioctl(ifs, SIOCGETVLANPRIO, (caddr_t)&ifr);

			if(vreq.vlr_tag && (vreq.vlr_parent[0] != '\0')) {
				fprintf(output, " vlan %d parent %s",
				    vreq.vlr_tag, vreq.vlr_parent);
				if(preq.vlr_tag > 0)
					fprintf(output, " priority %d",
					    preq.vlr_tag);
				fprintf(output, "\n");
			}
		}

		conf_intrtlabel(output, ifs, ifnp->if_name);
		conf_intgroup(output, ifs, ifnp->if_name);

		snprintf(leasefile, sizeof(leasefile), "%s.%s",
		    LEASEPREFIX, ifnp->if_name);
		if ((dhcpif = fopen(leasefile, "r"))) {
			fprintf(output, " ip dhcp\n");
			fclose(dhcpif);
			ippntd = 1;
		} else {
			ippntd = conf_ifaddrs(output, ifnp->if_name, flags);
		}

		if (br) {
			conf_brcfg(output, ifs, ifn_list, ifnp->if_name);
		} else {
			char tmp[24];

			conf_media_status(output, ifs, ifnp->if_name);
			conf_ifmetrics(output, ifs, if_data, ifnp->if_name);
			conf_pfsync(output, ifs, ifnp->if_name);
			conf_carp(output, ifs, ifnp->if_name);
			conf_trunk(output, ifs, ifnp->if_name);
			if (timeslot_status(ifs, ifnp->if_name, tmp,
			    sizeof(tmp)) == 1) 
				fprintf(output, " timeslots %s\n", tmp);
			if (conf_dhcrelay(ifnp->if_name, tmp, sizeof(tmp))
			    != NULL)
				fprintf(output, " dhcrelay %s\n", tmp);
		}

		/*
		 * print various flags
		 */
		if (flags & IFF_DEBUG)
			fprintf(output, " debug\n");
		if (flags & (IFF_LINK0|IFF_LINK1|IFF_LINK2)) {
			fprintf(output, " link ");
			if(flags & IFF_LINK0)
				fprintf(output, "0 ");
			if(flags & IFF_LINK1)
				fprintf(output, "1 ");
			if(flags & IFF_LINK2)
				fprintf(output, "2");
			fprintf(output, "\n");
		}
		if (flags & IFF_NOARP)
			fprintf(output, " no arp\n");
		/*
		 * ip X/Y turns the interface up (just like 'no shutdown')
		 * ...but if we never had an ip address set and the interface
		 * is up, we need to save this state explicitly.
		 */
		if (!ippntd && (flags & IFF_UP))
			fprintf(output, " no shutdown\n");
		else if (!(flags & IFF_UP))
			fprintf(output, " shutdown\n");
		fprintf(output, "!\n");
	}
	close(ifs);
	if_freenameindex(ifn_list);
}
示例#25
0
int
intvlan(char *ifname, int ifs, int argc, char **argv)
{
	const char *errmsg = NULL;
	struct ifreq ifr;
	struct vlanreq vreq, preq;
	int set;

	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	argc--;
	argv++;

	if ((set && (argc < 3 || argc > 5)) || (!set && argc > 5) ||
	    argc == 4 ||
	    (argc > 3 && !isprefix(argv[1], "parent")) ||
	    (argc > 5 && !isprefix(argv[3], "priority"))) {
		printf("%% vlan <tag> parent <parent interface> [priority <priority>]\n");
		printf("%% no vlan [tag] [parent <parent interface>] [priority <priority>]\n");
		return 0;
	}

	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));

	bzero(&vreq, sizeof(vreq));
	bzero(&preq, sizeof(preq));

	ifr.ifr_data = (caddr_t)&vreq;

	if (ioctl(ifs, SIOCGETVLAN, (caddr_t)&ifr) == -1) {
		switch(errno) {
		case ENOTTY:
			printf("%% This interface does not support vlan"
			    " tagging\n");
			break;
		default:
			printf("%% intvlan: SIOCGETVLAN: %s\n",
			    strerror(errno));
		}
		return(0);
	}

	if (set) {
		if (!is_valid_ifname(argv[2]) || is_bridge(ifs, argv[2])) {
			printf("%% Invalid vlan parent %s\n", argv[2]);
			return 0;
		}
		strlcpy(vreq.vlr_parent, argv[2], sizeof(vreq.vlr_parent));
		vreq.vlr_tag = strtonum(argv[0], 0, 4096, &errmsg);
		if (errmsg) {
			printf("%% Invalid vlan tag %s: %s", argv[0], errmsg);
			return 0;
		}
		if (vreq.vlr_tag != EVL_VLANOFTAG(vreq.vlr_tag)) {
			printf("%% Invalid vlan tag %s\n", argv[0]);
			return 0;
		}
		if (argc == 5) {
			preq.vlr_tag = strtonum(argv[4], 0, 7, &errmsg);
			if (errmsg) {
				printf("%% Invalid vlan priority %s: %s\n",
				    argv[4], errmsg);
				return 0;
			}
		}
	} else {
		bzero(&vreq.vlr_parent, sizeof(vreq.vlr_parent));
		vreq.vlr_tag = 0;
	}

	if (ioctl(ifs, SIOCSETVLAN, (caddr_t)&ifr) == -1) {
		switch(errno) {
		case EBUSY:
			printf("%% Please disconnect the current vlan parent"
			    " before setting a new one\n");
			return 0;
			break;
		default:
			printf("%% intvlan: SIOCSETVLAN: %s\n",
			    strerror(errno));
			return 0;
		}
	}

	ifr.ifr_data = (caddr_t)&preq;
	if (ioctl(ifs, SIOCSETVLANPRIO, (caddr_t)&ifr) == -1)
		printf("%% intvlan: SIOCSETVLANPRIO: %s\n", strerror(errno));

	return 0;
}
示例#26
0
int
intip(char *ifname, int ifs, int argc, char **argv)
{
	int set, alias, flags, argcmax;
	ip_t ip;
	struct in_addr destbcast;
	struct ifaliasreq addreq, ridreq;
	struct sockaddr_in *sin;
	char  *msg, *cmdname;

	memset(&addreq, 0, sizeof(addreq));
	memset(&ridreq, 0, sizeof(ridreq));

	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	/*
	 * We use this function for ip and alias setup since they are
	 * the same thing.
	 */
	if (isprefix(argv[0], "alias")) {
		alias = 1;
		cmdname = "alias";
	} else if (isprefix(argv[0], "ip")) {
		alias = 0;
		cmdname = "ip";
	} else {
		printf("%% intip: Internal error\n");
		return 0;
	}

	argc--;
	argv++;

	flags = get_ifflags(ifname, ifs);
	if (flags & IFF_POINTOPOINT) {
		argcmax = 2;
		msg = "destination";
	} else if (flags & IFF_BROADCAST) {
		argcmax = 2;
		msg = "broadcast";
	} else {
		argcmax = 1;
		msg = NULL;
	}

	if (argc < 1 || argc > argcmax) {
		printf("%% %s <address>/<bits> %s%s%s\n", cmdname,
		    msg ? "[" : "", msg ? msg : "", msg ? "]" : "");
		printf("%% %s <address>/<netmask> %s%s%s\n", cmdname,
		    msg ? "[" : "", msg ? msg : "", msg ? "]" : "");
		printf("%% no %s <address>[/bits]\n", cmdname);
		printf("%% no %s <address>[/netmask]\n", cmdname);
		return(0);
	}

	/* ignore 'address' keyword, don't print error */
	if (isprefix(argv[0], "address")) {
		argc--;
		argv++;
	}

	if (isprefix(argv[0], "dhcp")) {
		char *args[] = { PKILL, "dhclient", ifname, '\0' };
		char leasefile[sizeof(LEASEPREFIX)+1+IFNAMSIZ];

		if (set)
			cmdarg(DHCLIENT, ifname);
		else {
			cmdargs(PKILL, args);
			snprintf(leasefile, sizeof(leasefile), "%s.%s",
			    LEASEPREFIX, ifname);
			rmtemp(leasefile);
		}
		return(0);
	}

	ip = parse_ip(argv[0], NO_NETMASK);

	if (ip.family == 0)
		/* bad IP specified */
		return(0);

	if (ip.bitlen == -1) {
		printf("%% Netmask not specified\n");
		return(0);
	}
	
	if (argc == 2)
		if (!inet_aton(argv[1], &destbcast)) {
			printf("%% Invalid %s address\n", msg);
			return(0);
		}
	
	strlcpy(addreq.ifra_name, ifname, sizeof(addreq.ifra_name));
	strlcpy(ridreq.ifra_name, ifname, sizeof(ridreq.ifra_name));

	if (!set) {
		sin = (struct sockaddr_in *)&ridreq.ifra_addr;
		sin->sin_len = sizeof(ridreq.ifra_addr);
		sin->sin_family = AF_INET;
		sin->sin_addr.s_addr = ip.addr.sin.s_addr;
	}

	if (!alias || !set) {
		/*
		 * Here we remove the top IP on the interface before we
		 * might add another one, or we delete the specified IP.
		 */
		if (ioctl(ifs, SIOCDIFADDR, &ridreq) < 0)
			if (!set)
				printf("%% intip: SIOCDIFADDR: %s\n",
				    strerror(errno));
	}

	if (set) {
		sin = (struct sockaddr_in *)&addreq.ifra_addr;
		sin->sin_family = AF_INET;
		sin->sin_len = sizeof(addreq.ifra_addr);
		sin->sin_addr.s_addr = ip.addr.sin.s_addr;
		sin = (struct sockaddr_in *)&addreq.ifra_mask;
		sin->sin_family = AF_INET;
		sin->sin_len = sizeof(addreq.ifra_mask);
		sin->sin_addr.s_addr = htonl(0xffffffff << (32 - ip.bitlen));
		if (argc == 2) {
			sin = (struct sockaddr_in *)&addreq.ifra_dstaddr;
			sin->sin_family = AF_INET;
			sin->sin_len = sizeof(addreq.ifra_dstaddr);
			sin->sin_addr.s_addr = destbcast.s_addr;
		}
		if (ioctl(ifs, SIOCAIFADDR, &addreq) < 0)
			printf("%% intip: SIOCAIFADDR: %s\n", strerror(errno));
	}

	return(0);
}
/* Receive the response after sending a request.
 */
static int
receive_response(struct replay_thread *thr)
{
	const char *next;
	int line_len;
	long chunk_length, content_length;
	int chunked, connclose, failed;
	int n, status;

	content_length = 0;
	chunked = connclose = failed = 0;

	/* Read header */
	for (;;) {
		line_len = read_line(thr);
		if (line_len < 0)
			return (-1);
		thread_log(2, 0, "< %.*s", line_len, thr->line);
		if (line_len == 0)
			break;
		if (strncmp(thr->line, "HTTP", 4) == 0) {
			sscanf(thr->line, "%*s %d %*s\r\n", &status);
			failed = (status != 200);
		} else if (isprefix(thr->line, "content-length:", &next)) {
			content_length = strtol(next, NULL, 10);
		} else if (isprefix(thr->line, "transfer-encoding:", &next)) {
			chunked = (strcasecmp(next, "chunked") == 0);
		} else if (isprefix(thr->line, "connection:", &next)) {
			connclose = (strcasecmp(next, "close") == 0);
		}
	}

	thread_log(1, 0, "status: %d", status);

	/* Read body */
	if (chunked) {
		/* Chunked encoding, read size and bytes until no more */
		thread_log(1, 0, "chunked encoding");
		for (;;) {
			line_len = read_line(thr);
			if (line_len < 0)
				return (-1);
			/* read_line() guarantees null-termination */
			chunk_length = strtol(thr->line, NULL, 16);
			if (chunk_length == 0)
				break;
			if ((n = read_block(thr, chunk_length)) < 0)
				return (-1);
			if (n < chunk_length)
				thread_log(0, 0, "short read: %d/%ld",
				    n, chunk_length);
			thread_log(1, 0, "chunk length: %ld", chunk_length);
			thread_log(1, 0, "bytes read: %d", n);
			/* trailing CR LF */
			if ((n = read_line(thr)) < 0)
				return (-1);
		}
		/* trailing CR LF */
		n = read_line(thr);
		if (n < 0)
			return (-1);
	} else if (content_length > 0) {
		/* Fixed body size, read content_length bytes */
		thread_log(1, 0, "fixed length");
		thread_log(1, 0, "content length: %ld", content_length);
		if ((n = read_block(thr, content_length)) < 0)
			return (1);
		if (n < content_length)
			thread_log(0, 0, "short read: %d/%ld",
			    n, content_length);
		thread_log(1, 0, "bytes read: %d", n);
	} else {
		/* No body --> stop reading. */
		thread_log(1, 0, "no body");
		return (-1);
	}

	return (connclose);
}
示例#28
0
文件: ppp.c 项目: sthen/nsh
/* auth proto, auth name, auth key, peer proto, peer name, peer key, peer flag */
int
intsppp(char *ifname, int ifs, int argc, char **argv)
{
	struct sauthreq spa;
	struct ifreq ifr;
	int set, ch, i, cmd = 0;

	/* command options for 'auth' */
	static struct nopts authopts[] = {
		{ "proto",	req_arg,	'p' },
		{ "name",	req_arg,	'n' },
		{ "key",	req_arg,	'k' },
		{ NULL,		0,		0 }
	};

	/* command options for 'peer' */
	static struct nopts peeropts[] = {
		{ "proto",	req_arg,	'p' },
		{ "name",	req_arg,	'n' },
		{ "key",	req_arg,	'k' },
		{ "flag",	req_arg,	'f' },
		{ NULL,		0,		0 }
	};

	/* intsppp commands */
	struct intspppcmd {
		char *name;
		int cmd;
		void (*usage)();
		struct nopts *nopts;
	};
	struct intspppcmd intspppcmds[] = {
		{ "auth",	SPPPIOSMAUTH,	authusage,	authopts },
		{ "peer",	SPPPIOSHAUTH,	peerusage,	peeropts },
	};
	struct intspppcmd *isc = NULL;

	/* get rid of 'no' arg */
	if (NO_ARG(argv[0])) {
		set = 0;
		argc--;
		argv++;
	} else
		set = 1;

	/* point to right intspppcmd */
	for (i = 0; i < nitems(intspppcmds); i++) {
		if (isprefix(argv[0], intspppcmds[i].name))
			isc = &intspppcmds[i];
	}
	if (isc == NULL) {
		printf("%% intsppp: Internal error\n");
		return(0);
	}

	argc--;
	argv++;

	ifr.ifr_data = (caddr_t)&spa;

	/* setup spa */
	memset(&spa, 0, sizeof(spa));
	spa.cmd = isc->cmd;

	/* usage? */
	if (argc < 1 && set) {
		(*isc->usage)();
		return(0);
	}

	/* parse */
	noptind = 0;
	while ((ch = nopt(argc, argv, isc->nopts)) != -1)
		switch (ch) {
#define	__proto 1<<0
		case 'p':	/* proto */
			cmd |= __proto;
			for (i = 0; i < nitems(spppproto); i++) {
				if (isprefix(argv[noptind - 1],
				    spppproto[i].name))
					spa.proto = spppproto[i].type;
			}
			if (!spa.proto) {
				printf("%% Unknown proto: %s\n",
				    argv[noptind -1 ]);
				return(0);
			}
			break;
#define	__name	1<<1
		case 'n':	/* name */
			cmd |= __name;
			if (strlcpy(spa.name, argv[noptind - 1],
			    sizeof(spa.name)) >= sizeof(spa.name)) {
				printf("%% Name too long (> %lu): %s\n",
				    sizeof(spa.name), argv[noptind - 1]);
				return(0);
			}
			break;
#define	__key	1<<2
		case 'k':	/* key */
			cmd |= __key;
			if (strlcpy(spa.secret, argv[noptind - 1],
			    sizeof(spa.secret)) >= sizeof(spa.secret)) {
				printf("%% Key too long (> %lu): %s\n",
				    sizeof(spa.secret), argv[noptind - 1]);
				return(0);
			}
			break;
#define	__flag	1<<3
		case 'f':	/* flag */
			cmd |= __flag;
			if (isprefix(argv[noptind - 1], "callin")) {
				spa.flags = AUTHFLAG_NOCALLOUT;
			} else if (isprefix(argv[noptind - 1],
			    "norechallenge")) {
				spa.flags = AUTHFLAG_NORECHALLENGE;
			} else {
				printf("%% Unknown flag: %s",
			    argv[noptind - 1]);
				return(0);
			}
			break;
		default:
			printf("%% intsppp: nopt table error\n");
			return(0);
		}

	if (argc - noptind != 0) {
		/* leftover salmon */
		printf("%% %s", nopterr);
		if (argv[noptind])
			printf(": %s", argv[noptind]);
		printf("\n");
		(*isc->usage)();
		return(0);
	}

	if (argc < 1)
		cmd = __proto | __name | __key | __flag;

	if (!set) {
		spa.proto = 0;
		spa.flags = 0;
		memset(&spa.name, '\0', sizeof(spa.name));
		memset(&spa.secret, '\0', sizeof(spa.secret));
	}

	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	if (ioctl(ifs, SIOCSSPPPPARAMS, &ifr) == -1) {
		printf("%% intspppproto: SIOCSSPPPPARAMS: "
		    "SPPPIOS%sAUTH: %s\n", strerror(errno),
		    isc->cmd == SPPPIOSMAUTH ? "M" : "H");
		return 0;
	}
	if (cmd & __key) {
		char type[TYPESZ];

		snprintf(type, TYPESZ, "%skey", isc->name);
		db_delete_flag_x_ctl(type, ifname);
		if (set) {
			db_insert_flag_x(type, ifname, 0, DB_X_ENABLE,
			    spa.secret);
		}
	}

	return 0;
}
static void *
replay_thread(void *arg)
{
	struct iovec iov[6];
	char space[1] = " ", crlf[2] = "\r\n";
	struct replay_thread *thr = arg;
	struct message *msg;
	enum shmlogtag tag;
	size_t len;
	char *ptr;
	const char *next;

	int i;

	int reopen = 1;

	while ((msg = mailbox_get(&thr->mbox)) != NULL) {
		tag = msg->tag;
		len = msg->len;
		ptr = msg->ptr;

		thread_log(2, 0, "%s(%s)", VSL_tags[tag], msg->ptr);

		switch (tag) {
		case SLT_RxRequest:
			if (thr->method != NULL)
				thr->bogus = 1;
			else
				thr->method = trimline(thr, ptr);
			break;

		case SLT_RxURL:
			if (thr->url != NULL)
				thr->bogus = 1;
			else
				thr->url = trimline(thr, ptr);
			break;

		case SLT_RxProtocol:
			if (thr->proto != NULL)
				thr->bogus = 1;
			else
				thr->proto = trimline(thr, ptr);
			break;

		case SLT_RxHeader:
			if (thr->nhdr >= sizeof thr->hdr / sizeof *thr->hdr) {
				thr->bogus = 1;
			} else {
				thr->hdr[thr->nhdr++] = trimline(thr, ptr);
				if (isprefix(ptr, "connection:", &next))
					thr->conn = trimline(thr, next);
			}
			break;

		default:
			break;
		}

		freez(msg->ptr);
		freez(msg);

		if (tag != SLT_ReqEnd)
			continue;

		if (!thr->method || !thr->url || !thr->proto) {
			thr->bogus = 1;
		} else if (strcmp(thr->method, "GET") != 0 &&
		    strcmp(thr->method, "HEAD") != 0) {
			thr->bogus = 1;
		} else if (strcmp(thr->proto, "HTTP/1.0") == 0) {
			reopen = !(thr->conn &&
			    strcasecmp(thr->conn, "keep-alive") == 0);
		} else if (strcmp(thr->proto, "HTTP/1.1") == 0) {
			reopen = (thr->conn &&
			    strcasecmp(thr->conn, "close") == 0);
		} else {
			thr->bogus = 1;
		}

		if (thr->bogus) {
			thread_log(1, 0, "bogus");
			goto clear;
		}

		if (thr->sock == -1) {
			for (;;) {
				thread_log(1, 0, "sleeping before connect...");
				usleep(1000 * (thr->fd % 3001));
				if ((thr->sock = VSS_connect(addr_info)) >= 0)
					break;
				thread_log(0, errno, "connect failed");
			}
		}

		thread_log(1, 0, "%s %s %s", thr->method, thr->url, thr->proto);

		iov[0].iov_base = thr->method;
		iov[0].iov_len = strlen(thr->method);
		iov[2].iov_base = thr->url;
		iov[2].iov_len = strlen(thr->url);
		iov[4].iov_base = thr->proto;
		iov[4].iov_len = strlen(thr->proto);
		iov[1].iov_base = iov[3].iov_base = space;
		iov[1].iov_len = iov[3].iov_len = 1;
		iov[5].iov_base = crlf;
		iov[5].iov_len = 2;
		if (writev(thr->sock, iov, 6) == -1) {
			thread_log(0, errno, "writev()");
			goto close;
		}

		for (i = 0; i < thr->nhdr; ++i) {
			thread_log(2, 0, "%d %s", i, thr->hdr[i]);
			iov[0].iov_base = thr->hdr[i];
			iov[0].iov_len = strlen(thr->hdr[i]);
			iov[1].iov_base = crlf;
			iov[1].iov_len = 2;
			if (writev(thr->sock, iov, 2) == -1) {
				thread_log(0, errno, "writev()");
				goto close;
			}
		}
		if (write(thr->sock, crlf, 2) == -1) {
			thread_log(0, errno, "writev()");
			goto close;
		}
		if (receive_response(thr) || reopen) {
close:
			thread_log(1, 0, "close");
			assert(thr->sock != -1);
			close(thr->sock);
			thr->sock = -1;
		}

		sleep(1);
clear:
		/* clean up */
		thread_clear(thr);
	}

	/* leftovers */
	thread_clear(thr);

	return (0);
}
示例#30
0
文件: conf.c 项目: sthen/nsh
void conf_interfaces(FILE *output, char *only)
{
	int ifs, flags, ippntd, br;
	char *lladdr;
	char ifdescr[IFDESCRSIZE];

	struct if_nameindex *ifn_list, *ifnp;
	struct ifreq ifr, ifrdesc;
	struct if_data if_data;

	if ((ifn_list = if_nameindex()) == NULL) {
		printf("%% conf_interfaces: if_nameindex failed\n");
		return;
	}

	if ((ifs = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		printf("%% conf_interfaces socket: %s\n", strerror(errno));
		if_freenameindex(ifn_list);
		return;
	}

	for (ifnp = ifn_list; ifnp->if_name != NULL; ifnp++) {
		if (only && !isprefix(only, ifnp->if_name))
			/* only display interfaces which start with ... */
			continue;
		if (!only && islateif(ifnp->if_name))
			/* interface prefixes to exclude on generic run */
			continue;

		strlcpy(ifr.ifr_name, ifnp->if_name, sizeof(ifr.ifr_name));

		if (ioctl(ifs, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
			printf("%% conf: SIOCGIFFLAGS: %s\n", strerror(errno));
			continue;
		}
		flags = ifr.ifr_flags;

		ifr.ifr_data = (caddr_t)&if_data;
		if (ioctl(ifs, SIOCGIFDATA, (caddr_t)&ifr) < 0) {
			printf("%% conf: SIOCGIFDATA: %s\n", strerror(errno));
			continue;
		}

		/* The output order is important! */

		/* set interface/bridge mode */
		if (!(br = is_bridge(ifs, ifnp->if_name)))
			br = 0;
		fprintf(output, "%s %s\n", br ? "bridge" : "interface",
		    ifnp->if_name);

		/*
		 * description, if available
		 * copied straight from ifconfig.c
		 */
		memset(&ifrdesc, 0, sizeof(ifrdesc));
		strlcpy(ifrdesc.ifr_name, ifnp->if_name,
		    sizeof(ifrdesc.ifr_name));
		ifrdesc.ifr_data = (caddr_t)&ifdescr;
		if (ioctl(ifs, SIOCGIFDESCR, &ifrdesc) == 0 &&
		    strlen(ifrdesc.ifr_data))
			fprintf(output, " description %s\n", ifrdesc.ifr_data);

		if ((lladdr = get_hwdaddr(ifnp->if_name)) != NULL)
			conf_db_single(output, "lladdr", lladdr, ifnp->if_name);
		conf_db_single(output, "rtadvd", NULL, ifnp->if_name);

		conf_vnetid(output, ifs, ifnp->if_name);
		conf_parent(output, ifs, ifnp->if_name);
		conf_rdomain(output, ifs, ifnp->if_name);
		conf_intrtlabel(output, ifs, ifnp->if_name);
		conf_intgroup(output, ifs, ifnp->if_name);
		conf_carp(output, ifs, ifnp->if_name);

		ippntd = conf_ifaddr_dhcp(output, ifnp->if_name, flags);

		if (br) {
			conf_brcfg(output, ifs, ifn_list, ifnp->if_name);
		} else {
			char tmp[24];

			conf_media_status(output, ifs, ifnp->if_name);
			conf_ifmetrics(output, ifs, if_data, ifnp->if_name);
			conf_keepalive(output, ifs, ifnp->if_name);
			conf_pfsync(output, ifs, ifnp->if_name);
			conf_trunk(output, ifs, ifnp->if_name);
			conf_pflow(output, ifs, ifnp->if_name);
			conf_ifxflags(output, ifs, ifnp->if_name);
			if (timeslot_status(ifs, ifnp->if_name, tmp,
			    sizeof(tmp)) == 1) 
				fprintf(output, " timeslots %s\n", tmp);
			if (conf_dhcrelay(ifnp->if_name, tmp, sizeof(tmp))
			    > 0)
				fprintf(output, " dhcrelay %s\n", tmp);
			conf_sppp(output, ifs, ifnp->if_name);
			conf_pppoe(output, ifs, ifnp->if_name);
		}
		conf_ifflags(output, flags, ifnp->if_name, ippntd);
	}
	close(ifs);
	if_freenameindex(ifn_list);
}