Overview
Features
Download
Documentation
Community
Add-Ons & Services

Set IP header via POCO network library

Please post support and help requests here.

Set IP header via POCO network library

Postby artem » 15 Jun 2014, 18:09

I started with a simple example of a UDP client

Code: Select all
int main(int argc, char** argv){
    Poco::Net::SocketAddress sa("localhost", 12345);
    Poco::Net::DatagramSocket dgs;
    dgs.connect(sa);
    Poco::Timestamp now;
    std::string msg("<14>%w %f %H:%M:%S Hello, world!");
    dgs.sendBytes(msg.data(), msg.size());
    return 0;
}

It works perfectly, however I want to somehow change some parameters in the IP header, if I use raw sockets I can do change the following

Code: Select all
struct ipheader {
 unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
 unsigned char ip_tos;
 unsigned short int ip_len;
 unsigned short int ip_id;
 unsigned short int ip_off;
 unsigned char ip_ttl;
 unsigned char ip_p;
 unsigned short int ip_sum;
 unsigned int ip_src;
 unsinged int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */

...


  iph->ip_hl = 5;
  iph->ip_v = 4;
  iph->ip_tos = 0;
  iph->ip_len = sizeof (struct ip);
  iph->ip_id = htonl (54321);   /* the value doesn't matter here */
  iph->ip_off = 0;
  iph->ip_ttl = 255;
  iph->ip_p = 6;
  iph->ip_sum = 0;      /* set it to 0 before computing the actual checksum later */
  iph->ip_src.s_addr = inet_addr ("1.2.3.4");/* SYN's can be blindly spoofed */
  iph->ip_dst.s_addr = sin.sin_addr.s_addr;

  iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);
...

    int one = 1;
    const int *val = &one;
    if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
      printf ("Warning: Cannot set HDRINCL!\n");

    sendto (s,  datagram,   iph->ip_len,    0,  (struct sockaddr *) &sin,   sizeof (sin));
...

There is a RawSocket class in Poco, but there is no clear example how to use it. I believe I have to call

Code: Select all
RawSocket(
    SocketImpl * pImpl
);


SocketImpl has many "set" methods, but I could not find how for example I can set ip_tos or ip_ttl.

Also what does level argument means in all get and set methods.

I would appreciate if someone could explain me how to form my own ip header and use it with POCO.
artem
 
Posts: 2
Joined: 15 Jun 2014, 17:59

Re: Set IP header via POCO network library

Postby alex » 17 Jun 2014, 07:03

You should set the IP_HDRINCL option and provide your own IP header. See this article for more details.
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Set IP header via POCO network library

Postby artem » 17 Jun 2014, 17:58

Thanks for the prompt reply. Is there any way to do it via poco?
artem
 
Posts: 2
Joined: 15 Jun 2014, 17:59

Re: Set IP header via POCO network library

Postby alex » 18 Jun 2014, 04:22

Yes, create Poco::Net::RawSocket, call setOption with IP_HDRINCL on it, assemble the header + payload buffer and call sendTo(). It's off the top of my head, I did not check the details nor do I recall ever trying it but it should work.
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: cB32Dg3H2TTm and 1 guest