Domain name resolution using C language

Source: Internet
Author: User
Tags htons

Source: domain name resolution in C Language

The original text is modified a little. The original text may be compiled in the C ++ environment, for example, ". CPP "suffix file compiled,". the CPP file is compiled in C ++ by default, so if the original file is copied and saved as ". C "suffix, compilation is not good, many errors. The following is the modified Code. Change the DNS address to the DNS address of your region. After testing, you can directly use the Route IP address. Enter a domain name, for example, "www.baidu.net", without quotation marks. The IP obtained from the test "www.csdn.net" is not csdn in the browser. I don't know why, but other domain names are not tested.

Domain name resolution in C language:

# Include <stdio. h> # include <winsock2.h> # include <windows. h> typedef Enum {false = 0, true =! False} bool; typedef struct _ dnshead {// DNS header ushort ID; ushort tag; // DNS flag (parameter) ushort numq; // number of problems ushort NUMA; // number of answers ushort numa1; // number of authoritative answers ushort numa2; // number of additional answers} dnshead; typedef struct _ dnsquery // DNS query Structure {// char name [64]; // The queried domain name, which is a string between 0 and 63; ushort type; // query type. There are about 20 different types of ushort classes; // query class. Generally, Class A queries both IP addresses.} Dnsquery; # pragma comment (Lib, "ws2_32.lib") // initialize bool initwsa (); // display the error void displayerrwsa (char * Str ); // create a Socket socket createsocket (INT type); // UDP sendtoint mysendto (socket sockto, const char far * Buf, int Len, char * ADDR, ushort port ); // TCP connection bool myconnect (socket S, char * ADDR, ushort); // UDP recvfromint myrecvfrom (socket S, char far * Buf, int Len, char * ADDR, ushort port); // set the DNS header bool setdnshead (CH Ar * Name, char * BUF); int main (INT Arg, char * are []) {int result = 0; char Buf [1024] = {0 }; char ADDR [16] = "192.168.1.1"; // DNS server address char * name = 0; // The domain name to be queried socket sockto; int Len; dnshead * dnsh; char * getip; // int I; If (! Initwsa () // initialize {displayerrwsa ("initwsa Err! "); Return 1;} // create a socket If (sockto = createsocket (sock_dgram) = 0) {displayerrwsa (" creatsocket Err! "); Return 1;} while (1) {If (ARG <2) {char temp [1024] = {0 }; printf ("\ n enter the domain name to be queried:"); scanf ("% s", temp ); if (temp [0] = 'q' | temp [0] = 'q') {break;} name = temp;} else {Arg = 1; name = are [1] ;}// set the DNS header setdnshead (name, Buf); // The length of the sent request data Len = sizeof (dnshead) + sizeof (dnsquery) + strlen (name) + 2; // For (INT I = 0; I <50; I + = 2) // {// printf ("X", (uchar) buf [I]); // printf ("X", (uchar) BUF [I + 1]);/ /} // Send DNS request if (result = mysendto (sockto, Buf, Len, ADDR, 53) <= 0) {displayerrwsa ("sendto Err! "); Continue;} // receives the response if (result = myrecvfrom (sockto, Buf, 1024, ADDR, 53) <= 0) {displayerrwsa (" recvfrom Err! "); Continue;} // obtain the returned IP address (the last 4 bytes received). dnsh = (dnshead *) BUF; If (dnsh-> NUMA = 0) {printf ("the IP address of % s cannot be resolved. \ N ", name); continue;} getip = (char *) BUF + result-4; printf (" % s IP Address: ", name ); for (result = 0; Result <4; Result ++) {printf ("% u. ", (uchar) getip [Result]);} printf (" \ n ");} return 0;} // initialize the bool initwsa () {word wversionrequested; wsadata; int result; wversionrequested = makeword (2, 2); Result = wsastartup (wversionrequested, & wsadata); If (result! = 0) {return false;} If (lobyte (wsadata. wversion )! = 2 | hibyte (wsadata. wversion )! = 2) {wsacleanup (); Return false;} return true;} socket createsocket (INT type) {socket sock = socket (af_inet, type, 0 ); if (sock = invalid_socket) {return 0;} return sock;} int mysendto (socket sockto, const char far * Buf, int Len, char * ADDR, ushort port) {// set the socket and address structure for sending data to sockaddr_in addrto; addrto. sin_addr.s_un.s_addr = inet_addr (ADDR); addrto. sin_family = af_inet; addrto. sin_port = htons (port); Return sendto (sockto, Buf, Len, 0, (struct sockaddr *) & addrto, sizeof (struct sockaddr);} bool myconnect (socket sockto, char * ADDR, ushort port) {int result; // set the connected socket and address structure sockaddr_in addrto; addrto. sin_addr.s_un.s_addr = (inet_addr (ADDR); addrto. sin_family = af_inet; addrto. sin_port = htons (port); // connection result = connect (sockto, (struct sockaddr *) & addrto, sizeof (sockaddr_in); If (socket_error = Result) {return false;} return true;} int myrecvfrom (socket S, char far * Buf, int Len, char * ADDR, ushort port) {// set the socket to which data is sent and the address structure int addrlen; sockaddr_in addrfrom; addrfrom. sin_addr.s_un.s_addr = inet_addr (ADDR); addrfrom. sin_family = af_inet; addrfrom. sin_port = htons (port); addrlen = sizeof (sockaddr_in); Return recvfrom (S, Buf, Len, 0, (sockaddr *) & addrfrom, & addrlen );} int chname (char * fname, char * tname); // domain name conversion bool setdnshead (char * Name, char * BUF) {dnshead * dnsh; dnsquery * dnsq; int namelen; memset (BUF, 0, sizeof (dnshead); // set the header dnsh = (dnshead *) BUF; dnsh-> id = (ushort) 1; dnsh-> tag = htons (0x0100); dnsh-> numq = htons (1); dnsh-> NUMA = 0; dnsq = (dnsquery *) (BUF + sizeof (dnshead); namelen = chname (name, (char *) dnsq); // set Query Information dnsq = (dnsquery *) (char *) dnsq + namelen); dnsq-> classes = htons (1); dnsq-> type = htons (1); Return true ;} // display the error message void displayerrwsa (char * Str) {printf ("\ n % s, err = % d \ n", STR, wsagetlasterror ()); getchar () ;}// convert the domain name to int chname (char * fname, char * tname) {Int J = 0; int I = strlen (fname)-1; int K = I + 1; tname [I + 2] = 0; For (; I> = 0; I --, k --) {If (fname [I] = '. ') {tname [k] = J; j = 0;} else {tname [k] = fname [I]; j ++ ;}} tname [k] = J; return strlen (tname) + 1 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.