DNS Client Utility

xiaoxiao2021-03-06  64

DNS Client UtilityBy Jaimon Mathew

DNS Client utility This is a cut down version of the DNS client, which can be used to retrieve the MX (mail exchange) record for any Internet address from a DNS server. You can use this MX record to send e-mails directly without the need for any mail server as a host. The library can be compiled to a DLL file. One test program is also given to show all the mail exchanger records for any particular address. More information on DNS can be found from these RFCs. Rfc1034 ( http://www.ietf.org/rfc/rfc1034.txt) and RFC1035 (http://www.ietf.org/rfc/rfc1035.txt). Once You Got The MX Record, You Can Directly Opens ITS SMTP Port , and can issue the SMTP commands. You may get more than one record for some sites. In that case do a sorting based on the preference. The lowest value is the most preferred address. (With a small change it can work on Beta 1 ALSO

Source Code for DNSLITE.CS

Source Code for Test.cs

The compile strings for Both .cs files is inclined .s .cs files.

Here Is A Sample Session, Which Is Sending Mail to a Hotmail Account Directly.

1) Test Hotmail.com

This Will Return All The MX Records for Hotmail.com

2) Do Telnet to One of the Address Got from the program (to port 25)

Telnet mc1.law5.hotmail.com 25

3) Issue The Following SMTP Commands.

Helo jaimonmathew

Mail from: god@heaven.org

RCPT to: jaimonmathew@hotmail.com

Data

From: god@heaven.org

Subject: Just Another Message from God

First line

SECOND ....

Last Line

.

Quit

/ **

@Author Jaimon Mathew

CSC.exe / T: EXE /R: System.dll /R: DNSLIB.DLL / D: Debug = 1 / debug /out:"test.exe "Test.cs"

* /

Using system;

Using system.collections;

Using DNSLIB;

Public class test {

// usage test // example: test hotmail.com

Public static void main (string [] s) {

Try {

ArrayList DNSSERVERS = New ArrayList ();

// Name / IP of the dnsservers

DNSSERVERS.ADD ("158.152.1.58");

DNSLITE DL = New DNSLITE ();

Dl.SetDnsservers (DNSSERVERS);

ArrayList Results;

Results = DL.getmxRecords (s [0]);

For (int i = 0; i

MXRecord MX = (MXRecord) Results [i];

Console.writeLine (i ":" mx);

}

Results = NULL;

} catch (exception e) {

Console.writeline ("Caught Exception: E);

}

}

}

/ **

@Author Jaimon Mathew

CSC / Target: library /out:dnslib.dll / d: debug = 1 / debug DNSLITE.CS

* /

Using system;

Using system.net;

Using system.io;

Using system.text;

Using system.net.sockets;

Using system.collections;

Namespace DNSLIB {

PUBLIC CLASS MXRECORD {

Public int preference = -1;

Public String Exchange = NULL;

Public Override string toString () {

Return "Preference:" Preference "Exchange:" Exchange;

}

}

Public Class DNSLITE {

PRIVATE BYTE [] DATA;

Private int position, id, length

PRIVATE STRING NAME;

Private arraylist dnsservers;

Private static int DNS_PORT = 53;

ENCODING ASCII = Encoding.ASCII;

Public DNSLITE () {

ID = DATETIME.NOW.MILLISECOND * 60;

DNSSERVERS = New ArrayList ();

}

Public void setdnsservers (arraylist dnsservers) {

THIS.DNSSERVERS = DNSSERVERS;

}

Public arraylist getmxrecords (String host) {

ArrayList MxRecords = NULL;

For (int i = 0; i

Try {

MXRecords = getMxRecords (Host, (String) DNSSERVERS [I]);

Break;

} catch (ioexception) {

CONTINUE;

}

}

Return MxRecords;

Private int getNewID () {

// Return A New ID

Return ID;

}

Public ArrayList getMxRecords (String Host, String ServerAddRess) {

// Opening The UDP Socket At DNS Server

// Use udpclient, if you are still with beta1

UDPCLIENT DNSCLIENT = New UDPClient (ServerAddress, DNS_Port);

// preparing the dns query packet.

MakeQuery (getNewID (), Host);

// send the data packet

DNSCLIENT.SEND (DATA, DATA.LENGTH);

IpendPoint Endpoint = NULL;

// receive the data packet from DNS Server

Data = DNSCLIENT.RECEIVE (Ref endpoint);

Length = data.length;

// Un Pack The Byte Array & Makes An Array of MxRecord Objects.

Return Makeresponse ();

}

// for packing the information to the format accept by Server

Public void makequery (int id, string name) {

Data = new byte [512];

For (int i = 0; i <512; i) {

DATA [I] = 0;

}

Data [0] = (Byte) (ID >> 8);

Data [1] = (Byte) (ID & 0xFF);

Data [2] = (Byte) 1; Data [3] = (byte) 0;

Data [4] = (byte) 0; DATA [5] = (byte) 1;

Data [6] = (byte) 0; Data [7] = (byte) 0;

Data [8] = (byte) 0; Data [9] = (byte) 0;

Data [10] = (byte) 0; DATA [11] = (byte) 0;

String [] tokens = name.split (new char [] {'.'});

String label;

Position = 12;

For (int J = 0; j

Label = tokens [j];

Data [Position ] = (Byte) (Label.Length & 0xFF);

Byte [] b = ascii.getbytes (label);

For (int K = 0; k

Data [Position ] = B [k];

}

}

Data [Position ] = (Byte) 0; Data [Position ] = (Byte) 0;

Data [Position ] = (Byte) 15; Data [Position ] = (Byte) 0; Data [Position ] = (Byte) 1;

}

// for unpacking the byte array

Public arraylist makeesponse () {

ArrayList mxrecords = new arraylist ();

MXRecord mxrecord;

// Note: We are ignoring the unnecessary fields.

// and takes only the data request to build

// mx records.

INT Qcount = ((DATA [4] & 0xFF) << 8) | (Data [5] & 0xFF);

IF (qcount <0) {

Throw New IOException ("Invalid Question Count");

}

INT ACOUNT = ((Data [6] & 0xFF) << 8) | (Data [7] & 0xFF);

IF (ACOUNT <0) {

Throw New IOException ("Invalid Answer Count");

}

Position = 12;

For (int i = 0; i

Name = ""

Position = proc (position);

Position = 4;

}

For (int i = 0; i

Name = ""

Position = proc (position);

POSITION = 10;

INT pref = (Data [Position ] << 8) | (Data [Position ] & 0xFF);

Name = ""

Position = proc (position);

MXRecord = new mxrecord ();

Mxrecord.preference = pref;

mxrecord.exchange = name;

MXRecords.Add (MxRecord);

}

Return MxRecords;

}

PRIVATE INT Proc (int position) {

INT LEN = (Data [Position ] & 0xFF);

IF (len == 0) {

Return Position;

}

Int offset

Do {

IF ((len & 0xc0) == 0xc0) {

IF (position> = length) {

Return -1;

}

OFFSET = ((Len & 0x3f) << 8) | (Data [Position ] & 0xFF);

PROC (OFFSET);

Return Position;

} else {

IF ((Position Len)> Length) {

Return -1;

}

Name = ascii.getstring (data, position, len);

Position = Len;}

IF (Position> Length) {

Return -1;

}

Len = data [Position ] & 0xFF;

IF (len! = 0) {

Name = "."

}

} while (len! = 0);

Return Position;

}

}

} http://www.groupsrv.com/dot.com/viewtopic.php?t=72621if You Have More Than NetWork Connection (Say a Dialup Connection and A LAN Connection), You'll Get A Message Box for Each Connection. Usually The Lan Connection is The First, SO You Only Need The First One. I think you can change this:

Quote: for Each Obj IN Objs MsgBox (Obj.dhcpserver.tostring) Next To: msgbox (objs.obj (0) .dhcpserver.tostring) 'OR (1) Depending if 0 zero based. " You'll Have to Play with It to get it right. "Vbsearch" wrote in message news: odmkd6ioeha.1992@tk2msftngp09.phx.gbl ...

转载请注明原文地址:https://www.9cbs.com/read-110330.html

New Post(0)