-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add a static test for ICMP socket
Add a ZDTM static test for the ICMP socket feature. Signed-off-by: समीर सिंह Sameer Singh <[email protected]>
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 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 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 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 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; | ||
} |