Commit 590c65ec authored by Alessandro Rubini's avatar Alessandro Rubini

tools: prepare dump-funcs for freestanding compilation

This commit allows dump-funcs.c to be build from a freestanding
environment. It uses __STDC_HOSTED__ to select what to include,
and relies on "network_types.h" to fill the gaps. Such header is not
part of this commit.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent fab90564
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <linux/if_ether.h>
#include <ppsi/ieee1588_types.h> /* from ../include */ #include <ppsi/ieee1588_types.h> /* from ../include */
#include "decent_types.h" #include "decent_types.h"
#include "ptpdump.h" #include "ptpdump.h"
...@@ -28,31 +22,43 @@ static int dumpstruct(char *prefix, char *name, void *ptr, int size) ...@@ -28,31 +22,43 @@ static int dumpstruct(char *prefix, char *name, void *ptr, int size)
return ret; return ret;
} }
static void dump_eth(struct ethhdr *eth) static void dump_eth(struct ethhdr *eth, struct TimeInternal *ti)
{ {
struct timeval tv; static struct TimeInternal prev_ti;
static struct timeval prev;
struct tm tm;
unsigned char *d = eth->h_dest; unsigned char *d = eth->h_dest;
unsigned char *s = eth->h_source; unsigned char *s = eth->h_source;
gettimeofday(&tv, NULL); if (prev_ti.seconds) {
if (prev.tv_sec) {
int i; int i;
int diffms; int diffms;
diffms = (tv.tv_sec - prev.tv_sec) * 1000 diffms = (ti->seconds - prev_ti.seconds) * 1000
+ (signed)(tv.tv_usec + 500 - prev.tv_usec) / 1000; + (ti->nanoseconds / 1000 / 1000)
- (prev_ti.nanoseconds / 1000 / 1000);
/* empty lines, one every .25 seconds, at most 10 of them */ /* empty lines, one every .25 seconds, at most 10 of them */
for (i = 250; i < 2500 && i < diffms; i += 250) for (i = 250; i < 2500 && i < diffms; i += 250)
printf("\n"); printf("\n");
printf("TIMEDELTA: %i ms\n", diffms); printf("TIMEDELTA: %i ms\n", diffms);
} }
prev = tv; prev_ti = *ti;
localtime_r(&tv.tv_sec, &tm);
printf("TIME: (%li - 0x%lx) %02i:%02i:%02i.%06li\n", #if __STDC_HOSTED__
tv.tv_sec, tv.tv_sec, {
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec); struct timeval tv;
struct tm tm;
tv.tv_sec = ti->seconds;
tv.tv_usec = ti->nanoseconds / 1000;
localtime_r(&tv.tv_sec, &tm);
printf("TIME: (%li - 0x%lx) %02i:%02i:%02i.%06li\n",
tv.tv_sec, tv.tv_sec,
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
}
#else
printf("TIME: (%li - 0x%lx) %li.%06li\n", (long)ti->seconds,
(long)ti->seconds, (long)ti->seconds, (long)ti->nanoseconds);
#endif
printf("ETH: %04x (%02x:%02x:%02x:%02x:%02x:%02x -> " printf("ETH: %04x (%02x:%02x:%02x:%02x:%02x:%02x -> "
"%02x:%02x:%02x:%02x:%02x:%02x)\n", ntohs(eth->h_proto), "%02x:%02x:%02x:%02x:%02x:%02x)\n", ntohs(eth->h_proto),
s[0], s[1], s[2], s[3], s[4], s[5], s[0], s[1], s[2], s[3], s[4], s[5],
...@@ -237,7 +243,7 @@ static void dump_payload(void *pl, int len) ...@@ -237,7 +243,7 @@ static void dump_payload(void *pl, int len)
dumpstruct("DUMP: ", "payload", pl, len); dumpstruct("DUMP: ", "payload", pl, len);
} }
int dump_udppkt(void *buf, int len) int dump_udppkt(void *buf, int len, struct TimeInternal *ti)
{ {
struct ethhdr *eth = buf; struct ethhdr *eth = buf;
struct iphdr *ip = buf + ETH_HLEN; struct iphdr *ip = buf + ETH_HLEN;
...@@ -253,7 +259,7 @@ int dump_udppkt(void *buf, int len) ...@@ -253,7 +259,7 @@ int dump_udppkt(void *buf, int len)
if (udpdest != 319 && udpdest != 320) if (udpdest != 319 && udpdest != 320)
return -1; return -1;
dump_eth(eth); dump_eth(eth, ti);
dump_ip(ip); dump_ip(ip);
dump_udp(udp); dump_udp(udp);
dump_payload(payload, len - (payload - buf)); dump_payload(payload, len - (payload - buf));
...@@ -261,12 +267,12 @@ int dump_udppkt(void *buf, int len) ...@@ -261,12 +267,12 @@ int dump_udppkt(void *buf, int len)
return 0; return 0;
} }
int dump_1588pkt(void *buf, int len) int dump_1588pkt(void *buf, int len, struct TimeInternal *ti)
{ {
struct ethhdr *eth = buf; struct ethhdr *eth = buf;
void *payload = (void *)(eth + 1); void *payload = (void *)(eth + 1);
dump_eth(eth); dump_eth(eth, ti);
dump_payload(payload, len - (payload - buf)); dump_payload(payload, len - (payload - buf));
putchar('\n'); putchar('\n');
return 0; return 0;
......
...@@ -68,6 +68,8 @@ int main(int argc, char **argv) ...@@ -68,6 +68,8 @@ int main(int argc, char **argv)
static unsigned char prev[1500]; static unsigned char prev[1500];
static int prevlen; static int prevlen;
unsigned char buf[1500]; unsigned char buf[1500];
struct TimeInternal ti;
struct timeval tv;
struct sockaddr_in from; struct sockaddr_in from;
socklen_t fromlen = sizeof(from); socklen_t fromlen = sizeof(from);
...@@ -75,6 +77,12 @@ int main(int argc, char **argv) ...@@ -75,6 +77,12 @@ int main(int argc, char **argv)
len = recvfrom(sock, buf, sizeof(buf), MSG_TRUNC, len = recvfrom(sock, buf, sizeof(buf), MSG_TRUNC,
(struct sockaddr *) &from, &fromlen); (struct sockaddr *) &from, &fromlen);
/* Get the receive time, copy it to TimeInternal */
gettimeofday(&tv, NULL);
ti.seconds = tv.tv_sec;
ti.nanoseconds = tv.tv_usec * 1000;
if (len > sizeof(buf)) if (len > sizeof(buf))
len = sizeof(buf); len = sizeof(buf);
/* for some reasons, we receive it three times, check dups */ /* for some reasons, we receive it three times, check dups */
...@@ -93,10 +101,10 @@ int main(int argc, char **argv) ...@@ -93,10 +101,10 @@ int main(int argc, char **argv)
continue; continue;
if (ip->protocol != IPPROTO_UDP) if (ip->protocol != IPPROTO_UDP)
continue; continue;
dump_udppkt(buf, len); dump_udppkt(buf, len, &ti);
break; break;
case ETH_P_1588: case ETH_P_1588:
dump_1588pkt(buf, len); dump_1588pkt(buf, len, &ti);
break; break;
default: default:
continue; continue;
......
#ifndef __PTPDUMP_H__
#define __PTPDUMP_H__
int dump_udppkt(void *buf, int len); #if __STDC_HOSTED__
int dump_1588pkt(void *buf, int len); #include <time.h>
#include <sys/time.h>
#include <netinet/ip.h> /* struct iphdr */
#include <netinet/udp.h> /* struct udphdr */
#include <linux/if_ether.h> /* struct ethhdr */
#else
#include <ppsi/ppsi.h>
#include "../lib/network_types.h"
#define printf pp_printf
#endif
int dump_udppkt(void *buf, int len, struct TimeInternal *ti);
int dump_payloadpkt(void *buf, int len, struct TimeInternal *ti);
int dump_1588pkt(void *buf, int len, struct TimeInternal *ti);
#endif /* __PTPDUMP_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment