Subject: chksniff
To: None <tech-userlevel@NetBSD.org>
From: Douwe Kiela <virtus@wanadoo.nl>
List: tech-userlevel
Date: 03/22/2003 17:00:40
Hey, I wrote a small tool to check if network interfaces are in promiscuous
mode,called chksniff. Is there interest for something like this in NetBSD's
src/usr.bin or whatever? I've seen things in there like 'chkpath' and such,
so I tought perhaps this would be a good additions to it.

By the way, other names are also welcome, 'chkprom','chksniff',I don't care
:-)

Greetings,
Douwe

------------------------------------
/*
Copyright (c) 2002 Douwe H. Kiela
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author or Sepulcrum may not be used to endorse or promote
   products derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

#define MAX 1024		/* Buffer length */
#define MAX_IF 8		/* A max of 8 interfaces to be checked */

int
main(void)
{				/* we don't take any arguments */
	int             fd, num;
	struct ifreq    ifr[MAX];	/* The actual info */
	struct ifconf   ifc;
	struct sockaddr_in *sin;

	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {	/* We open the socket
to
								 * use ioctl on */
		perror("socket");
		exit(EXIT_FAILURE);
	}
	ifc.ifc_len = sizeof(ifr);	/* The length of the buffer */
	ifc.ifc_buf = (caddr_t) ifr;	/* The buffer itself */

	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc)) {	/* The ioctl call the
							 * get all interfaces */
		perror("ioctl SIOCGIFCONF");
		close(fd);
		exit(EXIT_FAILURE);
	}
	num = ifc.ifc_len / sizeof(struct ifreq);	/* The number of
							 * interfaces found */



	if (num <= 0) {
		printf("chksniff: No interfaces found\n");
		close(fd);
		exit(EXIT_FAILURE);
	}
	while (num-- > 0) {
		if (ioctl(fd, SIOCGIFADDR, &ifr[num]) != 0) {
			continue;
		}

		sin = (struct sockaddr_in *)&ifr[num].ifr_addr;
		sin->sin_family=AF_INET;

		printf("%s [%s]:\t", ifr[num].ifr_name, inet_ntoa(sin->sin_addr));

		if (ifr[num].ifr_flags & IFF_PROMISC)
			printf("Promiscous\n");
		else
			printf("Normal\n");

	}

	close(fd);

	return 0;
}