示例#1
0
static int print_general_info(libmtd_t libmtd, const struct mtd_info *mtd_info,
			      int all)
{
	int i, err, first = 1;
	struct mtd_dev_info mtd;

	printf("Count of MTD devices:           %d\n", mtd_info->mtd_dev_cnt);
	if (mtd_info->mtd_dev_cnt == 0)
		return 0;

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		err = mtd_get_dev_info1(libmtd, i, &mtd);
		if (err == -1) {
			if (errno == ENODEV)
				continue;
			return sys_errmsg("libmtd failed to get MTD device %d "
					  "information", i);
		}

		if (!first)
			printf(", mtd%d", i);
		else {
			printf("Present MTD devices:            mtd%d", i);
			first = 0;
		}
	}
	printf("\n");
	printf("Sysfs interface supported:      %s\n",
	       mtd_info->sysfs_supported ? "yes" : "no");

	if (!all)
		return 0;

	printf("\n");

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		if (!mtd_dev_present(libmtd, i))
			continue;
		err = print_dev_info(libmtd, mtd_info, i);
		if (err)
			return err;
	}

	return 0;
}
示例#2
0
int main(int argc, char * const argv[])
{
	int err;
	libmtd_t libmtd;
	struct mtd_info mtd_info;

	err = parse_opt(argc, argv);
	if (err)
		return -1;

	libmtd = libmtd_open();
	if (libmtd == NULL) {
		if (errno == 0)
			return errmsg("MTD is not present in the system");
		return sys_errmsg("cannot open libmtd");
	}

	err = mtd_get_info(libmtd, &mtd_info);
	if (err) {
		if (errno == ENODEV)
			return errmsg("MTD is not present");
		return sys_errmsg("cannot get MTD information");
	}

	if (!args.all && args.node) {
		int mtdn;

		/*
		 * A character device was specified, translate this to MTD
		 * device number.
		 */
		mtdn = translate_dev(libmtd, args.node);
		if (mtdn < 0)
			goto out_libmtd;
		err = print_dev_info(libmtd, &mtd_info, mtdn);
	} else
		err = print_general_info(libmtd, &mtd_info, args.all);
	if (err)
		goto out_libmtd;

	libmtd_close(libmtd);
	return 0;

out_libmtd:
	libmtd_close(libmtd);
	return -1;
}
示例#3
0
int
main(int argc, char *argv[])
{
	pcap_if_t *all_devs;
	pcap_if_t *dev;
	char err_buf[PCAP_ERRBUF_SIZE];
	char dev_name[16];

	pcap_t *handle;			/* Session handle */
	bpf_u_int32 mask;
	bpf_u_int32 net;
	struct pcap_pkthdr header;	/* The header that pcap gives us */
	const u_char *packet;		/* The actual packet */



	/* Find all the devices */
	if (pcap_findalldevs(&all_devs, err_buf) == -1) {
		fprintf(stderr, "Error in pcap_findalldevs(): %s\n", err_buf);
		exit(EXIT_FAILURE);
	}

	/* Loop all the devices */
	printf("\nDevices in this computer:\n");
	for (dev = all_devs; dev != NULL; dev = dev->next)
		print_dev_info(dev);

	printf("Please enter the device name which you want to open:\n>> ");
	fgets(dev_name, sizeof(dev_name), stdin);
	/* Remove the '\n' in the buf */
	dev_name[strlen(dev_name) - 1] = '\0';

	/* Get the net id and the mask */
	if (pcap_lookupnet(dev_name, &net, &mask, err_buf) == -1) {
		fprintf(stderr, "Can't get netmask for device %s: %s\n",
			dev_name, err_buf);
		net = 0;
		mask = 0;
	}

	/* Open the device */
	handle = pcap_open_live(dev_name, 65535, 1, 0, err_buf);
	if (handle == NULL) {
		fprintf(stderr, "Couldn't open device %s: %s\n",
			dev_name, err_buf);
		exit(EXIT_FAILURE);
	}
/*
	if (pcap_datalink(handle) != DLT_EN10MB) {
		fprintf(stderr, "Device %s doesn't provide Ethernet headers - "
			"not supported\n", dev_name);
		exit(EXIT_FAILURE);
	}
*/

	printf("\nCapture Device: %s\n", dev_name);

	printf("------------------------------\n\n");


	int number = 0;
	while (1) {

		/* Grab a packet */
		packet = pcap_next(handle, &header);

		if (packet == NULL)
			continue;

		printf("Packet length: %d\n", header.len);
		printf("Number of bytes: %d\n", header.caplen);
		printf("Recieved time: %s\n",
			ctime((const time_t *) &(header.ts.tv_sec)));

		++number;
		if (number >= 10)
			break;
	}


	/* */
	number = 0;
	pcap_loop(handle, -1, got_packet, (u_char *) &number); 


	/* Close device */
	pcap_close(handle);

	return 0;
}