Re: working of setsockopt() and getsockopt()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Amit Kucheria wrote:

> > > I have set some IP options for my sockets (IP_HDRINCL & IP_ROUTER_ALERT)  
> > > using setsockopt(), but when I do a getsockopt(), nothing is reflected.
> > > I know I am forgetting something very elementary here, but cant put my 
> > > finger on it. Any pointers are welcome.
> > 
> > IP_HDRINCL and IP_ROUTER_ALERT aren't "options", in the sense of
> > IP_OPTIONS.
> 
> So what you mean is that they are not _IP_ options; they are
> more of _raw_ socket options?

Yes.

> > Calling getsockopt() on any of the above options should return
> > whatever value was set by a preceding setsockopt() call. However, each
> > of these options is independent; setting one of them won't change any
> > of the others.
> > 
> > Also, if you set both IP_HDRINCL and IP_OPTIONS, IP_OPTIONS is
> > ignored.
> 
> Then I guess the right question to ask here is:
> 
> How can I create an IP packet using raw sockets with the IP_ROUTER_ALERT 
> option set?
> 
> Once I allocate a buffer and fill out the IP and TCP headers, HOW then 
> should i add the IP_ROUTER_ALERT option and change the length of the IP 
> header (ip->ihl and ip->tot_len) corresponding to this option.

Just append the option (code byte, length byte, padding) to the IP
header, updating the length fields accordingly.

> ------ sample code of what i am doing currently -----------

+    u8 *ra;

>    iph = (struct iphdr *)buffer;

+    ra = (u8 *) buffer + sizeof(struct iphdr);

>    tcph = (struct tcphdr *) buffer + sizeof(struct iphdr);

[Note: This is incorrect; tcph would be offset by 20 TCP headers (400
bytes), not 20 bytes. You would need to enclose the addition in
parentheses (and "buffer" needs to be "char *" or similar).]

	tcph = (struct tcphdr *) (ra + 4);

>    printf("Set pointer to IP and TCP headers!\n");
>    
>    memset(buffer, 0, PACKET_SIZE); /* zero out the buffer */
>    
>    /* fill in the ip header */

-    iph->ihl = 5;

+    iph->ihl = 6;

>    iph->version = 4;
>    iph->tos = 0;

-    iph->tot_len = sizeof(struct iphdr)
-                   + sizeof(struct tcphdr); /* no payload */

+    iph->tot_len = sizeof(struct iphdr)
+                   + 4
+                   + sizeof(struct tcphdr); /* no payload */

>    iph->id = htonl(23456); /* some value..not imp */
>    iph->frag_off = 0;
>    iph->ttl = 255;
>    iph->protocol = 48;   /* protocol: 6 = TCP, 48 = new */
>    iph->check = 0; /* compute checksum later */
>    iph->saddr = inet_addr("1.2.3.4"); /* some spoofed src address */
>    iph->daddr = dest.sin_addr.s_addr;
>    printf("IP header filled\n");

+    ra[0] = IPOPT_RA;	/* option code */
+    ra[1] = 4;		/* option length */
+    ra[2] = 0;		/* padding */
+    ra[3] = 0;		/* padding */

>    /* fill in the tcp header */   

[snip]

-- 
Glynn Clements <glynn.clements@virgin.net>
-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux