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
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
*
* Released under GNU GPL v2 licence
*
* $Id: lowlevel-linux-link-state.c,v 1.4 2009-03-24 23:17:18 thomson Exp $
*/
#define __USE_UNIX98
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <bits/sigthread.h>
#include "Portable.h"
#include "interface.h"
#define IF_RECONNECTED_DETECTED -1
int report_link_state(const char* iface);
volatile struct link_state_notify_t * changed_links = 0;
volatile int * notifier = 0;
int isDone = 0;
pthread_t parent_id;
pthread_t ntid;
pthread_mutex_t lock;
struct state {
int id;
int stat;
struct state* next;
};
struct state* states = NULL;
int getStateById(int id){
struct state* item = states;
struct state* new_state;
for(;item!=NULL;item = item->next){
if(item->id==id)
return item->stat;
}
new_state = malloc(sizeof(struct state));
new_state->id = id;
new_state->stat = 1;
new_state->next = states;
states = new_state;
return new_state->stat;
}
void setStateById(int id,int stat){
struct state* item = states;
struct state* new_state;
for(;item!=NULL;item = item->next){
if(item->id==id){
item->stat = stat;
return;
}
}
new_state = malloc(sizeof(struct state));
new_state->id = id;
new_state->stat = stat;
new_state->next = states;
states = new_state;
}
/**
* this function should be called when interface link state change is detected
*
* @param ifindex index of the changed interface
*/
void link_state_changed(int ifindex)
{
if (changed_links)
{
if (changed_links->cnt<16)
changed_links->ifindex[changed_links->cnt++] = ifindex;
pthread_mutex_lock(&lock);
*notifier = 1; /* notify that change has occured */
pthread_mutex_unlock(&lock);
pthread_kill(parent_id,SIGUSR1);
} else
{
printf("Error: link state change detected, but notification system not initiated properly\n");
/* link_state_change_init() should have been called in advance */
}
}
void * checkLinkState(void * ptr){
struct iface * head = (struct iface *)ptr;
while(!isDone){
struct iface * item = head;
for(;item!=NULL;item = item->next){
int old_r = item->link_state;
int new_r = report_link_state(item->name);
if (new_r - old_r == IF_RECONNECTED_DETECTED){
link_state_changed(item->id);
}
item->link_state = new_r;
}
/* printf("."); */
sleep(1);
}
/* if_list_release(head); */
/* printf("Finishing link-state change\n"); */
/* pthread_exit(NULL); */
return NULL;
}
/**
* removes intefaces that are not to be monitored from the list
*
* @param head
* @param monitored_links
*
* @return
*/
struct iface * filter_if_list(struct iface *head, volatile struct link_state_notify_t * monitored_links)
{
struct iface * out = NULL;
struct iface * tmp = NULL;
int i;
while (head!=NULL) {
int copy = 0;
for (i=0; i<monitored_links->cnt; i++) {
if (head->id == monitored_links->ifindex[i]) {
copy = 1;
}
}
if (copy) {
tmp = head;
head = head->next;
tmp->next = out;
out = tmp;
} else {
tmp = head;
head = head->next;
tmp->next = 0;
if_list_release(tmp);
}
}
return out;
}
/**
* begin monitoring of those interfaces
*
* @param monitored_links head of the monitored links list
* @param notify pointer to variable that is going to be modifed if change is detected
*/
void link_state_change_init(volatile struct link_state_notify_t * monitored_links, volatile int * notify)
{
struct iface * ifacesLst;
int err;
/* the same structure will be used for notifying about link-state change */
notifier = notify;
changed_links = monitored_links;
parent_id = pthread_self();
/** @todo Add actual monitoring here (spawn extra thread or install some handlers, etc.) */
ifacesLst = if_list_get();
ifacesLst = filter_if_list(ifacesLst, monitored_links);
/* uncomment this section to get information regarding interfaces to be monitored */
/*
int i=0;
printf("About to start monitoring %d interfaces:", monitored_links->cnt);
for (i=0; i<monitored_links->cnt; i++)
printf(" %d", monitored_links->ifindex[i]);
printf("\n");
*/
err = pthread_create(&ntid, NULL, checkLinkState, (void*)ifacesLst);
if (err !=0 ){
printf("process create fail in dibbler/Port-linux/lowlevel-linux-link-state.c.");
return ;
}
}
/**
* cleanup code for link state monitoring
*
*/
void link_state_change_cleanup()
{
isDone = 1;
}
int report_link_state(const char* iface) {
int fd, r = 0;
interface_status_t s;
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
printf("Socket create failed\n");
return -1;
}
/* here just to make it more convenient for testing:if we define DEBUG,so a plugin-out and plugin-in event
* will be detected if we do "ifconfig interface-name down" and ifconfig interface-name up".Otherwise,
* if we do not define DEBUG, a plugin-in and plugin-out event can only be detected when the local link
* plugin in and out. Skip this comment if you did not catch it.
*/
/* #define DEBUG*/
#if 0
if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR) {
printf(" SIOCETHTOOL failed (%s)\n", strerror(errno));
close(fd);
return -1;
}
#else
if ((s = interface_detect_beat_iff(fd,iface)) == IFSTATUS_ERR) {
printf(" IFF_RUNNING failed (%s)\n", strerror(errno));
close(fd);
return -1;
}
#endif
switch(s) {
case IFSTATUS_UP:
/* link beat detected */
r = 1;
break;
case IFSTATUS_DOWN:
/* unplugged */
r = 2;
break;
default:
/* not supported (Retry as root?)*/
r = -1;
break;
}
close(fd);
return r;
}
|