Reposted using C # production "Mail Express"

xiaoxiao2021-03-06  51

[Original] use C # production "mail express delivery"

Http://itboy.cn/blog/read.asp? Number = 9

The download address of the source code (go to the page below: http://itboy.cn/product/index.asp 1. There is a function of "Mail Express" in the new version of Foxmail. I don't know how to use it first. Know

Tools -> System Options Set the IP address of the local DNS server.

I feel that this new feature is quite easy to use. You don't need to pass the SMTP proxy, you can send mail directly through the mail switch to your mailbox. I want to implement this function in VC at the beginning of the summer vacation. After the iRIS is intercepted, the program is found.

MX8.263.NET sends a mailbox, I don't know what this is something. Later, I thought this is the 263.NET MX record host. The principle of expressive express delivery is to send data to this host.

Run the NSLookup program:

SET TYPE = MX 263.NET

Yes, yes, get the result:

Non-Authoritative Answer: 263.NET MX preference = 10, mail exchanger = mx06.263.net 263.net MX preference = 10, mail exchanger =

MX08.263.NET

263.NET MX preference = 10, mail exchanger = mx09.263.net 263.net mx preference = 10, mail exchanger = mx11.263.net 263.net mx preference = 10, mail exchanger = mx12.263.net 263. Net mx preference = 40, mail exchanger = mx03.263.net 263.net mx preference = 10, mail exchanger = mx01.263.net

Not wrong. This is this. Later, I didn't know how to achieve NSLOOKUP's function, I gave up and learned about half a month of C #. Later, I found some related documents online. A few experiments. Take my development process to share, I wrote the tutorial document. So, if you don't standardize, please bear it. The domain name, mailbox and IP involved in this article are true.

Second, the principle of DNS protocol I think, to be a good network software programmer, you must read the RFC documentation. Because this article is a fans for most broad program enthusiasts, I try to write from the details. If the master, you can skip this part.

Related RFC Documents of the DNS Agreement:

RFC1034- "

Domain names - Concepts and facilities

RFC1035- "

Domain names - Implementation and Specification

The computer is used online as 220.162.75.1, which is called an IP address, is identified by a computer. And if you visit a computer accessed by entering such a stuff, isn't it too terrible? To have a good stuff like DNS, use the IP address you bind it, when we enter inside the browser

When http://zssysy.com, the browser doesn't know where to get it, so you will query to the set DNS server.

Zzsy.com This domain name. The DNS server will look for your own record library, and if you don't find it, turn to the previous DNS server for query (forwarding request). Tell your browser to find the IP. The record type of the browser query is a record. RFC1035 Documents Define 16 record types in page 11, and common A (address) record, CNAME (alias) record, MX (mail exchange) record. We must care about MX records. The process of queries is generally: the customer sends a UDP packet to the 53 port of the DNS server, and the DNS server is processed and processes the results records still returned in the form of UDP packets.

The general format of this UDP message:

---------------------

| Packet head |

---------------------

| Question | Query part of the server

---------------------

| Answer | Resource Record of Server Reply

---------------------

| Authorized | Authoritative resource record

---------------------

| Extraordinary | Extraordinary Resource Record

---------------------

In addition to the packet head is fixed 12 bytes, the length of each part is unordered byout.

We care about the news head, the problem, and answer these three parts.

among them

The format of the header:

1 1 1 1 1 1

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

- - - - - - -

| ID |

- - - - - - -

| Qr | opcode | AA | TC | RD | RA | Z | RCODE |

- - - - - - -

| QDCOUNT |

- - - - - - -

| Ancount |

- - - - - - -

| Nscount |

- - - - - - - | Arcount |

- - - - - - -

Good guy, what is a ghost pain? One of the top is the number identifier, 0-15 (note that the 10-15 behind the back is in the form of up and down, and I didn't understand it at the beginning. The next is: ID: 16 bits, 2 bytes. The number of this message is specified by the client. The DNS reply is taken on this identifier to request a request for the corresponding correspondence. QR: 1 bit, 1/8 bytes. 0 represents the query, 1 represents DNS reply opcode: 4 digits, 1/2 bytes. Indicates the type of query: 0: Standard query; 1: reverse query; 2: server status query; 3-15: Not used. AA: 1 bit, 1/8 bytes. Whether authority reply. Tc: 1 bit, 1/8 bytes. Because a UDP message is 512 bytes, the bit indicates whether the exceeded portion. RD: 1 bit, 1/8 bytes. This bit is specified in the query and the same time is the same. Set to 1 Indicate the server for recursive query. RA: 1 bit, 1/8 bytes. Returns the specified by DNS reply, indicating whether the DNS server supports recursive query. Z: 3 digits, 3/8 bytes. The preserved field must be set to 0. Rcode: 4 digits, 1/2 bytes. The return code specified by the reply: 0: No error; 1: Format error; 2: DNS error; 3: Domain name does not exist; 4: DNS does not support this type of query; 5: DNS refuse query; 6-15: Reserved field . QDCount: 16 bits, 2 bytes. An unsigned number indicating the number of query records. ANCOUNT: 16 bits, 2 bytes. An unsigned number indicates the number of reply records. Nscount: 16 bits, 2 bytes. An unsigned number indicates the number of authoritative records. Arcount: 16 bits, 2 bytes. An unsigned number indicates a number of exception records.

Each query resource record format:

1 1 1 1 1 1

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

- - - - - - -

| | |

/ QNAME /

/ /

- - - - - - -

| Qtype |

- - - - - - - | Qclass |

- - - - - - -

QNAME: Not set, indicating the domain name to query. (Bright boxes in both sides) Qtype: 2 bytes, according to RFC 1035 and NSLOOKUP's help document, I define the following enumeration type: Enum querytype // The resource record type of Query. {A = 0x01, // Specify the computer IP address. NS = 0x02, / / ​​Specifies the DNS name server for the naming area. MD = 0x03, // Specify the mail receiving station (this type is outdated, use MX instead) MF = 0x04, // Specify the mail transfer station (this type is outdated, use MX instead) CNAME = 0x05, // Specify The normative name of the alias. SOA = 0x06, / / ​​Specifies "starting authority" for the DNS area. MB = 0x07, // Specify the mailbox domain name. MG = 0x08, // Specify a member of the mail group. MR = 0x09, // Specify the mail rename domain name. NULL = 0x0a, // Specify an empty resource record WKS = 0x0b, // Describe known service. PTR = 0x0c, // If the query is an IP address, specify the computer name; otherwise specify a pointer to other information. Hinfo = 0x0d, // Specifies the computer CPU and the operating system type. Minfo = 0x0e, // Specify a mailbox or mailing list information. MX = 0x0f, // Specify a mail switch. TXT = 0x10, // Specify text information. UINFO = 0x64, // Specify user information. UID = 0x65, // Specify the user identifier. GID = 0x66, // Specify the group identifier of the group name. Any = 0xFF // Specify all data types. }; Qtype: 2 bytes. Depending on RFC1035 and NSlookup help documents, I define the following enumeration type: Enum queryclass // Specify the protocol group of information. {IN = 0x01, // Specify the Internet Category. Csnet = 0x02, // Specify the CSNet category. (Outtime) Chaos = 0x03, // Specify the chaos category. Hesiod = 0x04, // Specify Mit Athena HesioD category. Any = 0xFF // Specifies any previously listed wildcards. }; A, MX, CNAME in Qtype is common, and IN in QClass is common. Each recording format for each reply:

1 1 1 1 1 1

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5

- - - - - - -

| | |

/ /

/ Name / | |

- - - - - - -

| TYPE |

- - - - - - -

| Class |

- - - - - - -

| TTL |

| | |

- - - - - - -

| RDLENGTH |

- - - - - - - - - |

/ RDATA /

/ /

- - - - - - -

Name: Reply to the domain name of the query, not long.

TYPE: The type of reply. 2 bytes, synonymous with queries. Indicates the type of resource record in RDATA.

Class: Reply to the class. 2 bytes, synonymous with queries. Indicates the resource record class in RDATA.

TTL: Survival time. 4 bytes, indicating that the resource recorded in RDATA is recorded in the cache.

Rdlength: Length. 2 bytes, indicating the length of the RDATA block.

RDATA: Resource Record. Undefined, depending on Type, this record is different, usually one MX record is composed of a 2-byte indicated a priority value of the mail switch and an unregistered mail exchanger name.

This describes the combination of names.

The name consists of a plurality of identity sequences, and the first byte of each identifier sequence illustrates the length of the identifier, and then uses a ASCII code to represent characters, and multiple sequences are later indicated by byte 0. If the length of the first character of one of the identification sequences is 0xc0, it means that the next byte indication is not an identifier sequence, but instead indicates the offset position in the next portion within the receiving package.

For example, BBS.ZZSY.com. Separate BBS, ZZSY, COM. The length of each part is 3, 4, 3

In the form of the DNS packet, like 3 B B S 4 Z z y 3 C O m 0

If there is a name of 4 Z Z Y 3 C O m 0 in the 12th byte location in the package. It is possible to: 3 B B S 4 Z z y 0xc0 0x0c.

Third, DNS protocol instance explanation

Said so many theoretical fits, there may be two big heads. Or use an example method to explain it.

I chose the famous webcape and protocol analysis tool Iris 4.05, you can download from my site:

Http://itboy.cn/data/iris405full.rar

Run IRIS, click on the Filters selection port tab of the menu to use the 53 port after determination.

Click on the green run icon on the IRIS toolbar for listening.

Run the NSlookUp program in Windows.

Enter the following command:

SET TYPE = MX

Then return to the NSLookUP program.

Enter the command:

Yahoo.com.cn

Get up

Yahoo.com.cn mx preference = 20, mail exchanger = mx5.mail.yahoo.com

Yahoo.com.cn mx preference = 10, mail exchanger = MTA-V1.MAIL.VIP.CNB.YAHOO.com

Such two MX resource records are recorded. At this time, IRIs is as follows:

The current picture shows the package is the second message, which is a message replying from DNS. The first message is a query message, which is much simpler than the reply packet. Therefore, if you analyze the reply packet, the query message believes that smart is as easy as you can make it easy.

The red portion of the figure shown in the figure is a DNS packet, and the data content on the upper side is the MAC Joint, PPP Protocol, IPv4 Protocol Pack and UDP Protocol Pack. These packages are not within the scope of this article, not paying attention.

(Insert some nonsense: Click on the entry displayed in the left tree of IRIS, not necessarily reflected in the packet on the right, I don't know, I don't know at the beginning, and analyze it according to the indication of it, analyze it. For a long time, I don't know what to say. The analysis of the package is to study this program in this program. In the past two, the online class did not listen to the online class of our college, and weird. , 咋 叫 叫 教 教 教 专 专 专 专 专 专 专 专 专 专: 地 地 专: 地Speech is correct

For the convenience of explanation, I handle some of the DNS protocols in the bag, as shown below:

The red portion is the header, blue for the query section, green for the first resource record of the reply, gold is a second resource record for the reply. Others don't have the authority record, extra record, don't analyze it.

Red section:

The 0th byte, the first byte 00 03 identifies an ID.

The second byte, the third word 81 80 is chemically binary form 1000 0001 1000 0000

QR (0) is 1 reply to reply packets.

OpCode (2-5) represents a standard query.

AA (6) indicates a non-authoritative query.

TC (7) is 0 indicates that no more than 512 bytes of packets do not truncate.

RD (8) is 1 indicating that the NSLookUP program indicates that the DNS is recursive.

RA (9) is 1 to represent DNS support recursive query.

Z (10-12) Reserved field

Rcode (13-16) is 0 indicates that the query is not enough.

The 4th byte, the 5th byte 00 01 indicates that the number of resource records in the query is 1

The 6th byte, the 7th byte 00 02 indicates that the number of resource records of the reply is 2

The 8th byte, the 9th byte 00 06 indicates that the number of authoritative resources records is 6

The 10th byte, the 11th byte 00 04 indicates that the number of additional resource records is 4 and then reached the blue portion, the resource record section of the query:

The domain name is started at the beginning.

Start 05 indicates that the five bytes of the rear are sequence characters, turn 79 61 68 6f 6f to the ASCII code to Yahoo

Arrived 03 The three bytes of the rear side are sequence characters, turn the 63 6F 6D to the ASCII code to COM

Arrived 02 indicates the two bytes of the back to the sequence character, turn the 63 6E to the ASCII code to CN

The arrival 00 indicates the end.

The whole strings, use. Connection, ie: yahoo.com.cn

The next two byte 00 0F indicates that the query type is 15, which is the MX record query.

Next, the two bytes 00 01 indicate that the query class is 1, which is connected to the Internet.

The DNS packets in the first package in the IRIS package contain only red lines and blue lines, and the analysis methods are the same. No longer.

Green part, reply the first resource record section:

Open the domain name of the query,

The first byte c0 represents compression, the next bit is the offset bypad.

Next, 0C represents the first byte of the blue of the entire package, that is, the first byte of the blue.

Then the same analysis is the same as the above, the domain name is yahoo.com.cn

Then return to the next byte

The 3-4 bytes 00 0f in the green package indicates that the query type is 15, which is the MX record query.

Then the next 5-6 bytes 00 01 indicate that the query class is 1, which is connected to the Internet.

Then the next 7-10 bytes 00 00 05 95 is evolved as evolution equal to 1429 seconds. That is, the cache time is 23 minutes and 49 seconds.

Then 11-12 byte 00 16 indicates that the data portion recorded in this resource is 22 bytes. That is, the number of bytes left.

22 bytes starting from 13 bytes to the data part of the MX record. This section is formatted to two bytes of mail exchanger priority, unregulated mail switch name.

Section 13-14 byte 00 14 indicates a priority value of 20

Then, from 15, it is a mail switch name, according to the blue domain name analysis method, get mx5.mail.yahoo.com

(The byte this paragraph is the relative position in the green block, not the absolute position of the entire DNS package)

The patronic method of the golden piece is similar to the green, but there is a difference that looks at the mail exclusion name of the golden bag.

From the absolute position of the package, the 78th (the figure in the figure is bidding the purple 78) begins to be the email switch name. We have analyzed, get MTA-V1.MAIL.VIP.CNB, then 0xc0 is then compressed, and then the next offset is 0x0c and jumps to the 12-byte of the package to get Yahoo.com.cn. The whole combination is the second resource record name: MTA-v1.mail.vip.cnb.yahoo.com

Such a result is running the results with NSlookup:

Yahoo.com.cn mx preference = 20, mail exchanger = mx5.mail.yahoo.com

Yahoo.com.cn mx preference = 10, mail exchanger = MTA-V1.MAIL.VIP.CNB.YAHOO.com

the same.

I hope this energy can help network enthusiasts learn the package analysis method. Analysis of programming points of MX record query in our current DNS.

Fourth, DNS Query MX Record Programming Key Point Analysis Foxmail 5.0 Mail Express A very rubbish place is unable to automatically get a local ISP DNS server, but also user manual input, I think it is because the limit of API or Foxmail development group I didn't think of the method or other things that I didn't know. No matter what he, use WMI in C #, it is easy:

String [] DNSES;

ManagementClass MC = New ManagementClass ("Win32_NetworkAdapterConfiguration); ManagementObjectCollection moc = mc.getinstances ();

// Enumerate all the network cards on the current machine

Foreach (ManagementObject Mo in MoC)

{

IF (Bool) Mo ["iPenabled"])

{

DNSES = (String []) Mo ["DNSSERVERSEARCHORDER"];

IF (DNSES! = NULL)

{

DNSSERVER = DNSES [0]; // uses the first found DNS server.

}

}

}

Of course, you will not forget to add a reference to System.Management in the project -> add reference?

Put this in the static constructor of the DNSQuery class.

There is also a custom MXRecord structure that stores information for MX resource records.

Struct Mxrecord

{

Public String Domain; // Domain Name

Public QueryType QueryType; // Query Type

Public queryclass queryclass; // query class

Public timespan livetime; // Survival time

The length of the PUBLIC INT DATALENGTH; / / The length of the resource is indicative of the number of bytes of the resource of the priority and name of the mail server.

Public int preference; // Priority value, the smaller the value, the more preferred.

Public String Name; // Mail Exchanger Name

}

Make two bytes into an INT16 integer cannot be used by BitConverter class because it is different from the end of the DNS server. Therefore, it is displaced with the << method.

Others don't say, in the DNSQuery class of the program, I made a detailed code annotation. If you have some development experience, it should be easy to understand. If you have any questions, please contact me. I will help you with your energy and ability.

V. Analyze Foxmail's express delivery to send data.

Run IPConfig / ALL

Record the first IP address of the resulting DNS Servers.

2. Or run iris. Set Filters as SMTP, 25 ports. Run to listen.

3. Run Foxmail5.0, "Mail Express" tab within Tools -> System settings Set the domain name server 1 is the just IP address.

Click "Write" and write it with the format of the text file.

Recipient:

DreamChild@263.net

Cc:

theme:

Hello Dreamchild, take the liberty to fight

content:

Dear Mr. Dreamchild:

This is an email.

Attachment: (Add mm.gif and instructions. TXT two files. In order to ensure the integrity of this tutorial, these two additional stuff also put it in the package.)

4. Stop Iris after completion, point the decode of its menu bar to get the following:

220 Welcome To Coremail System (with Anti-spam) 2.1 for 263 (040326)

Helo DreamChild

250 mta6.x263.net

Mail from: <>

250 ok

RCPT TO:

250 ok

Data

354 End Data with

.

Date: Thu, 9 Sep 2004 01:00:35 0800FROM: "=? GB2312? B? W87qobqi? =" <

>

TO: "Dreamchild"

? Subject: = gb2312 B xOO6w2RyZWFtY2hpbGSjrMOww8G08sjE = X-mailer: Foxmail 5.0 [cn] Mime-Version: 1.0 Content-Type:??? Multipart / mixed; boundary = "===== 001_Dragon788446150325 _ =====" This is a multi-part message in MIME format - ===== 001_Dragon788446150325 _ ===== Content-Type: text / plain; charset = "gb2312" Content-Transfer-Encoding:. base64 1 / C tLXEZHJlYW1jaGlsZM / Iyfqjug0KoaGhodXiysfSu7fi08q8 / Q Gjdqo = - ===== 001_dragon788446150325 _ ===== CONTENT-TYPE: image / gif; Name = "mm.gif" Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename = "mm.gif" R0lGODlheAB4AIddABwNCixIjJtjkLO8iZux0NK2xpVJT9XayJWNuDA0Vt5Xh Sw7dLc7p6Fh3mD (side to be omitted on a tourbillon MM.gif Base64 encoded file content) w75UTdHFHwD / 1Ex6cBSs0jHpKaySFXI4XCwAEhPkRsUSBQQAOw == - ===== 001_Dragon788446150325 _ ===== content-Type: application / octet-stream; name = "= gb2312 B y7XD9y50eHQ = =????" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename = "= gb2312 B y7XD9y50eHQ = =????" 1eK49k1Nv8mwrrK7v8mwrqGjDQo = - ===== 001_Dragon788446150325_ = ==== -. 250 ok: queued as 7cda613a26e quit 221 bye

The SMTP protocol is involved, where the version of the RFC821 document http://itboy.cn/data/rfc821.doc is a Chinese version, so everyone will take some time, no more reason. . Only list status ID 211 system status or system help response 214 Help information 220

Service

221

Service Close Transport Channel

250 required mail operations

251 users are non-local, will forward

354 Start the mail input,

.

end

421

The service is not ready, turn off the transfer channel (this response can be used as a response to any command when it must be closed)

450 The required mail operation is not completed, the mailbox is unavailable (for example, mailbox busy)

451 Abandon the requirements of the requirements;

452 system storage is insufficient, the required operation is not performed

500 Format error, command is not recognized (this error also includes command lines)

501 parameter format error

502 commands are not implemented

503 Error command sequence

504 Command parameters are not implemented

550 The required mail operation is not completed, the mailbox is unavailable (for example, the mailbox is not found, or if you are not accessible)

551 users are non-local, please try

552 excess storage allocation, the required operation is not performed

553 mailbox name is unavailable, the required operation is not executed (such as mailbox format errors)

554 operation failed

It can be seen from this point that the user name and password of the SMTP server are different from the general via SMTP proxy.

Describe the entire process:

First, a mail switch of 263.NET is obtained by the aforementioned method, and then connected to this switch. Then connect to the 25 port of this server,

The server returns 220.

Then indicate the username, send an email (person), receive the mailbox (person). Receive the data written to the mail.

The data is divided into two parts: the body of the mail header and the message.

The email header contains: time, send mailbox (people), receive mailbox (people), theme, sender, MIME version number, email content type and split.

There are some strings encoded with Base64, which are the original Chinese characters. In fact, we can write directly to Chinese when making a SMTP proxy mail sending program.

This is the type and split of the email content, and others are easy to understand.

The type of mail content here is

Multipart / Mixed; Description is made up of a variety of formats.

The separator is used to separate the content part of the message and each accessory. use

Boundary keywords and key values ​​are defined.

For example, this example

===== 001_dragon788446150325 _ ===== to indicate that there is a detail question, the key value is best to use, do not have spaces. For example, if you use

Boundary ====== 001_dragon7884446150325 _ ===== to indicate, the foxmail5.0 will not process the mail correctly, and the content part of the message is used as a whole base64 garbled text, but I log in to the 263.net website. Receiving can see that the message is transformed normally.

The content part of the message is through two minusks.

- Reconnect the partition to separate the part.

Mail main body from the first one

- ===== 001_dragon788446150325 _ ===== start, to the second

- ===== 001_dragon788446150325 _ ===== for the first part of the content

Content-type: text / plain; charset = "GB2312" Content-Transfer-Encoding: Base64

These two sentences explain the character sets and encodings of their types and content. Here is the Base64, then a blank line, plus "Dear Dreamchild: / r / n This is an email." This string's base64 encodes the body part of the message.

In fact, we can specify

Content-Transfer-Encoding: 8bit then can be used in the body part.

Next, it is partially spaced apart parts.

one extra

Content-disposition: attachment; to illustrate this part is an attachment, as well as the relevant file name

FileName = "mm.gif".

The attachment content part is to read the file into a byte array, then turn the byte array into the Base64 encoded string. Here is

Mm.gif This file content.

The third part is annex 2

Test .txt file,

Test .TXT is also processed into base64 format by Foxmail, which can be represented by the original text.

After the last finished, use "Enter a Row. Return to the Rank" indicates the end of the DATA part.

If you send it to the server correctly, then it will return one.

250 state.

Then

Quit Command Heel Server 3166

6. Analysis of Mail Send Program Programming

Let's define a mail structure to describe the individual properties of the message.

Public struct mailcontent // message content

{

Public string to; // recipient address

Public String Toname; // Recipient Name

Public string from; // sender address

Public string fromname; // send a name

Public String Title; // Theme

Public string body; // text content

Public Bool UseAttachment; // Whether to use attachments

Public String [] attachmentlist; // attachment list

}

Then, from each control, it is assigned an example of this structure.

The value of the value includes determining whether the mailbox format is correct, we use a regular expression to determine. as follows:

Regex.ismatch (mailbox string, @ "@ ([/ w - /.] ) @ (/ [[0-9] {1, 3} /. [0-9] {1,3} /. [0-9] {1,3} /.) | ([/ W -] /.))) ([A-ZA-Z] {2, 4} | [0-9] {1, 3}) (/]?) $ ")

If the returned value is fake, use the error prompt to prompt.

After the value is finished, create a work thread to send the message.

The thread starts the list of mailbox domain parts through local DNS. The switch is then taken for the exclusator of its minimum priority to be used for future transmission. For example

DreamChild@263.net This mailbox domain name

263.NET's priority value is 10 MX record

MX12.263.NET

Then it is even connected to the server to send commands and receive returns. Some of the following code is listed below:

About Creating a SOCK Socket:

TCPCLIENT SOCK = New TcpClient ();

NetWorkstream NetStream;

Sock.NodeLay = true; // Does not use the delay algorithm to speed up the transmission of the small data packet.

Sock.receivetimeout = 10000; // Receive timeout is 10 seconds.

Sock.Connect (Server, Port);

NetStream = Sock.getStream ();

The Nagle algorithm of the Socket will reduce the transmission speed of the small datagram, and the system defaults to use the Nagle algorithm. So set nodelay to turn off it to speed up the transmission of small data.

About Send method:

Byte [] Sendarray = Encoding.default.getbytes (SendString); NetStream.write (Sendarray, 0, Sendarray.Length);

First turn the string to byte arrays and send out

About Receive method:

Const int maxReceivesize = 1460;

....

Byte [] buffer = new byte [maxReceivesize];

Length = netstream.read (buffer, 0, maxreceivesize);

IF (Length == 0)

Return NULL;

....

ReceiveString = encoding.default.getstring (buffer, 0, length);

According to "In the Sock_Stream mode, if the single transmission data exceeds 1460, the system will be divided into multiple datagram, which will be a data stream in the other party, and the application needs to increase the determination of the broken frame. Of course, you can use a modified registry. The mode changes the size of 1460, but Micrcosoft believes that 1460 is the best efficiency parameter, not recommended. "

This is set once to receive 1460 bytes, but in fact, the return code can not be used so many bytes. This is before I write a part of the receiving function written based on the HTTP network program, keeping this method.

After receiving, turn it back to the string back.

Send = "Date: {$ time} / r / n"

"From: {$ fromname} <{$ from}> / r / n"

"To: {$ toname} <{$ to}> / r / n"

"Subject: {$ TITLE} / r / n"

"X-MAILER: NOSMTPSENDER [Cai Xiaohui Making] / R / N"

"MIME_VERSION: 1.0 / r / n"

"Content-Type: Multipart / Mixed; Boundary = /" {$ SPLITLINE} / "/ r / n"

"/ r / n" // The head ends, and the body is started.

"- {$ SPLITLINE} / r / n" // Content section.

"Content-Type: Text / Plain; Charset = GB2312 / R / N"

"Content-Transfer-Encoding: 8bit / R / N"

"/ r / n"

"{$ BODY} / R / N"

"/ r / n";

Attachment = "";

Attachment = "- {$ SPLITLINE} / R / N";

Attachment = "Content-Type: Application / OcTet-stream; Name = {$ FileName} / r / n";

Attachment = "Content-Disposition: attachment; filename = {$ filename} / r / n";

Attachment = "Content-Transfer-Encoding: Base64 / R / N";

Attachment = "/ r / n";

Attachment = attachment.replace ("{$ SPLITLINE}", splitline); // Advanced RREs to prevent the replacement of the accessory content. attachment = attachment.replace ("{$ filename}", file.name);

Attachment = Convert.TOBASE64STRING (Filebytes, 0, Length);

Attachment = "/ r / n / r / n";

Send = attachment;

According to the analysis of the previous section, the format is listed, and then replace the {$ variable name} is OK, where the TIME variable name is obtained:

DateTime.now.toString ("R"). Replace ("GMT", "China Standard Time")

I don't know if the VS2003 bug, with the "R" parameter output the current time RFC format Greenwich time, no localization time is automatically converted, such as China time 9 o'clock, it did not subtract 8 hours. And just add "GMT" directly after the current time. So we change the "GMT" to "China Standard Time" to school.

Press a question and answer "Communication", if there is a wrong, then save the wrong information, then

Return False;

If the entire transmission process is not wrong, you can

NetStream.close ();

Sock.close ();

end.

Interface screenshot of the program:

Seven, the last words

Thank you very much, I am very patiently finished. I spent four all night (actually sleeping in the morning) time to write software and tutorial's hard work. This software is a better finished product after learning for half a month. I started learning truly programming from the big two, using the nine months VB and wrote the C / MFC program for another year. Now studying C #. It feels that the foundation of C is very easy.

The method of sending the mail attachment of this document refers to Luo Seniors (

Http://www.luocong.com) C source code, otherwise it may be repeated to think about it. Thanks to Teacher Luo's selflessness.

After writing this stuff, after four days, my summer vacation is over, the next semester is a big four. A very fascinating school year, I want to find a good job in Shanghai after graduation. I want to invite my seniors in the industry to guide me, do you have to cross the threshold of Shanghai University software? I will strive to go beyond the rest of this year.

You can disseminate this software, source, and related documents arbitrarily, but keep integrity.

If it is used for commercial purposes, I need to agree.

my contact information:

Address: Zhangzhou Teachers College 082 Mailbox (363000)

Name: Cai Xiaomei

e-mail:

QQ: 22415

I hope we can make a friend.

If you need me to answer within a year, please send an inquiry to the [Information Technology] section on http://bbs.zzsy.com.

I will help you with your efforts and capabilities.

Programmed by Cai Xiaomei

2004.9.7 - 2004.9.9

[Network Programming] Programming Network Package Monitoring 2004-9-9 with Raw Socket Using C # 2004-9-3

Talking up Socket programming, everyone may think of QQ and IE, yes. There are also many network tools such as P2P, NetMeeting, etc. Applications implemented by the application layer, but also to be implemented with socket. Socket is a network programming interface that implements the network application layer, and Windows Socket includes a system component that takes advantage of the characteristics of Microsoft Windows messaging. The Socket specification version 1.1 is released in January 1993 and is widely used in the Windows9x operating system that will thereafter. Socket Specification 2.2 (which is Winsock2.2 on the Windows platform, also called Winsock2) In May 1996, Windows NT 5.0 and later version of Windows system support Winsock2, in Winsock2, support multiple transport protocols Original socket, overlapping I / O model, service quality control, etc. This article introduces some of Windows Sockets to the programming of the raw socket (Raw Socket) implemented with C #, and the network packet monitoring technology implemented on this basis. Compared with Winsock1, Winsock2 is the most obvious that supports the RAW Socket socket type. Using RAW socket, you can set the network card into a mixed mode. In this mode, we can receive the IP package on the network, of course, including the purpose Not a native IP package, through the original socket, we can also control the various protocols under Windows more freely, and can control the transmission mechanism of the network underlayer. In this example, I implemented the RawSocket class in the NBYTE.BASICCLASS namespace, which contains core technologies we implements packet monitoring. Before you implement this class, you need to write an IP header structure to temporarily store some information about the network package: [structLayout (layoutkind.explicit)] public struct ipheader {[fieldoffset (0)] public byte ip_verlen; // i4 bit First length 4-bit IP version number [Fieldoffset (1)] public byte ip_tos; // 8-bit service type TOS [fieldoffset (2)] public ushort ip_totallength; // 16-bit packet total length (bytes) [fieldoffset 4)] public ushort ip_id; // 16-bit identifier [fieldoffset (6)] public ushort ip_offset; // 3-bit flag [Fieldoffset (8)] public byte ip_ttl; // 8-bit survival time TTL [fieldoffset (9) ] Public Byte IP_Protocol; // 8-bit protocol (TCP, UDP, ICMP, ETC.) [Fieldoffset (10)] public ushort ip_checksum; // 16-bit IP header checksum [Fieldoffset (12)] public uint ip_srcaddr; / / 32-bit source IP address [fieldoffset (16)] public uint ip_destaddr; // 32 bit of IP address} This way, when each packet arrives, the data stream in the package can be converted into one iPheader object. .

Let's start writing the Rawsocket class. First, a few parameters are defined first, including: private bool error_occurred; // Sockets Whether to generate error public bool keeprunning when receiving the package; // Continue Private Static int LEN_RECEIVE_BUF; / / Get the length of the data stream byte [] receive_buf_bytes; // Received byte private socket socket = null; // The declaration socket also has a constant: const Int sio_rcvall = unchecked ((int) 0x98000001); // Monitoring All Packets The SiO_rcvall here is that the RawSocket receives all the packets, in the later IOCONTRL function, in the following constructor, initialization of some variable parameters: public rawsocket () // Constructor {error_occurred = false; len_receive_buf = 4096; receive_buf_bytes = new byte [len_receive_buf];} implement the following function creates RawSocket, and bring it to the endpoint (IPEndPoint: local IP and port) binding: public void CreateAndBindSocket (string IP ) // establish and bind socket {socket = new socket (addressfamily.internetwork, sockettype.raw, protocoltype.ip); socket.blocking = false; // Set Socket Non-Block State Socket.bind (New Ipendpain .PARSE (IP), 0)); // Binding socket IF (setsocketoption () == false error_occurred = true;} where a SOC is created Ket = new socket (addressfamily.internetwork, sockettype.raw, protocoltype.IP); 3 parameters: The first parameter is set to set the address family, the description on the MSDN is "Specify the address instance to resolve the address. Program ", when you want to bind the socket to the endpoint (iPendPoint), you need to use the InternetWork member, ie the address format of IP version 4, which is also a addressing scheme that uses most of today's socket programming (AddressFamily) ). The second parameter set type is the type of RAW we use. SocketType is an enumeration data type, and the RAW socket type supports access to the underlying Transfer Protocol. By using sockettype.raw, you can communicate using the Transmission Control Protocol (TCP) and User Data Normalization (UDP), or use the Internet Control Protocol (ICMP) and Internet Group Management Protocol (IGMP) to communicate. When sending, your application must provide a complete IP header. The received datagram is returned while returning the IP header and option constant.

The third parameter setting protocol type, the Socket class uses the ProtocolType enumeration data type to notify the requested protocol to the Windows Socket API. The IP protocol is used here, so use the protocoltype.ip parameter. There is a custom setSocketoption function in the CreateAndBindSocket function, which is different from the setSocketopption in the socket class. We define the setSocketoption with the IO control function. It is defined as follows: private bool setsocketoption () // Setting Raw Socket { Bool ret_value = true; try {socket.setsocketoption (socketoptionne.ip, socketoptionname.headerincluded, 1); byte [] in = new byte [4] {1, 0, 0, 0}; byte [] out = new byte [ 4]; // Low level mode of operation accepts all packets, this step is the key, you must set up Socket to RAW and IP Level to use SiO_RcVall int RET_CODE = Socket.iocontrol (SiO_RCVALL, IN, OUT); RET_CODE = OUT [0] OUT [1] OUT [2] OUT [3]; // Synthes 1 32-bit integer IF (RET_CODE! = 0) RET_VALUE = false;} catch (socketexception) RET_VALUE = false;} Return Ret_Value;} where the setup option must be set to contain the IP cladding, otherwise the iPheter structure cannot be populated, and packet information cannot be obtained. int RET_CODE = Socket.ioControl (SiO_RCVALL, IN, OUT); is the most critical step in the function, because we can't use the Receive function in Windows to receive data on the Raw Socket, because all IP packs It is first submitted to the system core, then transfer to the user program, when sending a RAWS Socket package (such as SYN), the core does not know, there is no record that is sent or established, so when the remote host When responding, the core of the system throws all of these packages so that it is not possible. Therefore, it is not possible to simply use the received function to receive these datagrams. To reach the purpose of receiving data, you must use sniffing, receive all passed packets, and then filter, leaving the need to meet what we need. You can display the data packets on all networks by setting SiO_RCVALL. Let's introduce the IOCONTROL function. MSDN explains that it is to set the socket for low level mode, how low level operation method? In fact, this function is similar to the WSAIOCTL function in the API.

The WSAIOCTL function is defined as follows: int WSAIOCTL (Socket S, // A specified socket dword dwioControlCode, / / ​​control operand lpvoid lpvinbuffer, // pointing to the pointer DWORD cbinbuffer of the input data stream, // Enter the size of the data stream (word Number of: LPVOID LPVOUTBUFFER, / / ​​Pointer DWORD CBOUTBUFFER, / / ​​Output Data Stream (byte) LPDWORD LPCBBYTESRETURNED, / / ​​Point to the real value of output byte stream LPWSAOVERLAPPED LPOVERLAPPED, // Point to one WSAOVERLAPPED Structure LPWSAOVERLAPPED_COMPLETION_ROUTINE LPCOMPETIONROUTINE / / The routines that are executed when the operation is complete); the IoControl function of the C # is not as complex as the WSAIOctL function, which includes only the control operation code, input byte stream, output byte three parameters, but These three parameters are already enough. We see a one-byte array in the function: byte [] in = new byte [4] {1, 0, 0, 0} In fact, it is a value of 1 DWORD or INT32, the same Byte [] OUT = New byte [4]; also, it is a value that is interested in the WSAiocTl function.

Because an error may occur when setting the socket option, you need to deliver the error flag with a value: public bool erroroccot {get {return error_occurred;}} The following functions implemented the received packet reception: // Analyze the received packet, formation PacketArrivedEventArgs event data class object, and initiator PacketArrival event unsafe private void Receive (byte [] buf, int len) {byte temp_protocol = 0; uint temp_version = 0; uint temp_ip_srcaddr = 0; uint temp_ip_destaddr = 0; short temp_srcport = 0; short temp_dstport = 0; IPAddress temp_ip; PacketArrivedEventArgs e = new PacketArrivedEventArgs (); // new network packet information event fixed (byte * fixed_buf = buf) {IPHeader * head = (IPHeader *) fixed_buf; // data stream as a whole and Ipheter structure E.HeaderLength = (UINT) (Head-> ip_verlen & 0x0f) << 2; Temp_Protocol = head-> ip_protocol; switch (temp_protocol) // extraction protocol type {Case 1: E.Protocol = "ICMP"; BREAK Case 2: E.Protocol = "IGMP"; Break; Case 6: E.Protocol = "TCP"; Break; Case 17: E.Protocol = "udp"; break; default: E.Protocol = "UNKNOWN"; Break;} temp_version = (uint) (Head-> ip_verlen & 0xf0) >> 4; // Extract the IP protocol version e.IPVersion = temp_version.ToString (); // The following statement extracting an additional object parameters temp_ip_srcaddr PacketArrivedEventArgs = head-> ip_srcaddr; temp_ip_destaddr = head-> ip_destaddr; temp_ip = new IPAddress (temp_ip_srcaddr); e.OriginationAddress = temp_ip .ToString (); temp_ip = new IPAddress (temp_ip_destaddr); e.DestinationAddress = temp_ip.ToString (); temp_srcport = * (short *) & fixed_buf [e.HeaderLength]; temp_dstport = * (short *) & fixed_buf [e.HeaderLength 2]; e.originationport =

IPAddress.NetworkToHostOrder (temp_srcport) .ToString (); e.DestinationPort = IPAddress.NetworkToHostOrder (temp_dstport) .ToString (); e.PacketLength = (uint) len; e.MessageLength = (uint) len - e.HeaderLength; e. ReceiveBuffer = BUF; // assigns the IP head in BUF to iPheaderBuffer Array.copy in PacketarriveDeventargs; 0, (int) E.HEADERBUFER, 0, (int) E.HEADERLENGTH); // Assignment in the package in BUF Give the MessageBuffer Array.copy in PacketarriveDeventargs (Buf, (int)); E.MESSAGELENGTH, (INT) E.MESSAGELENGTH);} // triggers Packetarrival Event OnPacketarrival (e);} Everyone is noted, on it In the function, we use the so-called unsafe code of the pointer, which can be seen in the C #, the pointer and shift operations can also bring programmers to make programmers. Declaring the PacketarriveDeventargs class object in a function to pass the information package information through the event through the event. The PacketarriveDeventargs class is the nested class in the RawSocket class, which inherits the system event (Event) class, encapsulated information contained in other data clauses of packets, ports, protocols. In the function of starting the received packet, we use the asynchronous operation method. The following functions open the interface of asynchronous listening: public void run () // Start listening {ioasyncResult ar = socket.beginReceive (Receive_BUF_BYTES, 0, LEN_RECEIVE_BUF, SocketFlags .None, new asyncCallback (callReceive), this);} Socket.BeginReceive function returns an asynchronous operation interface, and declares that the asynchronous callback function CallReceive is declared in the generated function of this interface, and streams the received network data to receive_buf_bytes, so that the interface can be used with a parameter of asynchronous operation asynchronous callback continuously receive packets: private void CallReceive (IAsyncResult ar) // asynchronous callback {int received_bytes; received_bytes = socket.EndReceive (ar); receive (receive_buf_bytes Received_bytes); if (KeePrunning) Run ();} This function receives a new packet when it hangs or ends asynchronous read, so that each packet can be detected by programs.

Below is achieved and external communications through a proxy statement event handler: public delegate void PacketArrivedEventHandler (Object sender, PacketArrivedEventArgs args); // event handler: raise an event public event PacketArrivedEventHandler PacketArrival packet arrives; // declaration handler function of time so that you can achieve The acquisition of packet information is used, and the asynchronous callback function can improve the efficiency of the received data packet, and pass the package information to the outside through the proxy event. Since all the package information can be passed, you can achieve the analysis of the packet :) But RawSocket's task has not finished, and finally don't look close to the socket: public void shutdown () // Close Raw Socket { IF (socket! = null) {socket.shutdown (socketshutdown.both); socket.close ();}} The RawSocket class has introduced the information in the package by constructing the IP header, and implements a packet through the asynchronous callback function. Receive, and use the time agent handle and the custom packet information event class to send the packet information to the monitoring of the network packet, so we can add some functions to the data package outside. [Network Programming] MX record acquisition component writing 2004-9-4

First, the application of MX record Email is the most successful application of the Internet so far. Improve the networker if there is no own Email mailbox, it will be difficult to call "netizen". The Internet is also increased with Email on the Internet. When we write network applications, we often need to involve embed Email applications into their own applications. This application is often used by using third-party Email related components, completes Email's writing, sending, charged, and decoding. During the transmission of email, the NT platform generally uses the CDO component comes with the NT system, and such components often rely on the service or a specific SMTP server in this unit, and cannot ensure that the letters arrive, and because There is a relay delay, and there is a possibility of lost letters. Therefore, many masters use direct Email to deliver Email sent directly to the destination mailbox, and implement this function key is three points: 1. MIME encoding 2, SMTP session control 3, MX record acquisition. Through my understanding, there are many articles about 1, 2 on the Internet. There are many corresponding referenced code, and for how to locate the target mailbox receiving server - get the MX record of the mailbox domain name, very few detailed discussion, search related components get one ASP environment server components (charges), there is no detailed source code. Based on this, I have written a similar component by learning Java language to help you understand the details of this. Second, DNS record classification and principles We know that the domain name system is the cornerstone of the Internet, we can access your interested websites through the names such as Yahoo.com is completely domain name system. When you tap www.yahoo.com, your browser doesn't know where the page pointing from this domain name, you will ask the set domain name resolution server (DNS), the domain name resolution server Looking for whether there is a request domain record, if there is a direct return, there is no need to resolve the server request (forward request) to the top level domain, until finally find the resolution record of this domain, tell you Which IP address is to get the relevant content. Such a process of converting the Internet domain name into a corresponding IP is called parsing. There are a variety of domain names, common with a (address) record, alias (CNAME) record, MX (mail exchange) record. A Record is a record of a domain name corresponding to an IP. The www.yahoo.com just mentioned corresponds to a query process of a record. Alias ​​records are mainly used to replace the domain name that already exist a record, such as www.yahoo.coms, www.yahoo.akadns.net, and further resolution www.yahoo.akadns.net Will get IP address 64.58.76.177. Mail exchange records are used for mail delivery. For example, you need to send an email to abc@yahoo.com, in general, your ISP's SMTP server (professional terminology is a mail agent, instead of user delivery) will accept letters you sent, then SMTP server Query Yahoo.com's MX records, see which server should be sent to which server, if you query Yahoo.com's MX record, then your ISP's SMTP server creates the TCP connection of the server specified by the MX record, and transmits Out your email to the destination server, complete the delivery process of Email. If you get a domain name of the MX record, you get the way to direct delivery of the email, and you must get this MX record, you must be able to make your request to the domain name server DNS, but also understand DNS returned Information, read the content you need.

The details of this are relatively complicated. I will briefly introduce it here: the client sends a UDP message to the 53-port of the DNS, then the server query (in the middle may be forwarded or referred to as recursive query), send back the client needs record, Also UDP packet. The general format of this type of message is: | 2 bytes Identifier | 2 bytes of logo | 2 bytes of problem number | 2 bytes of resource record number | 2 bytes authorized resource record number | 2 words Extra resource record number | Query domain (not fixed length) | Document (length is not fixed) | Authorized resource record (length not fixed) | Extra record information (length not fixed) identification field is used to point out The number of the message is generally specified by the customer. When the DNS server returns information, this identity is told to the client to answer which request. The 16-bit division of the flag field is 8 subfields, from left to right (high to low): QR 1 bit: 0 query packet 1 Response message OPCode 4 bit: usually 0, representation standard query, 1 reverse Query, 2 Server status query AA 1 bit: It is used to return packets, indicating whether it is an authorized answer TC 1 bit: Due to the length limit of UDP itself, it will tend to cut off the content of 512 bytes. This bit indicates whether the RD 1 can be truncated Bit: This is used to set it in the query message and returned by the server response packet. This bit tells the server to handle this query. If this bit is 0, and the authorized answer number is 0, then the server must return a list of other servers that can answer the query RA 1 bit: If the server supports recursive, then The server sets this bit in the response message. The subsequent 3bit must be 0 rcode 4 bit: Last to return code, 0 no error, 3 words error, that is, there is no record of the domain name to query on the server, generally used to return from the final authorization name server. The query problem is constructed by the query name query type query class. The query name consists of the sequence of multiple identifiers, each identifier, indicating the length of the identifier, and finally the name is ended by byte 0. For example, Cn..yahoo.com consists of 2 C N 5 Y A H O O 3 C O M 0. If this domain is used later, it is generally used to use a compressed format, then the first byte is not length, but a byte of the highest bit of 1, typically 0xc0, because the length of the identifier is not exceeding 64. After the symbol byte of the compression format is the offset value of the original identity of the domain name. The query type is 2 bytes, and 1 means A record query 5 indicates that the CNAME record query 15 represents the MX record query. Class represents whether it is Internet data. Recognize Record in the response message by the domain name (length not fixed) type (2 bytes) class (2 bytes) Survival time (4 bytes, second number) resource data length (2 bytes) resource data (not fixed) . The domain name format is the same as the query domain name format. Type, interpretation of the class with the query problem section. Resource data is different depending on the record type. If we follow the above format simultaneously combined with your needs, you can receive a UDP report to the server with a domain name resolution function, so that we can get the record. Therefore, it is mainly difficult to analyze the construction and response packets. Third, write the MX record component to start VJ. A DLL project is required, then modify the name of the main class name as the you want, the project is established.

If everything is correct, you will get a DLL, with a domain name MX record query function. The specific code is seen from the source code analysis. The key is to construct query packets and interpretation of response messages in conjunction with your own query problem, and the server's network connection is UDP mode. For the sake of debugging, I added a main function entry, which can be used to call JVEW in the DOS window to see the result of the domain name MX record, and it is also convenient to debug. 4. Application examples and expansion There is such a query component, which can directly obtain the MX record of the domain name, which can help us get the delivery of Email. At the same time, we also know that the location of the domain name mail receives the server, and we can also get the mailbox user name by the post office of the domain name. You may be surprised by others, it is likely that some Email searches are queried by the MX record to get the address of the mail server, and then obtain different usernames by special username generation algorithms, one by one to get a mailbox. Add this feature in your application, you can pass your mail to the target, not dependent on the components on the server. Even you can write an email forwarding server, serving your user (the general sender is not open). V. Source and analysis Code: [Copy to Clipboard]

Import java.net. *; import java.io. *; import java.util.StringTokenizer; // Import java.util. *; / ** * this class is designed to be packaged with a com DLL OUTPUT FORMAT. * The class has no standard entry points, other than the constructor. * Public methods will be exposed as methods on the default COM interface. * @ com.register (clsid = 20AE856F-E2F8-488E-B41E-753E6BEBD375, typelib = 36611559-AFAB- 479E-8572-9A0B1F06CCDA) * / public class MXDNS {private String theDnsServer; // current address private DatagramPacket outPk DNS server; // send packets private DatagramPacket inPk; // receive packet private DatagramSocket UDPSocket; // UDP connection Tao Word Private INETDRESS DNSSERVERIP; // Domain Name Resolution Server IP Private INT POSITION, ID, LENGTH; // Analysis DNS Record Multi-String Dmname; // Query Domain Private MXRECS; // DNS Answer record private Static Int DNS_Port = 53; // DNS Service Port Private Byte [] PKDATA = New Byte [512]; // Get 512 bytes of Packet PUBLIC MXDNS () // Constructor {ID = (New Java.util. Date ()). GetSe CONDS () * 60 * (new java.util.random ()). nextint (); // Get unique ID} // The following is exposed attributes and methods public void setDnsserver (String DNSSERVER) {these DNSERVER = DNSSERVER; //// Set the name of the current DNS server or IP} public string getMxRecords (String DM) // Get all DNSMX record arrays {Return (GetMxRecords (DM, THEDNSSSERVER));} public string getMxRecords (String DM, String DNSSERVER) {Try { DNSServerIP = InetAddress.getByName (DNSServer); // get the DNS server InetAddress outPk = new DatagramPacket (pkdata, pkdata.length, DNSServerIP, DNS_PORT); // outgoing data packets UDPSocket = new DatagramSocket (DNS_PORT);

// Packet Port MakednsQuery (ID, DM); // Generate Query Packet UDPSocket.send (OUTPK); // Send Data Packet Inpk = New DataGrampacket (Pkdata, Pkdata.length); UDPSocket.Receive (InPK); / / Receive returned response packets pkdata = INPK.GETDATA (); // Get the word length = pkdata.length;} catch (socketexception se) {} catch (ooException ie) {} recaturn (GetResponse ()); // Analysis Returned Data Packet, Get Record Results} PUBLIC VOID MAKEDNSQUERY (INT ID, STRING DM) {// Generate Query Data in PKDATE BYTE array (int i = 0; i <512 ; i ) {pkdata [i] = 0;} PKDATA [0] = (byte) (ID >> 8); pkdata [1] = (byte) (id & 0xff); // Query ID 2 byte PKDATA [2] = (byte) 1; // Qrbit bit is 1, indicating that the query packet pkdata [3] = (byte) 0; pkdata [4] = (byte) 0; pkdata [5] = (Byte) 1 ; // 1 problem pkdata [6] = (byte) 0; // resource record number, authorization resource record, extra resource records are 0, because it is query packet pkdata [7] = (byte 0; pkdata [8] = (byte) 0; pkdata [9] = (byte) 0; pkdata [10] = (byte) 0; pkdata [11] = (byte) 0; StringTokenizer st = new StringTokenizer (DM) , "."); //. Separation domain name String label; position = 12; // Start generation query problem from the 12th byte (st.hasmoretokens ()) {label = st.nextToken (); pkdata [position ] = (Byte) (Label.Length () & 0xFF); // Convert to byte byte [] b = label.getbytes (); for (int J = 0; j

}}} PKDATA [POSITION ] = (byte) 0; // End the domain pkdata [position ] = (byte) 0; pkdata [position ] = (byte) 15; // Query type 15 Represents MX record query PKDATA [Position ] = (byte) 0; PKDATA [POSITION ] = (Byte) 1; // Internet Data Record Query} // Construction Query Complete Private String getResponse () // Get feedback information {string temp = ""; int qcount = (PKDATA [4] & 0xFF) << 8) | (PKData [5] & 0xFF); // Get the number of questions if (qcount <0) {return (");} // problem number less than 0, return Empty string int access = ((PKDATA [6] & 0xFF) << 8) | (PKData [7] & 0xFF); // Get a number of answers if (ACOUNT <0) {return (");} / / Quality less than 0, return empty string POSITION = 12; // Query problem part of the start position for (int i = 0; i

} Private int proc (int position) {// This process looks for domain name int LEN = (PKDATA [POSITION ] & 0xFF) from the PKData byte array; // Take the length IF of the identifier to process the identifier (LEN == 0) // Without other identifiers, end returning {return position;} int offset; // Offset Do {IF ((Len & 0xC0) == 0xc0) // Compressed format {if (position> = length) // More than package Size, then offset obviously wrong {return (-1);} offset = ((Len & 0x3F) << 8) | (PKData [POSITION ] & 0xFF); // Get offset Proc (Offset) of the compressed source identifier; // Recursive call Gets the name return (position) before compression;} else // Non-compressed format {IF ((position len)> length) // exceeds length {RETURN (-1);} Dmname = new string (PKDATA , position, len); // Get the domain name identifier various parts POSITION = LEN;} if (position> length) {return (-1);} len = pkdata [position ] & 0xff; // Last 0 ending IF (len! = 0) {DMNAME = "."; // plus. Compositions the complete domain name }}}} While (position);} public static void main (string args []) throws exception {mxDNS MX = new mxdns (); string s = mx.getmxRecords ("sina.com", "202.96.128.68"); system.out.println (s); int k = system.in.read ();}} Click here to download source code >>

[Network Programming] Raw Socket Programming in Windows 2000 2004-9-3

Windows2000 has made a lot of improvements on TCP / IP protocol components, and features are also enhanced. For example, the adjustment on the protocol stack increases the default window size, and the high delay link new algorithm. At the same time, IPSec is applied to security, and there are many improvements in NT. The Microsoft TCP / IP component contains "core protocol", "service", and "interfaces" between the two. Transmission Driver Interface (TDI) and Network Device Interface Specification (NDIS) are public. In addition, there are more advanced interfaces for many user model applications. The most common interface is Windows Sockets, Remote Process Call (RPC) and NetBIOS. Windows Sockets is a programming interface that is defined on the basis of the socket interface developed by the University of California. It includes a set of expansions to make full use of the Microsoft Windows messaging feature. The normative version 1.1 is released in January 1993, and version 2.2.0 was issued in May 1996. Windows 2000 supports Winsock 2.2. In Winsock2, the original socket, overlapping I / O model, service quality control, etc. are supported by multiple transport protocols. Here you will introduce some of the programming of the Raw Socket for Windows Sockets. Compared with Winsock1, the most obvious is to support the RAW Socket socket type. Through the original socket, we can control a variety of protocols under Windows, and can control the transmission mechanism of the network underlayer. 1. Create an original socket and set the IP header option. Socket Sock; Sock = Socket (AF_INET, SOCK_RAW, IPPROTO_IP); or: S = WSASOCCKET (AF_INET, SOCK_RAW, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED); Here we set up the SOCK_RAW flag, indicating that we declare a raw socket Word type. After creating the original socket, the IP header will be included in the received data, if we set the IP_HDRINCL option, then you need yourself to construct an IP header. Note that if the IP_HDRINCL option is set, you must have Administrator permissions, or you must modify the registry: hkey_local_machine / system / currentControlSet / Services / AFD / Parameter / Modification: DisableRawSecurity (Type DWORD), modify the value to 1. If not, add it. Bool Blnflag = true; setsockopt (Sock, Ipproto_IP, IP_HDRINCL, (Char *) & blnflag, Sizeof (BLNFLAG); For the primitive socket, pay attention to this: 1. If the received datagram The protocol type and the defined original socket match, then all the received data is copied into the socket. 2, if the local address is bound, only the corresponding remote address matching, received by the received data IP header Data is copied into the socket. 3, if the external address is defined, for example, using connect (), then only the corresponding source address matching in the received data IP header is copied into the socket.

2, construct IP head and TCP head here, provide IP head and TCP head structure: // standard TCP Flags #define urg 0x20 #define Ack 0x10 #define psh 0x08 #define rst 0x04 #define syn 0x02 #define Fin 0x01 TypedEf struct _iphdr // Define IP head {UNSIGNED Char h_lenver; // 4 top length 4-bit IP version number UNSigned char TOS; // 8-bit service type TOS unsigned short total_len; // 16-bit total length (byte) unsigned short Ident; // 16-bit identifier unsigned short frag_and_flags; // 3 bit flag bit unsigned char TTL; // 8-bit survival time TTL unsigned char proto; // 8-bit protocol (TCP, UDP, or other) Unsigned Short Checksum; // 16-bit IP header checksum unsigned int sourceip; // 32 bitsource IP address unsigned int desip; // 32 bit ip address}}}}}}}}}}}}}}}}}}}}}}}}}}}} {UNSIGNED Long Saddr; // Source address Unsigned long daddr; // destination address char MBZ; char PTCL; // protocol type unsigned short tcpl; // TCP length} PSD_HEADER; typef struct _tcphdr // Define TCP header {ushort th_sport; // 16-bit source port Ushort TH_DPORT; // 16-bit destination port unsigned int th_seq; // 32-bit serial number UNSIGNED INT TH_ACK; // 32 bit confirmation number unsigned char th_lenres; // 4 top length / 6 reserved word unsigned char th_flag; // 6 bits flag Bit Ushort TH_WIN; / / 16-bit window size ushort th_sum; // 16-bit checksum USHORT TH_U RP; // 16-bit emergency data offset} TCP_Header; TCP fake department is not really existing, just for calculating inspection and. Calibration and function: ushort checksum (ushort * buffer, int size) {unsigned long cksum = 0; while (size> 1) {CKSUM = * buffer ; size - = sizeof (ushort);} if (size) {CKSUM = * (Uchar *) Buffer;} CKSUM = (CKSUM >> 16) (CKSUM & 0xFFFF); CKSUM = (CKSUM >> 16); return (~ cksum);} When you need you to fill IP When the head and TCP head, it is necessary to calculate their inspection and. 3. Send the original socket datagram to populate these heads slightly troubles, and send more simple. Just use Sendto () OK. Sendto (SOCK, (CHAR *) & TCPHEADER, SIZEOF (TCPHEADER), 0, (SockAddr *) & addr_in, sizeof (addr_in)); the following is a sample program that can be used as part of the SYN scan. # include # include

#include

#define Source_Port 7234

#define max_receivebyte 255typedef struct ip_hdr // Defines IP header

{

Unsigned char h_verlen; // 4 top length, 4-digit IP version number

Unsigned char TOS; // 8 service type TOS

Unsigned short total_len; // 16-bit total length (bytes)

UNSIGNED short ident; // 16 bit identity

Unsigned short frame_and_flags; // 3 bit flag

Unsigned char TTL; // 8-bit survival time TTL

Unsigned char proto; // 8-bit protocol (TCP, UDP or other)

UNSIGNED Short Checksum; // 16-bit IP header checksum

Unsigned int sourceip; // 32 bitsource IP address

UNSIGNED INT Destip; // 32 bit IP address

} Ipheader;

Typedef struct tsd_hdr // Defines TCP pseudo

{

Unsigned long saddr; // source address

Unsigned long daddr; // destination address

CHAR MBZ;

CHAR PTCL; // Protocol Type

Unsigned short tcpl; // TCP length

} PSDHeader;

Typedef struct tcp_hdr // Defines TCP header

{

Ushort th_sport; // 16 bond source port

USHORT TH_DPORT; / / 16-bit destination port

Unsigned int th_seq; // 32-bit serial number

UNSIGNED INT TH_ACK; / / 32 bit confirmation number

Unsigned char th_lenres; // 4 head length / 6 reserved words

Unsigned char th_flag; // 6 bit flag

Ushort TH_WIN; / / 16-bit window size

USHORT TH_SUM; / / 16-bit checksum

Ushort TH_URP; // 16-bit emergency data offset

} TCPHEADER;

// Checksum: Computing the subunies of the checksum

Ushort Checksum (Ushort * Buffer, int size)

{

Unsigned long cksum = 0;

While (size> 1)

{

CKSUM = * Buffer ;

Size - = SizeOf (Ushort);

}

size

{

CKSUM = * (Uchar *) BUFFER;

}

CKSUM = (CKSUM >> 16) (CKSUM & 0xFFF);

CKSUM = (CKSUM >> 16);

Return (Ushort) (~ CKSUM);

}

Void users ()

{

Printf ("**************************************************************************************************************);

Printf ("tcpping / n");

Printf ("/ T Written By Refdom / N);

Printf ("/ t email: refDom@263.net/n");

Printf ("Useage: tcpping.exe target_ip target_port / n");

Printf ("******************************************************************************************************) ;

}

Int main (int Argc, char * argv [])

{

Wsadata wsadata;

Socket sock;

SockAddr_in addr_in;

Ipheader ipheader; tcpheader tcpheader;

PSDHeader PSDHead;

Char szsendbuf [60] = {0};

Bool flag;

Int Rect, NTimeover;

Useage ();

IF (argc! = 3)

{Return False;}

IF (WsaStartup (MakeWord (2, 2), & WSADATA)! = 0)

{

Printf ("WSAStartup Error! / N");

Return False;

}

IF ((SOCK = WSASOCKET (AF_INET, SOCK_RAW, IPPROTO_RAW, NULL, 0, WSA_FLAG_OVERLAPPED) == Invalid_socket

{

Printf ("socket setup error! / n");

Return False;

}

Flag = true;

IF (setsockopt (sock, ipproto_ip, ip_hdrincl, (char *) & flag, sizeof (flash) == Socket_ERROR)

{

Printf ("setsockopt ip_hdrincl error! / n");

Return False;

}

NTIMEOVER = 1000;

IF (setsockopt (SOCK, SOL_SOCKET, SO_SNDTIMEO, (CHAR *) & ntimeover, sizeof (ntimeover)) == Socket_ERROR)

{

Printf ("setsockopt so_sndtimeo error! / n");

Return False;

}

Addr_in.sin_family = af_INet;

Addr_in.sin_port = htons (ATOI (ARGV [2]));

Addr_in.sin_addr.s_un.s_addr = inet_addr (Argv [1]);

//

//

// Fill the IP head

Ipheader.h_verlen = (4 << 4 | sizeof (iPheter) / sizeof (unsigned long);

// ipHeader.tos = 0;

Ipheter.total_len = Htons (Ipheter) SizeOf (TCPHEADER);

ipHeader.Ident = 1;

Ipheader.frag_and_flags = 0;

Ipheader.ttl = 128;

Ipheader.proto = ipproto_tcp;

Ipheader.checksum = 0;

Ipheader.SourceIP = INET_ADDR ("Local Address");

Ipheader.destip = inet_addr (argv [1]);

// Pack TCP header

TCPHEADER.TH_DPORT = HTONS (ATOI (ARGV [2]));

TCPHEADER.TH_SPORT = HTONS (Source_Port); // Source Port number

TCPHEADER.TH_SEQ = HTONL (0x12345678);

TCPHEADER.TH_ACK = 0;

TCPHEADER.TH_LENRES = (SIZEOF (TCPHEADER) / 4 << 4 | 0);

TCPHEADER.TH_FLAG = 2; // Modify here to achieve different flag detections, 2 is SYN, 1 is Fin, 16 is ACK detection, etc.

TCPHEADER.TH_WIN = HTONS (512); tcpheader.th_urp = 0;

TCPHEADER.TH_SUM = 0;

PSDHeader.saddr = ipheader.sourceIP;

PSDHeader.daddr = ipheader.destip;

PSDHeader.mbz = 0;

PSDHeader.ptcl = ipproto_tcp;

PSDHeader.tcpl = htons (sizeof (tcpHeader));

// calculate the checksum

Memcpy (Szsendbuf, & Psdheader, Sizeof (psdheader));

Memcpy (Szsendbuf Sizeof (Psdheader), & Tcpheader, Sizeof (TCPHEADER);

TCPHEADER.TH_SUM = Checksum ((Ushort *) SzsendBuf, SizeOf (PSDHeader) Sizeof (TCPHEADER));

Memcpy (SzsendBuf, & ipheader, sizeof (ipheader);

Memcpy (szsendbuf sizeof (ipheader), & tcpheader, sizeof (tcpheader);

MEMSET (SzsendBuf SizeOf (Ipheter) Sizeof (TCPHEADER), 0, 4);

Ipheader.checksum = Checksum ((Ushort *) SzsendBuf, SizeOf (Ipheader) Sizeof (TCPHEADER));

Memcpy (SzsendBuf, & ipheader, sizeof (ipheader);

Rect = Sendto (Sock, Szsendbuf, Sizeof (Ipheader) Sizeof (TCPHEADER),

0, (struct socketdr *) & addr_in, sizeof (addr_in));

IF (Rect == Socket_ERROR)

{

Printf ("Send Error!:% D / N", wsagetlasterror ());

Return False;

}

Else

Printf ("Send OK! / N");

CloseSocket (SOCK);

WSACLEANUP ();

Return 0;

}

4, receive data

Receive is more troublesome compared to sending original socket data. Because we can't use RECV () to receive data on the Raw Socket, all IP packages are first submitted to the system core, then transfer to the user program, when sending a Raws socket package (for example SYN), the core does not know, there is no record that is sent or connected or connected, so when the remote host responds, the system is lost all of them, thus can't get the application. Therefore, it is not possible to simply use the received function to receive these datagrams.

To reach the purpose of receiving data, you must use sniffing, receive all passed packets, and then filter, leaving the need to meet what we need. A raw socket can be defined, used to complete the task of receiving data, need to set SiO_RCVALL, indicating that all data is received.

Socket sniffersock;

Sniffsock = WSASOCKET (AF_INET, SOCK_RAW, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);

DWORD LPVBUFFER = 1; DWORD LPCBBYTESRETURNED = 0;

WSAIOCTL (SnifferSock, SiO_RCVALL, & LPVBUFFER, SIZEOF (LPVBUFFER), NULL, 0, & LPCBBYTESRETURNED, NULL, NULL

Create a raw socket for receiving data, we can use the received function to receive the packet. Then use a filter function to reach the desired purpose to receive the packet we need.

At http://www.xici.net/board/doc.asp?id=5891453&sub=15 provides a complete sector, which can be referred to.

* .CS how a file is compiled into a DLL file ah (thank you) http://community.9cbs.net/Expert/topic/3367/3367953.xml?temp=.1050531 Author:? Lyzhong79 (Snow Wolf Lake) rating : Reputation value: 100 community: .NET technology C # problem points: 10 Reply number: 4 Posted: 2004-09-13 16:29:34 For me in a project, add a * .cs file, when I This * .cs file is not compiled into * .dll files when compiling this project. Is it a * .cs file added in a project, not a new Class project, please ask everyone, how can you compile it into a * .dll file? Thank you, replying: zyhjolly (zyhjolly) () Reputation: 100 2004-09-13 16:43:00 Score: 0 If you want to compile this CS file into a DLL, create a "class library" project, add the CS file in Compile Top Reply to: Designonline () Reputation: 100 2004-09-13 16:43:00 Score: 0 In the project, only one DLL If you need a separate compilation command line example compile File.cs to produce File.exe: CSC file.cs Compile File.cs to generate file.dll: CSC / Target: library file.cs compile file.cs and create My.exe: CSC /out :my.exe file.cs by using optimization and Define Debug symbols, compile all C # files in the current directory. The output is file2.exe: CSC / Define: debug / optimize /out:file2.exe * .cs compiles all C # files in the current directory to generate file2.dll debug versions. No logo and warning: CSC / target: library /out:file2.dll / warn: 0 / noogo / debug * .cs Compile all the C # files in the current directory to Something.xyz (a DLL): CSC / Target : library /out:something.xyz * .cs

Top Reply: Laodeng72586 (only passing) () Reputation: 100 2004-09-13 16:49:00 Score: 0 example: Start the command line tool for VS.NET; CSC / T: library /R :system.dll, System.Web.Services.dll, System.xml.dll Service1.cs Description: CSC: C # compiler If it is VB.NET should be VBC / T: library compile results for DLL file / R: is a referenced namespace service1. CS is the code file to be compiled Top Reply to: lyzhong79 (Snow Wolf Lake) () Reputation: 100 2004-09-13 16:58:00 Score: 0 Can you automatically compile with VS? Do you have a very troublesome first floor Method, you need to create a new project - class library (this method is mainly inconvenient to debug, if you compile the DLL file, add it into the project, if you have a wrong, then open the base library to check, recompile, re- Add) the second floor and the first floor is almost (not convenient) to build a library, can build a control unit in the class library, regenerative achievements can be newly built a project 1, project menu -> (project file name) -> Output type, selected class library.

Create a new project 2, add an app, click Browse, find the generated class library, determine the generated class library

Add a namespace of the USING project in the source code of the item 2.

This is no problem.

The following code is always used in VB.NET, with C # writes me a lot of time. public static MainForm FormDefInstance; public static MainForm DefInstance {get {if (FormDefInstance == null || FormDefInstance.IsDisposed) {FormDefInstance = new MainForm ();} return FormDefInstance;} set {FormDefInstance = value;}} static void Main () {Application.enableVisualStyles (); Application.doevents (); // Handling the current message queue. Application.Run (MainForm.DefInstance);} http://www.vbcity.com/forums/topic.asp?tid=32927&highlight=regex|email Private Function IsValidEmail (ByVal UncheckedEmailAddr As String) As Boolean Const RegExpr As String = " ^ ([A-ZA-Z0-9 _ / - /.] ) @ (/ [[0-9] {1, 3} /. [0-9] {1,3} /. [0-9 ] {1,3} /.) / (([A-ZA-Z0-9 / -] /.) )) ([A-ZA-Z] {2,4} / [0-9] { 1,3}) (/]?) $ "Dim InputEmail As String = UncheckedEmailAddr.Trim Try Dim r As New Regex (RegExpr) If r.IsMatch (InputEmail) Then Return True Else Return False End If Catch ex As Exception DebugWriter ( "Source of Error:" & Ex. Source & Vbcrf & _ "Error Message:" & ex.Message) End Try End Function / * * Module Name: Operation INI file. * Module Source: Internet, * Note: Waiting for the empty functionality of this module. * Time: September 9, 2004 * / using system.text; using system.io; using system.Runtime.InterOpServices;

Namespace CXH_MK {

///

/// INI file operation class /// public class inifile {private string filename; // ini full file name

// read API function declaration INI file [DllImport ( "kernel32")] private static extern long WritePrivateProfileString (string section, string key, string val, string filePath); [DllImport ( "kernel32")] private static extern int GetPrivateProfileString Structure, string key, string def, stringbuilder return, int size, string filepath; ///

/// /// INI file name public inifile (string inifilename) {if (inIfilename.indexof (@ ": //") == -1) {filename = system.windows.forms.Application.startuppath "//" inIfilename;} else {filename = inifilename;}}}}

///

/// write INI file /// /// section /// keyword /// Value public void WriteValue (String Section, String Key, String Value) {WritePrivateProfileString (Section, Key, Value, this.filename);} // < Summary> /// Write INI file /// /// section /// keyword // / Value /// INI file So path public static void Writevalue (String Section, String Key, String Value, String FileName) {WritePrivateProfileString (Section, Key, Value, FileName);

///

/// read INI file specified /// /// section /// keyword /// the specified section - keyword value public string ReadValue (string section, string Key) {StringBuilder temp = new StringBuilder (255); int i = GetPrivateProfileString (section, Key, "" "" ", TEMP, 255, this.filename); return temp.tostring ();} /// /// read INI file specified /// /// Section /// Keyword /// Specify the value of the key /// the path ini file public string ReadValue (string Section, string Key, string filename) {StringBuilder temp = new StringBuilder (255); int i = GetPrivateProfileString (Section, Key," ", temp, 255, filename Return Temp.toString ();

}

} // INIFILE INI = New INIFILE ("User.ini"); // txttto.text = INI.ReadValue ("MailInfo", "To"); // txttoname.text = ini.readvalue ("MailInfo", " "); // txtFrom.text = INI.ReadValue (" MailInfo "," from "); // txtfromname.text = INI.ReadValue (" MailInfo "," fromname "); // txttitle.text = INI. Readvalue ("Mailinfo", "Title"); // IF (Ini.ReadValue ("MailInfo", "DNS") == "" // {// txtdns.text = DNSQUERY.DNSSERVER; ////} // else // {//// txtdns.text = Ini.readValue ("MailInfo", "DNS"); //} // txtbody.text = Ini.ReadValue ("MailInfo", "Body"); / / INIFILE INI = New INIFILE ("User.ini"); //ini.writevalue ("mailinfo ", "to" ,mail.to); //ini.writevalue ("mailinfo ", "toname" ,mail.toname ); //Ini.writevalue ("mailinfo ", "from" ,mail.from); //ini.writevalue ("mailinfo", "fromname",mail.fromname); //ini.writevalue ("mailinfo ", "Title", mail.title; //ini.writevalue ("mailinfo "," DNS ", DNSQUERY.DNSSERVER); //ini.writevalue ("mailinfo"," body" ,mail.body); // mail .attachmentlist = new string [lstfile.Items.count ]; // if (Lstfile.Items.count! = 0) // {// Lstfile.Items.copy, 0); //} // Thread TH = New ThreadStart (this.run ))); // th.Start ();

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

New Post(0)