-
Winsock problem
I'm trying to write a program that simply pings 127.0.0.1 until it is closed to learn more about raw sockets. For some reason, when I compile it in MSVC++ 2008, I get:
Code:
1>------ Rebuild All started: Project: echo, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'echo', configuration 'Debug|Win32'
1>Compiling...
1>echo.c
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(21) : warning C4244: 'return' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(28) : error C2143: syntax error : missing ';' before 'type'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(29) : error C2143: syntax error : missing ';' before 'type'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(30) : error C2143: syntax error : missing ';' before 'type'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(31) : error C2143: syntax error : missing ';' before 'type'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(32) : error C2065: 'sin' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(32) : error C2224: left of '.sin_family' must have struct/union type
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(33) : error C2065: 'sin' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(33) : error C2224: left of '.sin_port' must have struct/union type
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(34) : error C2065: 'sin' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(34) : error C2224: left of '.sin_addr' must have struct/union type
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(35) : error C2143: syntax error : missing ';' before 'type'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(36) : error C2065: 'datagram' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(36) : warning C4022: 'memset' : pointer mismatch for actual parameter 1
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(38) : error C2065: 'one' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(39) : error C2065: 'val' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(39) : error C2065: 'one' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(39) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(41) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(41) : error C2223: left of '->headerlen' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(42) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(42) : error C2223: left of '->version' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(43) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(43) : error C2223: left of '->typeofservice' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(44) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(44) : error C2223: left of '->length' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(45) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(45) : error C2223: left of '->id' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(46) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(46) : error C2223: left of '->offset' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(47) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(47) : error C2223: left of '->TTL' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(48) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(48) : error C2223: left of '->protocol' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(49) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(49) : error C2223: left of '->checksum' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(50) : error C2146: syntax error : missing ';' before identifier 'ipheader'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(50) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(50) : error C2223: left of '->source' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(51) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(51) : error C2223: left of '->destination' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(51) : error C2065: 'sin' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(51) : error C2224: left of '.sin_addr' must have struct/union type
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(53) : error C2065: 'icmpheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(53) : error C2223: left of '->type' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(54) : error C2065: 'icmpheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(54) : error C2223: left of '->code' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(55) : error C2065: 'icmpheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(55) : error C2223: left of '->checksum' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(56) : error C2065: 'icmpheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(56) : error C2223: left of '->id' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(57) : error C2065: 'icmpheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(57) : error C2223: left of '->sequence' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(59) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(59) : error C2223: left of '->checksum' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(59) : error C2065: 'datagram' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(59) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(59) : error C2223: left of '->length' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(59) : error C2198: 'csum' : too few arguments for call
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(60) : error C2065: 'icmpheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(60) : error C2223: left of '->checksum' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(60) : error C2065: 'datagram' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(60) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(60) : error C2223: left of '->length' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(60) : error C2198: 'csum' : too few arguments for call
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(61) : warning C4047: 'function' : 'SOCKET' differs in levels of indirection from 'SOCKET (__stdcall *)(int,int,int)'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(61) : warning C4024: 'setsockopt' : different types for formal and actual parameter 1
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(61) : error C2065: 'IP_HDRINCL' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(61) : error C2065: 'val' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(61) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(61) : warning C4024: 'setsockopt' : different types for formal and actual parameter 4
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(61) : error C2065: 'one' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4047: 'function' : 'SOCKET' differs in levels of indirection from 'SOCKET (__stdcall *)(int,int,int)'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4024: 'sendto' : different types for formal and actual parameter 1
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : error C2065: 'datagram' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'int'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4024: 'sendto' : different types for formal and actual parameter 2
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : error C2065: 'ipheader' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : error C2223: left of '->length' must point to struct/union
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : error C2065: 'sin' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4047: 'function' : 'int' differs in levels of indirection from 'sockaddr *'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4024: 'sendto' : different types for formal and actual parameter 4
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : error C2065: 'sin' : undeclared identifier
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4047: 'function' : 'const sockaddr *' differs in levels of indirection from 'unsigned int'
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : warning C4024: 'sendto' : different types for formal and actual parameter 5
1>c:\documents and settings\wolf\desktop\projects\c\networking\raw sockets\echo\echo.c(65) : error C2198: 'sendto' : too few arguments for call
1>Build log was saved at "file://c:\Documents and Settings\Wolf\Desktop\Projects\C\Networking\Raw Sockets\echo\Debug\BuildLog.htm"
1>echo - 71 error(s), 15 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
The code is:
Code:
/*
Remember:
unsigned char - 1 byte (8 bits), unsigned short int - 2 bytes (16 bits) and unsigned int - 4 bytes (32 bits)
*/
#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <winsock2.h>
#include "socket.h"
unsigned short csum(unsigned short *buf, int nwords)
{
unsigned long sum;
for(sum=0;nwords>0;nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int main()
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2,2), &wsadata);
char datagram[4096];
struct ipheader *ipheader = (struct ipheader *) datagram;
struct icmpheader *icmpheader = (struct icmpheader *) datagram + sizeof(struct ipheader);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(9466);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
unsigned int *socket = WSASocket(PF_INET, SOCK_RAW, IP_PROTOCOL_ICMP, 0, 0, 0);
memset(datagram, 0, 4096);
one = 1;
val = &one;
ipheader->headerlen = 5; /* No payload */
ipheader->version = 4; /* IP version 4 */
ipheader->typeofservice = 0; /* No routing priority, no TOS */
ipheader->length = sizeof(struct ipheader) + sizeof(struct icmpheader); /* Remeber, no payload */
ipheader->id = 0xF00D; /* Value doesn't matter */
ipheader->offset = 0; /* No fragmentation */
ipheader->TTL = 128; /* Let's not use the max value XD */
ipheader->protocol = IP_PROTOCOL_ICMP; /* ICMP packet */
ipheader->checksum = 0 /* It should be set to 0 when we compute it */
ipheader->source = inet_addr("10.100.2.7"); /* Spoofed source, it's a reserved address */
ipheader->destination = sin.sin_addr.s_addr; /* Myself */
icmpheader->type = ICMP_ECHO_REQUEST; /* Self explanatory */
icmpheader->code = ICMP_ECHO_REQUEST_CODE; /* Again, self explanatory */
icmpheader->checksum = 0; /* See IP header checksum */
icmpheader->id = (unsigned short)GetCurrentProcessId(); /* I dunno. */
icmpheader->sequence = 0; /* Not in a sequence */
ipheader->checksum = csum((unsigned short *)datagram, ipheader->length >> 1); /* Calculate IP checksum, put in header */
icmpheader->checksum = csum((unsigned short *)datagram, ipheader->length >> 1); /* Do same for ICMP header */
setsockopt(socket, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)); /* Tell Winsock we are supplying headers */
for(;;) /* For() ever */
{
if(sendto(socket, datagram, ipheader->length, 0, (struct sockaddr *) &sin, sizeof(sin)) < 0);
printf("\n Error");
}
return(0);
}
Code:
/*
Socket.h, by Wolf, to help working with raw sockets
Remember:
unsigned char - 1 byte (8 bits), unsigned short int - 2 bytes (16 bits) and unsigned int - 4 bytes (32 bits)
*/
/**********************************************
IP Section
**********************************************/
/* Protocols */
#define IP_PROTOCOL_ICMP 1
#define IP_PROTOCOL_TCP 6
#define IP_PROTOCOL_UDP 17
/* IP Header */
struct ipheader
{
unsigned char headerlen:4, version:4; /* Make each one 4 bits, headerlen is the ip header length in 32bit octets. Value of 5 for the hl means 20 bytes */
/* (5 * 4). values other than 5 only need to be set it the ip header contains options, version is 4 */
unsigned char typeofservice; /* Normal is 0x00 the first 3 bits stand for routing priority, the next 4 bits for the type of service */
unsigned short int length; /* total length of the ip datagram. this includes ip header, icmp or tcp or udp header and payload size in bytes. */
unsigned short int id; /* The ID sequence number is mainly used for reassembly of fragmented IP datagrams. If not fragmented, can be random */
unsigned short int offset; /* Used for reassembly of fragmented datagrams, first 3 bits fragment flags, 1st always 0, 2nd don't fragment bit, */
/* set by offset |= 0x4000 3rd the more fragments coming bit, set by offset |= 0x2000, next 13 bits are the fragment */
/* offset, containing the number of 8-byte big packets already sent. */
unsigned char TTL; /* Time To Live, kill counter decremented by one each hop. Max is 0xFF */
unsigned char protocol; /* Transport layer protocol, ICMP = 1, TCP = 6, UDP = 17 etc. */
unsigned short int checksum; /* Checksum for the whole datagram, self-explanatory */
unsigned int source; /* Source IP for the datagram */
unsigned int destination; /* Destination IP adderss for the datagram */
};
/*********************************************
ICMP Section
*********************************************/
/* ICMP Types */
#define ICMP_ECHO_REPLY 0
/* Two is reserved */
#define ICMP_DEST_UNREACHABLE 3
#define ICMP_SOURCE_QUENCH 4
#define ICMP_REDIRECT_MSG 5
#define ICMP_ALT_HOST_ADDR 6
/* Seven is reserved */
#define ICMP_ECHO_REQUEST 8
#define ICMP_ROUTER_ADVERTISEMENT 9
#define ICMP_ROUTER_SOLICIT 10
#define ICMP_TIME_EXCEEDED 11
#define ICMP_BAD_IP_HEADER 12
#define ICMP_TIMESTAMP_REQUEST 13
#define ICMP_TIMESTAMP_REPLY 14
#define ICMP_INFORMATION_REQUEST 15
#define ICMP_INFORMATION_REPLY 16
#define ICMP_ADDR_MASK_REQUSET 17
#define ICMP_ADDR_MASK_REPLY 18
/* Nineteen is reserved */
/* 20 - 29 are reserved */
#define ICMP_TRACEROUTE 30
#define ICMP_DATAGRAM_CONV_ERROR 31
#define ICMP_MOBILE_HOST_REDIRECT 32
#define ICMP_WHERE_ARE_YOU 33
#define ICMP_HERE_I_AM 34
#define ICMP_MOBILE_REG_REQUEST 35
#define ICMP_MOBILE_REG_REPLY 36
#define ICMP_DOMAIN_NAME_REQUEST 37
#define ICMP_DOMAIN_NAME_REPLY 38
#define ICMP_SKIP_DISCOVERY 39
#define ICMP_PHOTURIS_SEC_FAIL 40
#define ICMP_EXPERIMENTAL_PROTOCOL 41
/* 42 - 255 are reserved */
/* ICMP Codes */
#define ICMP_DEST_NETWORK_UNREACH 0
#define ICMP_DEST_HOST_UNREACH 1
#define ICMP_DEST_PROTOCOL_UNREACH 2
#define ICMP_DEST_PORT_UNREACH 3
#define ICMP_FRAG_REQUIRED 4
#define ICMP_SRC_ROUTE_FAILED 5
#define ICMP_DEST_NETWORK_UNKNOWN 6
#define ICMP_DEST_HOST_UNKNOWN 7
#define ICMP_SRC_HOST_ISOLATED 8
#define ICMP_NETWORK_ADMIN_PROHIBIT 9
#define ICMP_HOST_ADMIN_PROHIBIT 10
#define ICMP_NETWORK_UNREACH_TOS 11
#define ICMP_HOST_UNREACH_TOS 12
#define ICMP_COMMS_ADMIN_PROHIBIT 13
#define ICMP_SRC_QUENCH 0
#define ICMP_REDIR_DATAGRAM_NETWORK 0
#define ICMP_REDIR_DATAGRAM_HOST 1
#define ICMP_REDIR_DATAGRAM_NETWORK_TOS 2
#define ICMP_REDIR_DATAGRAM_HOST_TOS 3
#define ICMP_TTL_EXPIRED_TRANSIT 0
#define ICMP_FRAGMENT_REASM_TIMEOUT 1
#define ICMP_PTR_INDICATES_ERROR 0
#define ICMP_MISSING_REQUIRED_OPTION 1
#define ICMP_BAD_LENGTH 2
#define ICMP_ECHO_REQUEST_CODE 0
#define ICMP_ECHO_REPLY_CODE 0
/* ICMP Header */
struct icmpheader
{
unsigned char type; /* See ICMP types */
unsigned char code; /* Depends heavily on type. If unsure, set to 0 */
unsigned short int checksum; /* Same as IP header checksum */
unsigned short int id; /* Used it echo request or echo reply messages to ID the request */
unsigned short int sequence; /* Sequence number, if more than one is sent */
};
-
So the first bit is definitely returning an unsigned long value from a function that returns unsigned short -- using ~ doesn't magically truncate a variable.
Also VS is a !#$@#$ about C: C hasn't changed since C89, in their opinion, and the idea of mixing code with variable declarations is heresy. Move the WSAStartup line after your variable declarations.