Re: working of setsockopt() and getsockopt()

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

 



HI Amit,

To get the option which is already set use the same options. IP_OPTIONS will
not get whether IP_HDRINCL is set or not.

Also while setting options like this, use SOL_IP as socket option level. This
sets the level to IP. Since this Macro is also defined to zero like IPPROTO_IP

you didn't find anything mysterious.

the correct way is

setsockopt(fd, SOL_IP, IP_HDRINCL, &one, sizeof(one));

sameway same arguements for getsockopt also to check whether the options is
set. IP_OPTIONS is only for standard IP header's options. IP_HDRINCL is not of

such kind.

THanks,
Ramesh



Amit Kucheria wrote:

> Hi,
>
> 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.
>
> Sincerely,
> Amit
>
> ---------------- Start of code -----------------------
>
> #include <stdio.h>
> #include <netinet/in.h>
> #include <sys/socket.h>
> #include <netinet/ip.h>
> #include <netinet/tcp.h>
> #define OPTLEN 50
>
> int main(int argc, char *argv[])
> {
>    int fd = socket(PF_INET, SOCK_RAW, 48);
>    int i;
>    int optlen = OPTLEN;
>    char optbuff[optlen];
>    int one = 1;
>    int zero = 0;
>
>    if (fd < 0)
>    {
>        perror("socket");
>        exit(1);
>    }
>
>    printf("Socket created successfully!\n");
>
>    /* set socket options to IP_HDRINCL to prevent kernel
>     * from including another IP header
>     */
>    if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0)
>    {
>        perror("setsockopt: IP_HDRINCL");
>        exit(1);
>    }
>    printf("Socket IP_HDRINCL set!\n");
>
>    if (setsockopt(fd, IPPROTO_IP, IP_ROUTER_ALERT, &one, sizeof(one)) < 0)
>    {
>        perror("setsockopt: IP_ROUTER_ALERT");
>        exit(1);
>    }
>    printf("Socket IP_ROUTER_ALERT set!\n");
>
>    memset(optbuff, 0, optlen);
>    printf("b4 getsockopt: optlen: %d\n", optlen);
>    if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS, optbuff, &optlen) != 0)
>    {
>        perror("getsockopt");
>        exit(1);
>    }
>
>    /* Print options */
>    printf("after getsockopt: optlen: %d\n", optlen);
>    for (i=0; i < OPTLEN; i+=2)
>    {
>        printf("%02x %02x  ", optbuff[i], optbuff[i+1]);
>    }
>    putchar('\n');
>
>    return 1;
> }
>
> ---------------- End of code -----------------------
>
> --
> I'm an angel!!! Honest!
> The horns are just there to hold the halo up straight.
> ^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^
>                   Amit Kucheria
>           EECS Grad. Research Assistant
>          University of Kansas @ Lawrence
>    (R): +1-785-830-8521 ||| (C): +1-785-760-2871
> ____________________________________________________
>
> -
> : 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

-
: 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