File: arp_cache.c

package info (click to toggle)
4g8 1.0-3.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 632 kB
  • ctags: 145
  • sloc: sh: 3,736; ansic: 1,397; makefile: 75
file content (174 lines) | stat: -rw-r--r-- 4,861 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * author: Darren Bounds <dbounds@intrusense.com>
 * copyright: Copyright (C) 2002 by Darren Bounds
 * license: This software is under GPL version 2 of license
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * 4g8 official page at http://.net
 */

#include <pthread.h>
#include "arp_cache.h"

pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER;

libnet_t *
arp_cache_mgr(u_int8_t action, libnet_t *pkt_d)
{
    u_int32_t s_paddr, r_paddr;
    u_int8_t gw_neaddr[6], host_neaddr[6];
    u_int8_t r_neaddr[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; 

    struct libnet_ether_addr *hw_addr; 

#ifdef DEBUG
    fprintf(stdout, "DEBUG: arp_cache_mgr()\n");
#endif

    hw_addr = malloc(sizeof(struct libnet_ether_addr *));
    memset(hw_addr, 0, sizeof(struct libnet_ether_addr *));

    if((r_paddr = libnet_name2addr4(pkt_d, "0.0.0.0", 0)) == -1)
        fatal_error("Invalid receiver protocol address");

 
    // Host

    if(action == 0)
    {
        if(format_ethernet_addr(gw_mac, hw_addr->ether_addr_octet) == 0)
            fatal_error("Invalid sender ethernet address: %s", gw_mac);

        if((s_paddr = libnet_name2addr4(pkt_d, gw_ip, 0)) == -1)
            fatal_error("Invalid receiver protocol address: %s", gw_ip);
    }
    else
    if(action == 1)
    {
        hw_addr = libnet_get_hwaddr(pkt_d); 
 
        if((s_paddr = libnet_name2addr4(pkt_d, gw_ip, 0)) == -1)
            fatal_error("Invalid receiver protocol address: %s", gw_ip);
    }

    if(libnet_build_arp(
        ARPHRD_ETHER,                    
        ETHERTYPE_IP,                          
        6,                                      
        4,                                      
        2,                     
        hw_addr->ether_addr_octet,                           
        (u_int8_t *)&s_paddr,                          
        r_neaddr, 
        (u_int8_t *)&r_paddr,                          
        NULL,                                   
        0,                                     
        pkt_d,                                      
        0) == -1)                              
    {
        fatal_error("Unable to build ARP header: %s", libnet_geterror(pkt_d)); 
    } 

    if(format_ethernet_addr(host_mac, host_neaddr) == 0)
        fatal_error("Invalid sender ethernet address");

    if(libnet_build_ethernet(
        host_neaddr,
        hw_addr->ether_addr_octet,
        ETHERTYPE_ARP,
        NULL,
        0,
        pkt_d,
        0) == -1)
    {
        fatal_error("Unable to build ethernet header");
    }

    libnet_write(pkt_d);
    libnet_clear_packet(pkt_d);

    // GATEWAY

    if(action == 0)
    {
        if(format_ethernet_addr(host_mac, hw_addr->ether_addr_octet) == 0)
            fatal_error("Invalid sender ethernet address: %s", host_mac);

        if((s_paddr = libnet_name2addr4(pkt_d, host_ip, 0)) == -1)
            fatal_error("Invalid receiver protocol address: %s", host_ip);
    }
    else
    if(action == 1)
    {
        hw_addr = libnet_get_hwaddr(pkt_d);

        if((s_paddr = libnet_name2addr4(pkt_d, host_ip, 0)) == -1)
            fatal_error("Invalid receiver protocol address: %s", host_ip);
    }

    if(libnet_build_arp(
        ARPHRD_ETHER,
        ETHERTYPE_IP,
        6,
        4,
        2,
        hw_addr->ether_addr_octet,
        (u_int8_t *)&s_paddr,
        r_neaddr,
        (u_int8_t *)&r_paddr,
        NULL,
        0,
        pkt_d,
        0) == -1)
    {
        fatal_error("Unable to build ARP header: %s", libnet_geterror(pkt_d));
    }

    if(format_ethernet_addr(gw_mac, gw_neaddr) == 0)
        fatal_error("Invalid sender ethernet address");

    if(libnet_build_ethernet(
        gw_neaddr,
        hw_addr->ether_addr_octet,
        ETHERTYPE_ARP,
        NULL,
        0,
        pkt_d,
        0) == -1)
    {
        fatal_error("Unable to build ethernet header");
    }

    libnet_write(pkt_d);
    libnet_clear_packet(pkt_d);

    return pkt_d;
}

void
arp_cache_alrm(int sig)
{
#ifdef DEBUG
    fprintf(stdout, "DEBUG: arp_cache_alrm(%d)\n", sig);
#endif

//    pthread_mutex_lock(&conn_mutex);
    arp_cache_mgr(1, pkt_d);
//    pthread_mutex_unlock(&conn_mutex);

    return;
}