🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #15832

Raw sockets with winsock

#15832
From: "Kjartan T. Hjörvar"
Date: Sun, 15 Jun 2003 01:26
56 lines
2711 bytes
Hi

I'm having a spot of trouble with the code below. It should open a raw 
socket and set the IP_HDRINCL option. After that it should send an 
ippacket (the sent packet in this case is invalid but should work). The 
code works fine when compiled with visual c++ 6 but not with borland 
builder 5, 6 nor bcc55.

Kjartan T. Hjorvar
kjartant@simnet.is


//---------------------------------------------------------------------------

#pragma comment( lib, "Ws2_32.lib" )  // for MS visual c++ 6

#include <stdio.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
//---------------------------------------------------------------------------

int main(char* args)
{
   int err;
   int res;

   WORD wVersionRequested;
   WSADATA wsaData;

   wVersionRequested = MAKEWORD( 2, 2 );

   err = WSAStartup( wVersionRequested, &wsaData );
   if (err == 0)
   {
     SOCKET s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
     int socketerror = WSAGetLastError();
     printf("socketerror                 : %i\n", socketerror);
     int on = 1;

     setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on));
     int setopterr = WSAGetLastError();
     printf("setsockopterror             : %i\n", setopterr);

     struct sockaddr_in dest;
     dest.sin_family = AF_INET;
     dest.sin_port = htons(80);
     dest.sin_addr.S_un.S_addr = inet_addr("192.168.0.5");

     int sentbytes = sendto(s, "01234567890123456789", 20, 0, (struct 
sockaddr *)&dest, sizeof(dest));
     int sendtoerror = WSAGetLastError();
     printf("sentbytes                   : %i\n", sentbytes);
     printf("sendtoerror                 : %i\n", sendtoerror);
   }
}
//---------------------------------------------------------------------------
Technical Details
Message-ID: <3eebcb3d$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,37b140341e7bf2c2,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-06-14 18:31:19 PST
Date: Sun, 15 Jun 2003 01:26:32 +0000
From: "Kjartan T. Hjörvar" <kjartant@simnet.is>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3) Gecko/20030312
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Raw sockets with winsock
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 212.30.210.28
Message-ID: <3eebcb3d$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1055640381 212.30.210.28 (14 Jun 2003 18:26:21 -0700)
Lines: 57
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail