1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| #include <windows.h>
#include <winsock2.h>
#include <commctrl.h>
#define IDB_SCAN 1
#define IDB_STOP 2
#define IDB_SAVINFILE 3
typedef struct d_lpParam{
char szIP[15+1];
unsigned int uiStartPort;
unsigned int uiEndPort;
HWND hwListV;
}LPPARAMTHREAD;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void __stdcall GetPorts(char * szPorts,unsigned int * uiStart,unsigned int * uiEnd);
void Tproc(LPPARAMTHREAD * lpParam);
char szClassName[ ] = "CodeBlocksWindowsApp";
HINSTANCE GlobalHInstance;
WSADATA wsaData;
SOCKET sTarget;
SOCKADDR_IN saiTarget = {0};
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (hThisInstance,"A");
wincl.hIconSm = LoadIcon (hThisInstance,"A");
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
if (!RegisterClassEx (&wincl))
return 0;
/* Initialisation du WSADATA. */
WSAStartup(MAKEWORD(2,0),&wsaData);
/* -- fin initialisation -- */
if(sTarget = socket(AF_INET,SOCK_STREAM,0) == -1)return 1;
saiTarget.sin_family = AF_INET;
InitCommonControls();
GlobalHInstance = hThisInstance;
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"DCKScan", /* Title Text */
WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwEditLimit,hwEditIP;
static HWND hwListVPorts;
static HWND hwButtonScan,hwButtonStop,hwSaveInFile;
HWND hwLabelLimit;
switch (message)
{
case WM_CREATE : hwListVPorts = CreateWindow(WC_LISTVIEW,0,LVS_REPORT | WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP,
10,20,200,300,hwnd,NULL,GlobalHInstance,NULL);
LV_COLUMN lvcCol;
lvcCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
lvcCol.cx = 100;
lvcCol.pszText = "Ports";
ListView_InsertColumn(hwListVPorts,1,&lvcCol);
lvcCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
lvcCol.cx = 100;
lvcCol.pszText = "Status";
ListView_InsertColumn(hwListVPorts,1,&lvcCol);
/* Appliquer les styles etendu à la listview.
* 0x1000+54 = LVM_SETEXTENDEDLISTVIEWSTYLE.
* 0x20 = LVM_EX_FULLROWSELECT.
*/
SendMessage(hwListVPorts,0x1000+54,0x20,0x20);
hwEditIP = CreateWindowEx(WS_EX_CLIENTEDGE,"edit","127.0.0.1",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
230,50,100,20,hwnd,NULL,GlobalHInstance,NULL);
hwEditLimit = CreateWindowEx(WS_EX_CLIENTEDGE,"edit","0,65535",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
230,90,100,20,hwnd,NULL,GlobalHInstance,NULL);
hwButtonScan = CreateWindow("button","Scan",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
230,130,100,20,hwnd,(HMENU)IDB_SCAN,GlobalHInstance,NULL);
hwButtonStop = CreateWindow("button","Stop",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
230,150,100,20,hwnd,(HMENU)IDB_STOP,GlobalHInstance,NULL);
HFONT PoliceTahoma = CreateFont(13,0,0,0,0,0,0,0,0,0,0,0,0,"tahoma");
SendMessage(hwListVPorts,WM_SETFONT,(long)PoliceTahoma,0);
SendMessage(hwEditIP,WM_SETFONT,(long)PoliceTahoma,0);
SendMessage(hwEditLimit,WM_SETFONT,(long)PoliceTahoma,0);
SendMessage(hwButtonScan,WM_SETFONT,(long)PoliceTahoma,0);
SendMessage(hwButtonStop,WM_SETFONT,(long)PoliceTahoma,0);
break;
case WM_COMMAND : switch(HIWORD(wParam)){
case BN_CLICKED : switch(LOWORD(wParam)){
case IDB_SCAN : { char szPorts[11+1]="";
DWORD dwTid;
static LPPARAMTHREAD lpParam;
GetWindowText(hwEditLimit,szPorts,11+1);
if(!*szPorts){
MessageBox(hwnd,"Please select ports.","Error :",MB_OK | MB_ICONERROR);
SetFocus(hwEditLimit);
}
GetPorts(szPorts,&(lpParam.uiStartPort),&(lpParam.uiEndPort));
GetWindowText(hwEditIP,lpParam.szIP,15+1);
if(!*(lpParam.szIP)){
MessageBox(hwnd,"Please enter a ip.","Error",MB_OK | MB_ICONERROR);
SetFocus(hwEditIP);
}
lpParam.hwListV = hwListVPorts;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Tproc,(LPPARAMTHREAD*)&lpParam,0,&dwTid);
}
break;
case IDB_STOP :
break;
case IDB_SAVINFILE :
break;
}
break;
}
break;
case WM_DESTROY:
WSACleanup();
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
/* TASK : Récupère les ports de début et de fin pour le scan. */
void __stdcall GetPorts(char * szPorts,unsigned int * uiStart,unsigned int * uiEnd){
char szStart[5+1]="";
char * ptStart = szStart;
char * ptPorts = szPorts;
do{
*ptStart = *ptPorts;
++ptStart;
++ptPorts;
}while(*ptPorts != ',' && *ptPorts);
++ptStart = 0;
++ptPorts; /* Passe la virgule. */
*uiStart = atoi(szStart);
*uiEnd = atoi(ptPorts);
}
void Tproc(LPPARAMTHREAD * lpParam){
int uiX;
unsigned short usCount;
char szPort[5+1];
LV_ITEM lviItem;
SOCKET test;
lviItem.mask = LVIF_TEXT;
saiTarget.sin_addr.s_addr = inet_addr(lpParam->szIP);
for(uiX = lpParam->uiStartPort;uiX <= lpParam->uiEndPort;++uiX){
saiTarget.sin_port = htons(uiX);
itoa(uiX,szPort,10);
lviItem.iItem = 65535;
lviItem.iSubItem = 0;
lviItem.pszText = szPort;
usCount = ListView_InsertItem(lpParam->hwListV,&lviItem);
lviItem.iItem = usCount;
lviItem.iSubItem = 1;
test = connect(sTarget,(SOCKADDR*)&saiTarget,sizeof(saiTarget));
if(test == -1)lviItem.pszText = "Close";
else lviItem.pszText = "Open";
ListView_SetItem(lpParam->hwListV,&lviItem);
}
closesocket(sTarget);
} |
Partager