|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
[WIN32][C][DEV-C++] SPEEDSCAN SCANNER RÉSEAU
Information sur la source
Description
bonjour à tous !!! Voila une petire source de scanner réseau. Ce scanner permet: - ping + traitement des machines (évaluation du ttl) - requête pour adresse mac - requête résolution DNS - scanne de port TCP en connect() (je sais ces long) - récupération des partages distants Réglages: - sauvegarde de la configuration - choix de désactiver la résolution DNS - choix de fichier de log de rapport de scanne - choix de récupération d'un certains nombres de ports ouverts - choix de récupération des partages réseaux - définition du nombre de threads simultanées : 100max - définition du Time Out du Ping Pour les utilisations: Sémaphores, zones critiques, threads, ListView, ProgressBAR, IPAddress, redimmension de l'application, application d'icone à la fenêtre, etc... Le code fonctionne sous DEV, borland et vcc (appriori sans grosse modifications :p L'exe est dans le zip, il suffit de renommer "SpeedScan.ex" en "SpeedScan.exe" Toutes les critiques sont bonne à prendre :) toutes ces informations peuvent bien sur être contournées, mais permet d'avoir un apercu assez rapide du réseau :)
Source
- //------------------------------------------------------------------------------
- // Projet SpeedScan : Scanner réseau
- // Auteur : Hanteville Nicolas
- // Fichier : proc.c
- // Version : 0.5
- // Date de modification : 03/09/2007
- // Description : procédure & fonctions de scan et de traitement
- //------------------------------------------------------------------------------
- #include "ressources.h"
- //******************************************************************************
- //fonction de tri barbar
- //******************************************************************************
- short EstPlusGrand(char*ip1,char*ip2)
- {
- unsigned short tailleIp1 = strlen(ip1);
- unsigned short tailleIp2 = strlen(ip2);
- unsigned short i;
-
- //on test la longueur
- if (tailleIp1 != tailleIp2)
- {
- if (tailleIp1>tailleIp2) return 1;
- else return 0;
- }else
- {
- //sinon test par numéro
- if (tailleIp1 == 0) return -1;
-
- for (i=0;i<tailleIp1;i++)
- {
- if (ip1[i] > ip2[i]) return 1;
- if (ip1[i] < ip2[i]) return 0;
- }
- return -1;
- }
- }
- //******************************************************************************
- //déplacer 2 lignes
- //******************************************************************************
- void DeplacerLignes(unsigned int ligne1,unsigned int ligne2)
- {
- char tmp1[VAR_TMP];
- char tmp2[VAR_TMP];
- unsigned short i=0;
-
- while (i <nb_COL)
- {
- ListView_GetItemText(hListView,ligne1,i,tmp1,VAR_TMP);
- ListView_GetItemText(hListView,ligne2,i,tmp2,VAR_TMP);
-
- ListView_SetItemText(hListView,ligne1,i,tmp2);
- ListView_SetItemText(hListView,ligne2,i,tmp1);
- i++;
- }
- }
-
- //******************************************************************************
- //boucle de tri des items (barbare)
- //******************************************************************************
- void Tri()
- {
- short nbitem = ListView_GetItemCount(hListView)-1;
- char tmp1[VAR_TMP];
- char tmp2[VAR_TMP];
- unsigned int i,j;
- if (nbitem>-1)
- {
- for (j=0;j<nbitem;j++)
- {
- for (i=0;i<nbitem;i++)
- {
- //récupération de l'item actuel + item après
- ListView_GetItemText(hListView,i,COL_IP,tmp1,VAR_TMP);
- ListView_GetItemText(hListView,i+1,COL_IP,tmp2,VAR_TMP);
-
- if (EstPlusGrand(tmp1,tmp2) == 1)DeplacerLignes(i,i+1);
- }
- }
- }
- }
- //******************************************************************************
- //activer désactiver composants editables
- //******************************************************************************
- void ActiveEdits(BOOL etat)
- {
- EnableWindow(IP_DEBUT,etat);
- EnableWindow(IP_FIN,etat);
-
- EnableWindow(TOMS,etat);
- EnableWindow(NB_Threads,etat);
- EnableWindow(H_Log,etat);
- EnableWindow(H_COL_PARTAGES,etat);
- EnableWindow(H_COL_PORTS,etat);
- EnableWindow(H_DNS,etat);
- }
- //******************************************************************************
- //scanne de port non rapide
- //******************************************************************************
- void Liste_Port(LCONFLIGNE Lligneconf)
- {
- unsigned short LPorts[38]={7,13,20,21,22,23,25,33,37,38,42,43,53,69,70,79,80,88,93,107,110,118,119,123,134,137,138,139
- ,143,153,156,161,170,443,445,469,0};
- unsigned short i=0;
-
- SOCKET sock;
- SOCKADDR_IN sin;
- char tmp[256]="";
- char tmp2[256]="";
- char tmp_i[4]="";
- sock = socket(AF_INET, SOCK_STREAM, 0);
-
- while (LPorts[i] && activescan)
- {
- sin.sin_addr.s_addr = inet_addr(Lligneconf.ip);
- sin.sin_family = AF_INET;
- sin.sin_port = htons(LPorts[i]);
-
- strcpy(tmp2,"port:");
- EnterCriticalSection(&Sync);
- strcat(tmp2,itoa(LPorts[i],tmp_i,10));
- strcat(tmp2,"\0");
- ListView_SetItemText(hListView,Lligneconf.ligne,COL_INFOS,tmp2);
- LeaveCriticalSection(&Sync);
-
- if((connect(sock,(struct sockaddr*)&sin,sizeof(struct sockaddr))) == 0)
- {
- strcat(tmp,itoa(LPorts[i],tmp_i,10));
- strcat(tmp," ");
- }
- i++;
- }
- closesocket(sock);
- strcat(tmp,"\0");
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,Lligneconf.ligne,COL_PORTS,tmp);
- LeaveCriticalSection(&Sync);
- }
- //******************************************************************************
- //permet de retourner les partages comptenues sur une machines dans la case d'une colonne
- //******************************************************************************
- BOOL Partages(char *ip,unsigned int item)
- {
- NET_API_STATUS res;
- char tmp[VAR_TMP];
- char tmp_fin[TAILLE_MAX];
- wchar_t server[VAR_TMP];
- PSHARE_INFO_1 BufPtr,p;
- DWORD er=0,tr=0,resume=0;
- unsigned short i;
-
- //init de la chaine pour récupération des partages
- sprintf(tmp,"\\\\%s",ip);//ici on implémente l'ip avec scan de toute le réseau proche et de leurs partages
- mbstowcs( server,tmp, VAR_TMP );//implémentation pour transformation et utilisation
-
- //init chaine finale
- tmp_fin[0]=0;
-
- //récupération des partages et indentation dans la chaine
- do
- {
- res = NetShareEnum (server, 1, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);//NULL renvoie la machine locale
- if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
- {
- p=BufPtr;
-
- for(i=1;i<=er;i++)
- {
- sprintf(tmp,"%S | ",p->shi1_netname);
-
- strcat(tmp_fin,tmp);
-
- p++;
- }
- NetApiBufferFree(BufPtr);
- }
- }while(res==ERROR_MORE_DATA && activescan);
-
- strcat(tmp_fin,"\0");
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,item,COL_PARTAGES,tmp_fin);
- LeaveCriticalSection(&Sync);
- return 0;
- }
- //******************************************************************************
- // verifie si le fichier existe
- //******************************************************************************
- int FichierExiste(char *fichierTest)
- {
- WIN32_FIND_DATA data;
- HANDLE hF = FindFirstFile(fichierTest, &data);
- if(hF == INVALID_HANDLE_VALUE)
- return 0;
- FindClose(hF);
- return 1;
- };
-
- //******************************************************************************
- // init des threads + KILL si encore en cours
- //******************************************************************************
- DWORD WINAPI InitEtKillThreads(LPVOID lParam)
- {
- unsigned short i;
- DWORD IDThread;
- char tmp[MAX_TOMS];
-
- EnableWindow(BScanner,FALSE);
-
- //_sleep(1000);// 1 seconde
- GetExitCodeThread(ThKey,&IDThread);
- TerminateThread(ThKey,IDThread);
-
- while (StartBlogTestError>0)_sleep(100);
-
- SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)0, 0);
- SendMessage(ProgressBAR2, PBM_SETPOS, (WPARAM)0,0);
-
- CloseHandle(hSemaphore);
- CloseHandle(hSemaphore2);
-
- //init état des composants
- ActiveEdits(TRUE);
- EnableWindow(BScanner,TRUE);
-
- //init bouton de scan
- SetWindowText(BScanner,TXT_LFR_BT_SCAN);
-
- //init de la fenêtre
- snprintf(tmp,TAILLE_TITRE_TEXTE,"%s OK %d %s\0",NOM_APPLI,ListView_GetItemCount(hListView),TXT_LFR_MSG_ETAT_SCAN);
- SetWindowText(hwnd,tmp);
-
- //écriture dans la fichier de log
- if (EtatLogs) CreateThread(0,0,EcrireCTR,0,0,0);
-
- DeleteCriticalSection(&Sync);
- activescan=0;
-
- //on libère la mémoire
- WSACleanup();
- };
-
- //******************************************************************************
- // Récupération d'une adresse mac: tiré de la msdn:
- // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/sendarp.asp
- //******************************************************************************
- int __cdecl RecupMacAdress(char * ip,char *macadress)
- {
- //variables
- IPAddr ipAddr;
- ULONG pulMac[2];
- ULONG ulLen;
-
- //transformation pour traitement de l'adresse ip
- ipAddr = inet_addr (ip);
-
- //init
- memset (pulMac, 0xff, sizeof (pulMac));
- ulLen = 6;
-
- //on récupère l'adresse mac ici si possible (si après un routage type internet = impossible)
- if (SendARP (ipAddr, 0, pulMac, &ulLen)==NO_ERROR)
- {
- PBYTE pbHexMac = (PBYTE) pulMac;
- snprintf (macadress,25,"%02X:%02X:%02X:%02X:%02X:%02X\0",pbHexMac[0],pbHexMac[1],pbHexMac[2],pbHexMac[3],pbHexMac[4],pbHexMac[5],pbHexMac[6] /*szMac*/);
- return 1;
- }
-
- sprintf(macadress,TXT_LFR_MSG_ERROR);
- return 0;
- }
-
- //******************************************************************************
- // Ping d'une ip + traitement IP + MAC + nom + TTL + OS
- //******************************************************************************
- DWORD WINAPI Ping(LPVOID lParam)
- {
- LVITEM lvi;
- LCONFLIGNE Lligneconf;
- struct hostent* remoteHost;
- struct in_addr in;
- struct in_addr iaDest;
- LPHOSTENT pHost;
- DWORD *dwAddress;
- HANDLE hndlFile;
- ICMPECHO icmpEcho;
-
- BOOL reqArp =1;
- char tmp[VAR_TMP];
- int itemPos;
-
- HANDLE hndlIcmp;
- HANDLE (WINAPI *pIcmpCreateFile)(VOID);
- BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
- DWORD (WINAPI *pIcmpSendEcho) (HANDLE,DWORD,LPVOID,WORD, PIPINFO, LPVOID,DWORD,DWORD);
-
- strcpy((char*)Lligneconf.ip,(char*)lParam);
- ReleaseSemaphore(hSemaphore,1,NULL);
-
- EnterCriticalSection(&Sync);
- StartBlogTestError++;
- LeaveCriticalSection(&Sync);
-
- WaitForSingleObject(hSemaphore2,INFINITE);
-
- //init de la config
- iaDest.s_addr = inet_addr(Lligneconf.ip);
- pHost = gethostbyname(Lligneconf.ip);
-
- // copie de l'ip a pinger
- dwAddress = (DWORD *)(*pHost->h_addr_list);
-
- EnterCriticalSection(&Sync);
- if ((hndlIcmp = LoadLibrary("ICMP.DLL"))!=0)
- {
- //fonctions pointeurs de la DLL
- //récupération d'un pointeur vers chacune de ces fonctions fonctions
- pIcmpCreateFile = (HANDLE (WINAPI *)(void))GetProcAddress((HMODULE)hndlIcmp,"IcmpCreateFile");
- pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))GetProcAddress((HMODULE)hndlIcmp,"IcmpCloseHandle");
- pIcmpSendEcho = (DWORD (WINAPI *)(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD)) GetProcAddress((HMODULE)hndlIcmp,"IcmpSendEcho");
- LeaveCriticalSection(&Sync);
-
- if (pIcmpCreateFile!=0 && pIcmpCloseHandle!=0 && pIcmpSendEcho!=0)
- {
- // récupéré un handle sur le reply
- if ((hndlFile = pIcmpCreateFile())!= INVALID_HANDLE_VALUE)
- {
- pIcmpSendEcho(
- hndlFile,
- *dwAddress,
- 0,
- 0,
- 0,
- &icmpEcho,
- sizeof(icmpEcho),
- Toms);
-
- //si Ok
- if ((icmpEcho.Status==0)&&(icmpEcho.Options.Ttl>0))
- {
- reqArp =0;
- //ajout d'une ligne
- lvi.mask = LVIF_TEXT|LVIF_PARAM;
- lvi.iSubItem = 0;
- lvi.lParam = LVM_SORTITEMS;
- lvi.pszText="";
-
- EnterCriticalSection(&Sync);
- lvi.iItem = NB_Machines++;
- itemPos=ListView_InsertItem(hListView, &lvi);
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"PING");
- LeaveCriticalSection(&Sync);
-
- Lligneconf.ligne = (unsigned long)itemPos;
-
- //ip
- tmp[0]='[';
- tmp[1]=0;
- strcat(tmp,Lligneconf.ip);
- strcat(tmp,"]\0");
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_IP,tmp);
- LeaveCriticalSection(&Sync);
-
- //ttl
- sprintf(tmp,"TTL:%d",icmpEcho.Options.Ttl);
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_TTL,tmp);
-
- //OS
- if (icmpEcho.Options.Ttl<=MACH_LINUX)//linux
- {
- ListView_SetItemText(hListView,itemPos,COL_OS,TXT_LFR_MSG_LINUX);
- }else if (icmpEcho.Options.Ttl<=MACH_WINDOWS)//windows
- {
- ListView_SetItemText(hListView,itemPos,COL_OS,TXT_LFR_MSG_WIN);
- }else //routeur
- {
- ListView_SetItemText(hListView,itemPos,COL_OS,TXT_LFR_MSG_ROUTEUR);
- };
- LeaveCriticalSection(&Sync);
-
- //DNS
- if (EtatDns)
- {
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"DNS");
- LeaveCriticalSection(&Sync);
- in.s_addr = inet_addr(Lligneconf.ip);
- if ((remoteHost=gethostbyaddr((char *)&in, 4, AF_INET))!=0)
- strncpy(tmp,remoteHost->h_name,VAR_TMP);
- else
- strcpy(tmp,TXT_LFR_MSG_ERROR);
-
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_NOM,tmp);
- LeaveCriticalSection(&Sync);
- }
-
- //adresse mac
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"ARP");
- LeaveCriticalSection(&Sync);
-
- if (RecupMacAdress(Lligneconf.ip,tmp))
- {
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_MAC,tmp);
- LeaveCriticalSection(&Sync);
- }else
- {
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_MAC,TXT_LFR_MSG_ERROR);
- LeaveCriticalSection(&Sync);
- }
-
- //partages
- if (EtatPartages)
- {
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"Partages");
- LeaveCriticalSection(&Sync);
- Partages(Lligneconf.ip,Lligneconf.ligne);
- }
-
- }
- pIcmpCloseHandle(hndlFile);
- }
-
- }
- FreeLibrary((HMODULE)hndlIcmp);//libère la DLL
- }else LeaveCriticalSection(&Sync);
-
- if (reqArp)//on a pas trouver en ping on fait par requète arp
- {
- if (RecupMacAdress(Lligneconf.ip,tmp))
- {
- //ok la machine existe
- int itemPos;
-
- //ajout d'une ligne
- lvi.mask = LVIF_TEXT|LVIF_PARAM;
- lvi.iSubItem = 0;
- lvi.lParam = LVM_SORTITEMS;
- lvi.pszText="";
-
- lvi.iItem = NB_Machines++;
- EnterCriticalSection(&Sync);
- itemPos=ListView_InsertItem(hListView, &lvi);
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"ARP");
- LeaveCriticalSection(&Sync);
-
- Lligneconf.ligne = (unsigned long)itemPos;
-
- //mac
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_MAC,tmp);
- LeaveCriticalSection(&Sync);
-
- //ip
- tmp[0]='[';
- tmp[1]=0;
- strcat(tmp,Lligneconf.ip);
- strcat(tmp,"]\0");
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_IP,tmp);
- LeaveCriticalSection(&Sync);
-
- //on récupère le nom de la machine / DNS
- if (EtatDns)
- {
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"DNS");
- LeaveCriticalSection(&Sync);
- in.s_addr = inet_addr(Lligneconf.ip);
- if ((remoteHost=gethostbyaddr((char *)&in, 4, AF_INET))!=0)
- {
- strncpy(tmp,remoteHost->h_name,VAR_TMP);
- }else
- {
- strcpy(tmp,TXT_LFR_MSG_ERROR);
- };
- }
-
- EnterCriticalSection(&Sync);
- //DNS
- if (EtatDns)
- ListView_SetItemText(hListView,itemPos,COL_NOM,tmp);
-
- //OS
- ListView_SetItemText(hListView,itemPos,COL_OS,"FIREWALL");
-
- //TTL
- ListView_SetItemText(hListView,itemPos,COL_TTL,"TTL:---");
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"OK");
- LeaveCriticalSection(&Sync);
- reqArp =0;
- }
- }
- //liste des ports
- if (reqArp ==0 && EtatPorts)
- {
- EnterCriticalSection(&Sync);
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"PORTS");
- LeaveCriticalSection(&Sync);
- Liste_Port(Lligneconf);
- }
-
- EnterCriticalSection(&Sync);
- //info de chargement
- SendMessage(ProgressBAR2, PBM_SETPOS, (WPARAM)(++nb*100)/max,0);
- StartBlogTestError--;
- ListView_SetItemText(hListView,itemPos,COL_INFOS,"OK");
- LeaveCriticalSection(&Sync);
-
- ReleaseSemaphore(hSemaphore2,1,NULL);
-
- return 0;
- };
-
- //******************************************************************************
- // Scan : traitement des ip a scanner
- //******************************************************************************
- DWORD WINAPI ScanReseau(LPVOID lParam)
- {
- //récupération des 2 ip d'intervales
- DWORD LIp1,LIp2;
- BYTE L11,L12,L13,L14,L21,L22,L23,L24;
- char tmp[MAX_TOMS];
- char tmp2[MAX_TOMS];
- WSADATA WSAData;
- int j=0,i=0;
-
- WSAStartup(0x02, &WSAData );
-
- StartBlogTestError=0;
-
- //init
- max=0;
- nb=0;
- //init des threads
- NB_Machines=0; //init du nombre de machines
-
- //instance du Time out
- GetWindowText(TOMS,tmp,50);
- Toms=atoi(tmp);
- if (Toms>MAX_TOMS)
- {
- SetWindowText(TOMS,"10000");
- Toms = MAX_TOMS;
- }else if (Toms<1)
- {
- SetWindowText(TOMS,"500");
- Toms = 500;
- }
-
- //instance du nombre de threads
- GetWindowText(NB_Threads,tmp,50);
- NB_MAX_THREAD=atoi(tmp);
- if (NB_MAX_THREAD>MAX_THREADS)
- {
- SetWindowText(NB_Threads,"1000");
- NB_MAX_THREAD = MAX_THREADS;
- }else if (NB_MAX_THREAD<1)
- {
- SetWindowText(NB_Threads,"10");
- NB_MAX_THREAD = 10;
- }
-
- //récupération du comptenu des champs
- SendMessage(IP_DEBUT,IPM_GETADDRESS, 0 ,(LPARAM) &LIp1);
- SendMessage(IP_FIN,IPM_GETADDRESS, 0 ,(LPARAM) &LIp2);
-
- //on récupère chacun des bits
- L11 = LIp1 >> 24;
- L12 = (LIp1 >> 16) & 0xFF;
- L13 = (LIp1 >> 8) & 0xFF;
- L14 = LIp1 & 0xFF;
-
- L21 = LIp2 >> 24;
- L22 = (LIp2 >> 16) & 0xFF;
- L23 = (LIp2 >> 8) & 0xFF;
- L24 = LIp2 & 0xFF;
-
- hSemaphore=CreateSemaphore(NULL,1,1,NULL);
- hSemaphore2=CreateSemaphore(NULL,NB_MAX_THREAD,NB_MAX_THREAD,NULL);
-
- //test de la validité des ip
- if (LIp1!=0 && L14 >0 && L14 < 255 && L13 <255 && L12 <255 && L11<255 || LIp2!=0 && L24 >0 && L24 < 255 && L23 <255 && L22 <255 && L21<255)
- {
- if (LIp1 == 0 || LIp1 == LIp2)
- {
- max = 1;
- //alors on ajoute seulement 1 ip: le 2
- WaitForSingleObject(hSemaphore,INFINITE);
- sprintf(tmp,"%d.%d.%d.%d",L21,L22,L23,L24);
- CreateThread(NULL,0,Ping,tmp,0,0);
- }else if (LIp2 == 0 && L14 >0 && L14 < 255 && L13 <255 && L12 <255 && L11<255)
- {
- max = 1;
- //alors on ajoute seulement 1 ip: le 1
- WaitForSingleObject(hSemaphore,INFINITE);
- sprintf(tmp,"%d.%d.%d.%d",L11,L12,L13,L14);
- CreateThread(NULL,0,Ping,tmp,0,0);
- }else
- {
- if ( L21>=L11 && (L22>=L12 || L21>L11 && L12 > L22) && (L23>=L13 || (L22>L12 || L21>L11)&& L13 > L23) && (L24>=L14 || (L23>L13 || L22>L12 || L21>L11)&& L14 > L24))
- {
- //calcul de max
-
- max=((L21-L11)*(255*255*255))+1;//octet 1
-
- if (L12<=L22)//octet 2
- max+=(L22-L12)*(255*255);
- else
- max+=((255-L12)+L22)*(255*255);
-
- if (L13<=L23)//octet 3
- max+=(L23-L13)*(255);
- else
- max+=((255-L13)+L23)*(255);
-
- if (L14<=L24)//octet 4
- max+=(L24-L14);
- else
- max+=((255-L14)+L24);
-
- //on ajoute en boucle les ip
- while (L21>=L11)
- {
- while (L22>=L12 || L12<255 && L21>L11)
- {
- while (L23>=L13 || L13<255 && (L22>L12 || L21>L11))
- {
- while (L24>=L14 || L14<255 && (L22>L12 || L21>L11 || L23>L13))
- {
- while (StartBlogTestError > NB_MAX_THREAD)_sleep(10);
-
- WaitForSingleObject(hSemaphore,INFINITE);
-
- sprintf(tmp,"%d.%d.%d.%d",L11,L12,L13,L14);
- CreateThread(NULL,0,Ping,tmp,0,0);
- j++;
- L14++;
-
- snprintf(tmp2,TAILLE_TITRE_TEXTE,"%s [%s] %d/%d",NOM_APPLI,tmp,j,max);
- SetWindowText(hwnd,tmp2);
-
- //progression
- SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)j*100/max, 0);
- }
- L13++;
- L14=1;
- }
- L12++;
- L13=0;
- }
- L11++;
- L12 =0;
- }
- }else MessageBox(hwnd,"Interval d'IP non valide!","Erreur",MB_OK);
- }
- }else MessageBox(hwnd,"Interval d'IP non valide!","Erreur",MB_OK);
-
- //on attend que tous les threads soient fini pour dir que le scan est fini
- while (StartBlogTestError>0)_sleep(100);
-
- WaitForSingleObject(hSemaphore2,0);
- ReleaseSemaphore(hSemaphore2,1,NULL);
-
- CloseHandle(hSemaphore);
- CloseHandle(hSemaphore2);
-
- //fin du scan
- SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)100, 0);
-
- //init état des composants
- ActiveEdits(TRUE);
- _sleep(100);
-
-
- //init des progresse bar
- SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)0, 0);
- SendMessage(ProgressBAR2, PBM_SETPOS, (WPARAM)0,0);
-
- //init bouton de scan
- SetWindowText(BScanner,TXT_LFR_BT_SCAN);
-
- //init de la fenêtre
- snprintf(tmp,TAILLE_TITRE_TEXTE,"%s OK %d %s\0",NOM_APPLI,ListView_GetItemCount(hListView),TXT_LFR_MSG_ETAT_SCAN);
- SetWindowText(hwnd,tmp);
-
- Tri();
-
- //écriture dans la fichier de log
- if (EtatLogs) EcrireCTR(0);
-
- activescan=0;
-
- //on libère la mémoire
- WSACleanup();
-
- return 0;
- }
- //******************************************************************************
- // écrire dans un fichier txt le rapport
- //******************************************************************************
- DWORD WINAPI EcrireCTR(LPVOID lParam)
- {
-
- //variables
- char tmp[EXTRA_TMP];
- char var[TAILLE_MAX];
- int NBLigne;
- int ligne,colonne;
- time_t dateEtHMs;//structure de la date
- int taille;
- DWORD copiee;
-
- // on récupère le nombre de lignes
- NBLigne=ListView_GetItemCount(hListView);
-
- //ouverture du fichier et écriture à la suite
- HANDLE HLog = CreateFile(NOMFICLOG,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_ALWAYS,FILE_FLAG_SEQUENTIAL_SCAN,0);
- //on se place a la fin du fichier
- SetFilePointer(HLog,GetFileSize(HLog,NULL),0,FILE_BEGIN);
-
- if (HLog == INVALID_HANDLE_VALUE || NBLigne==0)
- {
- CloseHandle(HLog);
- return FALSE;
- }
-
- //entête
- time(&dateEtHMs);//nombre d'item + recuperation de l'heure et la date actuelle
- snprintf(tmp,EXTRA_TMP,"\r\n\r\n|%s\t\t%s: %d\t\t%s: %s |\r\n",NOM_APPLI,NB_MACHINES,NBLigne,TXT_LFR_MSG_DATE_SCAN,(char *)ctime(&dateEtHMs));
- tmp[strlen(tmp)-29]=' '; //gestion du retour à la ligne après la date
-
- WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
-
- WriteFile(HLog,"|-------------------------------------------------------------------------------------------------------------------------------|\r\n",131,&copiee,0);
- snprintf(tmp,EXTRA_TMP,"|%s\t\t\t\t\t|%s\t\t|%s\t\t\t\t|%s\t\t\t|%s\t\t\t|\r\n",TXT_LFR_BTLST_IP,TXT_LFR_BTLST_MAC,TXT_LFR_BTLST_NOM,TXT_LFR_BTLST_TTL,TXT_LFR_BTLST_OS);
- WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
- WriteFile(HLog,"|-------------------------------------------------------------------------------------------------------------------------------|\r\n",131,&copiee,0);
- snprintf(tmp,EXTRA_TMP,"|");
-
- // on récupère les items un par un , par ligne et on les met dans une variable ligne puis écriture
- for (ligne=0;ligne<NBLigne;ligne++)
- {
- for (colonne=0;colonne<NB_COLONNE;colonne++)
- {
- ListView_GetItemText(hListView,ligne,colonne,var,TAILLE_MAX);
-
- taille = strlen(var);
-
- //gestion affichage des colonnes
- if (taille<15&&colonne==0)
- snprintf(tmp,EXTRA_TMP,"%s%s\t\t|",tmp,var);
- else if (taille==0&&colonne>2)
- snprintf(tmp,EXTRA_TMP,"%s%s\t\t\t|",tmp,var);
- else if ((taille<5)&&(colonne<3))
- snprintf(tmp,EXTRA_TMP,"%s%s\t\t\t\t|",tmp,var);
- else if ((taille<7)&&(colonne<3))//7
- snprintf(tmp,EXTRA_TMP,"%s%s\t\t\t|",tmp,var);
- else if ((colonne!=0)&&(taille<11)||(taille<16)&&(colonne<3)&&(colonne!=0)||(taille<7)&&(colonne>2))
- snprintf(tmp,EXTRA_TMP,"%s%s\t\t|",tmp,var);
- else if (taille<23)
- snprintf(tmp,EXTRA_TMP,"%s%s\t|",tmp,var);
- else
- snprintf(tmp,EXTRA_TMP,"%s%s|",tmp,var);
- }
- snprintf(tmp,EXTRA_TMP,"%s\r\n|",tmp);
- WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
-
- //ici on ajoute la gestion des partages et des ports
- if(EtatPorts)
- {
- ListView_GetItemText(hListView,ligne,COL_PORTS,var,TAILLE_MAX);
- if (strlen(var)>1)
- {
- snprintf(tmp,EXTRA_TMP,"Liste des ports ouverts standards:%s\r\n|",var);
- WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
- }
- }
- if(EtatPartages)
- {
- ListView_GetItemText(hListView,ligne,COL_PARTAGES,var,TAILLE_MAX);
- if (strlen(var)>1)
- {
- snprintf(tmp,EXTRA_TMP,"Liste des partages:%s\r\n|",var);
- WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
- }
- }
- tmp[0]=0;
- }
- WriteFile(HLog,"-------------------------------------------------------------------------------------------------------------------------------|\r\n",130,&copiee,0);
- CloseHandle(HLog);
- return 0;
-
- };
//------------------------------------------------------------------------------
// Projet SpeedScan : Scanner réseau
// Auteur : Hanteville Nicolas
// Fichier : proc.c
// Version : 0.5
// Date de modification : 03/09/2007
// Description : procédure & fonctions de scan et de traitement
//------------------------------------------------------------------------------
#include "ressources.h"
//******************************************************************************
//fonction de tri barbar
//******************************************************************************
short EstPlusGrand(char*ip1,char*ip2)
{
unsigned short tailleIp1 = strlen(ip1);
unsigned short tailleIp2 = strlen(ip2);
unsigned short i;
//on test la longueur
if (tailleIp1 != tailleIp2)
{
if (tailleIp1>tailleIp2) return 1;
else return 0;
}else
{
//sinon test par numéro
if (tailleIp1 == 0) return -1;
for (i=0;i<tailleIp1;i++)
{
if (ip1[i] > ip2[i]) return 1;
if (ip1[i] < ip2[i]) return 0;
}
return -1;
}
}
//******************************************************************************
//déplacer 2 lignes
//******************************************************************************
void DeplacerLignes(unsigned int ligne1,unsigned int ligne2)
{
char tmp1[VAR_TMP];
char tmp2[VAR_TMP];
unsigned short i=0;
while (i <nb_COL)
{
ListView_GetItemText(hListView,ligne1,i,tmp1,VAR_TMP);
ListView_GetItemText(hListView,ligne2,i,tmp2,VAR_TMP);
ListView_SetItemText(hListView,ligne1,i,tmp2);
ListView_SetItemText(hListView,ligne2,i,tmp1);
i++;
}
}
//******************************************************************************
//boucle de tri des items (barbare)
//******************************************************************************
void Tri()
{
short nbitem = ListView_GetItemCount(hListView)-1;
char tmp1[VAR_TMP];
char tmp2[VAR_TMP];
unsigned int i,j;
if (nbitem>-1)
{
for (j=0;j<nbitem;j++)
{
for (i=0;i<nbitem;i++)
{
//récupération de l'item actuel + item après
ListView_GetItemText(hListView,i,COL_IP,tmp1,VAR_TMP);
ListView_GetItemText(hListView,i+1,COL_IP,tmp2,VAR_TMP);
if (EstPlusGrand(tmp1,tmp2) == 1)DeplacerLignes(i,i+1);
}
}
}
}
//******************************************************************************
//activer désactiver composants editables
//******************************************************************************
void ActiveEdits(BOOL etat)
{
EnableWindow(IP_DEBUT,etat);
EnableWindow(IP_FIN,etat);
EnableWindow(TOMS,etat);
EnableWindow(NB_Threads,etat);
EnableWindow(H_Log,etat);
EnableWindow(H_COL_PARTAGES,etat);
EnableWindow(H_COL_PORTS,etat);
EnableWindow(H_DNS,etat);
}
//******************************************************************************
//scanne de port non rapide
//******************************************************************************
void Liste_Port(LCONFLIGNE Lligneconf)
{
unsigned short LPorts[38]={7,13,20,21,22,23,25,33,37,38,42,43,53,69,70,79,80,88,93,107,110,118,119,123,134,137,138,139
,143,153,156,161,170,443,445,469,0};
unsigned short i=0;
SOCKET sock;
SOCKADDR_IN sin;
char tmp[256]="";
char tmp2[256]="";
char tmp_i[4]="";
sock = socket(AF_INET, SOCK_STREAM, 0);
while (LPorts[i] && activescan)
{
sin.sin_addr.s_addr = inet_addr(Lligneconf.ip);
sin.sin_family = AF_INET;
sin.sin_port = htons(LPorts[i]);
strcpy(tmp2,"port:");
EnterCriticalSection(&Sync);
strcat(tmp2,itoa(LPorts[i],tmp_i,10));
strcat(tmp2,"\0");
ListView_SetItemText(hListView,Lligneconf.ligne,COL_INFOS,tmp2);
LeaveCriticalSection(&Sync);
if((connect(sock,(struct sockaddr*)&sin,sizeof(struct sockaddr))) == 0)
{
strcat(tmp,itoa(LPorts[i],tmp_i,10));
strcat(tmp," ");
}
i++;
}
closesocket(sock);
strcat(tmp,"\0");
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,Lligneconf.ligne,COL_PORTS,tmp);
LeaveCriticalSection(&Sync);
}
//******************************************************************************
//permet de retourner les partages comptenues sur une machines dans la case d'une colonne
//******************************************************************************
BOOL Partages(char *ip,unsigned int item)
{
NET_API_STATUS res;
char tmp[VAR_TMP];
char tmp_fin[TAILLE_MAX];
wchar_t server[VAR_TMP];
PSHARE_INFO_1 BufPtr,p;
DWORD er=0,tr=0,resume=0;
unsigned short i;
//init de la chaine pour récupération des partages
sprintf(tmp,"\\\\%s",ip);//ici on implémente l'ip avec scan de toute le réseau proche et de leurs partages
mbstowcs( server,tmp, VAR_TMP );//implémentation pour transformation et utilisation
//init chaine finale
tmp_fin[0]=0;
//récupération des partages et indentation dans la chaine
do
{
res = NetShareEnum (server, 1, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);//NULL renvoie la machine locale
if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
{
p=BufPtr;
for(i=1;i<=er;i++)
{
sprintf(tmp,"%S | ",p->shi1_netname);
strcat(tmp_fin,tmp);
p++;
}
NetApiBufferFree(BufPtr);
}
}while(res==ERROR_MORE_DATA && activescan);
strcat(tmp_fin,"\0");
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,item,COL_PARTAGES,tmp_fin);
LeaveCriticalSection(&Sync);
return 0;
}
//******************************************************************************
// verifie si le fichier existe
//******************************************************************************
int FichierExiste(char *fichierTest)
{
WIN32_FIND_DATA data;
HANDLE hF = FindFirstFile(fichierTest, &data);
if(hF == INVALID_HANDLE_VALUE)
return 0;
FindClose(hF);
return 1;
};
//******************************************************************************
// init des threads + KILL si encore en cours
//******************************************************************************
DWORD WINAPI InitEtKillThreads(LPVOID lParam)
{
unsigned short i;
DWORD IDThread;
char tmp[MAX_TOMS];
EnableWindow(BScanner,FALSE);
//_sleep(1000);// 1 seconde
GetExitCodeThread(ThKey,&IDThread);
TerminateThread(ThKey,IDThread);
while (StartBlogTestError>0)_sleep(100);
SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)0, 0);
SendMessage(ProgressBAR2, PBM_SETPOS, (WPARAM)0,0);
CloseHandle(hSemaphore);
CloseHandle(hSemaphore2);
//init état des composants
ActiveEdits(TRUE);
EnableWindow(BScanner,TRUE);
//init bouton de scan
SetWindowText(BScanner,TXT_LFR_BT_SCAN);
//init de la fenêtre
snprintf(tmp,TAILLE_TITRE_TEXTE,"%s OK %d %s\0",NOM_APPLI,ListView_GetItemCount(hListView),TXT_LFR_MSG_ETAT_SCAN);
SetWindowText(hwnd,tmp);
//écriture dans la fichier de log
if (EtatLogs) CreateThread(0,0,EcrireCTR,0,0,0);
DeleteCriticalSection(&Sync);
activescan=0;
//on libère la mémoire
WSACleanup();
};
//******************************************************************************
// Récupération d'une adresse mac: tiré de la msdn:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/sendarp.asp
//******************************************************************************
int __cdecl RecupMacAdress(char * ip,char *macadress)
{
//variables
IPAddr ipAddr;
ULONG pulMac[2];
ULONG ulLen;
//transformation pour traitement de l'adresse ip
ipAddr = inet_addr (ip);
//init
memset (pulMac, 0xff, sizeof (pulMac));
ulLen = 6;
//on récupère l'adresse mac ici si possible (si après un routage type internet = impossible)
if (SendARP (ipAddr, 0, pulMac, &ulLen)==NO_ERROR)
{
PBYTE pbHexMac = (PBYTE) pulMac;
snprintf (macadress,25,"%02X:%02X:%02X:%02X:%02X:%02X\0",pbHexMac[0],pbHexMac[1],pbHexMac[2],pbHexMac[3],pbHexMac[4],pbHexMac[5],pbHexMac[6] /*szMac*/);
return 1;
}
sprintf(macadress,TXT_LFR_MSG_ERROR);
return 0;
}
//******************************************************************************
// Ping d'une ip + traitement IP + MAC + nom + TTL + OS
//******************************************************************************
DWORD WINAPI Ping(LPVOID lParam)
{
LVITEM lvi;
LCONFLIGNE Lligneconf;
struct hostent* remoteHost;
struct in_addr in;
struct in_addr iaDest;
LPHOSTENT pHost;
DWORD *dwAddress;
HANDLE hndlFile;
ICMPECHO icmpEcho;
BOOL reqArp =1;
char tmp[VAR_TMP];
int itemPos;
HANDLE hndlIcmp;
HANDLE (WINAPI *pIcmpCreateFile)(VOID);
BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
DWORD (WINAPI *pIcmpSendEcho) (HANDLE,DWORD,LPVOID,WORD, PIPINFO, LPVOID,DWORD,DWORD);
strcpy((char*)Lligneconf.ip,(char*)lParam);
ReleaseSemaphore(hSemaphore,1,NULL);
EnterCriticalSection(&Sync);
StartBlogTestError++;
LeaveCriticalSection(&Sync);
WaitForSingleObject(hSemaphore2,INFINITE);
//init de la config
iaDest.s_addr = inet_addr(Lligneconf.ip);
pHost = gethostbyname(Lligneconf.ip);
// copie de l'ip a pinger
dwAddress = (DWORD *)(*pHost->h_addr_list);
EnterCriticalSection(&Sync);
if ((hndlIcmp = LoadLibrary("ICMP.DLL"))!=0)
{
//fonctions pointeurs de la DLL
//récupération d'un pointeur vers chacune de ces fonctions fonctions
pIcmpCreateFile = (HANDLE (WINAPI *)(void))GetProcAddress((HMODULE)hndlIcmp,"IcmpCreateFile");
pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))GetProcAddress((HMODULE)hndlIcmp,"IcmpCloseHandle");
pIcmpSendEcho = (DWORD (WINAPI *)(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD)) GetProcAddress((HMODULE)hndlIcmp,"IcmpSendEcho");
LeaveCriticalSection(&Sync);
if (pIcmpCreateFile!=0 && pIcmpCloseHandle!=0 && pIcmpSendEcho!=0)
{
// récupéré un handle sur le reply
if ((hndlFile = pIcmpCreateFile())!= INVALID_HANDLE_VALUE)
{
pIcmpSendEcho(
hndlFile,
*dwAddress,
0,
0,
0,
&icmpEcho,
sizeof(icmpEcho),
Toms);
//si Ok
if ((icmpEcho.Status==0)&&(icmpEcho.Options.Ttl>0))
{
reqArp =0;
//ajout d'une ligne
lvi.mask = LVIF_TEXT|LVIF_PARAM;
lvi.iSubItem = 0;
lvi.lParam = LVM_SORTITEMS;
lvi.pszText="";
EnterCriticalSection(&Sync);
lvi.iItem = NB_Machines++;
itemPos=ListView_InsertItem(hListView, &lvi);
ListView_SetItemText(hListView,itemPos,COL_INFOS,"PING");
LeaveCriticalSection(&Sync);
Lligneconf.ligne = (unsigned long)itemPos;
//ip
tmp[0]='[';
tmp[1]=0;
strcat(tmp,Lligneconf.ip);
strcat(tmp,"]\0");
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_IP,tmp);
LeaveCriticalSection(&Sync);
//ttl
sprintf(tmp,"TTL:%d",icmpEcho.Options.Ttl);
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_TTL,tmp);
//OS
if (icmpEcho.Options.Ttl<=MACH_LINUX)//linux
{
ListView_SetItemText(hListView,itemPos,COL_OS,TXT_LFR_MSG_LINUX);
}else if (icmpEcho.Options.Ttl<=MACH_WINDOWS)//windows
{
ListView_SetItemText(hListView,itemPos,COL_OS,TXT_LFR_MSG_WIN);
}else //routeur
{
ListView_SetItemText(hListView,itemPos,COL_OS,TXT_LFR_MSG_ROUTEUR);
};
LeaveCriticalSection(&Sync);
//DNS
if (EtatDns)
{
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_INFOS,"DNS");
LeaveCriticalSection(&Sync);
in.s_addr = inet_addr(Lligneconf.ip);
if ((remoteHost=gethostbyaddr((char *)&in, 4, AF_INET))!=0)
strncpy(tmp,remoteHost->h_name,VAR_TMP);
else
strcpy(tmp,TXT_LFR_MSG_ERROR);
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_NOM,tmp);
LeaveCriticalSection(&Sync);
}
//adresse mac
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_INFOS,"ARP");
LeaveCriticalSection(&Sync);
if (RecupMacAdress(Lligneconf.ip,tmp))
{
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_MAC,tmp);
LeaveCriticalSection(&Sync);
}else
{
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_MAC,TXT_LFR_MSG_ERROR);
LeaveCriticalSection(&Sync);
}
//partages
if (EtatPartages)
{
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_INFOS,"Partages");
LeaveCriticalSection(&Sync);
Partages(Lligneconf.ip,Lligneconf.ligne);
}
}
pIcmpCloseHandle(hndlFile);
}
}
FreeLibrary((HMODULE)hndlIcmp);//libère la DLL
}else LeaveCriticalSection(&Sync);
if (reqArp)//on a pas trouver en ping on fait par requète arp
{
if (RecupMacAdress(Lligneconf.ip,tmp))
{
//ok la machine existe
int itemPos;
//ajout d'une ligne
lvi.mask = LVIF_TEXT|LVIF_PARAM;
lvi.iSubItem = 0;
lvi.lParam = LVM_SORTITEMS;
lvi.pszText="";
lvi.iItem = NB_Machines++;
EnterCriticalSection(&Sync);
itemPos=ListView_InsertItem(hListView, &lvi);
ListView_SetItemText(hListView,itemPos,COL_INFOS,"ARP");
LeaveCriticalSection(&Sync);
Lligneconf.ligne = (unsigned long)itemPos;
//mac
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_MAC,tmp);
LeaveCriticalSection(&Sync);
//ip
tmp[0]='[';
tmp[1]=0;
strcat(tmp,Lligneconf.ip);
strcat(tmp,"]\0");
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_IP,tmp);
LeaveCriticalSection(&Sync);
//on récupère le nom de la machine / DNS
if (EtatDns)
{
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_INFOS,"DNS");
LeaveCriticalSection(&Sync);
in.s_addr = inet_addr(Lligneconf.ip);
if ((remoteHost=gethostbyaddr((char *)&in, 4, AF_INET))!=0)
{
strncpy(tmp,remoteHost->h_name,VAR_TMP);
}else
{
strcpy(tmp,TXT_LFR_MSG_ERROR);
};
}
EnterCriticalSection(&Sync);
//DNS
if (EtatDns)
ListView_SetItemText(hListView,itemPos,COL_NOM,tmp);
//OS
ListView_SetItemText(hListView,itemPos,COL_OS,"FIREWALL");
//TTL
ListView_SetItemText(hListView,itemPos,COL_TTL,"TTL:---");
ListView_SetItemText(hListView,itemPos,COL_INFOS,"OK");
LeaveCriticalSection(&Sync);
reqArp =0;
}
}
//liste des ports
if (reqArp ==0 && EtatPorts)
{
EnterCriticalSection(&Sync);
ListView_SetItemText(hListView,itemPos,COL_INFOS,"PORTS");
LeaveCriticalSection(&Sync);
Liste_Port(Lligneconf);
}
EnterCriticalSection(&Sync);
//info de chargement
SendMessage(ProgressBAR2, PBM_SETPOS, (WPARAM)(++nb*100)/max,0);
StartBlogTestError--;
ListView_SetItemText(hListView,itemPos,COL_INFOS,"OK");
LeaveCriticalSection(&Sync);
ReleaseSemaphore(hSemaphore2,1,NULL);
return 0;
};
//******************************************************************************
// Scan : traitement des ip a scanner
//******************************************************************************
DWORD WINAPI ScanReseau(LPVOID lParam)
{
//récupération des 2 ip d'intervales
DWORD LIp1,LIp2;
BYTE L11,L12,L13,L14,L21,L22,L23,L24;
char tmp[MAX_TOMS];
char tmp2[MAX_TOMS];
WSADATA WSAData;
int j=0,i=0;
WSAStartup(0x02, &WSAData );
StartBlogTestError=0;
//init
max=0;
nb=0;
//init des threads
NB_Machines=0; //init du nombre de machines
//instance du Time out
GetWindowText(TOMS,tmp,50);
Toms=atoi(tmp);
if (Toms>MAX_TOMS)
{
SetWindowText(TOMS,"10000");
Toms = MAX_TOMS;
}else if (Toms<1)
{
SetWindowText(TOMS,"500");
Toms = 500;
}
//instance du nombre de threads
GetWindowText(NB_Threads,tmp,50);
NB_MAX_THREAD=atoi(tmp);
if (NB_MAX_THREAD>MAX_THREADS)
{
SetWindowText(NB_Threads,"1000");
NB_MAX_THREAD = MAX_THREADS;
}else if (NB_MAX_THREAD<1)
{
SetWindowText(NB_Threads,"10");
NB_MAX_THREAD = 10;
}
//récupération du comptenu des champs
SendMessage(IP_DEBUT,IPM_GETADDRESS, 0 ,(LPARAM) &LIp1);
SendMessage(IP_FIN,IPM_GETADDRESS, 0 ,(LPARAM) &LIp2);
//on récupère chacun des bits
L11 = LIp1 >> 24;
L12 = (LIp1 >> 16) & 0xFF;
L13 = (LIp1 >> 8) & 0xFF;
L14 = LIp1 & 0xFF;
L21 = LIp2 >> 24;
L22 = (LIp2 >> 16) & 0xFF;
L23 = (LIp2 >> 8) & 0xFF;
L24 = LIp2 & 0xFF;
hSemaphore=CreateSemaphore(NULL,1,1,NULL);
hSemaphore2=CreateSemaphore(NULL,NB_MAX_THREAD,NB_MAX_THREAD,NULL);
//test de la validité des ip
if (LIp1!=0 && L14 >0 && L14 < 255 && L13 <255 && L12 <255 && L11<255 || LIp2!=0 && L24 >0 && L24 < 255 && L23 <255 && L22 <255 && L21<255)
{
if (LIp1 == 0 || LIp1 == LIp2)
{
max = 1;
//alors on ajoute seulement 1 ip: le 2
WaitForSingleObject(hSemaphore,INFINITE);
sprintf(tmp,"%d.%d.%d.%d",L21,L22,L23,L24);
CreateThread(NULL,0,Ping,tmp,0,0);
}else if (LIp2 == 0 && L14 >0 && L14 < 255 && L13 <255 && L12 <255 && L11<255)
{
max = 1;
//alors on ajoute seulement 1 ip: le 1
WaitForSingleObject(hSemaphore,INFINITE);
sprintf(tmp,"%d.%d.%d.%d",L11,L12,L13,L14);
CreateThread(NULL,0,Ping,tmp,0,0);
}else
{
if ( L21>=L11 && (L22>=L12 || L21>L11 && L12 > L22) && (L23>=L13 || (L22>L12 || L21>L11)&& L13 > L23) && (L24>=L14 || (L23>L13 || L22>L12 || L21>L11)&& L14 > L24))
{
//calcul de max
max=((L21-L11)*(255*255*255))+1;//octet 1
if (L12<=L22)//octet 2
max+=(L22-L12)*(255*255);
else
max+=((255-L12)+L22)*(255*255);
if (L13<=L23)//octet 3
max+=(L23-L13)*(255);
else
max+=((255-L13)+L23)*(255);
if (L14<=L24)//octet 4
max+=(L24-L14);
else
max+=((255-L14)+L24);
//on ajoute en boucle les ip
while (L21>=L11)
{
while (L22>=L12 || L12<255 && L21>L11)
{
while (L23>=L13 || L13<255 && (L22>L12 || L21>L11))
{
while (L24>=L14 || L14<255 && (L22>L12 || L21>L11 || L23>L13))
{
while (StartBlogTestError > NB_MAX_THREAD)_sleep(10);
WaitForSingleObject(hSemaphore,INFINITE);
sprintf(tmp,"%d.%d.%d.%d",L11,L12,L13,L14);
CreateThread(NULL,0,Ping,tmp,0,0);
j++;
L14++;
snprintf(tmp2,TAILLE_TITRE_TEXTE,"%s [%s] %d/%d",NOM_APPLI,tmp,j,max);
SetWindowText(hwnd,tmp2);
//progression
SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)j*100/max, 0);
}
L13++;
L14=1;
}
L12++;
L13=0;
}
L11++;
L12 =0;
}
}else MessageBox(hwnd,"Interval d'IP non valide!","Erreur",MB_OK);
}
}else MessageBox(hwnd,"Interval d'IP non valide!","Erreur",MB_OK);
//on attend que tous les threads soient fini pour dir que le scan est fini
while (StartBlogTestError>0)_sleep(100);
WaitForSingleObject(hSemaphore2,0);
ReleaseSemaphore(hSemaphore2,1,NULL);
CloseHandle(hSemaphore);
CloseHandle(hSemaphore2);
//fin du scan
SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)100, 0);
//init état des composants
ActiveEdits(TRUE);
_sleep(100);
//init des progresse bar
SendMessage(ProgressBAR, PBM_SETPOS, (WPARAM)0, 0);
SendMessage(ProgressBAR2, PBM_SETPOS, (WPARAM)0,0);
//init bouton de scan
SetWindowText(BScanner,TXT_LFR_BT_SCAN);
//init de la fenêtre
snprintf(tmp,TAILLE_TITRE_TEXTE,"%s OK %d %s\0",NOM_APPLI,ListView_GetItemCount(hListView),TXT_LFR_MSG_ETAT_SCAN);
SetWindowText(hwnd,tmp);
Tri();
//écriture dans la fichier de log
if (EtatLogs) EcrireCTR(0);
activescan=0;
//on libère la mémoire
WSACleanup();
return 0;
}
//******************************************************************************
// écrire dans un fichier txt le rapport
//******************************************************************************
DWORD WINAPI EcrireCTR(LPVOID lParam)
{
//variables
char tmp[EXTRA_TMP];
char var[TAILLE_MAX];
int NBLigne;
int ligne,colonne;
time_t dateEtHMs;//structure de la date
int taille;
DWORD copiee;
// on récupère le nombre de lignes
NBLigne=ListView_GetItemCount(hListView);
//ouverture du fichier et écriture à la suite
HANDLE HLog = CreateFile(NOMFICLOG,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_ALWAYS,FILE_FLAG_SEQUENTIAL_SCAN,0);
//on se place a la fin du fichier
SetFilePointer(HLog,GetFileSize(HLog,NULL),0,FILE_BEGIN);
if (HLog == INVALID_HANDLE_VALUE || NBLigne==0)
{
CloseHandle(HLog);
return FALSE;
}
//entête
time(&dateEtHMs);//nombre d'item + recuperation de l'heure et la date actuelle
snprintf(tmp,EXTRA_TMP,"\r\n\r\n|%s\t\t%s: %d\t\t%s: %s |\r\n",NOM_APPLI,NB_MACHINES,NBLigne,TXT_LFR_MSG_DATE_SCAN,(char *)ctime(&dateEtHMs));
tmp[strlen(tmp)-29]=' '; //gestion du retour à la ligne après la date
WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
WriteFile(HLog,"|-------------------------------------------------------------------------------------------------------------------------------|\r\n",131,&copiee,0);
snprintf(tmp,EXTRA_TMP,"|%s\t\t\t\t\t|%s\t\t|%s\t\t\t\t|%s\t\t\t|%s\t\t\t|\r\n",TXT_LFR_BTLST_IP,TXT_LFR_BTLST_MAC,TXT_LFR_BTLST_NOM,TXT_LFR_BTLST_TTL,TXT_LFR_BTLST_OS);
WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
WriteFile(HLog,"|-------------------------------------------------------------------------------------------------------------------------------|\r\n",131,&copiee,0);
snprintf(tmp,EXTRA_TMP,"|");
// on récupère les items un par un , par ligne et on les met dans une variable ligne puis écriture
for (ligne=0;ligne<NBLigne;ligne++)
{
for (colonne=0;colonne<NB_COLONNE;colonne++)
{
ListView_GetItemText(hListView,ligne,colonne,var,TAILLE_MAX);
taille = strlen(var);
//gestion affichage des colonnes
if (taille<15&&colonne==0)
snprintf(tmp,EXTRA_TMP,"%s%s\t\t|",tmp,var);
else if (taille==0&&colonne>2)
snprintf(tmp,EXTRA_TMP,"%s%s\t\t\t|",tmp,var);
else if ((taille<5)&&(colonne<3))
snprintf(tmp,EXTRA_TMP,"%s%s\t\t\t\t|",tmp,var);
else if ((taille<7)&&(colonne<3))//7
snprintf(tmp,EXTRA_TMP,"%s%s\t\t\t|",tmp,var);
else if ((colonne!=0)&&(taille<11)||(taille<16)&&(colonne<3)&&(colonne!=0)||(taille<7)&&(colonne>2))
snprintf(tmp,EXTRA_TMP,"%s%s\t\t|",tmp,var);
else if (taille<23)
snprintf(tmp,EXTRA_TMP,"%s%s\t|",tmp,var);
else
snprintf(tmp,EXTRA_TMP,"%s%s|",tmp,var);
}
snprintf(tmp,EXTRA_TMP,"%s\r\n|",tmp);
WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
//ici on ajoute la gestion des partages et des ports
if(EtatPorts)
{
ListView_GetItemText(hListView,ligne,COL_PORTS,var,TAILLE_MAX);
if (strlen(var)>1)
{
snprintf(tmp,EXTRA_TMP,"Liste des ports ouverts standards:%s\r\n|",var);
WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
}
}
if(EtatPartages)
{
ListView_GetItemText(hListView,ligne,COL_PARTAGES,var,TAILLE_MAX);
if (strlen(var)>1)
{
snprintf(tmp,EXTRA_TMP,"Liste des partages:%s\r\n|",var);
WriteFile(HLog,tmp,strlen(tmp),&copiee,0);
}
}
tmp[0]=0;
}
WriteFile(HLog,"-------------------------------------------------------------------------------------------------------------------------------|\r\n",130,&copiee,0);
CloseHandle(HLog);
return 0;
};
Historique
- 08 septembre 2007 10:22:20 :
- ...
- 08 septembre 2007 14:00:42 :
- ...
- 23 septembre 2007 11:50:53 :
Sources du même auteur
|