All pastes #614682 Raw Edit

Untitled

public text v1 · immutable
·published 2007-07-11 18:40 UTC
rendered paste body
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "sniff.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TmForm *mForm;
//---------------------------------------------------------------------------
__fastcall TmForm::TmForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TmForm::FormCreate(TObject *Sender)
{
        DWORD dwThreadId;
        this->mStrGrid->Cells[0][0]="To Channel/Nick";
        this->mStrGrid->Cells[1][0]="SourceIp";
        this->mStrGrid->Cells[2][0]="Message";
        this->mStrGrid->ColWidths[2]=500;
        frm=this;
        CreateThread(0,0,sniffThread,NULL,0,&dwThreadId);
}
//---------------------------------------------------------------------------






sniff.h




#include "winsock2.h"
#include "mstcpip.h"
#include "SyncObjs.hpp"
#include "unit1.h"
#include "parser.h"

TmForm *frm;

char *filter(char *buf);
String str;

DWORD _stdcall sniffThread(LPVOID param){
        SOCKET           sock;
        WSADATA          data;
        HOSTENT          *host;
        SOCKADDR_IN      addr;
        unsigned long    flag=1;
        char             buf[1024];
        char             *filtered;
        TCriticalSection *sect=new TCriticalSection;
        char    name[255];
        if(WSAStartup(MAKEWORD(2,2),&data)!=0){
                MessageBox(0,"Cant load library","Error",MB_ICONERROR);
                return 0;
        }
        sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP);
        if(sock==NULL){
                MessageBox(0,"Cant create socket","Error",MB_ICONERROR);
                return 0;
        }
        gethostname(name,255);
        host=gethostbyname(name);
        addr.sin_family=AF_INET;
        addr.sin_addr.S_un=((in_addr *)host->h_addr_list[0])->S_un;
        bind(sock,(sockaddr*)&addr,sizeof(addr));
        ioctlsocket(sock,SIO_RCVALL,&flag);
        while(1){
           recv(sock,buf,1024,0);
           filtered=filter(buf);
           if(filtered!=NULL){
                if(splitBuf(filtered)){
                        sect->Enter();
                                frm->mStrGrid->Cells[0][frm->mStrGrid->RowCount-1]=split.nick;
                                frm->mStrGrid->Cells[1][frm->mStrGrid->RowCount-1]=str;
                                frm->mStrGrid->Cells[2][frm->mStrGrid->RowCount-1]=split.message;
                                frm->mStrGrid->RowCount++;
                        sect->Leave();
                }
           }
           memset(buf,0,1024);
        }
}


char *filter(char *buf){
        char asdf[10];
        char *ip;
        int size=buf[0] & 0xF;
        size=size*4;
        ip=buf+12;
        str.sprintf("%d.%d.%d.%d",ip[0] & 0xFF,ip[1] & 0xFF,ip[2]& 0xFF,ip[3]& 0xFF);
        char *msg=buf+size;
        int len;
        String str;
        if((buf[9] & 0xFF )!=6){
                return NULL;
        }
        msg=msg+20;
        if(strstr(msg,"PRIVMSG")==NULL){
                return NULL;
        }
        return msg;
}






parser.h



#include "stdio.h"
#include "conio.h"
#include "string.h"
#define Len 255


struct message{
        char            nick[Len];
        char            host[Len];
        char            message[Len];
};





struct message  split;
//------------------------------------------------------------------------------
                        /*Split function*/
//------------------------------------------------------------------------------
bool splitBuf(char *buf){
        char            *mes;
        char            *nick;
        int             len;
        char            *str;
        memset(split.nick,0,Len);
        memset(split.host,0,Len);
        memset(split.message,0,Len);
        mes=strchr(buf+1,':');
        if(mes==NULL){
                return false;
        }
        len=mes-buf;
        strcpy(split.message,mes+1);
        nick=strstr(buf,"PRIVMSG") ;
        nick+=8;
        len=mes-nick;
        strncpy(split.nick,nick,len);
return true;
}