#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<winsock2.h>
#include<ctype.h>
#include<windows.h>
char szAddress[128];
int iStartPort,
iEndPort;
void usage();
void validateArgs(int , char **);
//...........................................................................main() FUNC of ragnascan
int main(int argc, char **argv)
{
WSADATA wsadata;
SOCKET sScan;
struct hostent *host;
struct sockaddr_in suffer;
int iPort,
xPort,
returnValue;
validateArgs(argc, argv);
printf("Target IP is %s Ports strat at %d End at %d/nNow Scanning ....../n",
szAddress,iStartPort,iEndPort); //-------debug_10
for (iPort = iStartPort; iPort <=iEndPort; iPort++)
{
xPort = iPort;
if ((WSAStartup (MAKEWORD(2,2), &wsadata)) != 0)
{
printf("winsock load ERROR!/n");
return 1;
}
//printf("winsock loaded succeed!/n"); //-------debug_20
if ((sScan = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
printf("Creat socket ERROR! ERROR NO.:/n",WSAGetLastError());
return 1;
}
//printf("Creat socket succeed!/n"); //-------debug_30
suffer.sin_family = AF_INET;
suffer.sin_port = htons(xPort);
suffer.sin_addr.s_addr = inet_addr(szAddress);
if (suffer.sin_addr.s_addr = INADDR_NONE)
{
host = gethostbyname(szAddress);
if (host == NULL)
{
printf("unable resolve host: %s ",szAddress);
return 1;
}
CopyMemory(&suffer.sin_addr, host->h_addr_list[0], host->h_length );
if ((returnValue = (connect(sScan, (struct sockaddr *)&suffer, sizeof (suffer)))) == 0)
{
printf("Port %d open/n",xPort);
}
WSACleanup();
closesocket(sScan);
shutdown(sScan,SD_BOTH);
}
}
printf("finished the scan! press ANY key to quit/n");
getch();
return (0);
}
//...........................................................................all of SUB FUNCTIONS
//...........................................................................usage() func of ragnascan
void usage()
{
printf("===============================================================================/n");
printf("This is a scanner which named RAGNASCAN. It is only a test for WinsockAPI !/n");
printf("It's my first scanner.If you have any question,mailto: picobsd@163.net/n");
printf(" kaoru fujiwara/n");
printf("-------------------------------------------------------------------------------/n/n");
printf("USAGE: kscan [-a:IP] [-s:X] [-e:Y]/n");
printf(" -a:IP Target IP Address/n");
printf(" -s:X Port start at No. X/n");
printf(" -s:X Port End at No. Y/n");
exit(0);
}
//...........................................................................validateArgs() func
void validateArgs(int argc, char **argv)
{
int i;
if (argc < 4)
usage();
for (i=1; i<argc; i++)
{
if (((argv[0] == '/')||(argv[0] == '-'))&&(argv[2] == ':'))
{
switch (tolower(argv[1]))
{
case 'a':
if (strlen(argv) > 3)
strcpy(szAddress, &argv[3]);
break;
case 's':
iStartPort = atoi (&argv[3]);
if ((iStartPort > 65355)||(iStartPort < 1))
{
printf("the Ports that you assigned must between 1-65355!!/n ");
exit (0);
}
break;
case 'e':
iEndPort = atoi (&argv[3]);
if ((iEndPort > 65355)||(iEndPort < 1))
{
printf("the Ports that you assigned must between 1-65355!!/n ");
exit (0);
}
break;
default:
usage();
break;
}
}
else
usage();
}
if (iStartPort > iEndPort)
{
printf("start at %d,end at %d, this porg only accept ASCEND sequence/n",iStartPort,iEndPort);
usage();
}
}