Skip to content

Commit

Permalink
ping: Move optlen calculation to a function
Browse files Browse the repository at this point in the history
This is a preparation for next commit.

Signed-off-by: Petr Vorel <[email protected]>
  • Loading branch information
pevik committed Aug 26, 2024
1 parent ea08978 commit afbc48b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ping/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ static char *str_socktype(int socktype)
return "";
}

static int get_ipv4_optlen(struct ping_rts *rts)
{
if (rts->opt_rroute || rts->opt_timestamp || rts->opt_sourceroute)
return 40;

return 0;
}

static void create_socket(struct ping_rts *rts, socket_st *sock, int family,
int socktype, int protocol, int requisite)
{
Expand Down Expand Up @@ -937,7 +945,6 @@ int ping4_run(struct ping_rts *rts, int argc, char **argv, struct addrinfo *ai,
rspace[1 + IPOPT_OPTVAL] = IPOPT_RR;
rspace[1 + IPOPT_OLEN] = sizeof(rspace) - 1;
rspace[1 + IPOPT_OFFSET] = IPOPT_MINOFF;
rts->optlen = 40;
if (setsockopt(sock->fd, IPPROTO_IP, IP_OPTIONS, rspace, sizeof rspace) < 0)
error(2, errno, "record route");
}
Expand All @@ -960,7 +967,6 @@ int ping4_run(struct ping_rts *rts, int argc, char **argv, struct addrinfo *ai,
if (setsockopt(sock->fd, IPPROTO_IP, IP_OPTIONS, rspace, rspace[1]) < 0)
error(2, errno, "ts option");
}
rts->optlen = 40;
}
if (rts->opt_sourceroute) {
int i;
Expand All @@ -976,9 +982,10 @@ int ping4_run(struct ping_rts *rts, int argc, char **argv, struct addrinfo *ai,

if (setsockopt(sock->fd, IPPROTO_IP, IP_OPTIONS, rspace, 4 + rts->nroute * 4) < 0)
error(2, errno, "record route");
rts->optlen = 40;
}

rts->optlen = get_ipv4_optlen(rts);

/* Estimate memory eaten by single packet. It is rough estimate.
* Actually, for small datalen's it depends on kernel side a lot. */
hold = rts->datalen + 8;
Expand Down

0 comments on commit afbc48b

Please sign in to comment.