home *** CD-ROM | disk | FTP | other *** search
- /* this is one */
- /* it prints to stdout everything, it sniffes */
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <net/if.h>
- #include <unistd.h>
- #include <signal.h>
- #include <stdio.h>
-
- static volatile int done;
-
- void handler(int signum)
- {
- done = 1;
- }
-
- int main(int argc, char **argv)
- {
- char buff[0x10000];
- struct ifreq ifr;
- int s, n;
-
- if (argc < 2)
- {
- fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
- return 1;
- }
-
- s = socket(PF_INET, SOCK_PACKET, htons(0x0003));
- if (s == -1)
- {
- perror("socket");
- return 1;
- }
-
- strcpy(ifr.ifr_name, argv[1]);
-
- if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
- {
- perror("ioctl(SIOCGIFFLAGS)");
- return 1;
- }
-
- ifr.ifr_flags |= IFF_PROMISC;
-
- if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
- {
- perror("ioctl(SIOCGIFFLAGS)");
- return 1;
- }
-
- signal(SIGINT, handler);
-
- puts("starting capturing:\n");
- fflush(stdout);
- for (done = 0; !done; )
- {
- n = read(s, buff, sizeof(buff));
- if ( n!=-1 ) write(STDOUT_FILENO, buff, n);
- }
-
- ifr.ifr_flags &= ~IFF_PROMISC;
-
- if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
- {
- perror("ioctl(SIOCGIFFLAGS)");
- return 1;
- }
-
- close(s);
- printf("Finished\n");
-
-
- return 0;
- }
-
-