Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 16, 2016
0 parents
commit 6656ea4
Showing
34 changed files
with
4,900 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2011 Peder Jungck, CloudShield Technologies Inc An SAIC Company, Ralph Duncan, and Dwight Mulcahy | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#Apress Source Code | ||
|
||
This repository accompanies [*packetC Programming*](http://www.apress.com/9781430241584) by Peder Jungck, CloudShield Technologies Inc An SAIC Company, Ralph Duncan, and Dwight Mulcahy (Apress, 2011). | ||
|
||
![Cover image](9781430241584.jpg) | ||
|
||
Download the files as a zip using the green button, or clone the repository to your machine using Git. | ||
|
||
##Releases | ||
|
||
Release v1.0 corresponds to the code in the published book, without corrections or updates. | ||
|
||
##Contributions | ||
|
||
See the file Contributing.md for more information on how you can contribute to this repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Contributing to Apress Source Code | ||
|
||
Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. | ||
|
||
## How to Contribute | ||
|
||
1. Make sure you have a GitHub account. | ||
2. Fork the repository for the relevant book. | ||
3. Create a new branch on which to make your change, e.g. | ||
`git checkout -b my_code_contribution` | ||
4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. | ||
5. Submit a pull request. | ||
|
||
Thank you for your contribution! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// ************************************************************************** | ||
// ArpPing.ph - Include file for ARP/Ping handling. | ||
// ------------ | ||
// Author(s) : dWiGhT | ||
// Date Created: 03/01/2011 | ||
// Version : 1.00 | ||
// ************************************************************************** | ||
// Description: This provides an application with the ability to respond | ||
// to ARPs and ICMP (Ping) requests. | ||
// *************************************************************************** | ||
#ifndef ARPPING_PH_ | ||
#define ARPPING_PH_ | ||
|
||
// *************************************************************************** | ||
// Error check to make sure that the correct #defines have been used | ||
// and can be used correct | ||
// *************************************************************************** | ||
#ifndef ARPPING_MACID | ||
#error ARPPING_MACID needs to be #defined to identify the CS box. It should look like this "#define ARPPING_MACID 0x00, 0x0C, 0x41, 0x97, 0x24, 0x15" | ||
#else | ||
const byte MacId_[6] = { ARPPING_MACID }; | ||
#endif | ||
|
||
#ifndef ARPPING_IPADDR | ||
#error ARPPING_IPADDR needs to be #defined to define the IP address this response to. It should look like this "#define ARPPING_IPADDR 10.101.1.241" | ||
#else | ||
IpAddress CMD_PROCESS_IPADDR = ARPPING_IPADDR; // IP that this responses to | ||
#endif | ||
|
||
|
||
// *************************************************************************** | ||
// Packet request type that is returned from HandleArpPing() | ||
// *************************************************************************** | ||
enum int ReqType { | ||
REQTYPE_NONE = 0, // Pkt was not an ARP or a PING | ||
REQTYPE_PING_PROCESSED = 1, // Pkt was a PING request that we processed | ||
REQTYPE_ARP_PROCESSED = 2 // Pkt was a ARP request that we processed | ||
}; | ||
|
||
|
||
// *************************************************************************** | ||
// | ||
// Handle responding to a Ping request. | ||
// | ||
// Here we filling the source to be this (mac/ip) and turn the | ||
// destination around to respond back to the requestor. | ||
// | ||
// *************************************************************************** | ||
int pingId_ = 0; | ||
bool HandlePingRequest() { | ||
bool handlePingRequest = false; | ||
|
||
// Only process valid packets | ||
if ( pib.flags.l4CheckSumValid == true ) | ||
if ( ipv4.destinationAddress == CMD_PROCESS_IPADDR ) { | ||
// Set the time to live (ttl) to 30secs | ||
ipv4.ttl = 30; | ||
|
||
// Increment the ping id field, increments for each response | ||
ipv4.identification = (short)(++pingId_); | ||
|
||
// Set the type/code to a response echo (ping) | ||
icmpEcho.type = ICMP_TYPE_ECHO_RESPONSE; // RESPONSE | ||
icmpEcho.code = 0x00; // ECHO | ||
|
||
// Return this modified packet to the sender | ||
// Swap source/desc mac addrs | ||
ethernet.destinationAddress = ethernet.sourceAddress; | ||
ethernet.sourceAddress = (MacAddress)MacId_; | ||
|
||
// Swap source/desc IP addrs | ||
ipv4.destinationAddress = ipv4.sourceAddress; | ||
ipv4.sourceAddress = CMD_PROCESS_IPADDR; | ||
|
||
// We have to recalculate L3/L4 checksums before sending it on its way | ||
pib.flags.l3CheckSumRecalc = true; | ||
pib.flags.l4CheckSumRecalc = true; | ||
|
||
handlePingRequest = true; | ||
} | ||
return handlePingRequest; | ||
} | ||
|
||
// *************************************************************************** | ||
// | ||
// Handle responding to a ARP request. | ||
// | ||
// Here we filling the source to be this (macid/ip) and turn the | ||
// destination around to respond back to the requestor with our macid. | ||
// | ||
// *************************************************************************** | ||
bool HandleArpRequest() { | ||
bool handleArpRequest = false; | ||
|
||
if ( arp.destinationProtocolAddress == (IpQuads)CMD_PROCESS_IPADDR ) { | ||
// Set up the source mac/ip to be here | ||
ethernet.destinationAddress = ethernet.sourceAddress; | ||
ethernet.sourceAddress = (MacAddress)MacId_; | ||
|
||
// Flip the dest to be the source | ||
arp.destinationHardwareAddress = arp.sourceHardwareAddress; | ||
arp.destinationProtocolAddress = arp.sourceProtocolAddress; | ||
|
||
// the source is from here | ||
arp.sourceHardwareAddress = (MacAddress)MacId_; | ||
arp.sourceProtocolAddress = (IpQuads)CMD_PROCESS_IPADDR; | ||
|
||
// Change this packet into a ARP reply | ||
arp.opcode = ARP_OPCODE_REPLY; | ||
|
||
handleArpRequest = true; | ||
} | ||
|
||
return handleArpRequest; | ||
} | ||
|
||
|
||
// *************************************************************************** | ||
// | ||
// Handles classifying a packet to see if it is an ARP or PING request. | ||
// | ||
// We call the correct handler and return the type of packet we processed. | ||
// | ||
// *************************************************************************** | ||
ReqType HandleArpPing() { | ||
ReqType handleArpPing = REQTYPE_NONE; | ||
|
||
switch (ethernet.type) | ||
{ | ||
case ETHERNET_TYPE_IP: | ||
// ************************ | ||
// Internet Control Message Protocol | ||
// ************************ | ||
if ( ipv4.protocol == IP_PROTOCOL_ICMP ) | ||
if ( icmpEcho.type == ICMP_TYPE_ECHO_REQUEST ) { | ||
// *PING* request | ||
if ( HandlePingRequest() == true ) { | ||
handleArpPing = REQTYPE_PING_PROCESSED; | ||
} | ||
} | ||
break; | ||
|
||
// *************************** | ||
// Address Resolution Protocol | ||
// *************************** | ||
case ETHERNET_TYPE_ARP: | ||
// See if this is a ARP request or reply packet | ||
if ( arp.opcode == ARP_OPCODE_REQUEST ) { | ||
// ARP request | ||
if ( HandleArpRequest()== true ) { | ||
handleArpPing = REQTYPE_ARP_PROCESSED; | ||
} | ||
} | ||
break; | ||
|
||
default: | ||
// do nothing with any other packet type | ||
} | ||
|
||
return handleArpPing; | ||
} | ||
|
||
#endif /*ARPPING_PH_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// ************************************************************************** | ||
// Sample-ArpPing.pc - Main file for the Example Arp/Ping program. | ||
// ------------ | ||
// Author(s) : dWiGhT | ||
// Date Created: 03/01/2011 | ||
// Version : 1.00 | ||
// ************************************************************************** | ||
// Description: This project provides a simple example of how to use ARP and | ||
// ping along with showing how an UDP packet can be used to | ||
// communicate commands to the main processor. | ||
// | ||
// *************************************************************************** | ||
|
||
packet module Sample_ARP_PING; | ||
|
||
// *************************************************************************** | ||
// Include supporting header files | ||
// *************************************************************************** | ||
#include <cloudshield.ph> | ||
#include "ClassifyPacket.ph" | ||
#include "UdpProcessor.ph" | ||
|
||
// *************************************************************************** | ||
// Define constants used to define this box | ||
// *************************************************************************** | ||
|
||
// IP that this application responses to | ||
#define ARPPING_IPADDR 10.101.1.241 | ||
|
||
// 00:0B:A9:Fn:nn:nn � Is allocated to packetC developer private internal | ||
// cabinet addressing. This is documented as a CloudShield private | ||
// address similar to RFC1918 IP addresses. | ||
// | ||
// Define this uniquely in the form 00:0B:A9:Fn:nn:nn | ||
// | ||
// MAC id of this box that you want this to be identified as | ||
#define ARPPING_MACID 0x00, 0x0B, 0xA9, 0xF1, 0x23, 0x45 | ||
|
||
// :NOTE: ArpPing.ph will error out if either of the above are not defined!!! | ||
#include "ArpPing.ph" | ||
|
||
// UDP port that this will respond to. | ||
int CMD_UDP_PORT = 31337; | ||
|
||
|
||
// *************************************************************************** | ||
// Define the global variables that track the type of packets processed | ||
// *************************************************************************** | ||
|
||
// Global variables that track number of packets processed in types | ||
int totalPktsProcessed_ = 0; | ||
%pragma control totalPktsProcessed_ (export); | ||
int cmdPktsProcessed_ = 0; | ||
%pragma control cmdPktsProcessed_ (export); | ||
int arpPktsProcessed_ = 0; | ||
%pragma control arpPktsProcessed_ (export); | ||
int pingPktsProcessed_ = 0; | ||
%pragma control pingPktsProcessed_ (export); | ||
|
||
// *************************************************************************** | ||
// Helper function to determine if this packet is directed at "me". Note | ||
// this "borrows" the use of the ARPPING defines. Reuse at its best. | ||
// *************************************************************************** | ||
const byte MyMacId_[6] = { ARPPING_MACID }; | ||
bool ThisIsForMe() { | ||
bool thisIsForMe = false; | ||
|
||
if (ethernet.destinationAddress == (MacAddress)MyMacId_ ) | ||
if ( ipv4.destinationAddress == (IpAddress)ARPPING_IPADDR ) { | ||
thisIsForMe = true; | ||
} | ||
return thisIsForMe; | ||
} | ||
|
||
|
||
// *************************************************************************** | ||
// Main code block | ||
// *************************************************************************** | ||
void main($PACKET pkt, $PIB pib, $SYS sys) | ||
{ | ||
// We will only respond to traffic we are interested in, send the rest on. | ||
pib.action = FORWARD_PACKET; | ||
|
||
++totalPktsProcessed_; | ||
|
||
// Determine the packet type | ||
PktType pktType; | ||
pktType = classifyPacket(); | ||
|
||
switch ( pktType ) { | ||
|
||
// ******************** | ||
// PING request | ||
// ******************** | ||
case (PKTTYPE_PING_REQUEST): | ||
// Process the PING request with a reply | ||
if ( HandlePingRequest() == true ) { | ||
// Increment the number of these packets processed | ||
++pingPktsProcessed_; | ||
} | ||
break; | ||
|
||
// ******************** | ||
// ARP request | ||
// ******************** | ||
case (PKTTYPE_ARP_REQUEST): | ||
if ( HandleArpRequest() == true ) { | ||
// Increment the number of these packets processed | ||
++arpPktsProcessed_; | ||
} | ||
break; | ||
|
||
// ******************** | ||
// UDP packet | ||
// ******************** | ||
case (PKTTYPE_UDP): | ||
// We only accept UDP packets on a special port addressed to our | ||
// ip adress, macId and UDP port. | ||
if ( ThisIsForMe() == true ) | ||
if ( udp.destinationPort == (short)CMD_UDP_PORT ) | ||
if ( udp.sourcePort == (short)CMD_UDP_PORT ) { | ||
// Increment the number of these packets processed | ||
++cmdPktsProcessed_; | ||
|
||
// The read_counter command is the only one we send back a reply | ||
if ( HandleUdp() != CMDTYPE_READ_COUNTER ) { | ||
// We want to drop these control packets | ||
pib.action = DROP_PACKET; | ||
} | ||
} | ||
break; | ||
|
||
default: | ||
// We don't do anything with this type of packet. | ||
// Drop through. | ||
} | ||
|
||
} |
Oops, something went wrong.