NetLink ARP Table

Source: Internet
Author: User
Tags readfile rfc strtok
#include <netinet/ip.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <syslog.h> #include <fcntl.h> #include <net/if_arp.h> #include <sys/socket.h> #include <
netinet/in.h> #include <string.h> #include <errno.h> #include <time.h> #include <sys/uio.h>     
#include <bits/sockaddr.h> #include <asm/types.h> #include <linux/rtnetlink.h> FILE *ip4to6;
Char Ipv4[inet_addrstrlen];
Char Ipv6[inet6_addrstrlen];

unsigned long long lladdr;
    int Injectmacipv4 () {int FD = socket (Pf_netlink, SOCK_DGRAM, Netlink_route);
    struct SOCKADDR_NL la;
    memset (&la, 0, sizeof (LA));
    la.nl_family = Af_netlink;
    La.nl_pad = 0;
    La.nl_pid = Getpid ();
    
    la.nl_groups = 0;

    Bind (FD, (struct sockaddr*) &la, sizeof (LA));
        struct {struct NLMSGHDR n;
        struct NDMSG R;
    Char buf[1024];
    } REQ2;
   memset (&req2, 0, sizeof (REQ2)); int ndmsglen = nlmsg_length (sizeof (struct ndmsg));
    Create the IP attribute struct rtattr *dstattr;
    dstattr = (struct rtattr *) req2.buf;
    Dstattr->rta_type = NDA_DST;
    Dstattr->rta_len = sizeof (struct rtattr) + 4;
    int intip;
    Inet_pton (Af_inet, IPv4, ((char *) dstattr) + sizeof (struct rtattr));

Ndmsglen + + dstattr->rta_len;
    Create the MAC attribute//struct rtattr *dstattr;
    dstattr = (struct rtattr *) Req2.buf + 2;
    Dstattr->rta_type = nda_lladdr;
    Dstattr->rta_len = sizeof (struct rtattr) + 6;
    memcpy ((char *) dstattr) + sizeof (struct rtattr), &lladdr, 6);

Ndmsglen + + dstattr->rta_len; Fill in the NetLink message header Req2.n.nlmsg_len = Ndmsglen;
    48;
    Req2.n.nlmsg_type = Rtm_newneigh; Req2.n.nlmsg_flags = nlm_f_request| nlm_f_create| NLM_F_EXCL;
    1537 Req2.n.nlmsg_seq;

Req2.n.nlmsg_pid;
    Fill in the NetLink message newneigh req2.r.ndm_family = af_inet; REQ2.R.Ndm_pad1;
    Req2.r.ndm_pad2; Req2.r.ndm_ifindex = 2;
    /sys/class/net/eth2/ifindex req2.r.ndm_state = nud_permanent;
    Req2.r.ndm_flags;

Req2.r.ndm_type;
    Debug stuff about the packet to be send via NetLink/* printf ("The Sent packet:\n\n"); printf ("nlmsg header:\nnlmsg_len =%i\nnlmsg_type =%i\nnlmsg_flags =%i\nnlmsg_seq =%i\nnlmsg_pid =%i\n\n", Req2.n.nlms
    G_len, Req2.n.nlmsg_type, Req2.n.nlmsg_flags, Req2.n.nlmsg_seq, req2.n.nlmsg_pid); printf ("nlmsg message:\nndm_family =%i\nndm_pad1 =%i\nndm_pad2 =%i\nndm_ifindex =%i\nndm_state =%i\nndm_flags =%i\nn Dm_type =%i\n\n ", req2.r.ndm_family, Req2.r.ndm_pad1, Req2.r.ndm_pad2, Req2.r.ndm_ifindex, Req2.r.ndm_state,
    Req2.r.ndm_flags, Req2.r.ndm_type);
    printf ("buf:\n");
    int bufteller = 0;
        while (Bufteller < 256) {printf ("%i", Req2.buf[bufteller]);
    bufteller++;
 printf ("\ n \ nthe");
    */int status2; Status2 = Send (FD, &AMP;REQ2, Req2.n.nlmsg_len, 0);
    Close (FD);
return 1;

    int Getmacipv6 () {int FD = socket (Pf_netlink, SOCK_DGRAM, Netlink_route);
    struct SOCKADDR_NL la;
    memset (&la, 0, sizeof (LA));
    la.nl_family = Af_netlink;
    La.nl_pad = 0;
    La.nl_pid = Getpid ();

    la.nl_groups = 0;

Bind (FD, (struct sockaddr*) &la, sizeof (LA));
        Send request via NetLink, the message was in req struct {struct NLMSGHDR n;
    struct NDMSG R;
    
    } req;
    memset (&req, 0, sizeof (req));
    Fill in the NetLink message header Req.n.nlmsg_len = nlmsg_length (sizeof (struct ndmsg));
    Req.n.nlmsg_type = Rtm_getneigh; Req.n.nlmsg_flags = Nlm_f_request |
    Nlm_f_dump;
    Req.n.nlmsg_seq;

    req.n.nlmsg_pid;//= Getpid ();
    Fill in the NetLink message getneigh req.r.ndm_family = Af_inet6;
    Req.r.ndm_pad1;
    Req.r.ndm_pad2;
    Req.r.ndm_ifindex;
    req.r.ndm_state;//= nud_permanent;
    Req.r.ndm_flags;

    Req.r.ndm_type;

    int status; Status = SEnd (FD, &req, Req.n.nlmsg_len, 0);

    if (Status < 0) printf ("RFC 5549:problem sending to netlink\n");
    Receive data and put it in buf char buf[256];
    Char *p;
    struct NLMSGHDR *nlp;
    memset (buf, 0, sizeof (BUF));
    p = buf;
    int NLL = 0;
    struct NDMSG *NDP;
    struct rtattr *ndap;
 
    int NDL;
            while (1) {status = recv (FD, p, sizeof (BUF)-NLL, 0);
            if (Status < 0) printf ("RFC 5549:problem receiving from netlink\n");
            
            Close (FD);
            NLP = (struct NLMSGHDR *) p;
            if (Nlp->nlmsg_type = = Nlmsg_done) {break;     while (1) {NDP = (struct ndmsg *) nlmsg_data (NLP);      Get Rtnetlink message header Ndap = (struct rtattr *) Rtm_rta (NDP);                         Get start of attributes Ndl = Rtm_payload (NLP);    
                    while (1) {if (Ndap->rta_type = = NDA_DST)
                            {char Dstp[inet6_addrstrlen];
                            memset (DSTP, 0, sizeof (DSTP));
                            Long Long *intippoint = Rta_data (NDAP);                          
                            
                            Long Long intip = *intippoint;
                            printf ("Intip =%lli\n", intip);                     
                            Inet_ntop (Af_inet6, Rta_data (NDAP), DSTP, Inet6_addrstrlen);
                            
                            printf ("DSTP =%s, IPv6 =%s\n", DSTP, IPv6);
                                if (strcmp (DSTP, ipv6) = = 0) {Ndap = Rta_next (Ndap, NDL);
                                Long Long *macpoint = Rta_data (NDAP);
           LLADDR = *macpoint;                     printf ("lladdr =%llx\n", lladdr);
                            return 2;
                        } NDAP = Rta_next (Ndap, NDL);
                        if (RTA_OK (Ndap, NDL)!= 1) {break;
                    }} p + = status;
                    NLL + = status;
                    NLP = Nlmsg_next (NLP, NLL);
                    if (NLMSG_OK (NLP, NLL)!= 1) {break;
}} return 1;
    int ReadFile (int teller) {ip4to6 = fopen ("/usr/local/sbin/ip4to6", "R");
    Char line[100];
        while (1) {int teller2 = 0;
                while (Teller2 <= Teller) {fgets (line, IP4TO6);
        teller2++; } Char *worD
        Word = Strtok (line, "");
        memcpy (IPv6, Word, inet6_addrstrlen);
        Word = Strtok (NULL, "");    Word[strlen (Word)-1] = Word[strlen (word)];
        Remove the last \ IPv4 address memcpy (IPv4, Word, inet_addrstrlen);
    Break
    } fclose (IP4TO6);
return 1;
    int Countlines () {ip4to6 = fopen ("/usr/local/sbin/ip4to6", "R");
    char c;
    int teller = 0;
        while ((c = fgetc (IP4TO6))!= EOF) {if (c = = ' \ n ') {teller++;
    } fclose (IP4TO6);
return teller;
        void Main () {while (1) {int lines = Countlines ();
        int teller = 0;
            while (Teller < lines) {lladdr = 0;
            ReadFile (Teller);
           Getmacipv6 ();
            printf ("\ n");
            if (lladdr!= 0) {Injectmacipv4 ();
        } teller++;
    Sleep (5); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.