示例#1
0
文件: asn.c 项目: CoolerVoid/squid
static int
printRadixNode(struct squid_radix_node *rn, void *w)
{
    StoreEntry *sentry = w;
    rtentry *e = (rtentry *) rn;
    intlist *q;
    as_info *asinfo;
    struct in_addr addr;
    struct in_addr mask;
    assert(e);
    assert(e->e_info);
    (void) get_m_int(addr.s_addr, e->e_addr);
    (void) get_m_int(mask.s_addr, e->e_mask);
    storeAppendPrintf(sentry, "%15s/%d\t",
	inet_ntoa(addr), mask_len(ntohl(mask.s_addr)));
    asinfo = e->e_info;
    assert(asinfo->as_number);
    for (q = asinfo->as_number; q; q = q->next)
	storeAppendPrintf(sentry, " %d", q->i);
    storeAppendPrintf(sentry, "\n");
    return 0;
}
示例#2
0
int main(int argc, char **argv)
{
	struct fwr rule;
	int fd, ret, f = 0;

	exec = argv[0];
	if (argc < 2) {
		print_usage();
		return -1;
	}

	if (!strcmp(argv[1], "mknod") && argc == 2) {
		if (mknod(IPFIREWALL_DEV, S_IFCHR, makedev(IPFIREWALL_MAJOR, 0)) < 0) {
			perror("mknod " IPFIREWALL_DEV);
			return -1;
		}

		return 0;
	}

	if (!strcmp(argv[1], "unlink") && argc == 2) {
		if (unlink(IPFIREWALL_DEV) < 0) {
			perror("unlink " IPFIREWALL_DEV);
			return -1;
		}

		return 0;
	}

	if (!strcmp(argv[1], "insmod") && argc == 2) {
		if (system_check("insmod " IPFIREWALL_MOD IPFIREWALL_MOD_EXT) < 0)
			return -1;

		return 0;
	}

	if (!strcmp(argv[1], "rmmod") && argc == 2) {
		if (system_check("rmmod " IPFIREWALL_MOD) < 0)
			return -1;

		return 0;
	}

	fd = open(IPFIREWALL_DEV, O_RDWR);
	if (fd < 0) {
		perror("open " IPFIREWALL_DEV);
		return -1;
	}

	if (!strcmp(argv[1], "test") && argc == 2) {
		close(fd);
		return 0;
	}

	if (!strcmp(argv[1], "invalid") && argc == 2) {
		if (ioctl(fd, 42, NULL) < 0) {
			if (errno != ENOTTY) {
				perror("ioctl 42");
				close(fd);
				return -1;
			}

			close(fd);
			return 0;
		}

		perror("ioctl 42");
		close(fd);
		return -1;
	}

	if (!strcmp(argv[1], "enable") && argc == 2) {
		if (ioctl(fd, FW_ENABLE, NULL) < 0) {
			perror("ioctl FW_ENABLE");
			close(fd);
			return -1;
		}

		close(fd);
		return 0;
	}

	if (!strcmp(argv[1], "disable") && argc == 2) {
		if (ioctl(fd, FW_DISABLE, NULL) < 0) {
			perror("ioctl FW_DISABLE");
			close(fd);
			return -1;
		}

		close(fd);
		return 0;
	}

	if ((!strcmp(argv[1], "add") || !strcmp(argv[1], "find")) && argc == 6) {
		parse_ip(argv[2], &rule.ip_src, &rule.ip_src_mask);
		parse_ip(argv[3], &rule.ip_dst, &rule.ip_dst_mask);
		parse_range(argv[4], &rule.port_src[0], &rule.port_src[1]);
		parse_range(argv[5], &rule.port_dst[0], &rule.port_dst[1]);

		if (argv[1][0] == 'a') {
			/* add rule */
			if (ioctl(fd, FW_ADD_RULE, &rule) < 0) {
				perror("ioctl FW_ADD_RULE");
				close(fd);
				return -1;
			}

			close(fd);
			return 0;
		}

		f = 1;
	}

	if (f || (!strcmp(argv[1], "list") && argc == 2)) {
		struct fwr *fwr = NULL;
		int size, i;

		do {
			size = 0;

			if (ioctl(fd, FW_LIST, &size) < 0 && errno != ENOSPC) {
				perror("ioctl FW_LIST");
				close(fd);
				return -1;
			}

			fwr = (struct fwr *) realloc(fwr, size * sizeof(struct fwr));
			if (!fwr) {
				fprintf(stderr, "out of memory\n");
				close(fd);
				return -1;
			}
			*(int*) fwr = size;

			size = ioctl(fd, FW_LIST, fwr);
			if (size < 0 && errno != ENOSPC) {
				perror("ioctl FW_LIST");
				close(fd);
				return -1;
			}
		} while (size < 0);

		close(fd);

		if (f) {
			/* find rule */
			ret = -1;
			for (i = 0; i < size && ret; i++)
				if (!memcmp(&fwr[i], &rule, sizeof(struct fwr)))
					ret = 0;
			if (ret)
				fprintf(stderr, "rule not found\n");
		} else {
			/* list rules */
			ret = size;
			for (i = 0; i < size; i++)
				printf(NIPQUAD_FMT "/%d " NIPQUAD_FMT "/%d %d:%d %d:%d\n",
					NIPQUAD(fwr[i].ip_src), mask_len(fwr[i].ip_src_mask),
					NIPQUAD(fwr[i].ip_dst), mask_len(fwr[i].ip_dst_mask),
					ntohs(fwr[i].port_src[0]), ntohs(fwr[i].port_src[1]),
					ntohs(fwr[i].port_dst[0]), ntohs(fwr[i].port_dst[1]));
		}

		if (fwr)
			free(fwr);

		return ret;
	}

	print_usage();

	close(fd);
	return -1;
}