/*============================================================================ | Copyright (C) 2001 Jonathan Harm | | | This program is free software; you can redistribute it and/or | modify it under the terms of the GNU General Public License | as published by the Free Software Foundation; either version 2 | of the License, or (at your option) any later version. | This program is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | You should have received a copy of the GNU General Public License | along with this program; if not, write to the Free Software | | Foundation, Inc., | 59 Temple Place - Suite 330 | Boston, MA 02111-1307, USA. | | | | portscan version 1 | | This program is under the GPL see http://www.gnu.org for more info | | | http://change.to/opensource | 110983@lycos.com | \============================================================================*/ //TODO: add more error checking // tell what is probably on that port(i.e. 21 ftp 6699 napster etc...) #include /* for printf() and fprintf() */ #include /* for socket(), connect(), send(), and recv() */ #include /* for sockaddr_in and inet_addr() */ #include /* for atoi() */ #include /* for memset() */ #include /* for close() */ int main(int argc, char *argv[]) { int scansock; //the actuall socket unsigned short portend, i; //the variables to hold the beginning and ending port numbers char *remote_ip_p, remote_ip[16]; //remote ip number, the 16 is there because an ip number can't //take up more than 16 characters including dots struct sockaddr_in p_scan; //struct to hold data about remote pc unsigned short portbegin; /* Echo server port */ remote_ip_p = remote_ip_p; //check for correct number of arguments if(( argc < 3 || argc > 4)) { printf("Usage: %s \n", argv[0]); exit(1); } if( (scansock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { fprintf(stderr, "Error Creating socket.\n\n"); exit(1); } //now that we have all the arguments, put them in the variables remote_ip_p = argv[1]; portbegin = atoi(argv[2]); portend = atoi(argv[3]); ++portend; //this is for the loop so it will increment all the way to the end port //fill in struct with data that isn't going to change memset(&p_scan, 0, sizeof(struct sockaddr_in)); p_scan.sin_family = AF_INET; p_scan.sin_addr.s_addr = inet_addr(remote_ip_p); for(i = portbegin; i < portend; i++) { p_scan.sin_port = htons(i); //now that the port number has changed, put it in the struct //now try to connect to that port if ( connect(scansock, (struct sockaddr *) &p_scan, sizeof(p_scan)) == 0 ) { printf("open: %d\n", i); } } close(scansock); return 0; }