All pastes #29338 Raw Edit

Miscellany

public text v1 · immutable
#29338 ·published 2005-11-19 20:34 UTC
rendered paste body
//This is a work in progress!
//The irctranslate program will serve to translate any speech from IRC to google's translator
//Then this new language will be displaid in the program's window
//Released under the Creative Commons Attribution-NonCommercial-ShareAlike 2.5 licence
//Comments are welcome! drzigman@bellsouth.net
//Shouts to the Stankdawg, LogosX, tehbizz, rmpants, tiocsti, and all the members of the DDP
//-Dr^ZigMan
#include <stdio.h>
#include <pcap.h>
#include <unistd.h>
#include <string.h>
#define SIZE 512

void command_line(int argc,char *argv[],char device[],char filter_string[],char logfile[]);

int main(int argc, char *argv[])
{
	//Declare variables
	//pcap related variables
	char device[SIZE];			//Holds the name of the device I'm using
	pcap_t *handle;				//Session handle for pcap	
	char errbuff[SIZE];			//Holds error messages for pcap
	struct bpf_program filter;		//Where the compiled filter is held
	bpf_u_int32 ip;				//IP of sniffing device	
	char filter_string[SIZE];		//Holds the string form of the filter

	//IRC related variables
	char logfile[SIZE];			//Hold the name of the logfile for output	

	//Google related variables

	//Program display related variables

	//handles command line arguments
	command_line(argc,argv,device,filter_string,logfile);

	//open device for sniffing
	handle = pcap_open_live(device, BUFSIZ, 0, 0, errbuff);  //using device, with a buffer size of buffsize, do not set promis, no timeout, store errors in errbuff
	if (handle == NULL)
	{
		printf("\nFailed to open device %s, %s\n", device, errbuff);
		exit(2);
	}
	else
	{
		printf("Opened %s succsefully!\n", device);
	}

	//filter out all but the IRC traffic
	if ((pcap_compile(handle, &filter, filter_string, 0, ip)) == -1)  //Using handle, store in struct filter, the compiled FILTER_STRING, no optimization, using ip 
	{
		printf("Error with filter \"%s\": %s\n", filter_string, pcap_geterr(handle));
	}
	else
	{
		printf("Filter \"%s\" compiled succsfully!...\n",filter_string);
	}

	//Actual sniffing

	//parse out string to translate

	//send to google for translation

	//get back from google's translation

	//display to the user
}

void command_line(int argc,char *argv[],char device[],char filter_string[],char logfile[])
{
	char usage[]=	"Usage: irctranslate -i <interface> -p <irc port> -l <log file>\n\n";

	int c;
	int errflag = 0;			//Hold if I need to print usage

	printf("\n");
	//handles the options using getopt()
	do
	{
		c = getopt(argc, argv, ":hi:p:l:");

		switch (c)
		{
			case 'h' :	errflag++;
					break;
	
			case 'i' :	strcpy(device,optarg);
					printf("Sniffing for IRC traffic on %s...\n",device);
					break;

			case 'l' :	strcpy(logfile,optarg);
					printf("Using log file: %s\n",logfile);
					break;
			
			case 'p' :	strcpy(filter_string,"tcp port ");
					strcat(filter_string, optarg);
					printf("Assuming port %s for IRC...\n",optarg);
					break;

					case ':' :	printf("Option -%c requires an argument\n",optopt);
							errflag++;
							break;

			case '?' :	printf("Invaild option: -%c\n",optopt);
					errflag++;
					break;

			//default :	errflag++;
		}
	} while (c != -1);

	if (errflag)
	{
		printf(usage);
		exit(2);
	}

	return;
}