Everyone knows the Wry.dll file, it is a global IP address allocation table, there is a location corresponding to the IP address, using this table, can easily find its address location according to the IP you know. Although it is defined as a DLL file, it is actually a database. This article is to tell everyone its structure and write a query program example. If you already know, or you don't know such a simple play, Oh, please leave. ^ _ ^
The main idea of the program is simple. According to IP or Host, the data in each Wry.dll is compared, and the information is displayed. This program must have a Wry.dll file.
Wry.dll consists of thousands of records, since it is a record, then it has its structure, in fact, each record contains 5 fields, STARTIP (17), Endip (17), Country (16), Local 54), Thank (23), the length of the field is the length of the field, in bytes. The meaning of each field is as follows:
Startip: The start address endip: a termination address of an IP address section Country: The country local locked the address segment: the specific location of the country where THANK: this information
Ok, since I know the structure of the record, then we can define its data structure as follows:
Struct_wry {char startip [17]; char allry [16]; char local [54]; char thanks [23];
Next, I know this most important message, we can write a program to display information according to IP or Host.
However, the IP address length of the string is up to 15 (xxx.xxx.xxx.xxx), why is the length of Startip and Endip in the record to be 17? If you write simple programs to test some The first character of these two fields is space, the last character is NULL, the real address starts from the second character, ie & (startip [1]), recognizing this is also very important.
In addition, the IP string length in each record is 15, ie 11.123.11.2 IP string in the form of the IP string is 011.123.011.002, then if it is not to be queried, it is more troublesome. Therefore, it is necessary to convert an IP string into a standard format of 15 lengths.
Also, all records in the Wry.dll file are all structures, special is the first record, tested, to know that its length is 320-128 = 192, while the length of all other records is 128.
The last question is if the Query is compared to the Query: if (the I Ok, all the places you need to pay attention to, start writing the program below. The write code is as follows: *********************** *********************************************************** ******************** File: // in wry.dll, The Startip and endip format is: // len = 17, And THE FIRST IS Blank and The Last Is Null #include #pragma comment (Lib, "WS2_32.LIB") Struct_wry {char startip [17]; char allry [16]; char local [54]; char thanks [23]; Struct_wry * wry; Char Str [320]; char TEMP [16]; struct _wry maxw [10]; int count = 0; Char * Convertip (const char * ip) {char * TEMP2; INT I, J; STRCPY (TEMP, "000.000.000"); TEMP2 = (Char *) Malloc (Strlen); STRCPY (TEMP2, IP) ; J = Strlen (TEMP2); for (i = 0; i For (i = 0; i <4; i ) {strncpy (& Temp [i * 4 3-strlen (TEMP2)], TEMP2, STRLEN (TEMP2)); TEMP2 = Strlen (TEMP2) 1;} Return Temp; INT DO_DIS (Const Char * IP) {IF (Strncmp (& (& (Wry-> Endip [1]), IP, Strlen (IP)) <= 0) RETURN 0; IF (Strncmp (& (Wry-> Startip [1]), "000.000.000", 15) == 0) Return 0; IF (Strncmp (& (Wry-> Startip [1]), IP, Strlen (IP)) <= 0) Memcpy (& Maxw [Count ), Wry, SizeOf (Struct_wry)); Return 1; } Void Main (int Argc, char ** argv) {WSADATA WSD; struct hostent * h = null; file * fp = null; int i = 0; char CIP [16]; IF (argc! = 2) {Printf ("USAGE% s [ip | host] / n", argv [0]); return;} IF (WSAStartup (MakeWord (2, 2), & WSD)! = 0) RETURN; if (INET_ADDR (Argv [1]) == INADDR_NONE) {IF ((h = gethostbyname (argv [1]))! = null) STRCPY (* (struct in_addr *) h-> h_addr); Else {Printf ("Resolve Host To IP Failed / N); Return;}} Else Strcpy (CIP, Argv [1]); Convertip (CIP); Printf ("/ n ************************************************ *********** / n "); Printf (" CONVERT IP:% S / N ", TEMP); IF ((fp = fopen ("wry.dll", "r"))) == null) {printf ("can not open file!"); exit (0);} While (! feof (fp)) { IF (i == 0) {IF (FREAD (STR, SIZEOF (STR) - 128, 1, FP)! = 1) {Printf ("Read File Error / N"); exit (0);} else i = 1; Continue;} else if (FREAD (STR, 128, 1, FP)! = 1) {file: // read end of file} wry = (struct _wry *) str; wry-> thank [23] = '/ 0 '; Wry-> local [53] =' / 0 '; wry-> country [15] =' / 0 '; wry-> endip [16] =' / 0 '; Wry-> Startip [16] =' / 0 '; do_dis (temp); Fclose (fp); wsacleanup (); IF (count == 0) Printf ("not match found! / n"); else {printf ("Found% D match, and the best matchs: / n", count); printf ("==== ======================== / n "); Printf (" Startip:% S / N ", Maxw [- count] .startip; Printf "endip:% s / n", maxw [count] .endip); Printf ("country:% s / n", maxw [count] .country); Printf ("Local:% S / N", Maxw [Count ] .local); Printf ("***************************************************** ********** / n / n "); } ********************************************************* *************************************************** This program is in Windows Compile, compile, compile, compile, compile, compile, and wry.dll files can be in address Http://brdw.myetang.com/vc/hunt.zip download. Thank you for seeing the last! ^ _ ^. Any Problem Please mailto: mrh123@etang.com. 2002.7.28