Well i dont know if this is relevant to this website, but hell its the programming section, so i thought might aswell. Its a simple TCP port scanner using the connect method with a if statement to determine whether the port is open or closed. It also uses a for loop for the port range. BTW this is for the beginners who starting to program in C using sockets so no flaming for how crap my code is lol . /* A TCP port scanner created by billy www.softhardware.co.uk*/ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdlib.h> #include <errno.h> /* Main programs starts*/ int main(int argc, char **argv) { int sd; //socket descriptor int port; //port number int start; //start port int end; //end port int rval; //socket descriptor for connect char responce[1024]; //to receive data char *message="shell"; //data to send struct hostent *hostaddr; //To be used for IPaddress struct sockaddr_in servaddr; //socket structure if (argc < 4 ) { printf("------Created By www.h4ck-y0u.org-----------\n"); printf("--------------------------------------------------\n"); printf("Usage: ./tscan <IPaddress> <Start Port> <End Port>\n"); printf("--------------------------------------------------\n"); return (EINVAL); } start = atoi(argv[2]); end = atoi(argv[3]); for (port=start; port<=end; port++) { //portno is ascii to int second argument sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //created the tcp socket if (sd == -1) { perror("Socket()\n"); return (errno); } memset( &servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port); //set the portno hostaddr = gethostbyname( argv[1] ); //get the ip 1st argument memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length); //below connects to the specified ip in hostaddr rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr)); if (rval == -1) { printf("Port %d is closed\n", port); close(sd); } else printf("Port %d is open\n",port); close(sd); //socket descriptor } }
Bah.. I know almost everyone from h4ck-y0u.org.. Tell me your username there. no offense, but.. I highly doubt you leeched this source code from h4ck-y0u.org.