Skip to content

Commit

Permalink
test: add a static test for ICMP socket
Browse files Browse the repository at this point in the history
Add a ZDTM static test for the ICMP socket feature.

Signed-off-by: समीर सिंह Sameer Singh <[email protected]>
  • Loading branch information
ss141309 committed Dec 28, 2024
1 parent 71d69b7 commit 5238f07
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/zdtm/static/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ TST_NOFILE := \
socket_udp-corked \
socket6_udp \
socket_udp_shutdown \
socket_icmp \
sk-freebind \
sk-freebind-false \
socket_udplite \
Expand Down
109 changes: 109 additions & 0 deletions test/zdtm/static/socket_icmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "zdtmtst.h"

const char *test_doc = "static test for ICMP socket\n";
const char *test_author = "समीर सिंह Sameer Singh <[email protected]>\n";

/* Description:
* Send a ping to localhost using ICMP socket
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>

#define PACKET_SIZE 64

Check warning on line 20 in test/zdtm/static/socket_icmp.c

View workflow job for this annotation

GitHub Actions / build

#define RECV_TIMEOUT 1

int ping(void)
{
int ret, sock, seq = 0, recv_len = 0;
char packet[PACKET_SIZE], recv_packet[PACKET_SIZE];

struct timeval tv;
struct icmphdr icmp_header, *icmp_reply;
struct sockaddr_in addr, recv_addr;
socklen_t addr_len;

sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP);
if (sock < 0) {
pr_perror("Can't create socket");
return 1;
}


Check warning on line 39 in test/zdtm/static/socket_icmp.c

View workflow job for this annotation

GitHub Actions / build

tv.tv_sec = RECV_TIMEOUT;
tv.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
pr_perror("Can't set socket option");
return 1;
}

memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");

for (;;) {
memset(&icmp_header, 0, sizeof(icmp_header));
icmp_header.type = ICMP_ECHO;
icmp_header.code = 0;
icmp_header.un.echo.id = getpid();
icmp_header.un.echo.sequence = seq++;

memcpy(packet, &icmp_header, sizeof(icmp_header));
memset(packet + sizeof(icmp_header), 0xa5,
PACKET_SIZE - sizeof(icmp_header));

Check warning on line 60 in test/zdtm/static/socket_icmp.c

View workflow job for this annotation

GitHub Actions / build


ret = sendto(sock, packet, PACKET_SIZE, 0,
(struct sockaddr *)&addr, sizeof(addr));

if (ret < 0) {
fail("Can't send");
return 1;
}

addr_len = sizeof(recv_addr);

ret = recvfrom(sock, recv_packet, sizeof(recv_packet), 0,
(struct sockaddr*)&recv_addr, &addr_len);

Check warning on line 73 in test/zdtm/static/socket_icmp.c

View workflow job for this annotation

GitHub Actions / build


if (ret < 0) {
fail("Can't recv");
return 1;
}

icmp_reply = (struct icmphdr *)recv_packet;

if (icmp_reply->type == ICMP_ECHOREPLY) {
printf("%d bytes from %s: icmp_seq=%d\n",
recv_len,

Check warning on line 84 in test/zdtm/static/socket_icmp.c

View workflow job for this annotation

GitHub Actions / build

inet_ntoa(recv_addr.sin_addr),
icmp_reply->un.echo.sequence);
}

sleep(1);
}

close(sock);
}

int main(int argc, char **argv)
{
int ret;
test_init(argc, argv);

test_daemon();
test_waitsig();

ret = ping();
if (ret == 1)
fail("Cannot ping");

pass();
return 0;
}

0 comments on commit 5238f07

Please sign in to comment.