commit edb0739883edd8687448b682784f8aface4bddf9
parent afcc565771a37978eb0b54e185fa80482cdebe3a
Author: Michael Buch <michaelbuch12@gmail.com>
Date: Mon, 25 Mar 2019 11:52:35 +0000
Add FreeBSD support. Mainly missing includes and struct icmphdr differences
Diffstat:
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/cnping.c b/cnping.c
@@ -11,6 +11,10 @@
#endif
#include <windows.h>
#else
+ #ifdef __FreeBSD__
+ #include <sys/types.h>
+ #include <netinet/in.h>
+ #endif
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
diff --git a/ping.c b/ping.c
@@ -27,11 +27,14 @@
#include <ws2tcpip.h>
#include <stdint.h>
#else
+ #ifdef __FreeBSD__
+ #include <netinet/in.h>
+ #endif
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
- #ifdef __APPLE__
+ #if defined(__APPLE__) || defined(__FreeBSD__)
#ifndef SOL_IP
#define SOL_IP IPPROTO_IP
#endif
@@ -69,8 +72,13 @@ int precise_ping;
struct packet
{
+#ifdef __FreeBSD__
+ struct icmp hdr;
+ unsigned char msg[PACKETSIZE-sizeof(struct icmp)];
+#else
struct icmphdr hdr;
unsigned char msg[PACKETSIZE-sizeof(struct icmphdr)];
+#endif
};
int sd;
@@ -166,12 +174,19 @@ void ping(struct sockaddr_in *addr )
{
int rsize = load_ping_packet( pckt.msg, sizeof( pckt.msg ) );
memset( &pckt.hdr, 0, sizeof( pckt.hdr ) ); //This needs to be here, but I don't know why, since I think the struct is fully populated.
-
+#ifdef __FreeBSD__
+ pckt.hdr.icmp_code = 0;
+ pckt.hdr.icmp_type = ICMP_ECHO;
+ pckt.hdr.icmp_id = pid;
+ pckt.hdr.icmp_seq = cnt++;
+ pckt.hdr.icmp_cksum = checksum((const unsigned char *)&pckt, sizeof( pckt.hdr ) + rsize );
+#else
pckt.hdr.code = 0;
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = pid;
pckt.hdr.un.echo.sequence = cnt++;
pckt.hdr.checksum = checksum((const unsigned char *)&pckt, sizeof( pckt.hdr ) + rsize );
+#endif
int sr = sendto(sd, (char*)&pckt, sizeof( pckt.hdr ) + rsize , 0, (struct sockaddr*)addr, sizeof(*addr));