hey thats almost what i got...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc != 5)
{
fprintf(stderr, "Usage: scan [target] [start port] [end port] [log filename]\n");
exit(1);
}
if (atoi(argv[3]) > 65535)
{
fprintf(stderr, "There is no port larger than 65535\n");
exit(1);
}
if (atoi(argv[2]) < 1)
{
fprintf(stderr, "There is no port smaller than 1\n");
exit(1);
}
ofstream fileOut(argv[4]);
printf("*************** Port Scan 0.01 ***************\n");
fileOut << "*************** Port Scan 0.01 ***************" << endl;
struct hostent *h;
h = gethostbyname(argv[1]);
if (h == NULL)
{
herror("gethostbyname");
exit(1);
}
char *ip = inet_ntoa(*((struct in_addr *) h -> h_addr));
int start = atoi(argv[2]);
int end = atoi(argv[3]);
int sockfd;
struct sockaddr_in dest_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
for(int i = start; i < (end + 1) ; i++)
{
if( (sockfd == -1))
{
herror("socket");
exit(1);
}
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(i);
dest_addr.sin_addr.s_addr = inet_addr(ip);
if(connect(sockfd, (struct sockaddr *)& dest_addr, sizeof(struct sockaddr)) == -1)
{
printf("Port %d closed!\n", i);
}
else
{
printf("Port %d open!\n", i);
fileOut << "Port " << i << " open!" << endl;
}
}
close(sockfd);
return 0;
}
the for loop at the end doesnt work because it just gets to the first port that is open and sez that the rest are closed when i'm sure they are not. help?