Questions for beginners ~ Why is this program wrong ~~ Problem canceled!

Source: Internet
Author: User
Questions for beginners ~ Why is this program wrong ~~ Problem canceled! -- Linux general technology-Linux programming and kernel information. The following is a detailed description. [I = s] This post was last edited by lang1437 at 2009-3-21 20:57.

The following section of the program is a port scan program I typed on Emacs, which may always cause errors ~ Later, I compiled a prompt in VC ++:
Fatal error C1083: Cannot open include file: 'unistd. H': No such file or directory
No header file ?? Why? That's right! Is the book wrong! I ignored a step!

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

// Define a port range
Typedef struct _ port_segment
{Struct in_addr dest_ip; // target IP Address
Unsigned short int min_port; // start Port
Unsigned short int max_port; // maximum Port
} Port_segment;
/* Custom error handling function */
Void my_err (const char * err_string, int line)
{Fprintf (stderr, "line: % d", line );
Perror (err_string );
Exit (1 );
}
/*
* Description: A function used to scan a port on an IP address.
* Return value:-1 error
* 0 the target port is not enabled
* 1 the target port is enabled
*/
Int do_scan (struct sockaddr_in serv_addr)
{Int conn_fd;
Int ret;
// Create a TCP socket
Conn_fd = socket (AF_INET, SOCK_STREAM, 0 );
If (conn_fd <0)
{My_err ("socket", _ LINE _);}
// Send a connection request to the server
If (ret = connect (conn_fd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr) <0)
{If (errno = ECONNREFUSED)
{// The target port is not enabled
Close (conn_fd );
Return 0;
}
Else
{// Other errors
Close (conn_fd );
Return-1;
}
}
Else if (ret = 0)
{Printf ("port % d found in % s \ n", ntohs (serv_addr.sin_port), inet_ntoa (serv_addr.sin_addr ));
Close (conn_fd );
Return 1;
}
Return-1; // the actual execution is not here, just to eliminate the warning generated when compiling the program
}
// The thread that executes the scan to scan the port of a certain interval
Void * scaner (woid * arg)
{Unsigned short int I;
Struct sockaddr_in serv_addr;
Port_segment portinfo; // port information
// Read the port range information
Memcpy (& portinfo, arg, sizeof (struct_port_segment ));
// Initialize the server address Structure
Memset (& serv_addr, 0, sizeof (struct sockaddr_in ));
Serv_addr.sin_family = AF_INET;
Serv_addr.sin_addr = portinfo. dest_ip.s_addr;
For (I = porinfo. min_port; I <= portinfo. max_port; I ++)
{Serv_addr.sin_port = htons (I );
If (do_scan (serv_addr) <0)
{Continue; // exit the process if an error occurs.
}
}
Return NULL;
}
/* Command line parameter:-m maximum port,-a destination host IP address,-n maximum number of threads */
Int main (int argc, char ** argv)
{
Pthread_t * thread; // point to all thread IDS
Int max_port; // The maximum port number.
Int thread_num; // The maximum number of threads.
Int seg_len; // port Interval Length
Structin_addr dest_ip; // target host IP Address
Int I; // check the number of parameters
If (argc! = 7)
{Printf ("Usage: [-m] [max_port] [-a] [serv_address] [-n] [thread_number] \ n ");
Exit (1 );
}
// Parse command line parameters
For (I = 1; I {if (strcmp ("-m", argv ) = 0)
{Max_port = atoi (argv [I + 1]);
Printf ("Usafe: invalid max dest port \ n ");
Exit (1 );
}
Continue;
}
If (strcmp ("-a", argv= 0) = 0 ){
If (inet_aton (argv [I + 1], & dest_ip) = 0 {
Printf ("Usage: invalid dest ip address \ n ");
Exit (1 );
}
Continue;
}
If (strcmp ("-n", argv) = 0 ){
Thread_num = atoi (argv [I + 1]);
If (thread_num <= 0 ){
Printf ("Usage: invalid thread_number \ n ");
} Continue;
}
}
// If the input maximum port number is smaller than the number of threads, set the number of threads to the maximum port number.
If (max_port Thread_num = max_port;
}
Seg_len = max_port/thread_num;
If (max_port % thread_num )! = 0 ){
Thread_num + = 1;
}
// Allocate memory space for storing all thread IDS
Thread = (pthread_t *) malloc (thread_num * sizeof (pthread_t ));
// Create a thread and allocate the port range scanned by each thread based on the maximum port number and number of threads
For (I = 0; I {Port_segment portinfo;
Portinfo. dest_ip = dest_ip;
Portinfo. min_port = I * seg_len + 1;
If (I = thread_num-1)
{Portinfo. max_port = max_port ;}
Else {
Portinfo. max_port = portinfo. min_port + seg_len-1;
}
// Create a thread
If (pthread_create (& thread, NULL, scaner, (void *) & portinfo )! = 0)
{My_err ("pthread_create", _ LINE _);}
// The main thread waits for the sub-thread to end
Pthread_jion (thread, NULL );
}
Retun 0;
}
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.