commit 6a18c72b0c28ad128d7e081af73f53607de98959 Author: rnhmjoj Date: Sat Jun 17 09:20:27 2023 +0200 initial commit diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..e69de29 diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..e69de29 diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..48ca93a --- /dev/null +++ b/ChangeLog @@ -0,0 +1,7 @@ +0.2: +* Added auto-daemonize. +* Fixed lame getopt bug. +* Switched to autoconf project. + +0.1: +Initial Version diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000..80e406f --- /dev/null +++ b/INSTALL @@ -0,0 +1,54 @@ +-- fakeroute v0.2 -- + +Install Instructions: + +tar zxvf fakeroute-0.2.tar.gz +cd fakeroute-0.2 +./configure +make +make install + +Usage: + +To use fakeroute, you must define a route file with your fake route. The file +should be a newline separated list of dotted quad IP addresses with your IP +address last. For example, 10.0.0.34 could have a route.conf that looks like: + +216.102.187.130 +165.87.161.74 +4.24.4.146 +4.0.1.122 +198.137.241.43 +198.116.142.34 +63.199.8.242 + +...be careful not to include any leading or trailing blank newlines. +Optionally, you could include any IP address last that you wish to be the +destination of the remote traceroute. The above route will stop at: +"adsl-63-199-8-242.carcionelaw.com" as if it were the traceroute destination: + +traceroute to localhost (127.0.0.1), 30 hops max, 40 byte packets + 1 core4-g3-0.snfc21.pbi.net (216.102.187.130) 0.324 ms 0.247 ms 0.162 ms + 2 sfra1sr3-so-1-1-1-0.ca.us.prserv.net (165.87.161.74) 0.164 ms 0.265 ms 0.159 ms + 3 p3-0.washdc3-br2.bbnplanet.net (4.24.4.146) 0.166 ms 0.219 ms 0.156 ms + 4 p5-0.vienna1-nbr2.bbnplanet.net (4.0.1.122) 0.162 ms 0.243 ms 0.170 ms + 5 wh243.eop.gov (198.137.241.43) 0.162 ms 0.255 ms 0.159 ms + 6 foundation.hq.nasa.gov (198.116.142.34) 0.168 ms 0.281 ms 0.164 ms + 7 adsl-63-199-8-242.carcionelaw.com (63.199.8.242) 0.182 ms 0.240 ms 0.156 ms + + +To run, become root, and simply execute "fakeroute -f ". + +To test your configuration, just traceroute to localhost. + +Bugs: + +Currently, fakeroute only works for UDP-based (ie: default unix) traceroute. +It does not work for unix traceroute with the -I option, or windows tracert. +Hopefully support for ICMP-based traceroute will come in a later version. + +If you have any questions, comments, or problems, you can send email to: + +moxie@thoughtcrime.org + + diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..100e664 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,2 @@ +SUBDIRS = src +EXTRA_DIST = example-route.conf diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..e69de29 diff --git a/README b/README new file mode 100644 index 0000000..80e406f --- /dev/null +++ b/README @@ -0,0 +1,54 @@ +-- fakeroute v0.2 -- + +Install Instructions: + +tar zxvf fakeroute-0.2.tar.gz +cd fakeroute-0.2 +./configure +make +make install + +Usage: + +To use fakeroute, you must define a route file with your fake route. The file +should be a newline separated list of dotted quad IP addresses with your IP +address last. For example, 10.0.0.34 could have a route.conf that looks like: + +216.102.187.130 +165.87.161.74 +4.24.4.146 +4.0.1.122 +198.137.241.43 +198.116.142.34 +63.199.8.242 + +...be careful not to include any leading or trailing blank newlines. +Optionally, you could include any IP address last that you wish to be the +destination of the remote traceroute. The above route will stop at: +"adsl-63-199-8-242.carcionelaw.com" as if it were the traceroute destination: + +traceroute to localhost (127.0.0.1), 30 hops max, 40 byte packets + 1 core4-g3-0.snfc21.pbi.net (216.102.187.130) 0.324 ms 0.247 ms 0.162 ms + 2 sfra1sr3-so-1-1-1-0.ca.us.prserv.net (165.87.161.74) 0.164 ms 0.265 ms 0.159 ms + 3 p3-0.washdc3-br2.bbnplanet.net (4.24.4.146) 0.166 ms 0.219 ms 0.156 ms + 4 p5-0.vienna1-nbr2.bbnplanet.net (4.0.1.122) 0.162 ms 0.243 ms 0.170 ms + 5 wh243.eop.gov (198.137.241.43) 0.162 ms 0.255 ms 0.159 ms + 6 foundation.hq.nasa.gov (198.116.142.34) 0.168 ms 0.281 ms 0.164 ms + 7 adsl-63-199-8-242.carcionelaw.com (63.199.8.242) 0.182 ms 0.240 ms 0.156 ms + + +To run, become root, and simply execute "fakeroute -f ". + +To test your configuration, just traceroute to localhost. + +Bugs: + +Currently, fakeroute only works for UDP-based (ie: default unix) traceroute. +It does not work for unix traceroute with the -I option, or windows tracert. +Hopefully support for ICMP-based traceroute will come in a later version. + +If you have any questions, comments, or problems, you can send email to: + +moxie@thoughtcrime.org + + diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..be26a50 --- /dev/null +++ b/configure.ac @@ -0,0 +1,13 @@ +ss this file with autoconf to produce a configure script. +AC_INIT(src/Fakeroute.cpp) + +dnl Setup for automake +AM_INIT_AUTOMAKE(fakeroute, 0.2) + +dnl Check for tools +AC_PROG_CC +AC_PROG_CXX +AC_PROG_INSTALL + +# Finally create all the generated files +AC_OUTPUT(Makefile src/Makefile) diff --git a/example-route.conf b/example-route.conf new file mode 100644 index 0000000..13e24b8 --- /dev/null +++ b/example-route.conf @@ -0,0 +1,7 @@ +216.102.187.130 +165.87.161.74 +4.24.4.146 +4.0.1.122 +198.137.241.43 +198.116.142.34 +63.199.8.242 diff --git a/src/ConnectionManager.cpp b/src/ConnectionManager.cpp new file mode 100644 index 0000000..a1a5bfe --- /dev/null +++ b/src/ConnectionManager.cpp @@ -0,0 +1,128 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "ConnectionManager.h" +#include "DestHostUnreachableFakeroutePacket.h" +#include "TimeToLiveExpiredFakeroutePacket.h" + +#include +#include +#include + +ConnectionManager::ConnectionManager(char* routeFile) : ENTRY_LIMIT(2) { + head = NULL; + tail = NULL; + + routeCount = 0; + entryCount = 0; + + loadRoutePath(routeFile); +} + +class FakeroutePacket* ConnectionManager::getPacketFor(char* sourceAddress, char* destinationAddress, + u_int16_t sourcePort, u_int16_t destinationPort) +{ + class SourceIpEntry *entry = findEntryFor(sourceAddress); + char *sourceIp = getIpForHop((*entry).getTtl()/3); + + class FakeroutePacket *spoofedPacket; + + if (isTransitHop((*entry).getTtl()/3)) { + spoofedPacket = new TimeToLiveExpiredFakeroutePacket(sourcePort, destinationPort, + sourceIp, destinationAddress); + } else { + spoofedPacket = new DestHostUnreachableFakeroutePacket(sourcePort, destinationPort, + sourceIp, destinationAddress); + } + + (*entry).incrementTtl(routeCount * 3); + + return spoofedPacket; +} + +class SourceIpEntry* ConnectionManager::findEntryFor(char* sourceAddress) { + class SourceIpEntry *current; + + for (current = head; current != NULL; current = (*current).getNext()) { + if (strcmp((*current).getSourceIp(), sourceAddress) == 0) { + return current; + } + } + + if (tail == NULL) { + head = tail = new SourceIpEntry(sourceAddress); + } else { + (*tail).setNext(new SourceIpEntry(sourceAddress)); + tail = (*tail).getNext(); + } + + if (++entryCount > ENTRY_LIMIT) { + releaseHeadNode(); + } + + return tail; +} + +void ConnectionManager::releaseHeadNode(void) { + class SourceIpEntry *tmp = (*head).getNext(); + delete head; + + head = tmp; + entryCount--; +} + +int ConnectionManager::isTransitHop(u_int16_t hop) { + return (hop < routeCount -1); +} + +char* ConnectionManager::getIpForHop(u_int16_t hop) { + if (hop < routeCount-1) { + return routePath[hop]; + } else { + return routePath[routeCount-1]; + } +} + +void ConnectionManager::loadRoutePath(const char* routeFile) { + FILE *configFile; + char line[1024]; + + routePath = (char**)malloc(30 * sizeof(char*)); + configFile = fopen(routeFile, "r"); + + if (configFile == NULL) { + fprintf(stderr, "ERROR: Specified route file does not exist, or no route file\nwas specified and default route.conf does not exist.\n"); + exit(1); + } + + while ((fgets((char*)line, 1024, configFile)) != NULL) { + routePath[routeCount] = strdup((char*)line); + routeCount++; + } +} diff --git a/src/ConnectionManager.h b/src/ConnectionManager.h new file mode 100644 index 0000000..cbff30e --- /dev/null +++ b/src/ConnectionManager.h @@ -0,0 +1,62 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef CONNECTION_MANAGER_H +#define CONNECTION_MANAGER_H + +#include "SourceIpEntry.h" +#include "FakeroutePacket.h" + +class ConnectionManager { + +private: + const u_int16_t ENTRY_LIMIT; + + class SourceIpEntry *head; + class SourceIpEntry *tail; + + char** routePath; + + u_int16_t routeCount; + u_int16_t entryCount; + + class SourceIpEntry* findEntryFor(char* sourceAddress); + void loadRoutePath(const char* routePath); + void releaseHeadNode(void); + char* getIpForHop(u_int16_t hop); + int isTransitHop(u_int16_t hop); + +public: + ConnectionManager(char* routeFile); + class FakeroutePacket* getPacketFor(char* sourceAddress, char* destinationAddress, + u_int16_t sourcePort, u_int16_t destinatonPort); + +}; + +#endif diff --git a/src/DestHostUnreachableFakeroutePacket.cpp b/src/DestHostUnreachableFakeroutePacket.cpp new file mode 100644 index 0000000..0274bed --- /dev/null +++ b/src/DestHostUnreachableFakeroutePacket.cpp @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "DestHostUnreachableFakeroutePacket.h" +#include + +DestHostUnreachableFakeroutePacket::DestHostUnreachableFakeroutePacket(u_int16_t localReceivedSourcePort, + u_int16_t localReceivedDestinationPort, + char* localSpoofedSourceAddress, + char* localSpoofedDestinationAddress) : + FakeroutePacket(localReceivedSourcePort, localReceivedDestinationPort, + localSpoofedSourceAddress, localSpoofedDestinationAddress) +{} + +u_int16_t DestHostUnreachableFakeroutePacket::getIcmpType(void) { + return ICMP_UNREACH; +} + +u_int16_t DestHostUnreachableFakeroutePacket::getIcmpCode(void) { + return ICMP_UNREACH_PORT; +} diff --git a/src/DestHostUnreachableFakeroutePacket.h b/src/DestHostUnreachableFakeroutePacket.h new file mode 100644 index 0000000..fb30f32 --- /dev/null +++ b/src/DestHostUnreachableFakeroutePacket.h @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef DEST_HOST_UNREACHABLE_FAKEROUTE_PACKET_H +#define DEST_HOST_UNREACHABLE_FAKEROUTE_PACKET_H + +#include +#include "FakeroutePacket.h" + +class DestHostUnreachableFakeroutePacket : public FakeroutePacket { + + protected: + u_int16_t getIcmpType(void); + u_int16_t getIcmpCode(void); + + public: + DestHostUnreachableFakeroutePacket(u_int16_t receivedSourcePort, u_int16_t receivedDestinationPort, + char* spoofedSourceAddress, char* spoofedDestinationAddress); +}; + +#endif diff --git a/src/Fakeroute.cpp b/src/Fakeroute.cpp new file mode 100644 index 0000000..1dfca2c --- /dev/null +++ b/src/Fakeroute.cpp @@ -0,0 +1,123 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +#include "TracerouteListener.h" +#include "TracerouteEvents.h" +#include "TracerouteReader.h" +#include "TraceroutePacket.h" +#include "ConnectionManager.h" +#include "FakeroutePacket.h" + +void start(char*); +void printUsage(char*); +static void daemonize(); + +int main(int argc, char** argv) { + char* fileName = "route.conf"; + int op; + + if (getuid()) { + fprintf(stderr, "Must be root to run fakeroute.\n"); + exit(1); + } + + while ((op = getopt(argc, argv, "hf:")) != -1) { + switch (op) { + case 'h': + printUsage(argv[0]); + exit(1); + case 'f': + fileName = optarg; + break; + default: + printUsage(argv[0]); + exit(1); + } + } + + daemonize(); + start(fileName); +} + +void printUsage(char* name) { + printf("Usage: %s [options]\n", name); + printf(" -h\t\tPrint Help (This)\n"); + printf(" -f \tLoad fake route information from\n\t\t rather than route.conf\n"); +} + +void start(char* routeFile) { + int* fds; + int fdCount, i; + + class TracerouteListener listener; + class TracerouteReader reader; + class ConnectionManager manager(routeFile); + + for (;;) { + class TracerouteEvents events; + + listener.listen(events); + + fds = events.getActiveFds(); + fdCount = events.getActiveFdCount(); + + for (i=0;i +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "in_cksum.h" + +FakeroutePacket::FakeroutePacket(u_int16_t localReceivedSourcePort, + u_int16_t localReceivedDestinationPort, + char* localSpoofedSourceAddress, + char* localSpoofedDestinationAddress) : + ICMP_HDR_SIZE(sizeof(struct icmp)), + IP_HDR_SIZE(sizeof(struct iphdr)), + UDP_HDR_SIZE(sizeof(struct udphdr)) +{ + receivedSourcePort = localReceivedSourcePort; + receivedDestinationPort = localReceivedDestinationPort; + spoofedSourceAddress = localSpoofedSourceAddress; + spoofedDestinationAddress = localSpoofedDestinationAddress; +} + +void FakeroutePacket::sendOn(int fd) { + + int sockfd; + int one = 1; + + struct sockaddr_in packetTo; + unsigned char packet[IP_HDR_SIZE + ICMP_HDR_SIZE + UDP_HDR_SIZE]; + + memset((void*)packet, 0, sizeof(packet)); + memset((void*)&packetTo, 0, sizeof(packetTo)); + + if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { + fprintf(stderr, "Error Creating Raw Socket."); + exit(1); + } + + if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, (char*)&one, sizeof(one)) < 0) { + fprintf(stderr, "Error Setting Raw Socket Socket Option."); + exit(1); + } + + packetTo.sin_family = AF_INET; + packetTo.sin_addr.s_addr = inet_addr(spoofedDestinationAddress); + + buildIpHeader(spoofedSourceAddress, spoofedDestinationAddress, (char*)packet); + buildIcmpHeader((char*)(packet + IP_HDR_SIZE), IPPROTO_UDP); + buildUdpHeader((char*)(packet + IP_HDR_SIZE + ICMP_HDR_SIZE), + receivedSourcePort, receivedDestinationPort); + + if (sendto(sockfd, packet, sizeof(packet), 0x0, (struct sockaddr*)&packetTo, sizeof(packetTo)) < 0) { + fprintf(stderr, "Packet Send Failed."); + exit(1); + } +} + +void FakeroutePacket::buildUdpHeader(char* packet, u_int16_t sourcePort, + u_int16_t destinationPort) +{ + struct udphdr* udphdr = (struct udphdr*)packet; + + udphdr->source = htons(sourcePort); + udphdr->dest = htons(destinationPort); +} + +void FakeroutePacket::buildIcmpHeader(char* packet, int protocol) { + struct iphdr *fakeIpHdr; + struct icmp *icmpHeader = (struct icmp*)packet; + + char fakePacket[IP_HDR_SIZE]; + fakeIpHdr = (struct iphdr*)fakePacket; + fakeIpHdr->ihl = 5; + fakeIpHdr->version = 4; + fakeIpHdr->protocol = protocol; + + icmpHeader->icmp_type = getIcmpType(); + icmpHeader->icmp_code = getIcmpCode(); + icmpHeader->icmp_ip = *(struct ip*)fakePacket; + icmpHeader->icmp_cksum = (int)in_cksum((unsigned short*)icmpHeader, ICMP_HDR_SIZE); +} + +void FakeroutePacket::buildIpHeader(char* source, char* destination, char* packet) { + struct iphdr *ipHeader; + + ipHeader = (struct iphdr*)packet; + ipHeader->ihl = 5; + ipHeader->version = 4; + ipHeader->tot_len = htons(IP_HDR_SIZE + ICMP_HDR_SIZE); + ipHeader->id = 31337; + ipHeader->ttl = 60; + ipHeader->protocol = IPPROTO_ICMP; + ipHeader->saddr = inet_addr(source); + ipHeader->daddr = inet_addr(destination); + ipHeader->check = (unsigned short)in_cksum((unsigned short*)ipHeader, IP_HDR_SIZE); +} diff --git a/src/FakeroutePacket.h b/src/FakeroutePacket.h new file mode 100644 index 0000000..cb27b47 --- /dev/null +++ b/src/FakeroutePacket.h @@ -0,0 +1,61 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef FAKEROUTE_PACKET_H +#define FAKEROUTE_PACKET_H + +#include + +class FakeroutePacket { + + private: + const int ICMP_HDR_SIZE; + const int IP_HDR_SIZE; + const int UDP_HDR_SIZE; + + u_int16_t receivedSourcePort; + u_int16_t receivedDestinationPort; + char* spoofedSourceAddress; + char* spoofedDestinationAddress; + + virtual void buildUdpHeader(char* packet, u_int16_t sourcePort, u_int16_t destinationPort); + virtual void buildIcmpHeader(char* packet, int protocol); + virtual void buildIpHeader(char* source, char* destination, char* packet); + + protected: + virtual u_int16_t getIcmpType(void) = 0; + virtual u_int16_t getIcmpCode(void) = 0; + + public: + FakeroutePacket(u_int16_t receivedSourcePort, u_int16_t receivedDestinationPort, + char* spoofedSourceAddress, char* spoofedDestinationAddress); + virtual void sendOn(int fd); +}; + +#endif diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..4fa0b8e --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,5 @@ +AM_CFLAGS = -ggdb -Wall +AM_CXXFLAGS = -ggdb -Wall + +bin_PROGRAMS = fakeroute +fakeroute_SOURCES = TracerouteListener.cpp TracerouteListener.h Fakeroute.cpp TracerouteEvents.cpp TracerouteEvents.h TraceroutePacket.cpp TraceroutePacket.h TracerouteReader.cpp TracerouteReader.h FakeroutePacket.cpp FakeroutePacket.h ConnectionManager.cpp ConnectionManager.h SourceIpEntry.cpp SourceIpEntry.h TimeToLiveExpiredFakeroutePacket.cpp TimeToLiveExpiredFakeroutePacket.h DestHostUnreachableFakeroutePacket.cpp DestHostUnreachableFakeroutePacket.h in_cksum.cpp in_cksum.h diff --git a/src/SourceIpEntry.cpp b/src/SourceIpEntry.cpp new file mode 100644 index 0000000..9a61270 --- /dev/null +++ b/src/SourceIpEntry.cpp @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "SourceIpEntry.h" +#include +#include + +#include + +SourceIpEntry::SourceIpEntry(char* localSourceIp) { + sourceIp = strdup(localSourceIp); + ttl = 0; + next = NULL; +} + +SourceIpEntry::~SourceIpEntry() { + free(sourceIp); +} + +char* SourceIpEntry::getSourceIp() { + return sourceIp; +} + +u_int16_t SourceIpEntry::getTtl() { + return ttl; +} + +void SourceIpEntry::incrementTtl(int modValue) { + ttl = (ttl + 1) % modValue; +} + +void SourceIpEntry::setNext(class SourceIpEntry *localNext) { + next = localNext; +} + +class SourceIpEntry* SourceIpEntry::getNext(void) { + return next; +} diff --git a/src/SourceIpEntry.h b/src/SourceIpEntry.h new file mode 100644 index 0000000..4d31a31 --- /dev/null +++ b/src/SourceIpEntry.h @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef SOURCE_IP_ENTRY_H +#define SOURCE_IP_ENTRY_H + +#include + +class SourceIpEntry { + +private: + char* sourceIp; + u_int16_t ttl; + class SourceIpEntry *next; + +public: + SourceIpEntry(char* localSourceIp); + ~SourceIpEntry(void); + char* getSourceIp(); + u_int16_t getTtl(); + void incrementTtl(int modValue); + void setNext(class SourceIpEntry *next); + class SourceIpEntry* getNext(void); +}; + +#endif diff --git a/src/TimeToLiveExpiredFakeroutePacket.cpp b/src/TimeToLiveExpiredFakeroutePacket.cpp new file mode 100644 index 0000000..6acab91 --- /dev/null +++ b/src/TimeToLiveExpiredFakeroutePacket.cpp @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "TimeToLiveExpiredFakeroutePacket.h" + +#include + +TimeToLiveExpiredFakeroutePacket::TimeToLiveExpiredFakeroutePacket(u_int16_t localReceivedSourcePort, + u_int16_t localReceivedDestinationPort, + char* localSpoofedSourceAddress, + char* localSpoofedDestinationAddress) : + FakeroutePacket(localReceivedSourcePort, localReceivedDestinationPort, + localSpoofedSourceAddress, localSpoofedDestinationAddress) +{} + +u_int16_t TimeToLiveExpiredFakeroutePacket::getIcmpType(void) { + return ICMP_TIME_EXCEEDED; +} + +u_int16_t TimeToLiveExpiredFakeroutePacket::getIcmpCode(void) { + return ICMP_EXC_TTL; +} diff --git a/src/TimeToLiveExpiredFakeroutePacket.h b/src/TimeToLiveExpiredFakeroutePacket.h new file mode 100644 index 0000000..25e1d92 --- /dev/null +++ b/src/TimeToLiveExpiredFakeroutePacket.h @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef TIME_TO_LIVE_EXPIRED_FAKEROUTE_PACKET_H +#define TIME_TO_LIVE_EXPIRED_FAKEROUTE_PACKET_H + +#include +#include "FakeroutePacket.h" + +class TimeToLiveExpiredFakeroutePacket : public FakeroutePacket { + + protected: + u_int16_t getIcmpType(void); + u_int16_t getIcmpCode(void); + + public: + TimeToLiveExpiredFakeroutePacket(u_int16_t receivedSourcePort, u_int16_t receivedDestinationPort, + char* spoofedSourceAddress, char* spoofedDestinationAddress); +}; + +#endif diff --git a/src/TracerouteEvents.cpp b/src/TracerouteEvents.cpp new file mode 100644 index 0000000..65e00ff --- /dev/null +++ b/src/TracerouteEvents.cpp @@ -0,0 +1,50 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "TracerouteEvents.h" + +int* TracerouteEvents::getActiveFds(void) { + return activeFds; +} + +int TracerouteEvents::getActiveFdCount(void) { + return activeFdCount; +} + +void TracerouteEvents::setActiveFds(int* localActiveFds) { + activeFds = localActiveFds; +} + +void TracerouteEvents::setActiveFdCount(int localActiveFdCount) { + activeFdCount = localActiveFdCount; +} + +TracerouteEvents::~TracerouteEvents(void) { + free(activeFds); +} diff --git a/src/TracerouteEvents.h b/src/TracerouteEvents.h new file mode 100644 index 0000000..1a33ce6 --- /dev/null +++ b/src/TracerouteEvents.h @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef TRACEROUTE_EVENTS_H +#define TRACEROUTE_EVENTS_H + +#include + +class TracerouteEvents { + private: + int* activeFds; + int activeFdCount; + + public: + ~TracerouteEvents(void); + int* getActiveFds(void); + int getActiveFdCount(void); + void setActiveFds(int* activeFds); + void setActiveFdCount(int activeFdCount); +}; + +#endif diff --git a/src/TracerouteListener.cpp b/src/TracerouteListener.cpp new file mode 100644 index 0000000..f473ccd --- /dev/null +++ b/src/TracerouteListener.cpp @@ -0,0 +1,86 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "TracerouteListener.h" +#include + +TracerouteListener::TracerouteListener(void) : FD_COUNT(31), LOW_ORDER_PORT(32768 + 666) { + int i = 0; + + for (i=0;i<31;i++) { + fds[i].fd = bindSocket(LOW_ORDER_PORT + i); + fds[i].events = POLLIN|POLLPRI; + } +} + +void TracerouteListener::listen(class TracerouteEvents &tracerouteEvents) { + int *activeFds; + int eventCount, i, index = 0; + + if ((eventCount = poll(fds, (unsigned long)FD_COUNT, -1)) < 0) { + fprintf(stderr, "Error During Poll."); + exit(1); + } + + activeFds = (int*)malloc(eventCount * sizeof(int)); + + for (i=0;i +#include +#include +#include + +#include +#include + +#include "TracerouteEvents.h" + +class TracerouteListener { + +private: + const int FD_COUNT; + const int LOW_ORDER_PORT; + struct pollfd fds[31]; + + int bindSocket(int port); + +public: + TracerouteListener(void); + void listen(class TracerouteEvents &tracerouteEvents); + + +}; +#endif diff --git a/src/TraceroutePacket.cpp b/src/TraceroutePacket.cpp new file mode 100644 index 0000000..49f6af4 --- /dev/null +++ b/src/TraceroutePacket.cpp @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "TraceroutePacket.h" + +TraceroutePacket::TraceroutePacket(char* localSourceAddress, char* localDestinationAddress, + u_int16_t localSourcePort, u_int16_t localDestinationPort) +{ + sourceAddress = localSourceAddress; + sourcePort = localSourcePort; + destinationAddress = localDestinationAddress; + destinationPort = localDestinationPort; +} + +char* TraceroutePacket::getSourceAddress(void) { + return sourceAddress; +} + +char* TraceroutePacket::getDestinationAddress(void) { + return destinationAddress; +} + +u_int16_t TraceroutePacket::getSourcePort(void) { + return sourcePort; +} + +u_int16_t TraceroutePacket::getDestinationPort(void) { + return destinationPort; +} + diff --git a/src/TraceroutePacket.h b/src/TraceroutePacket.h new file mode 100644 index 0000000..94d5e6e --- /dev/null +++ b/src/TraceroutePacket.h @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef TRACEROUTE_PACKET_H +#define TRACEROUTE_PACKET_H + +#include + +class TraceroutePacket { + + private: + char* sourceAddress; + char* destinationAddress; + u_int16_t sourcePort; + u_int16_t destinationPort; + + public: +/* void setSourceAddress(char* localSourceAddress); */ +/* void setSourcePort(u_int16_t localSourcePort); */ +/* void setDestinationPort(u_int16_t localDestinationPort); */ + TraceroutePacket(char* sourceAddress, char* destinationAddress, + u_int16_t sourcePort, u_int16_t destinationPort); + char* getSourceAddress(void); + char* getDestinationAddress(void); + u_int16_t getSourcePort(void); + u_int16_t getDestinationPort(void); + +}; + +#endif diff --git a/src/TracerouteReader.cpp b/src/TracerouteReader.cpp new file mode 100644 index 0000000..64a1070 --- /dev/null +++ b/src/TracerouteReader.cpp @@ -0,0 +1,65 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "TracerouteReader.h" + +#include +#include +#include +#include +#include +#include +#include + +class TraceroutePacket* TracerouteReader::readPacket(int fd) { + char packet[32*1024]; + + struct sockaddr_in remoteAddress; + struct sockaddr_in localAddress; + + unsigned int size = sizeof(remoteAddress); + + memset((void*)&remoteAddress, 0, size); + memset((void*)&localAddress, 0, size); + + if ((recvfrom(fd, packet, 32*1024, 0, (struct sockaddr*)&remoteAddress, &size)) < 0) { + fprintf(stderr, "Error receiving datagram."); + exit(1); + } + + if ((getsockname(fd, (struct sockaddr*)&localAddress, &size)) < 0) { + fprintf(stderr, "Error getting local name."); + exit(1); + } + + return new TraceroutePacket(inet_ntoa(remoteAddress.sin_addr), + inet_ntoa(localAddress.sin_addr), + ntohs(remoteAddress.sin_port), + ntohs(localAddress.sin_port)); +} diff --git a/src/TracerouteReader.h b/src/TracerouteReader.h new file mode 100644 index 0000000..5fd9fd3 --- /dev/null +++ b/src/TracerouteReader.h @@ -0,0 +1,42 @@ +/*- + * Copyright (c) 2002, Matt Rosenfeld + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of this program nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef TRACEROUTE_READER_H +#define TRACEROUTE_READER_H + +#include "TraceroutePacket.h" + +class TracerouteReader { + + public: + class TraceroutePacket* readPacket(int fd); + +}; + +#endif diff --git a/src/in_cksum.cpp b/src/in_cksum.cpp new file mode 100644 index 0000000..86cdfe3 --- /dev/null +++ b/src/in_cksum.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Mike Muuss. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +/* + * in_cksum -- + * Checksum routine for Internet Protocol family headers (C Version) + */ +unsigned short in_cksum(unsigned short *addr,int len) +{ + register int sum = 0; + u_short answer = 0; + register u_short *w = addr; + register int nleft = len; + + /* + * Our algorithm is simple, using a 32 bit accumulator (sum), we add + * sequential 16 bit words to it, and at the end, fold back all the + * carry bits from the top 16 bits into the lower 16 bits. + */ + while (nleft > 1) { + sum += *w++; + nleft -= 2; + } + + /* mop up an odd byte, if necessary */ + if (nleft == 1) { + *(u_char *)(&answer) = *(u_char *)w ; + sum += answer; + } + + /* add back carry outs from top 16 bits to low 16 bits */ + sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ + sum += (sum >> 16); /* add carry */ + answer = ~sum; /* truncate to 16 bits */ + return(answer); +} diff --git a/src/in_cksum.h b/src/in_cksum.h new file mode 100644 index 0000000..0faf3f5 --- /dev/null +++ b/src/in_cksum.h @@ -0,0 +1,6 @@ +#ifndef __IN_CKSUM_H__ +#define __IN_CKSUM_H__ + +unsigned short in_cksum(unsigned short *addr,int len); + +#endif