mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:17:34 +00:00
Userland: Allow ping <hostname> :^)
This commit is contained in:
parent
696f9c5bc0
commit
4ea625e08b
1 changed files with 13 additions and 4 deletions
|
@ -1,9 +1,11 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/ip_icmp.h>
|
#include <netinet/ip_icmp.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
uint16_t internet_checksum(const void* ptr, size_t count)
|
uint16_t internet_checksum(const void* ptr, size_t count)
|
||||||
|
@ -33,6 +35,11 @@ inline void timersub(struct timeval* a, struct timeval* b, struct timeval* resul
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
if (argc != 2) {
|
||||||
|
printf("usage: ping <host>\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
|
@ -46,9 +53,11 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* addr_str = "127.0.0.1";
|
auto* hostent = gethostbyname(argv[1]);
|
||||||
if (argc > 1)
|
if (!hostent) {
|
||||||
addr_str = argv[1];
|
printf("Lookup failed for '%s'\n", argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
pid_t pid = getpid();
|
pid_t pid = getpid();
|
||||||
|
|
||||||
|
@ -57,7 +66,7 @@ int main(int argc, char** argv)
|
||||||
peer_address.sin_family = AF_INET;
|
peer_address.sin_family = AF_INET;
|
||||||
peer_address.sin_port = 0;
|
peer_address.sin_port = 0;
|
||||||
|
|
||||||
rc = inet_pton(AF_INET, addr_str, &peer_address.sin_addr);
|
peer_address.sin_addr.s_addr = *(const in_addr_t*)hostent->h_addr_list[0];
|
||||||
|
|
||||||
struct PingPacket {
|
struct PingPacket {
|
||||||
struct icmphdr header;
|
struct icmphdr header;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue