Solution
// File: hw3.cpp
//
// Purpose: Simulate processing of messages on Homer
// Simpson's network.
// If a ping message is received, and the source
// is Homer Simpson, and the # occurrences exceeds
// a certain # of times, flag it as a smurf
// attack (which "crashes" the network).
// If a malformed ping message is received, detect
// it as such, and generate a network "crash."
// Other messages are "acknowledged" with a simple
// response.
// Processing is terminated if the system "crashes"
// or after a certain # of messages have been
// processed.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const short BANDWIDTH = 50;
const short MAX_MESSAGES_TO_PROCESS = 100;
const short PING_MSG_START = 0; // range of codes for
const short PING_MSG_END = 84; // ping msg's
const short NON_PING_MSG_START = 85; // range of codes for
const short NON_PING_MSG_END = 98; // non-ping msg's
const short VALID_IP_ADDR_START = 1; // range of valid
const short VALID_IP_ADDR_END = 4; // IP addresses
short smurf_count = 0; // # ping msg's with Homer as the source
short ping_count = 0; // # ping msg's
short message_count = 0; // total msg's received
bool system_crash = false; // flags signaling to
bool system_shutDown = false; // shut down network
short source_address; // parts of a msg
short destination_address;
short message_code;
short homer_ip_address; // Homer's IP address
bool is_valid_address; // used when validating user input for Homer's IP address
// Seed the random number generator
srand(time(NULL));
// Greeting
cout << "Starting Homer's network...\n";
// Get Homer's IP address
do
{
cout << "Homer, what is your network address? ";
cin >> homer_ip_address;
is_valid_address = (homer_ip_address >= VALID_IP_ADDR_START)
&&
(homer_ip_address <= VALID_IP_ADDR_END);
if (! is_valid_address)
cout << "A valid network address is between "
<< VALID_IP_ADDR_START << " and "
<< VALID_IP_ADDR_END << " (inclusive)!\n\n";
} while (! is_valid_address);
cout << endl;
// Begin simulation, terminating when system crash detected
// or MAX_MESSAGES_TO_PROCESS many msg's have been processed
while ((!system_crash) && (!system_shutDown))
{
// "Get" a message
message_code = rand() % MAX_MESSAGES_TO_PROCESS;
source_address =
(rand() % (VALID_IP_ADDR_END - VALID_IP_ADDR_START + 1)) + VALID_IP_ADDR_START;
destination_address =
(rand() % (VALID_IP_ADDR_END - VALID_IP_ADDR_START + 1)) + VALID_IP_ADDR_START;
cout << "\nReceived message code " << message_code
<< " sent from source " << source_address
<< " to destination " << destination_address << endl;
message_count++;
if ((message_code >= PING_MSG_START) && (message_code <= PING_MSG_END))
{
ping_count++;
// Check for smurf attack
if (source_address == homer_ip_address)
{
smurf_count++;
cout << "Possible attempted smurf attack (detected "
<< smurf_count
<< " time" << (smurf_count > 1? "s": "")
<< " so far)\n";
if (smurf_count > (static_cast<float>(BANDWIDTH )/3))
{
cout << "Network has crashed due to excessive "
<< "smurf attacks!\n";
system_crash = true;
}
}
else // Respond to ping message from someone other than Homer
{
cout << "Yo, " << source_address
<< ", I'm here\n";
}
// Check for ping flood
if (ping_count >= BANDWIDTH)
{
cout << "D'oh, done in by a ping flood ("
<< ping_count << " pings received)!\n";
system_crash = true;
}
}
else if ((message_code >= NON_PING_MSG_START) && (message_code <= NON_PING_MSG_END))
{
cout << "Replying to " << destination_address
<< ": Got any donuts or beer?\n";
}
else // Assume it was a malformed ping message
{
cout << "Received message " << message_code << " - "
<< "D'oh, done in by the ping of death!\n";
system_crash = true;
}
// Check to see if max # messages have been processed
if ((!system_crash) && (message_count == MAX_MESSAGES_TO_PROCESS))
{
cout << "Well, processed my limit of messages for the day"
<< " – shutting down!\n";
system_shutDown = true;
}
}
// Sign-off
cout << "\nHomer's network stopped after processing "
<< message_count << " messages\n";
if (system_crash)
cout << "because of a system crash.\n";
else cout << "under normal conditions.\n";
return 0;
}