Salve ragazzi.
Alla fine sono riuscito a fare un port scanner per i servizi TCP.
Ora vorrei aggiungere una funzione per quelli UDP ma non so da dove cominciare!!
questo è ciò che ho fatto fin'ora:
------------- portscanner.c -------------
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
void TCP_connect_scan(char hts[255], unsigned int x, unsigned int tport);
struct hostent *hti;
int main()
{
char hts[255];
unsigned int fport, tport;
printf("\nInserire l'IP o l'hostname: ");
scanf("%s",hts);
printf("Inserire la porta iniziale: ");
scanf("%d",&fport);
printf("Inserire la porta finale: ");
scanf("%d",&tport);
if((hti = gethostbyname(hts)) == NULL) { printf("\nError: host not found\n"); exit(1); }
TCP_connect_scan(hts,fport,tport);
}
void TCP_connect_scan(char hts[255], unsigned int x, unsigned int tport)
{
int sd, new_sd;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr = *((struct in_addr *)hti->h_addr);
while(x < tport)
{
sd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_port = htons(x);
new_sd = connect(sd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
if(new_sd == 0) printf("%d tcp\n",x);
x++;
}
close(new_sd);
close(sd);
}
-----------------------------------------
E ora mi serve una funzione pressochè identica a TCP_connect_scan() ma ovviamente non so come fare per sapere se la porta è aperta, dato che non posso con l'if confrontare un socket descriptor dato da connect() con 0(trattandosi di un SOCK_DGRAM)...
Aiutatemi
P.S. tanto avete un mese per pensarci ¬_¬
[Modificato da lordlothar 30/07/2003 16.37]
[Modificato da lordlothar 30/07/2003 16.44]
[Modificato da lordlothar 30/07/2003 16.45]