| 1 |
#include "protoApp.h" |
| 2 |
#include "protoSocket.h" |
| 3 |
#include "protoPktRIP.h" |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#include <ctype.h> // for "isspace()" |
| 9 |
|
| 10 |
/** |
| 11 |
* @class RiposerApp |
| 12 |
* |
| 13 |
* @brief simple app that can transmit unsolicited RIP response |
| 14 |
* messages to advertise a route |
| 15 |
* |
| 16 |
*/ |
| 17 |
class RiposerApp : public ProtoApp |
| 18 |
{ |
| 19 |
public: |
| 20 |
RiposerApp(); |
| 21 |
~RiposerApp(); |
| 22 |
|
| 23 |
bool OnStartup(int argc, const char*const* argv); |
| 24 |
bool ProcessCommands(int argc, const char*const* argv); |
| 25 |
void OnShutdown(); |
| 26 |
|
| 27 |
static const char* RIP_ADDR; |
| 28 |
static const UINT16 RIP_PORT; |
| 29 |
static const double RIP_TIMEOUT; |
| 30 |
|
| 31 |
private: |
| 32 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 33 |
static const char* const CMD_LIST[]; |
| 34 |
static CmdType GetCmdType(const char* string); |
| 35 |
bool OnCommand(const char* cmd, const char* val); |
| 36 |
void Usage(); |
| 37 |
|
| 38 |
bool OnRipTimeout(ProtoTimer& theTimer); |
| 39 |
void OnRipSocketEvent(ProtoSocket& theSocket, |
| 40 |
ProtoSocket::Event theEvent); |
| 41 |
|
| 42 |
ProtoTimer rip_timer; |
| 43 |
ProtoSocket rip_socket; |
| 44 |
ProtoAddress rip_addr; // dest group addr for RIP |
| 45 |
char rip_iface[256]; |
| 46 |
ProtoAddress route_addr; |
| 47 |
unsigned int route_mask_len; |
| 48 |
ProtoAddress route_next_hop; |
| 49 |
|
| 50 |
}; // end class RiposerApp |
| 51 |
|
| 52 |
const char* RiposerApp::RIP_ADDR = "224.0.0.9"; |
| 53 |
const UINT16 RiposerApp::RIP_PORT = 520; |
| 54 |
const double RiposerApp::RIP_TIMEOUT = 30.0; |
| 55 |
|
| 56 |
void RiposerApp::Usage() |
| 57 |
{ |
| 58 |
fprintf(stderr, "Usage: riposer [interface <ifaceName>][route <addr>/<maskLen>,<nextHop>]\n" |
| 59 |
" [debug <level>][help]\n"); |
| 60 |
} |
| 61 |
|
| 62 |
const char* const RiposerApp::CMD_LIST[] = |
| 63 |
{ |
| 64 |
"-help", // print help info an exit |
| 65 |
"+interface", // name of interface on which to listen / respond |
| 66 |
"+route", // <addr>/<maskLen>,<nextHop> |
| 67 |
"+debug", // <debugLevel> |
| 68 |
NULL |
| 69 |
}; |
| 70 |
|
| 71 |
/** |
| 72 |
* This macro creates our ProtoApp derived application instance |
| 73 |
*/ |
| 74 |
PROTO_INSTANTIATE_APP(RiposerApp) |
| 75 |
|
| 76 |
RiposerApp::RiposerApp() |
| 77 |
: rip_socket(ProtoSocket::UDP) |
| 78 |
{ |
| 79 |
rip_iface[0] = '\0'; |
| 80 |
rip_timer.SetListener(this, &RiposerApp::OnRipTimeout); |
| 81 |
rip_timer.SetInterval(RIP_TIMEOUT); |
| 82 |
rip_timer.SetRepeat(-1.0); // forever |
| 83 |
rip_socket.SetNotifier(&GetSocketNotifier()); |
| 84 |
rip_socket.SetListener(this, &RiposerApp::OnRipSocketEvent); |
| 85 |
rip_addr.ConvertFromString(RIP_ADDR); // default RIP group addr/port |
| 86 |
rip_addr.SetPort(RIP_PORT); |
| 87 |
} |
| 88 |
|
| 89 |
RiposerApp::~RiposerApp() |
| 90 |
{ |
| 91 |
} |
| 92 |
|
| 93 |
RiposerApp::CmdType RiposerApp::GetCmdType(const char* cmd) |
| 94 |
{ |
| 95 |
if (!cmd) return CMD_INVALID; |
| 96 |
unsigned int len = strlen(cmd); |
| 97 |
bool matched = false; |
| 98 |
CmdType type = CMD_INVALID; |
| 99 |
const char* const* nextCmd = CMD_LIST; |
| 100 |
while (*nextCmd) |
| 101 |
{ |
| 102 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 103 |
{ |
| 104 |
if (matched) |
| 105 |
{ |
| 106 |
// ambiguous command (command should match only once) |
| 107 |
return CMD_INVALID; |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
matched = true; |
| 112 |
if ('+' == *nextCmd[0]) |
| 113 |
type = CMD_ARG; |
| 114 |
else |
| 115 |
type = CMD_NOARG; |
| 116 |
} |
| 117 |
} |
| 118 |
nextCmd++; |
| 119 |
} |
| 120 |
return type; |
| 121 |
} // end RiposerApp::GetCmdType() |
| 122 |
|
| 123 |
|
| 124 |
bool RiposerApp::OnStartup(int argc, const char*const* argv) |
| 125 |
{ |
| 126 |
// Seed rand() with time of day usec |
| 127 |
// (comment out for repeatable results) |
| 128 |
struct timeval currentTime; |
| 129 |
ProtoSystemTime(currentTime); |
| 130 |
srand((unsigned int)currentTime.tv_usec); |
| 131 |
|
| 132 |
if (!ProcessCommands(argc, argv)) |
| 133 |
{ |
| 134 |
PLOG(PL_ERROR, "RiposerApp::OnStartup() error processing command line options\n"); |
| 135 |
Usage(); |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
// Open up the RIP UDP socket (port 520) and join the RIP_ADDR |
| 140 |
if (!rip_socket.Open(rip_addr.GetPort())) |
| 141 |
{ |
| 142 |
PLOG(PL_ERROR, "RiposerApp::OnStartup() error: unable to open rip socket\n"); |
| 143 |
return false; |
| 144 |
} |
| 145 |
// Check if specific iface was set |
| 146 |
const char* mcastIfaceName = NULL; |
| 147 |
if (0 != strlen(rip_iface)) |
| 148 |
mcastIfaceName = rip_iface; |
| 149 |
if (!rip_socket.JoinGroup(rip_addr, mcastIfaceName)) |
| 150 |
{ |
| 151 |
PLOG(PL_ERROR, "RiposerApp::OnStartup() error: unable to join rip multicast group\n"); |
| 152 |
return false; |
| 153 |
} |
| 154 |
if (NULL != mcastIfaceName) |
| 155 |
{ |
| 156 |
if (!rip_socket.SetMulticastInterface(mcastIfaceName)) |
| 157 |
{ |
| 158 |
PLOG(PL_ERROR, "RiposerApp::OnStartup() error: unable to join set rip multicast interface\n"); |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (route_addr.IsValid()) |
| 164 |
{ |
| 165 |
// We've been instructed advertise a route, so startup "rip_timer" |
| 166 |
OnRipTimeout(rip_timer); |
| 167 |
rip_timer.SetInterval(RIP_TIMEOUT); |
| 168 |
ActivateTimer(rip_timer); |
| 169 |
} |
| 170 |
|
| 171 |
PLOG(PL_INFO, "riposer: running on interface: %s\n", mcastIfaceName ? mcastIfaceName : "default"); |
| 172 |
|
| 173 |
return true; |
| 174 |
} // end RiposerApp::OnStartup() |
| 175 |
|
| 176 |
void RiposerApp::OnShutdown() |
| 177 |
{ |
| 178 |
if (rip_timer.IsActive()) |
| 179 |
rip_timer.Deactivate(); |
| 180 |
if (rip_socket.IsOpen()) |
| 181 |
rip_socket.Close(); |
| 182 |
|
| 183 |
PLOG(PL_INFO, "riposer: Done.\n"); |
| 184 |
|
| 185 |
} // end RiposerApp::OnShutdown() |
| 186 |
|
| 187 |
bool RiposerApp::ProcessCommands(int argc, const char*const* argv) |
| 188 |
{ |
| 189 |
// Dispatch command-line commands to our OnCommand() method |
| 190 |
int i = 1; |
| 191 |
while ( i < argc) |
| 192 |
{ |
| 193 |
// Is it a riposer command? |
| 194 |
switch (GetCmdType(argv[i])) |
| 195 |
{ |
| 196 |
case CMD_INVALID: |
| 197 |
{ |
| 198 |
PLOG(PL_ERROR, "RiposerApp::ProcessCommands() Invalid command:%s\n", |
| 199 |
argv[i]); |
| 200 |
Usage(); |
| 201 |
return false; |
| 202 |
} |
| 203 |
case CMD_NOARG: |
| 204 |
if (!OnCommand(argv[i], NULL)) |
| 205 |
{ |
| 206 |
PLOG(PL_ERROR, "RiposerApp::ProcessCommands() ProcessCommand(%s) error\n", |
| 207 |
argv[i]); |
| 208 |
Usage(); |
| 209 |
return false; |
| 210 |
} |
| 211 |
i++; |
| 212 |
break; |
| 213 |
case CMD_ARG: |
| 214 |
if (!OnCommand(argv[i], argv[i+1])) |
| 215 |
{ |
| 216 |
PLOG(PL_ERROR, "RiposerApp::ProcessCommands() ProcessCommand(%s, %s) error\n", |
| 217 |
argv[i], argv[i+1]); |
| 218 |
Usage(); |
| 219 |
return false; |
| 220 |
} |
| 221 |
i += 2; |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
return true; |
| 226 |
} // end RiposerApp::ProcessCommands() |
| 227 |
|
| 228 |
bool RiposerApp::OnCommand(const char* cmd, const char* val) |
| 229 |
{ |
| 230 |
// (TBD) move command processing into Mgen class ??? |
| 231 |
CmdType type = GetCmdType(cmd); |
| 232 |
ASSERT(CMD_INVALID != type); |
| 233 |
size_t len = strlen(cmd); |
| 234 |
if ((CMD_ARG == type) && !val) |
| 235 |
{ |
| 236 |
PLOG(PL_ERROR, "RiposerApp::ProcessCommand(%s) missing argument\n", cmd); |
| 237 |
Usage(); |
| 238 |
return false; |
| 239 |
} |
| 240 |
else if (!strncmp("help", cmd, len)) |
| 241 |
{ |
| 242 |
Usage(); |
| 243 |
exit(0); |
| 244 |
} |
| 245 |
else if (!strncmp("interface", cmd, len)) |
| 246 |
{ |
| 247 |
strncpy(rip_iface, val, 255); |
| 248 |
rip_iface[255] = '\0'; |
| 249 |
} |
| 250 |
else if (!strncmp("route", cmd, len)) |
| 251 |
{ |
| 252 |
// route <dstAddr>/<maskLen>,<nextHop> |
| 253 |
// 1) get <dstAddr> |
| 254 |
const char* txtPtr = val; |
| 255 |
const char* delimiter = strchr(txtPtr, '/'); |
| 256 |
if (NULL == delimiter) |
| 257 |
{ |
| 258 |
PLOG(PL_ERROR, "RiposerApp::OnCommand(route) error: missing <maskLen>\n"); |
| 259 |
return false; |
| 260 |
} |
| 261 |
size_t txtLen = delimiter - txtPtr; |
| 262 |
char tempTxt[256]; |
| 263 |
if (txtLen > 255) txtLen = 255; |
| 264 |
strncpy(tempTxt, txtPtr, txtLen); |
| 265 |
tempTxt[txtLen] = '\0'; // null terminate |
| 266 |
if (!route_addr.ConvertFromString(tempTxt)) |
| 267 |
{ |
| 268 |
PLOG(PL_ERROR, "RiposerApp::OnCommand(route) error: invalid <dstAddr>\n"); |
| 269 |
return false; |
| 270 |
} |
| 271 |
if (ProtoAddress::IPv4 != route_addr.GetType()) |
| 272 |
{ |
| 273 |
PLOG(PL_ERROR, "RiposerApp::OnCommand(route) error: non-IPv4 <dstAddr>\n"); |
| 274 |
return false; |
| 275 |
} |
| 276 |
// 2) get <maskLen> |
| 277 |
txtPtr = delimiter + 1; |
| 278 |
delimiter = strchr(txtPtr, ','); |
| 279 |
if (NULL != delimiter) |
| 280 |
txtLen = delimiter - txtPtr; |
| 281 |
else |
| 282 |
txtLen = strlen(txtPtr); |
| 283 |
if (txtLen > 255) txtLen = 255; |
| 284 |
strncpy(tempTxt, txtPtr, txtLen); |
| 285 |
tempTxt[txtLen] = '\0'; |
| 286 |
if (1 != sscanf(tempTxt, "%u", &route_mask_len)) |
| 287 |
{ |
| 288 |
PLOG(PL_ERROR, "RiposerApp::OnCommand(route) error: invalid <maskLen>\n"); |
| 289 |
return false; |
| 290 |
} |
| 291 |
// 3) get <nextHop> |
| 292 |
if (NULL != delimiter) |
| 293 |
{ |
| 294 |
txtPtr = delimiter + 1; |
| 295 |
if (!route_next_hop.ConvertFromString(txtPtr)) |
| 296 |
{ |
| 297 |
PLOG(PL_ERROR, "RiposerApp::OnCommand(route) error: invalid <nextHop>\n"); |
| 298 |
return false; |
| 299 |
} |
| 300 |
if (ProtoAddress::IPv4 != route_next_hop.GetType()) |
| 301 |
{ |
| 302 |
PLOG(PL_ERROR, "RiposerApp::OnCommand(route) error: non-IPv4 <nextHop>\n"); |
| 303 |
return false; |
| 304 |
} |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
PLOG(PL_ERROR, "RiposerApp::OnCommand(route) error: missing <nextHop>\n"); |
| 309 |
return false; |
| 310 |
} |
| 311 |
} |
| 312 |
else if (!strncmp("debug", cmd, len)) |
| 313 |
{ |
| 314 |
SetDebugLevel(atoi(val)); |
| 315 |
} |
| 316 |
else |
| 317 |
{ |
| 318 |
PLOG(PL_ERROR, "riposer error: invalid command\n"); |
| 319 |
Usage(); |
| 320 |
return false; |
| 321 |
} |
| 322 |
return true; |
| 323 |
} // end RiposerApp::OnCommand() |
| 324 |
|
| 325 |
bool RiposerApp::OnRipTimeout(ProtoTimer& theTimer) |
| 326 |
{ |
| 327 |
// Send unsolicited RIP response with our route advertisement |
| 328 |
UINT32 buffer[256]; |
| 329 |
ProtoPktRIP response(buffer, 256); // zero inits buffer |
| 330 |
response.SetCommand(ProtoPktRIP::RESPONSE); |
| 331 |
response.SetVersion(ProtoPktRIP::VERSION); |
| 332 |
response.AddRouteEntry(route_addr, route_mask_len, route_next_hop); |
| 333 |
unsigned int numBytes = response.GetLength(); |
| 334 |
if (!rip_socket.SendTo((char*)response.GetBuffer(), numBytes, rip_addr)) |
| 335 |
{ |
| 336 |
PLOG(PL_ERROR, "RiposerApp::OnRipTimer() error: sendto() error: %s\n", GetErrorString()); |
| 337 |
} |
| 338 |
return true; |
| 339 |
} // end RiposerApp::OnRipTimeout() |
| 340 |
|
| 341 |
|
| 342 |
void RiposerApp::OnRipSocketEvent(ProtoSocket& theSocket, |
| 343 |
ProtoSocket::Event theEvent) |
| 344 |
{ |
| 345 |
if (ProtoSocket::RECV == theEvent) |
| 346 |
{ |
| 347 |
UINT32 buffer[8192]; |
| 348 |
unsigned int len = 8192; |
| 349 |
ProtoAddress srcAddr; |
| 350 |
if (theSocket.RecvFrom((char*)buffer, len, srcAddr)) |
| 351 |
{ |
| 352 |
if (0 == len) return; // false alarm |
| 353 |
// TBD - print out more detail RIP packet information ... |
| 354 |
TRACE("riposer: received a %u byte RIP packet from %s/%hu\n", |
| 355 |
len, srcAddr.GetHostString(), srcAddr.GetPort()); |
| 356 |
|
| 357 |
ProtoPktRIP rip(buffer, len, len); |
| 358 |
|
| 359 |
|
| 360 |
TRACE("riposer: received RIP message length:%d version:%d ", len, rip.GetVersion()); |
| 361 |
switch (rip.GetCommand()) |
| 362 |
{ |
| 363 |
case ProtoPktRIP::REQUEST: |
| 364 |
TRACE("REQUEST\n"); |
| 365 |
break; |
| 366 |
case ProtoPktRIP::RESPONSE: |
| 367 |
TRACE("RESPONSE\n"); |
| 368 |
break; |
| 369 |
default: |
| 370 |
TRACE("INVALID command type:%s\n", rip.GetCommand()); |
| 371 |
break; |
| 372 |
} |
| 373 |
unsigned int entryCount = rip.GetNumEntry(); |
| 374 |
for (unsigned int i = 0; i < entryCount; i++) |
| 375 |
{ |
| 376 |
ProtoPktRIP::RouteEntry entry; |
| 377 |
rip.AccessRouteEntry(i, entry); |
| 378 |
switch (entry.GetAddressFamily()) |
| 379 |
{ |
| 380 |
case ProtoPktRIP::IPv4: |
| 381 |
{ |
| 382 |
ProtoAddress addr; |
| 383 |
entry.GetAddress(addr); |
| 384 |
char routeAddr[64]; |
| 385 |
addr.GetHostString(routeAddr, 64); |
| 386 |
entry.GetNextHop(addr); |
| 387 |
char nextHop[64]; |
| 388 |
addr.GetHostString(nextHop, 64); |
| 389 |
TRACE(" route entry %s/%u via %s\n", routeAddr, entry.GetMaskLength(), nextHop); |
| 390 |
break; |
| 391 |
} |
| 392 |
case ProtoPktRIP::AUTH: |
| 393 |
TRACE(" AUTH entry\n"); |
| 394 |
break; |
| 395 |
default: |
| 396 |
TRACE(" UNKNOWN address family: %d\n", entry.GetAddressFamily()); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} // end RiposerApp::OnRipSocketEvent() |
| 402 |
|
| 403 |
|