All pastes #620288 Raw Edit

Someone

public text v1 · immutable
·published 2007-07-15 11:23 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#define PROTOCOL 0

main(int argc , char *argv[]){
	
	if (argc<2){
		fprintf(stderr , "Usage %s <ip_address> \n",argv[0]);
		exit(1);
	}
	int remoteaddfd,remaddlen,port ;
	unsigned long ipaddress;
	struct sockaddr_in remadd;
	struct sockaddr* remaddptr;
	struct hostent* hoststruct ;
	struct in_addr* hostnode ;
	remaddlen = sizeof(remadd);
	remaddptr = (struct sockaddr*)&remadd ;

/*CONVERT_TO_IP_ADDRESS*/
	if (isdigit(argv[1][0])){
		ipaddress = inet_addr(argv[1]);
	}	else{	
		
		hoststruct = gethostbyname(argv[1]);
		if (hoststruct != NULL ){
			hostnode = (struct in_addr*)hoststruct->h_addr;
			ipaddress = hostnode->s_addr;
		} 
		else {
			fprintf(stderr , "Error with hostname \n");
		}
	}	

/*CONVERT TO IP_ADDRESS*/

	for (port =0 ; port <65000;port++){
	
		bzero((char *)&remadd,remaddlen);
		remadd.sin_family = AF_INET ;
		remadd.sin_port = htons(port) ;
		remadd.sin_addr.s_addr = ipaddress;

		remoteaddfd = socket(AF_INET,SOCK_STREAM ,PROTOCOL);
		if ( remoteaddfd == -1) {
			perror("socket");
		}
		if ( (connect (remoteaddfd , remaddptr , remaddlen)) == -1) {
			close(remoteaddfd);
		}
		else 
			printf("PORT %d open \n", port) ;

}



}