diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..d83d6e4 --- /dev/null +++ b/.clang-format @@ -0,0 +1,117 @@ +--- +Language: Cpp +# BasedOnStyle: Google +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^' + Priority: 2 + - Regex: '^<.*\.h>' + Priority: 1 + - Regex: '^<.*' + Priority: 2 + - Regex: '.*' + Priority: 3 +IncludeIsMainRegex: '([-_](test|unittest))?$' +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 2 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: false +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +RawStringFormats: + - Delimiter: pb + Language: TextProto + BasedOnStyle: google +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 8 +UseTab: Never +... + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5b3721e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.16) +project(nettools) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(nettools main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..92e09b8 --- /dev/null +++ b/main.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main() { + int send_socket; + send_socket = socket(PF_INET, SOCK_DGRAM, 0); + if (send_socket < 0) { + std::cerr << "Error opening socket for sending"; + return 1; + } + + int receive_socket; + receive_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + if (receive_socket < 0) { + std::cerr << "Error opening socket for receiving"; + return 1; + } + + const char ip_cstring[]{"172.217.19.78"}; + in_addr ip; + if (!inet_aton(ip_cstring, &ip)) { + std::cerr << "Failed to parse IP address " << ip_cstring << '\n'; + return 1; + } + + hostent *remote_host; + remote_host = gethostbyaddr((const void *)&ip, sizeof(ip), AF_INET); + if (!remote_host) { + std::cerr << "Failed to get host by address\n"; + return 1; + } + + const char port_cstring[]{"80"}; + int port = atoi(port_cstring); + + sockaddr_in remote_address; + memset((char *)&remote_address, 0, sizeof(remote_address)); + remote_address.sin_family = AF_INET; + memcpy((void *)&remote_address.sin_addr, remote_host->h_addr, + remote_host->h_length); + remote_address.sin_port = htons(port); + + for (int ttl{0}; ttl < 20; ++ttl) { + setsockopt(send_socket, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); + + char message[]{"hello"}; + if (sendto(send_socket, message, strlen(message), 0, + (sockaddr *)&remote_address, sizeof(remote_address)) < 0) { + std::cerr << "Failed to send\n"; + return 1; + } + std::cout << "sent\n"; + + char buffer[2048]; + int received_length{}; + sockaddr_in received_addr; + socklen_t received_addr_size{sizeof(received_addr)}; + std::cout << "Listening...\n"; + received_length = recvfrom(receive_socket, buffer, 2047, 0, + (sockaddr *)&received_addr, &received_addr_size); + std::cout << "Received something of length " << received_length << ":\n"; + + iphdr *ip_hdr = reinterpret_cast(buffer); + in_addr source, dest; + source.s_addr = ip_hdr->saddr; + dest.s_addr = ip_hdr->daddr; + std::cout << "IP header length is " << ip_hdr->ihl * 4 << " bytes\n" + << "ttl=" << static_cast(ip_hdr->ttl) << '\n' + << "source=" << inet_ntoa(source) << '\n' + << "destination=" << inet_ntoa(dest) << '\n' + << '\n'; + + icmphdr *icmp_hdr = + reinterpret_cast((char *)ip_hdr + (4 * ip_hdr->ihl)); + std::cout << "ICMP msgtype=" << static_cast(icmp_hdr->type) << '\n' + << "code=" << static_cast(icmp_hdr->code) << '\n' + << "checksum=" << static_cast(icmp_hdr->checksum) << '\n' + << '\n'; + } + + return 0; +}