Learning between Unix concurrent technology and application on scanner

xiaoxiao2021-03-06  64

Learning between Unix concurrent technology and application on scanner

The previous concurrency technology is said to be a multi-process. I originally want to add synchronization and communication on the basis, but I haven't mastered it yet, I can only be replaced by multithreading.

Part 1 - Basic knowledge 1. Thread concept:

The thread is a great means to improve code response and performance, which is managed by the kernel according to time slice. The process can contain multi-threads, shared memory space between threads. Save Cup time, reduce system overhead. However, we must consider synchronization problems.

2. Related functions:

1.pthread_create () pthread_create to create threads prototype: #include extern int pthread_create (pthread_t * thread, const pthread_attr_t * attr, void * (* start_routine) (void *), void * arg); first The first parameter of the pointer to the thread identifier is used to set the third parameter of the thread attribute is the starting address of the thread run function. The last parameter is the parameter of the run function.

If the function start_routine does not require parameters, the last parameter is set to an empty pointer. The second parameter is an empty pointer, which will generate a thread of the default attribute.

When the thread is created, the function returns 0. If it is not 0, the creation thread fails, and the common error returns the code to eagain and EinVal. The former indicates that the system restrictions create new threads, such as excessive threads; the latter represents the thread attribute values ​​represented by the second parameter illegal. After creating a thread, the newly created thread runs the function of the parameter three and the parameter 4, and the original thread continues to run the next line of code.

2. Pthread_Join () pthread_join Waiting the thread ending the prototype: #include int pthread_join (pthread_t thread, void ** value_ptr)); the first parameter is the second parameter of the waiting thread identifier as a user The definition pointer can be used to store the return value of the waiting thread.

This function is a thread blocking function that calls its function to wait until the end of the waiting thread, when the function returns, the resource that is waiting for a thread is retracted.

3.Pthread_exit () pthread_exit exit the thread. The prototype is: #include extern void pthread_exit (void * value_ptr); value_ptr parameter points to thread exit status. No reference value. Part II - Multi-thread 80banner scanner Implementation

I. Idea:

1. Multi-IP processing module (using binary method processing) to process a subnet IP (VLSM). Convert iPunSigned long convert_ip (const char * t_addr); increase mask unsinged long add_masking (const char * t_addr, int mask); converted into Integer char * reverse_int (unsigned long addr);

2.usage prompt module int usage ("pthread 80 banner scanner); Printf (" INPUT SUBNET IP "); Printf (" USAGE:% S IP / 27 / N ", PRO);}

3. Scanning module Viod Scanip (char SIP [20])

4. Multi-process and processing module pthread ()

II. Implementation

/ ************************************************** ******************** PTHREAD 80 banner scanner ver1.0 ** to complel: * user $ gcc -o 80scaner 80scanner.c ** to use: * user $. / 80scanner IP / 27 (The Best Input Subnet IP Otherwise * Waste Too Many System Resource) ** Coded by Nightcat * July 2003 ********************* ***************************************************************************** // * The header of the desired header * / # include #include #include #include #include #include #include #include #include #define buf_size 20

/ * Declaration function * / int usage (char SIP [20]); unsigned long connection_ip (const char * t_addr); unsinge long add_masking (const char * t_addr, int mask); char * reverse_int UNSIGNED Long Addr;

INT Main (int Argc, char * argv []) {pthread_t pth; int count; char scan_ip [20]; char * network, * mask

/ * Enter parameter judgment * / if (argc! = 2) {usage (argv [0]); exit (1);}

/ * Processing IP * / Network = STRTOK (AGRV [1], "/"); Mask = Strtok (NULL, "/"); start = convert_ip; end = add_masking (network, atoi (mask);

/ * Cycle scan * / for (count = start; count <= end; count ) {scan_ip = reverse_int (count); / * Generate thread * / if (pthread_create (& pth, null, scanip, scan_ip) {PERROR (" Pthread_create error / n "); exit (1);}} return (0);}

/ * Usage function * / int usage (char * pro) {Printf ("pthread 80 banner scanner / n"); Printf ("Input Subnet IP"); Printf ("USAGE:% S IP / 27 / N", Pro }

/ * Scan function * / void scanip (char SIP [20]) {struct socmedddr_in addr; int S; char buffer [1024]; if ((s = socket (AF_INET, SOCK_STREAM, 0) <0) {PERROR (" Socket error / n "); exit (1);} addr.sin_family = Af_inet; addr.sin_addr.s_addr = inet_addr (SIP); addr.sin_port = HTONS (80); if (Connect (S, Struct SockAddr *) & Addr, SizeOf (AddR)) <0) {Perror ("Connect Error"); exit (1);} Send (S, "HEAD / HTTP / 1.0 / N / N", 17, 0); RECV (S, Buffer, sizeof (buffer), 0); Printf ("% s's http version: / n% s", siP, buffer; close (s); pthread_exit (null);} / * Convert ip * / unsigned long converart_ip ( Const char * t_addr) {int = 0; int = 0; int = 0; IF (SSCANF (T_ADDR, "% D.% d.% d.% d", & ocTet1, & octet2, & actet3, & ocTet4) <1) {Perror ("sscanf error");} return ((ocTet1 << 24) | (ocTet2 << 16) | (ocTet3 << 8) | ocTet4);} / * Increase Mask * / unsinged long add_masking (const char * t_addr, int mask) {if (Mask> 32 || Mask <0) {PERROR ("NOT A Valid Subnet Mask";} Return ((int) POW (2,32-Mask ) convert_ip (t_addr) -1);} / * Convert to integer format * / char * reverse_int (unsigned long) Addr) {static char * buffer [buf_size]; snprintf ((char *) buffer, buf_size, "% d.% d.% d.% d", (addr & 0xff000000) >> 24, (AddR & 0x00FF0000)> > 16, (AddR & 0x0000FF00) >> 8, (AddR & 0x000000FF)); Return ((char *) buffer;} Summary:

Among them, new students:

1. Processing method of subnet IP 2. Division method for subnet 3. Simple application of multi-thread 4. Application of Pow function 5. Display application

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

New Post(0)