Index: /timediff/Makefile
===================================================================
--- /timediff/Makefile	(revision 116)
+++ /timediff/Makefile	(revision 116)
@@ -0,0 +1,9 @@
+#/**
+# * Rick van der Zwet <info@rickvanderzwet.nl>
+# * Licence: BSD
+# */
+all: timediff timediffd
+	./timediff
+
+clean:
+	rm -f ./timediff ./timediffd
Index: /timediff/README
===================================================================
--- /timediff/README	(revision 116)
+++ /timediff/README	(revision 116)
@@ -0,0 +1,3 @@
+= Intro = 
+Checking wether both clock sources are in sync can be troublesome,
+this should Proof Of Concept code provide a basic pointer
Index: /timediff/timediff.c
===================================================================
--- /timediff/timediff.c	(revision 116)
+++ /timediff/timediff.c	(revision 116)
@@ -0,0 +1,114 @@
+/**
+ * Rick van der Zwet <info@rickvanderzwet.nl>
+ * Licence: BSD
+ */
+
+#include "timediff.h"
+#include <errno.h>
+
+void diep(char *s)
+{
+    perror(s);
+    exit(1);
+}
+
+int main(int argc, char * argv[]) {
+    printf("Size of .. : %li\n", sizeof(uint32_t));
+    printf("Size of .. : %li\n", sizeof(struct timeval_t));
+    printf("Size of .. : %li\n", sizeof(struct timeval));
+    struct timeval tv, tv2, tv3, tv4, tv_delta, tv_delta_system;
+    struct tm *tm, *tm2;
+    struct timeval_t packet;
+    gettimeofday(&tv, NULL);
+    tm=localtime(&tv.tv_sec);
+    printf("Started at: %d:%02d:%02d.%li \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
+
+    struct sockaddr_in si_other, si_me;
+    int s, r, i, slen=sizeof(si_other);
+    char buf[BUFLEN];
+
+    /* Set up receiving socket */
+    if ((r=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
+        diep("socket");
+
+    memset((char *) &si_me, 0, sizeof(si_me));
+    si_me.sin_family = AF_INET;
+    si_me.sin_port = htons(PORT_CLIENT);
+    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
+    if (bind(r, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
+        diep("bind");
+
+    if ((r=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
+        diep("socket");
+
+    /* Prepare server connection */
+    memset((char *) &si_other, 0, sizeof(si_other));
+    si_other.sin_family = AF_INET;
+    si_other.sin_port = htons(PORT);
+    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
+        fprintf(stderr, "inet_aton() failed\n");
+        exit(1);
+    }
+
+    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
+        diep("socket");
+
+    /* We don't care in slow packets anymore */
+    struct timeval tv_timeout;
+    tv_timeout.tv_sec = 1L;
+    tv_timeout.tv_usec = 0L;
+    if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) != 0)
+        diep("setsockopt");
+
+
+    for (i=0; i<NPACK; i++) {
+        printf("Sending packet %d and waiting for response\n", i);
+        gettimeofday(&tv, NULL);
+        packet.tv_sec = tv.tv_sec;
+        packet.tv_usec = tv.tv_usec;
+        if (sendto(s, (const void *)&packet, sizeof(packet), 0, (struct sockaddr *)&si_other, slen)==-1)
+            diep("sendto()");
+
+        /* This will be generated more and less the same time as on the target side */
+        gettimeofday(&tv3, NULL);
+
+        packet.tv_usec = 0;
+        packet.tv_sec = 0;
+
+        if (recv(s, &packet, sizeof(packet), 0)==-1) {
+            if (errno == EAGAIN) {
+                perror("ERROR: No response received in time");
+                continue;
+            } else {
+                diep("recvfrom()");
+            }
+        }
+        gettimeofday(&tv4, NULL);
+        tv2.tv_sec = packet.tv_sec;
+        tv2.tv_usec = packet.tv_usec;
+
+        printf("Received %li bytes\n", sizeof(packet));
+
+        /* Display and calculations */
+        tm=localtime(&tv.tv_sec);
+        tm2=localtime(&tv2.tv_sec);
+        printf("Send timestamp: %d:%02d:%02d.%06li \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
+        printf("Received timestamp: %d:%02d:%02d.%06li \n", tm2->tm_hour, tm2->tm_min, tm2->tm_sec, tv2.tv_usec);
+
+        tv_delta.tv_sec = tv4.tv_sec - tv.tv_sec;
+        tv_delta.tv_usec = tv4.tv_usec - tv.tv_usec;
+        printf("Time delta at local system (including transmit/receive): %02d.%06li\n",
+                (int)(tv_delta.tv_sec), tv_delta.tv_usec);
+
+        tv_delta_system.tv_sec = (tv2.tv_sec - tv.tv_sec) - tv_delta.tv_sec / 2;
+        tv_delta_system.tv_usec = (tv2.tv_usec - tv.tv_usec) - tv_delta.tv_usec / 2;
+        printf("Time delta between sender and target: %02d.%06li\n",
+                (int)(tv_delta_system.tv_sec), tv_delta_system.tv_usec);
+        printf("\n");
+    }
+
+    close(s);
+    return 0;
+
+    exit(0);
+}
Index: /timediff/timediff.h
===================================================================
--- /timediff/timediff.h	(revision 116)
+++ /timediff/timediff.h	(revision 116)
@@ -0,0 +1,29 @@
+/**
+ * Rick van der Zwet <info@rickvanderzwet.nl>
+ * Licence: BSD
+ * UDP handshake taken from http://www.abc.se/~m6695/udp.html
+ */
+
+#include <stdio.h>
+#include <time.h>
+#include <sys/time.h>
+#include <stdlib.h>
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <string.h>
+
+#define BUFLEN 512
+#define NPACK 10
+#define PORT 9930
+#define PORT_CLIENT 9931
+//#define SRV_IP "127.0.0.1"
+#define SRV_IP "192.168.42.1"
+
+struct timeval_t {
+    uint32_t tv_sec;
+    uint32_t tv_usec;
+    };
Index: /timediff/timediffd.c
===================================================================
--- /timediff/timediffd.c	(revision 116)
+++ /timediff/timediffd.c	(revision 116)
@@ -0,0 +1,55 @@
+/**
+ * Rick van der Zwet <info@rickvanderzwet.nl>
+ * Licence: BSD
+ * UDP handshake taken from http://www.abc.se/~m6695/udp.html
+ */
+
+#include "timediff.h"
+
+void diep(char *s)
+{
+    perror(s);
+    exit(1);
+}
+
+int main(int argc, char * argv[]) {
+    printf("Size of .. %li\n", sizeof(uint32_t));
+    printf("Size of .. %li\n", sizeof(struct timeval));
+    struct timeval tv;
+    struct tm *tm;
+
+    struct sockaddr_in si_me, si_other;
+    int s, i, slen=sizeof(si_other);
+    char buf[BUFLEN];
+
+    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
+        diep("socket");
+
+    memset((char *) &si_me, 0, sizeof(si_me));
+    si_me.sin_family = AF_INET;
+    si_me.sin_port = htons(PORT);
+    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
+    if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
+        diep("bind");
+
+    struct timeval_t packet;
+    for (i=0; i<NPACK; i++) {
+        if (recvfrom(s, &tv, sizeof(tv), 0, (struct sockaddr *)&si_other, &slen)==-1)
+            diep("recvfrom()");
+        gettimeofday(&tv,NULL);
+	packet.tv_sec = tv.tv_sec;
+	packet.tv_usec = tv.tv_usec;
+        if (sendto(s, (const void *)&packet, sizeof(packet), 0, (struct sockaddr *)&si_other, slen)==-1)
+            diep("sendto()");
+	printf("Send %i bytes\n", sizeof(packet));
+        printf("Received packet from %s:%d\n", 
+                inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
+        tm=localtime(&tv.tv_sec);
+        printf(" %d:%02d:%02d.%li \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
+    }
+
+    close(s);
+    return 0;
+
+    exit(0);
+}
