Sniphfer on s++ and Winsock2
-
I'm trying to create my own book, that's what I've done. The only thing I can't do to win, can tell you what's wrong. WSAGetLastError = 10038
#include <conio.h> #include <stdio.h> #include <winsock2.h> void main() {
struct sockaddr saddr; int sa_size,data_size; char Buffer[1024]; WSADATA wsaData; struct sockaddr_in sa; int Result = WSAStartup(MAKEWORD(2, 2), &wsaData); if (Result != 0){ cout << "WSA strtup failed " << Result << endl; } char hostname[100]; int in = 0; WSADATA wsa; if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR){ cout << "Unable to get hostname" << endl; exit(1); } struct hostent *local = gethostbyname(hostname); if (local == NULL){ cout << "Got an error " << endl; exit(1); } SOCKET raw_sock; cout << "Host name: " << hostname << endl; raw_sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP); if (raw_sock<0){ cout << "Unable to create raw socket: " << endl; exit(1); } in_addr addr; cout << "RAW socket has been created" << endl; cout << "Available Network Interfaces" << endl; for (int i = 0; local->h_addr_list[i] != 0; ++i){ memcpy(&addr, local->h_addr_list[i], sizeof(struct in_addr)); cout << "Interface [ " << i << " ]: Adress " << inet_ntoa(addr) << endl; } memset(&sa, 0, sizeof(sa)); memcpy(&sa.sin_addr.s_addr, local->h_addr_list[in], sizeof(sa.sin_addr.s_addr)); sa.sin_family = AF_INET; sa.sin_port = 0; if (bind(raw_sock, (struct sockaddr*)&sa, sizeof(sa)) == SOCKET_ERROR){ cout << "Error binding" << WSAGetLastError() << endl; closesocket(raw_sock); exit(1); } cout << "Bind completed" << endl;
}
-
Let's start by checking the correctness of the sket is being done wrong. It's like this:
if (raw_sock == INVALID_SOCKET) { // Обработка ошибки. }
In order to create a RAW-Socket, the program needs to start with the rights of the administrator. Then there's a mistake.
WSAEINVAL
(10022, Invalid argument). It's connected to the wrong parameters in the creation of a sket. Correct:raw_sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
Now.
bind
It's going well.