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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
*
* This is free software. You can redistribute it and/or modify under
* the terms of the GNU General Public License version 2.
*
* Copyright (C) 1998 by kra
*
*/
#include "hunt.h"
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
void mac_discover_range(unsigned int start_ip, unsigned int end_ip, int count)
{
unsigned int addr, j;
struct timespec ts;
for (j = 1; j <= count; j++) {
for (addr = start_ip;
ntohl(addr) <= ntohl(end_ip);
addr = htonl(ntohl(addr) + 1)) {
mac_discover(addr, 1);
}
ts.tv_sec = 0;
ts.tv_nsec = 200000000;
nanosleep(&ts, NULL);
}
}
void mac_discover(unsigned int ip, int count)
{
struct arp_spec as;
struct timespec ts;
int i;
as.src_mac = my_eth_mac;
as.dst_mac = mac_broadcast;
as.oper = htons(ARPOP_REQUEST);
as.sender_mac = my_eth_mac;
as.sender_addr = my_eth_ip;
as.target_mac = mac_zero;
as.target_addr = ip;
ts.tv_sec = 0;
ts.tv_nsec = 100000000;
for (i = 0; i < count; i++) {
send_arp_packet(&as);
if (i < count - 1)
nanosleep(&ts, NULL);
}
/*
* reply will be received by hunt
*/
}
/*
* arp discovery modul
*/
struct mac_disc_info {
unsigned int start_addr;
unsigned int end_addr;
struct mac_disc_info *next;
};
static struct list l_mdi = LIST_INIT(struct mac_disc_info, next);
static int wait_sec = 300;
static volatile int stop_break = 0;
static int thr_running = 0;
static pthread_t mac_thr;
static volatile int stop = 0;
static pthread_mutex_t mutex_stop = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond_stop = PTHREAD_COND_INITIALIZER;
static void *mac_disc_thr(void *arg)
{
struct list_iterator li;
struct mac_disc_info *mdi;
unsigned int addr;
struct timeval tv;
struct timespec ts;
int retval;
pthread_sigmask(SIG_BLOCK, &intr_mask, NULL);
setpriority(PRIO_PROCESS, getpid(), 10);
stop_break = 0;
while (!stop) {
list_iter_set(&li, &l_mdi);
while ((mdi = list_iter_get(&li)) && !stop) {
for (addr = mdi->start_addr;
ntohl(addr) <= ntohl(mdi->end_addr) && !stop;
addr = htonl(ntohl(addr) + 1)) {
mac_discover(addr, 1);
}
}
list_iter_end(&li);
pthread_mutex_lock(&mutex_stop);
retval = 0;
gettimeofday(&tv, NULL);
ts.tv_sec = tv.tv_sec + wait_sec;
ts.tv_nsec = tv.tv_usec * 1000;
while (!stop && retval != ETIMEDOUT && !stop_break)
retval = pthread_cond_timedwait(&cond_stop, &mutex_stop, &ts);
pthread_mutex_unlock(&mutex_stop);
stop_break = 0;
}
return NULL;
}
static int start_mac_discovery(void)
{
if (thr_running) {
printf("mac discoverer already running\n");
return -1;
}
pthread_mutex_init(&mutex_stop, NULL);
pthread_cond_init(&cond_stop, NULL);
stop = 0;
pthread_create(&mac_thr, NULL, mac_disc_thr, NULL);
thr_running = 1;
return 0;
}
static int stop_mac_discovery(void)
{
if (!thr_running) {
printf("mac discoverer isn't running\n");
return -1;
}
stop = 1;
pthread_mutex_lock(&mutex_stop);
pthread_cond_signal(&cond_stop);
pthread_mutex_unlock(&mutex_stop);
pthread_join(mac_thr, NULL);
thr_running = 0;
return 0;
}
void print_mac_daemon()
{
if (thr_running) {
if (pthread_kill(mac_thr, 0) != 0) {
pthread_join(mac_thr, NULL);
mac_thr = (pthread_t) 0;
thr_running = 0;
set_tty_color(COLOR_BRIGHTRED);
printf("MAC daemon failed - bug\n");
set_tty_color(COLOR_LIGHTGRAY);
} else
printf("M");
}
}
/*
* print_mac_table is in hunt.c which will receive arp reply
*/
static void mdi_list(void)
{
struct list_iterator li;
struct mac_disc_info *mdi;
int count = 0;
list_iter_set(&li, &l_mdi);
while ((mdi = list_iter_get(&li))) {
printf("%2d) %-24s - %-24s\n", count++,
host_lookup(mdi->start_addr, hl_mode),
host_lookup(mdi->end_addr, hl_mode));
if (count % lines_o == 0)
lines_o_press_key();
}
list_iter_end(&li);
}
static void mdi_add(void)
{
struct mac_disc_info *mdi;
unsigned int start_ip, end_ip;
if ((start_ip = menu_choose_hostname("start ip addr", NULL)) == -1)
return;
if ((end_ip = menu_choose_hostname("end ip addr", NULL)) == -1)
return;
mdi = malloc(sizeof(struct mac_disc_info));
assert(mdi);
mdi->start_addr = start_ip;
mdi->end_addr = end_ip;
list_enqueue(&l_mdi, mdi);
}
static void mdi_mod(void)
{
struct mac_disc_info *mdi;
unsigned int start_ip, end_ip;
int nr;
mdi_list();
if ((nr = menu_choose_unr("choose item", 0, list_count(&l_mdi) - 1, list_count(&l_mdi) - 1)) == -1)
return;
if (!(mdi = list_at(&l_mdi, nr)))
return;
if ((start_ip = menu_choose_hostname("start ip addr", host_lookup(mdi->start_addr, hl_mode))) == -1)
return;
if ((end_ip = menu_choose_hostname("end ip addr", host_lookup(mdi->end_addr, hl_mode))) == -1)
return;
mdi->start_addr = start_ip;
mdi->end_addr = end_ip;
}
static void mdi_del(void)
{
int i;
struct mac_disc_info *mdi;
mdi_list();
i = menu_choose_unr("item nr. to delete", 0,
list_count(&l_mdi) - 1, -1);
if (i >= 0) {
mdi = list_remove_at(&l_mdi, i);
free(mdi);
}
}
static void mdi_time_wait(void)
{
int min, sec;
min = wait_sec / 60;
sec = wait_sec % 60;
if ((min = menu_choose_unr("choose time interval min", 0, 1000, min)) == -1)
return;
if ((sec = menu_choose_unr("choose time interval sec", 0, 10000, sec)) == -1)
return;
wait_sec = min * 60 + sec;
stop_break = 1;
pthread_mutex_lock(&mutex_stop);
pthread_cond_signal(&cond_stop);
pthread_mutex_unlock(&mutex_stop);
}
void mac_disc_menu(void)
{
char *m_menu = "s/k) start/stop daemon\n"
"l) list discoverer setup h) list HW mac addresses\n"
"t) time to sleep\n"
"a/m/d) add/mod/del entry\n"
"x) return\n";
char *m_keys = "sklhtadmx";
int run_it;
run_it = 1;
while (run_it) {
switch (menu("mac disc. daemon", m_menu, "macd", m_keys, 0)) {
case 's':
start_mac_discovery();
break;
case 'k':
stop_mac_discovery();
break;
case 'l':
mdi_list();
break;
case 'h':
print_mac_table();
break;
case 't':
mdi_time_wait();
break;
case 'a':
mdi_add();
break;
case 'm':
mdi_mod();
break;
case 'd':
mdi_del();
break;
case 'x':
run_it = 0;
break;
}
}
}
|