Click here to Skip to main content
15,893,381 members
Articles / Desktop Programming / MFC

Internet Traffic Firewall and Sniffer

Rate me:
Please Sign up or sign in to vote.
4.91/5 (26 votes)
23 Oct 2007GPL33 min read 101.1K   9.1K   133  
The article demonstrates internet packets interception with firewall capabilities based on IpFilterDriver driver and sending TCP/UDP/ICMP packets using raw sockets with IP spoofing support.
// DlgUDP.cpp : implementation file
//

#include "stdafx.h"
#include "Sniffer.h"
#include "DlgUDP.h"
#include ".\dlgudp.h"

#include "SnifferDoc.h"


// CDlgUDP dialog

IMPLEMENT_DYNAMIC(CDlgUDP, CDialog)
CDlgUDP::CDlgUDP(CDocument *p1, CWnd* pParent /*=NULL*/)
                : CDialog(CDlgUDP::IDD, pParent)
                , ip4ver(4)
                , ip4hdl(5)
                , ip4delay(FALSE)
                , ip4thru(FALSE)
                , ip4rely(FALSE)
                , ip4precede(0)
                , ip4length(0)
                , ip4ident(0)
                , ip4mayfrgm(FALSE)
                , ip4lastfrgm(FALSE)
                , ip4frgmoffs(0)
                , ip4ttl(255)
                , ip4proto(17)
                , ip4saddr(_T(""))
                , ip4daddr(_T(""))
                , udpsport(0)
                , udpdport(0)
                , udplength(0)
                , udpdata(_T(""))
{
        pDoc = p1;
}

CDlgUDP::~CDlgUDP()
{
}

void CDlgUDP::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        DDX_Text(pDX, IDC_EDIT_VER, ip4ver);
        DDV_MinMaxUInt(pDX, ip4ver, 0, 15);
        DDX_Text(pDX, IDC_EDIT_HDL, ip4hdl);
        DDV_MinMaxUInt(pDX, ip4hdl, 0, 15);
        DDX_Check(pDX, IDC_CHECK_DELAY, ip4delay);
        DDX_Check(pDX, IDC_CHECK_THRU, ip4thru);
        DDX_Check(pDX, IDC_CHECK_RELY, ip4rely);
        DDX_CBIndex(pDX, IDC_COMBO_PRECEDE, ip4precede);
        DDX_Control(pDX, IDC_COMBO_PRECEDE, m_precedeCombo);
        DDX_Text(pDX, IDC_EDIT_LEN, ip4length);
        DDV_MinMaxUInt(pDX, ip4length, 0, 65535);
        DDX_Text(pDX, IDC_EDIT_IDENT, ip4ident);
        DDV_MinMaxUInt(pDX, ip4ident, 0, 65535);
        DDX_Check(pDX, IDC_CHECK_FRAGM, ip4mayfrgm);
        DDX_Check(pDX, IDC_CHECK_FRAGMLAST, ip4lastfrgm);
        DDX_Text(pDX, IDC_EDIT_FOFFS, ip4frgmoffs);
        DDV_MinMaxUInt(pDX, ip4frgmoffs, 0, 8191);
        DDX_Text(pDX, IDC_EDIT_TTL, ip4ttl);
        DDV_MinMaxUInt(pDX, ip4ttl, 0, 255);
        DDX_Text(pDX, IDC_EDIT_PROTO, ip4proto);
        DDX_Text(pDX, IDC_EDIT_SIP, ip4saddr);
        DDX_Text(pDX, IDC_EDIT_DIP, ip4daddr);
        DDX_Text(pDX, IDC_EDIT_SPORT, udpsport);
        DDV_MinMaxUInt(pDX, udpsport, 0, 65535);
        DDX_Text(pDX, IDC_EDIT_DPORT, udpdport);
        DDV_MinMaxUInt(pDX, udpdport, 0, 65535);
        DDX_Text(pDX, IDC_EDIT_UDPLEN, udplength);
        DDV_MinMaxUInt(pDX, udplength, 0, 65535);
        DDX_Text(pDX, IDC_EDIT_DATA, udpdata);
}


BEGIN_MESSAGE_MAP(CDlgUDP, CDialog)
        ON_WM_CLOSE()
        ON_BN_CLICKED(IDC_SENDUDP_BUTTON, OnBnClickedSendudpButton)
END_MESSAGE_MAP()


// CDlgUDP message handlers

void CDlgUDP::OnClose()
{
        ShowWindow(SW_HIDE);
        //CDialog::OnClose();
}

BOOL CDlgUDP::OnInitDialog()
{
        CDialog::OnInitDialog();

        // TODO:  Add extra initialization here
        /*  char name[NI_MAXHOST];
         if(gethostname(name,NI_MAXHOST) == 0)
         {
        hostent *host = gethostbyname(name);
         if(host)
                 {
              	   in_addr ip = *((in_addr *)host->h_addr_list[0]);
             m_srcip = inet_ntoa(ip);
              	    UpdateData(false);
                 }
         }*/

        RECT cr;
        m_precedeCombo.GetClientRect(&cr);
        m_precedeCombo.SetWindowPos(0, 0, 0, cr.right, cr.bottom + 12*10, SWP_NOMOVE | SWP_NOZORDER);


        return TRUE;  // return TRUE unless you set the focus to a control
        // EXCEPTION: OCX Property Pages should return FALSE
}



void CDlgUDP::OnBnClickedSendudpButton()
{
        UpdateData();
        STATIC_DOWNCAST(CSnifferDoc, pDoc)->SendUDP();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Engineer
Russian Federation Russian Federation
Highly skilled Engineer with 14 years of experience in academia, R&D and commercial product development supporting full software life-cycle from idea to implementation and further support. During my academic career I was able to succeed in MIT Computers in Cardiology 2006 international challenge, as a R&D and SW engineer gain CodeProject MVP, find algorithmic solutions to quickly resolve tough customer problems to pass product requirements in tight deadlines. My key areas of expertise involve Object-Oriented
Analysis and Design OOAD, OOP, machine learning, natural language processing, face recognition, computer vision and image processing, wavelet analysis, digital signal processing in cardiology.

Comments and Discussions