Windows socket source
2009. 8. 6. 18:23
#include#include #include #include #define BUFSIZE 1024 #define PORT 80 void ErrorHandling(char *message); int main(int argc, char *argv[]) { WSADATA wsaData; SOCKET hSocket; char message[BUFSIZE]; int len, i, option, result = 0; SOCKADDR_IN servAddr; struct hostent *host; char *lists[] = {0,}; #if 0 if(argc!=2) { Usuage(argv[0]); } #endif #if 1 if(WSAStartup(MAKEWORD(2, 2), &wsaData) !=0) ErrorHandling("WSAStartup() error!"); #endif for(i=0; i<4; i++) { host = gethostbyname(lists[i]); printf("trying to connect %s...\n", lists[i]); #if 1 hSocket=socket(PF_INET, SOCK_STREAM, 0); if(hSocket == INVALID_SOCKET) ErrorHandling("socket() error"); #endif option = TRUE; setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&option, sizeof(option)); memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = inet_addr(inet_ntoa( *(struct in_addr*)host->h_addr_list[0])); servAddr.sin_port = htons(PORT); if(connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) ErrorHandling("connect() error!"); //sprintf(message, "GET /index.html HTTP/1.1"); sprintf(message, "hello"); send(hSocket, message, strlen(message), 0); #if 1 len=recv(hSocket, message, BUFSIZE-1, 0); message[len] = 0; if(len == -1) { printf("Can`t connect %s.\n", lists[i]); result--; } if(len > 0) { printf("Success.connected to %s.\n", lists[i]); result++; } closesocket(hSocket); #endif } if(result > 2) { printf("This machine seems to be clean\n"); } else if (result <2 && result >0) printf("Warning. This machine needs to be other inspection.\n"); else { printf("Infected!. Need some help.\n"); } WSACleanup(); return 0; } void ErrorHandling(char *message) { fputs(message, stderr); fputc( '\n', stderr); exit(1); }