summaryrefslogtreecommitdiffstats
path: root/watchlink/netlink_linkstatus.cc
blob: cf6a4e5921084117c16606b65a8c1ebb138c7812 (plain)
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
/*
 * Module: netlink_linkstatus.cc
 *
 * **** License ****
 * Version: VPL 1.0
 *
 * The contents of this file are subject to the Vyatta Public License
 * Version 1.0 ("License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.vyatta.com/vpl
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * This code was originally developed by Vyatta, Inc.
 * Portions created by Vyatta are Copyright (C) 2008 Vyatta, Inc.
 * All Rights Reserved.
 *
 * Author: Michael Larson
 * Date: 2008
 * Description:
 *
 * **** End License ****
 *
 */
#include <stdio.h>
#include <sys/socket.h>
#include <iostream>
#include <string>
#include <syslog.h>
#include "rl_str_proc.hh"
#include "netlink_send.hh"
#include "netlink_event.hh"
#include "netlink_linkstatus.hh"


using namespace std;

/**
 *
 *
 **/
NetlinkLinkStatus::NetlinkLinkStatus(int send_sock, const string &link_dir, bool debug) : 
  _nl_send(debug),
  _send_sock(send_sock), 
  _link_dir(link_dir), 
  _debug(debug)
{
  if (_send_sock < 0) {
    syslog(LOG_ERR,"NetlinkListStatus::NetlinkLinkStatus(), send sock is bad value");
    cerr << "NetlinkListStatus::NetlinkLinkStatus(), send sock is bad value" << endl;
  }

  if (_link_dir.empty()) {
    syslog(LOG_ERR,"NetlinkListStatus::NetlinkLinkStatus(), no link status directory specified");
    cerr << "NetlinkListStatus::NetlinkLinkStatus(), no link status directory specified" << endl;
  }
}

/**
 *
 *
 **/
NetlinkLinkStatus::~NetlinkLinkStatus()
{
}

/**
 *
 *
 **/
void
NetlinkLinkStatus::process(const NetlinkEvent &event)
{
  bool state_event = false;

  IfaceStateIter iter = _iface_state_coll.find(event.get_index());
  if (iter == _iface_state_coll.end()) {
    if (event.get_type() == RTM_NEWLINK || event.get_type() == RTM_DELLINK) {
      //start maintaining state information here.
      _iface_state_coll.insert(pair<int,bool>(event.get_index(),event.get_running()));

      //let's clean up directory here!
      char buf[40];
      sprintf(buf,"%d",event.get_index());
      string file = _link_dir + "/" + buf;
      unlink(file.c_str());
    }
    return;
  }

  bool running_old = iter->second;
  bool running_new = event.get_running();

  //capture link status on link messages only
  if (event.get_type() == RTM_NEWLINK || 
      event.get_type() == RTM_DELLINK) {
    //    _iface_state_coll.insert(pair<int,bool>(event.get_index(),event.get_running()));
    _iface_state_coll[event.get_index()] = event.get_running();
    if (running_old != running_new) {
      state_event = true;
    }
  }

  //is this a transition from up->down, or down->up?
  if (state_event) {
    if (running_new) {
      process_going_up(event);
    }
    else {
      process_going_down(event);
    }
  }
  else {
    if (running_old) {
      process_up(event);
    }
    else {
      process_down(event);
    }
  }
}

/**
 *
 * file is in the format of IFINDEX,IP,MASK
 *
 **/
int
NetlinkLinkStatus::process_up(const NetlinkEvent &event)
{
  if (_debug) {
    cout << "NetlinkLinkStatus::process_up(): " << event.get_iface() << endl;
  }
  //can't think of anything that needs to go here yet.
  return 0;
}

/**
 *
 *
 **/
int
NetlinkLinkStatus::process_going_up(const NetlinkEvent &event)
{
  if (_debug) {
    cout << "NetlinkLinkStatus::process_going_up(): " << event.get_iface() << endl;
  }

  //check for link status file, otherwise return
  char buf[40];
  sprintf(buf,"%d",event.get_index());
  string file = _link_dir + "/" + buf;
  FILE *fp = fopen(file.c_str(), "r");
  if (fp == NULL) {
    syslog(LOG_INFO,"NetlinkLinkStatus::process_going_up(), failed to open state file");
    //    cerr << "NetlinkLinkStatus::process_going_up(), failed to open state file" << endl;
    return -1; //means we are still up, ignore...
  }

  char str[1025];
  while (fgets(str, 1024, fp)) {
    string line(str);

    StrProc tokens(line, ",");
    if (tokens.size() != 4) {
      syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure to parse link status file, exiting(): %s, size: %d", line.c_str(), tokens.size());
      //      cerr << "NetlinkLinkStatus::process_up(), failure to parse link status file, exiting(): " << line << ", size: " << tokens.size() << endl;
      fclose(fp);
      return -1;
    }

    int ifindex = strtoul(tokens.get(0).c_str(),NULL,10);
    uint32_t local_addr = strtoul(tokens.get(1).c_str(),NULL,10);
    uint32_t addr = strtoul(tokens.get(1).c_str(),NULL,10);
    int mask_len = strtoul(tokens.get(2).c_str(),NULL,10);
    
    //reinsert addresses to interface
    if (_nl_send.send_set(_send_sock, ifindex, local_addr, addr, mask_len, RTM_NEWADDR)) {
      syslog(LOG_INFO,"NetlinkLinkStatus::process_up(), failure in setting interface back to up");
      //      cerr << "NetlinkLinkStatus::process_up(), failure in setting interface back to up" << endl;
    }
  }

  fclose(fp);

  //remove file
  unlink(file.c_str());

  return 0;
}
  
/**
 * send an ip flush command and capture all the rtm_deladdr messages to the file...
 *
 **/
int
NetlinkLinkStatus::process_down(const NetlinkEvent &event)
{
  if (_debug) {
    cout << "NetlinkLinkStatus::process_down(): " << event.get_iface() << endl;
  }

  if (event.get_type() != RTM_NEWADDR) {
    return 0;
  }

  if (event.get_index() < 0 || event.get_local_addr().get() == 0 || event.get_mask_len() < 1) {
    return 0;
  }

  if (_debug) {
    cout << "netlinkLinkStatus::process_down(), processing valid request" << endl;
  }

  //append to file...
  char buf[40];
  sprintf(buf,"%d",event.get_index());
  string file = _link_dir + "/" + buf;
  FILE *fp = fopen(file.c_str(), "a");
  if (fp == NULL) {
    syslog(LOG_INFO,"NetlinkLinkStatus::process_down(), failed to open state file");
    //    cerr << "NetlinkLinkStatus::process_down(), failed to open state file" << endl;
    return -1; 
  }

  //create file on system
  //CRAJ--NEED TO HAVE THIS BE FROM A COLLECTION??? DEPENDS ON FORMAT OF NETLINK MSG
  sprintf(buf,"%d",event.get_index());
  string line = string(buf) + ",";
  sprintf(buf,"%d",event.get_local_addr().get());
  line += string(buf) + ",";
  sprintf(buf,"%d",event.get_addr().get());
  line += string(buf) + ",";
  sprintf(buf,"%d",event.get_mask_len());
  line += string(buf) + "\n";
  
  fputs(line.c_str(),fp);


 //pull interface addresses
  if (_nl_send.send_set(_send_sock, event.get_index(), event.get_local_addr().get(), event.get_addr().get(), event.get_mask_len(), RTM_DELADDR)) {
    fclose(fp);
    return -1;
  }

  fclose(fp);

  return 0;
}

int
NetlinkLinkStatus::process_going_down(const NetlinkEvent &event)
{
  if (_debug) {
    cout << "NetlinkLinkStatus::process_going_down(): " << event.get_iface() << "(" << event.get_index() << ")" << endl;
  }

  //pull interface addresses
  if (_nl_send.send_get(_send_sock, RTM_GETADDR, event.get_index())) {
    return -1;
  }
  return 0;
}