From bbcd1cf1ca013b08659aaeb80131d442a16fea82 Mon Sep 17 00:00:00 2001 From: Brett Gordon Date: Thu, 9 Feb 2017 12:40:28 -0500 Subject: [PATCH] a start. --- Applications/netd/Makefile.6809 | 3 +- Applications/netd/ntpdate.c | 125 ++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 Applications/netd/ntpdate.c diff --git a/Applications/netd/Makefile.6809 b/Applications/netd/Makefile.6809 index af4d166a..d7efdd07 100644 --- a/Applications/netd/Makefile.6809 +++ b/Applications/netd/Makefile.6809 @@ -16,7 +16,7 @@ CRT0 = ../../Library/libs/crt0_6809.o .SUFFIXES: .c .o SRCS = netd.c uip.c uiplib.c timer.c clock-arch.c uip_arp.c telnet.c lwwire.c -SRCS += echoping.c dig.c gethostbyname.c httpd.c coconic.c ping.c +SRCS += echoping.c dig.c gethostbyname.c httpd.c coconic.c ping.c ntpdate.c OBJS = $(SRCS:.c=.o) @@ -33,6 +33,7 @@ netd: $(OBJS) $(LINKER) $(LINKER_OPT) -o dig $(CRT0) dig.o $(LINKER) $(LINKER_OPT) -o httpd $(CRT0) httpd.o $(LINKER) $(LINKER_OPT) -o ping $(CRT0) ping.o gethostbyname.o + $(LINKER) $(LINKER_OPT) -o ntpdate $(CRT0) ntpdate.o gethostbyname.o diff --git a/Applications/netd/ntpdate.c b/Applications/netd/ntpdate.c new file mode 100644 index 00000000..97e9ba3c --- /dev/null +++ b/Applications/netd/ntpdate.c @@ -0,0 +1,125 @@ +/* + A Cheesey NTP client to set system time + + (C) 2017, Brett M. Gordon, GPL2 under Fuzix + + todo: + * add port no argument +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "netdb.h" + +#define AF_INET 1 +#define SOCK_DGRAM 2 + +typedef struct { + uint32_t sec; + uint32_t frac; +} ntime; + +struct ntp_t { + uint8_t lvm; + uint8_t statum; + uint8_t poll; + uint8_t prec; + uint32_t delay; + uint32_t disp; + uint32_t refid; + ntime ref; + ntime org; + ntime rec; + ntime xmit; +}; + + +#define MAXBUF 256 +int fd; +char buf[MAXBUF]; +struct sockaddr_in addr; + + +void alarm_handler( int signum ){ + return; +} + + + +/* sends query to remote */ +void send( void ){ + memset( buf, 0, MAXBUF ); + struct ntp_t *i = (struct ntp_t *)buf; + i->lvm = 0xe3; + write(fd, buf, 48); +} + +my_open( int argc, char *argv[]){ + struct hostent *h; + + h=gethostbyname( argv[1] ); + if( ! h ){ + fprintf( stderr, "cannot resolve hostname\n" ); + exit(1); + } + memcpy( &addr.sin_addr.s_addr, h->h_addr_list[0], 4 ); + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + perror("af_inet sock_stream 0"); + exit(1); + } + + /* fuzix raw sockets (for now) repurposes the connect() + address struct's port no to pass it's protocol number + */ + addr.sin_port = 123; + addr.sin_family = AF_INET; + if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + perror("connect"); + exit(1); + } +} + + +int main( int argc, char *argv[] ){ + int retries; + int rv; + + if( argc < 2 ){ + fprintf( stderr,"usage: ntpdate hostname\n"); + exit(1); + } + + my_open( argc, argv ); + + retries = 5; + while( retries--){ + send(); + signal( SIGALRM, alarm_handler ); + alarm(2); + rv=read( fd, buf, MAXBUF); + if (rv >= sizeof( struct ntp_t )) + goto process; + } + fprintf(stderr, "timeout"); + exit(1); + + process: + { + int i = 48; + while( i-- ){ + printf("%02x ", buf[i] ); + } + printf("\n"); + } + exit(0); + +} -- 2.34.1