Click here to Skip to main content
15,886,873 members
Articles / Desktop Programming / MFC

DNS if u need it

Rate me:
Please Sign up or sign in to vote.
4.11/5 (9 votes)
1 Oct 2002 109.5K   1.9K   20  
Simple quering DNS servers for retrieve information about host's DNS records
// dgrammView.cpp : implementation of the CDgrammView class
//

#include "stdafx.h"
#include "dgramm.h"

#include "dgrammDoc.h"
#include "dgrammView.h"
#include "dnsresolver.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDgrammView

IMPLEMENT_DYNCREATE(CDgrammView, CEditView)

BEGIN_MESSAGE_MAP(CDgrammView, CEditView)
	//{{AFX_MSG_MAP(CDgrammView)
	ON_COMMAND(ID_SEND, OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDgrammView construction/destruction

CDgrammView::CDgrammView()
{
}

CDgrammView::~CDgrammView()
{
}

BOOL CDgrammView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	BOOL bPreCreated = CEditView::PreCreateWindow(cs);
	cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);	// Enable word-wrapping

	return bPreCreated;
}

/////////////////////////////////////////////////////////////////////////////
// CDgrammView drawing

void CDgrammView::OnDraw(CDC* /* pDC*/)
{
	CDgrammDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CDgrammView diagnostics

#ifdef _DEBUG
void CDgrammView::AssertValid() const
{
	CEditView::AssertValid();
}

void CDgrammView::Dump(CDumpContext& dc) const
{
	CEditView::Dump(dc);
}

CDgrammDoc* CDgrammView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDgrammDoc)));
	return (CDgrammDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDgrammView message handlers

void CDgrammView::OnInitialUpdate() 
{
	CEditView::OnInitialUpdate();
/*
	{
		CDnsResolver dns;
		if (dns.Create("192.168.0.1")){
			CStringArray arr;
			if (dns.GetMX("hotbot.com", arr))
			{
				for(int i=0; i<arr.GetSize(); i++)
					Message(arr.GetAt(i));
			};
		}
	}
*/
	m_Socket.Init(this);

	//	Create socket object
	if (m_Socket.Create(1701, SOCK_DGRAM))
	{
		Message("Port opened");
	}else{
		Message("Can't open socket");
	};
}

//	Output text string to screen
void CDgrammView::Message(LPCSTR msg)
{
	CString tmp(msg);
	tmp += "\r\n";
	int len = GetEditCtrl().GetWindowTextLength();
	GetEditCtrl().SetSel(len, len);
	GetEditCtrl().ReplaceSel(tmp);
}

void CDgrammView::OnSend() 
{
	if (m_Dlg.DoModal()!=IDOK) return;

	GetEditCtrl().SetWindowText("Wait for answer...\r\n\r\n");
	m_Dns.Empty();

	_DNSRR rr(m_Dns);				//	DNS resource structure

	rr.q.qName.Add(m_Dlg.m_Host);	//	Adding host name to query
	rr.q.Type = typeAll;
	rr.q.Class = classIN;

	rr.Normalize();					//	Convert byte ordering
	rr.Add(m_Dns, m_Dns.Offset);	//	Fill DNS structure with data for query

	m_Dns.Normalize();				//	Convert byte ordering

	//	Sending ready data to DNS server
	if (m_Socket.SendTo(&m_Dns, m_Dns.GetSize(), 53, m_Dlg.m_Dns) != m_Dns.GetSize())
	{
		Message("Error sending datagramm to DNS server!\r\n");
	};
	//	Swith back byte ordering
	m_Dns.Normalize();
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
Programmer with 5 year of work
like networking applications and fun codes

Comments and Discussions