ttl because easy

This commit is contained in:
2025-02-24 11:19:24 -05:00
parent 82d8a71323
commit 21456260ad
4 changed files with 72 additions and 70 deletions

88
.idea/workspace.xml generated
View File

@ -7,8 +7,8 @@
<option name="/Default/RiderDebugger/RiderRestoreDecompile/RestoreDecompileSetting/@EntryValue" value="false" type="bool" /> <option name="/Default/RiderDebugger/RiderRestoreDecompile/RestoreDecompileSetting/@EntryValue" value="false" type="bool" />
<option name="/Default/Housekeeping/GlobalSettingsUpgraded/IsUpgraded/@EntryValue" value="true" type="bool" /> <option name="/Default/Housekeeping/GlobalSettingsUpgraded/IsUpgraded/@EntryValue" value="true" type="bool" />
<option name="/Default/Housekeeping/FeatureSuggestion/FeatureSuggestionManager/DisabledSuggesters/=SwitchToGoToActionSuggester/@EntryIndexedValue" value="true" type="bool" /> <option name="/Default/Housekeeping/FeatureSuggestion/FeatureSuggestionManager/DisabledSuggesters/=SwitchToGoToActionSuggester/@EntryIndexedValue" value="true" type="bool" />
<option name="/Default/Housekeeping/OptionsDialog/SelectedPageId/@EntryValue" value="CppFormatterOtherPage" type="string" />
<option name="/Default/Housekeeping/FeatureSuggestion/FeatureSuggestionManager/DisabledSuggesters/=SwitchToGoToActionSuggester/@EntryIndexRemoved" /> <option name="/Default/Housekeeping/FeatureSuggestion/FeatureSuggestionManager/DisabledSuggesters/=SwitchToGoToActionSuggester/@EntryIndexRemoved" />
<option name="/Default/Housekeeping/OptionsDialog/SelectedPageId/@EntryValue" value="CppFormatterOtherPage" type="string" />
<option name="/Default/Environment/Hierarchy/GeneratedFilesCacheKey/Timestamp/@EntryValue" value="9" type="long" /> <option name="/Default/Environment/Hierarchy/GeneratedFilesCacheKey/Timestamp/@EntryValue" value="9" type="long" />
</component> </component>
<component name="CMakePresetLoader">{ <component name="CMakePresetLoader">{
@ -30,12 +30,12 @@
<configuration PROFILE_NAME="Debug" ENABLED="true" GENERATION_DIR="build" CONFIG_NAME="Debug"> <configuration PROFILE_NAME="Debug" ENABLED="true" GENERATION_DIR="build" CONFIG_NAME="Debug">
<ADDITIONAL_GENERATION_ENVIRONMENT> <ADDITIONAL_GENERATION_ENVIRONMENT>
<envs> <envs>
<env name="MAKE_NAME" value="ft_ping" /> <env name="MAKE_C_SRCS" value="srcs/main.c srcs/util.c" />
<env name="MAKE_C_SRCS" value="srcs/main.c" />
<env name="MAKE_CFLAGS" value="-Wall -Werror -Wextra" /> <env name="MAKE_CFLAGS" value="-Wall -Werror -Wextra" />
<env name="MAKE_INCLUDE_DIRS" value="includes" /> <env name="MAKE_INCLUDE_DIRS" value="includes" />
<env name="MAKE_INCLUDE_SRCS" value="includes/main.h" /> <env name="MAKE_INCLUDE_SRCS" value="includes/main.h" />
<env name="MAKE_INSTALL_DIR" value="$PROJECT_DIR$/" /> <env name="MAKE_INSTALL_DIR" value="$PROJECT_DIR$/" />
<env name="MAKE_NAME" value="ft_ping" />
<env name="MAKE_SUBDIRECTORIES" value="" /> <env name="MAKE_SUBDIRECTORIES" value="" />
</envs> </envs>
</ADDITIONAL_GENERATION_ENVIRONMENT> </ADDITIONAL_GENERATION_ENVIRONMENT>
@ -44,20 +44,8 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="d3f69c6b-7fde-4401-b145-baf049527d67" name="Changes" comment=""> <list default="true" id="d3f69c6b-7fde-4401-b145-baf049527d67" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.clang-format" afterDir="false" /> <change afterPath="$PROJECT_DIR$/srcs/util.c" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.clang-tidy" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/codeStyles/codeStyleConfig.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/editor.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/ft_ping.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.name" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/cmake.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Makefile" beforeDir="false" afterPath="$PROJECT_DIR$/Makefile" afterDir="false" />
<change beforePath="$PROJECT_DIR$/includes/main.h" beforeDir="false" afterPath="$PROJECT_DIR$/includes/main.h" afterDir="false" /> <change beforePath="$PROJECT_DIR$/includes/main.h" beforeDir="false" afterPath="$PROJECT_DIR$/includes/main.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/srcs/main.c" beforeDir="false" afterPath="$PROJECT_DIR$/srcs/main.c" afterDir="false" /> <change beforePath="$PROJECT_DIR$/srcs/main.c" beforeDir="false" afterPath="$PROJECT_DIR$/srcs/main.c" afterDir="false" />
</list> </list>
@ -85,34 +73,34 @@
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
<option name="showVisibilityIcons" value="true" /> <option name="showVisibilityIcons" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"ASKED_ADD_EXTERNAL_FILES": "true", &quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
"ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true", &quot;ASKED_SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
"CMake Application.ft_ping.executor": "Debug", &quot;CMake Application.ft_ping.executor&quot;: &quot;Debug&quot;,
"Makefile Target.Makefile.executor": "Run", &quot;Makefile Target.Makefile.executor&quot;: &quot;Run&quot;,
"RunOnceActivity.RadMigrateCodeStyle": "true", &quot;RunOnceActivity.RadMigrateCodeStyle&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.cidr.known.project.marker": "true", &quot;RunOnceActivity.cidr.known.project.marker&quot;: &quot;true&quot;,
"RunOnceActivity.git.unshallow": "true", &quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;,
"RunOnceActivity.readMode.enableVisualFormatting": "true", &quot;RunOnceActivity.readMode.enableVisualFormatting&quot;: &quot;true&quot;,
"RunOnceActivity.west.config.association.type.startup.service": "true", &quot;RunOnceActivity.west.config.association.type.startup.service&quot;: &quot;true&quot;,
"SHARE_PROJECT_CONFIGURATION_FILES": "true", &quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
"cf.advertisement.text.overridden": "true", &quot;cf.advertisement.text.overridden&quot;: &quot;true&quot;,
"cf.first.check.clang-format": "false", &quot;cf.first.check.clang-format&quot;: &quot;false&quot;,
"cidr.known.project.marker": "true", &quot;cidr.known.project.marker&quot;: &quot;true&quot;,
"git-widget-placeholder": "master", &quot;git-widget-placeholder&quot;: &quot;master&quot;,
"last_opened_file_path": "/home/user/ft_ping", &quot;last_opened_file_path&quot;: &quot;/home/user/ft_ping&quot;,
"node.js.detected.package.eslint": "true", &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
"node.js.detected.package.tslint": "true", &quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
"node.js.selected.package.eslint": "(autodetect)", &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
"node.js.selected.package.tslint": "(autodetect)", &quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
"nodejs_package_manager_path": "npm", &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
"settings.editor.selected.configurable": "CMakeSettings", &quot;settings.editor.selected.configurable&quot;: &quot;CMakeSettings&quot;,
"structure.view.defaults.are.configured": "true", &quot;structure.view.defaults.are.configured&quot;: &quot;true&quot;,
"vue.rearranger.settings.migration": "true" &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
} }
}]]></component> }</component>
<component name="RdControllerToolWindowsLayoutState" isNewUi="true"> <component name="RdControllerToolWindowsLayoutState" isNewUi="true">
<layout> <layout>
<window_info id="Bookmarks" side_tool="true" /> <window_info id="Bookmarks" side_tool="true" />
@ -120,7 +108,7 @@
<window_info id="Commit_Guest" show_stripe_button="false" /> <window_info id="Commit_Guest" show_stripe_button="false" />
<window_info id="Pull Requests" /> <window_info id="Pull Requests" />
<window_info id="Learn" /> <window_info id="Learn" />
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.11744791" /> <window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.120052084" />
<window_info id="Commit" order="1" weight="0.25" /> <window_info id="Commit" order="1" weight="0.25" />
<window_info id="Structure" order="2" side_tool="true" weight="0.25" /> <window_info id="Structure" order="2" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="Database Changes" /> <window_info anchor="bottom" id="Database Changes" />
@ -133,12 +121,12 @@
<window_info anchor="bottom" id="Version Control" order="0" /> <window_info anchor="bottom" id="Version Control" order="0" />
<window_info anchor="bottom" id="Problems" order="1" /> <window_info anchor="bottom" id="Problems" order="1" />
<window_info anchor="bottom" id="Problems View" order="2" weight="0.26097327" /> <window_info anchor="bottom" id="Problems View" order="2" weight="0.26097327" />
<window_info active="true" anchor="bottom" id="Terminal" order="3" visible="true" weight="0.26097327" /> <window_info active="true" anchor="bottom" id="Terminal" order="3" visible="true" weight="0.23711833" />
<window_info anchor="bottom" id="Services" order="4" /> <window_info anchor="bottom" id="Services" order="4" />
<window_info anchor="bottom" id="CMake" order="5" weight="0.26097327" /> <window_info anchor="bottom" id="CMake" order="5" weight="0.24856871" />
<window_info anchor="bottom" id="Run" order="6" weight="0.30009544" /> <window_info anchor="bottom" id="Run" order="6" weight="0.30009544" />
<window_info anchor="bottom" id="Debug" order="7" weight="0.30009544" /> <window_info anchor="bottom" id="Debug" order="7" weight="0.30009544" />
<window_info anchor="bottom" id="Messages" order="8" weight="0.26097327" /> <window_info anchor="bottom" id="Messages" order="8" weight="0.23711833" />
<window_info anchor="bottom" id="Find" order="9" weight="0.32967556" /> <window_info anchor="bottom" id="Find" order="9" weight="0.32967556" />
<window_info anchor="right" id="make" side_tool="true" /> <window_info anchor="right" id="make" side_tool="true" />
<window_info anchor="right" id="Coverage" side_tool="true" /> <window_info anchor="right" id="Coverage" side_tool="true" />
@ -158,9 +146,9 @@
</key> </key>
</component> </component>
<component name="RunManager" selected="CMake Application.ft_ping"> <component name="RunManager" selected="CMake Application.ft_ping">
<configuration default="true" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true"> <configuration default="true" type="CLionExternalRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true">
<method v="2"> <method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" /> <option name="CLION.EXTERNAL.BUILD" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="ft_ping" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="ft_ping" TARGET_NAME="ft_ping" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="ft_ping" RUN_TARGET_NAME="ft_ping"> <configuration name="ft_ping" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="ft_ping" TARGET_NAME="ft_ping" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="ft_ping" RUN_TARGET_NAME="ft_ping">
@ -210,6 +198,8 @@
<workItem from="1739883688368" duration="87000" /> <workItem from="1739883688368" duration="87000" />
<workItem from="1739883850940" duration="13000" /> <workItem from="1739883850940" duration="13000" />
<workItem from="1739885059154" duration="7477000" /> <workItem from="1739885059154" duration="7477000" />
<workItem from="1740394061411" duration="1627000" />
<workItem from="1740397363198" duration="9126000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@ -13,4 +13,10 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
struct arguments {
char* args[1];
int verbose;
unsigned int ttl;
};
#endif //MAIN_H #endif //MAIN_H

View File

@ -7,17 +7,15 @@ static char doc[] = "Send ICMP ECHO_REQUEST packets to network hosts.\vMandatory
"only) are available only to superuser."; "only) are available only to superuser.";
static char args_doc[] = "HOST ..."; static char args_doc[] = "HOST ...";
#define ARG_TTL 256
static struct argp_option options[] = { static struct argp_option options[] = {
{0, 0, 0, 0, "Options valid for all request types:\n"}, {0, 0, 0, 0, "Options valid for all request types:\n"},
{"verbose", 'v', 0, 0, "verbose output"}, {"verbose", 'v', 0, 0, "verbose output"},
{"ttl", ARG_TTL, "N", 0, "specify N as time-to-live"},
{0}, {0},
}; };
struct arguments {
char* args[1];
int verbose;
};
static error_t parse_opt(int key, char* arg, struct argp_state* state) { static error_t parse_opt(int key, char* arg, struct argp_state* state) {
struct arguments* arguments = state->input; struct arguments* arguments = state->input;
@ -25,6 +23,8 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state) {
case 'v': case 'v':
arguments->verbose = 1; arguments->verbose = 1;
break; break;
case ARG_TTL:
arguments->ttl = strtoul(arg, NULL, 10);
case ARGP_KEY_ARG: case ARGP_KEY_ARG:
if (state->arg_num >= 2) if (state->arg_num >= 2)
argp_usage(state); argp_usage(state);
@ -44,42 +44,37 @@ static struct argp argp = {options, parse_opt, args_doc, doc};
static bool stop = false; static bool stop = false;
int sock = 0;
void send_ping() {}
void sig_handler(int sig) { void sig_handler(int sig) {
if (sig == SIGINT) { if (sig == SIGINT) {
stop = true; stop = true;
} else if (sig == SIGALRM) { } else if (sig == SIGALRM) {
send_ping();
} }
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
struct arguments arguments; struct arguments arguments;
arguments.verbose = 0; arguments.verbose = 0;
arguments.ttl = 64;
argp_parse(&argp, argc, argv, 0, 0, &arguments); argp_parse(&argp, argc, argv, 0, 0, &arguments);
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0) { if (sock < 0) {
dprintf(STDERR_FILENO, "%s: socket() failed: %s\n", argv[0], strerror(errno)); dprintf(STDERR_FILENO, "%s: socket() failed: %s\n", argv[0], strerror(errno));
exit(1); exit(1);
} }
struct sockaddr_in server;
bzero(&server, sizeof(server));
server.sin_family = AF_INET; if (setsockopt(sock, IPPROTO_IP, IP_TTL, &arguments.ttl, sizeof(arguments.ttl)) < 0) {
if (inet_aton(arguments.args[0], &server.sin_addr) < 1) { dprintf(STDERR_FILENO, "%s: setsockopt() failed: %s\n", argv[0], strerror(errno));
dprintf(STDERR_FILENO, "%s: unknown host\n", argv[0]);
close(sock); close(sock);
exit(1); exit(1);
} }
server.sin_port = htons(0);
if (bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
close(sock);
dprintf(STDERR_FILENO, "%s: bind() failed: %s\n", argv[0], strerror(errno));
exit(1);
}
struct sigaction sig_handle; struct sigaction sig_handle;
sig_handle.sa_handler = sig_handler; sig_handle.sa_handler = sig_handler;
@ -87,6 +82,11 @@ int main(int argc, char** argv) {
sigaction(SIGINT, &sig_handle, 0); sigaction(SIGINT, &sig_handle, 0);
sigaction(SIGALRM, &sig_handle, 0); sigaction(SIGALRM, &sig_handle, 0);
// alarm(1);
unsigned long xmit = 0;
unsigned long recv = 0;
while (true) { while (true) {
if (stop) { if (stop) {
@ -94,7 +94,8 @@ int main(int argc, char** argv) {
} }
} }
printf("---- %s ping statistics ----\n", arguments.args[0]); printf("---- %s ping statistics ----\n", arguments.args[0]);
// printf("%d packets transmitted, %d packets received, %d%% packet loss\n"); printf("%zu packets transmitted, %zu packets received, %%zu%% packet loss\n", xmit,
recv /*, (xmit - recv) / xmit * 100*/);
// printf("round-trip min/avg/max/stddev = %f/%f/%f/%f ms\n"); // printf("round-trip min/avg/max/stddev = %f/%f/%f/%f ms\n");
close(sock); close(sock);
} }

5
srcs/util.c Normal file
View File

@ -0,0 +1,5 @@
//
// Created by jrathelo on 2/24/25.
//
#include <main.h>