porting linux SOCK_RAW to freebsd

Alin-Adrian Anton aanton at reversedhell.net
Wed Dec 10 12:13:08 PST 2003


Andy Hilker wrote:

>Hi,
>
>i try porting a little utility from linux to freebsd.
>Maybe someone could give me a hint, what i am doing wrong.
>
>
>-- snip --
>    /* Note: not portable */
>    // if ((s = socket(AF_INET, SOCK_PACKET, SOCK_PACKET)) < 0) {
>    // ??? ported 
>    if ((s = socket(AF_INET,SOCK_RAW,IPPROTO_RAW)) < 0) {
>        if (errno == EPERM)
>            fprintf(stderr, "programm must run as root\n");
>        else
>            perror("programm: socket");
>        if (! debug)
>            return 2;
>    }
>
>
>    /* Fill in the source address, if possible.
>       The code to retrieve the local station address is Linux specific. */
>    /* Note: not portable */
>    /*  
>    if (! opt_no_src_addr){
>        struct ifreq if_hwaddr;
>        unsigned char *hwaddr = ifr_addr.sa_data;
>
>        strcpy(if_hwaddr.ifr_name, ifname);
>        if (ioctl(s, SIOCGIFHWADDR, &if_hwaddr) < 0) {
>            fprintf(stderr, "SIOCGIFHWADDR on %s failed: %s\n", ifname,
>                    strerror(errno));
>            return 1;
>        }
>        memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
>
>        if (verbose) {
>            printf("The hardware address (SIOCGIFHWADDR) of %s is type %d  "
>                   "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n", ifname,
>                   if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
>                   hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
>        }
>    }
>   */
>   // ??? ported, xx:xx:xx:xx:xx:xx = MAC Adress of sender
>        memcpy(outpack+6, "xx:xx:xx:xx:xx:xx", 6);
>
>
>   /* Note: not portable */
>   /*
>    whereto.sa_family = 0;
>    strcpy(whereto.sa_data, ifname);
>
>    if ((i = sendto(s, outpack, pktsize, 0, &whereto, sizeof(whereto))) < 0)
>        perror("sendto");
>    else if (debug)
>        printf("sendto returned %d.\n", i);
>   */
>   // ??? ported, fxp0 = sending interface
>    whereto.sa_family = 0;
>    strcpy(whereto.sa_data, "fxp0");
>
>    if ((i = sendto(s, outpack, pktsize, 0, &whereto, sizeof(whereto))) < 0)
>        perror("sendto");
>    else if (debug)
>        printf("sendto returned %d.\n", i);
>-- snip --
>
>bye,
>Andy
>
>  
>
I wrote a paper on FreeBSD raw sockets a while ago. You can find it here:
http://www.reversedhell.net/rawsockets/raw_tcp.tgz
or here:
http://packetstormsecurity.org/programming-tutorials/raw_tcp.tgz

Cheers/bye,
Alin.





More information about the freebsd-hackers mailing list