Support for poll server and sim client

This commit is contained in:
llsth 2015-03-18 20:32:00 +01:00
parent a25a43b7af
commit 8bb7d44c1d
6 changed files with 485 additions and 13 deletions

51
configure vendored
View File

@ -24,6 +24,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
set -e
@ -48,6 +49,7 @@ HDRS='
SRCS='
main_amiga_client.c
main_amiga_poll.c
main_amiga_sim.c
combine_delta.c
ntp_filter.c
ntp_packet.c
@ -69,6 +71,8 @@ SRCS='
mem.c
'
#################################################
# TODO: Rewrite logic
SRCS1='
main_amiga_client.c
combine_delta.c
@ -115,8 +119,30 @@ SRCS2='
mem.c
'
SRCS3='
main_amiga_sim.c
combine_delta.c
ntp_filter.c
ntp_packet.c
ntp_peer.c
ntp_peerset.c
ntp_tools.c
ocx_stdio.c
param.c
pll_std.c
suckaddr.c
time_sim.c
time_stuff.c
todo.c
udp.c
agetaddrinfo.c
apoll.c
mem.c
'
#################################################
if make -v 2>&1 | grep GNU > /dev/null 2>&1 ; then
echo "make(1) is GNU make."
echo "make is GNU make."
VALID=true
else
echo "Wrong version of make."
@ -127,7 +153,7 @@ if $VALID ; then
(
echo '# Portable Makefile generated by configure'
echo ''
echo 'all: ntimed ntimed-poll'
echo 'all: ntimed ntimed-poll ntimed-sim'
echo ''
if [ -n "$1" ] && [ $1 = "AMIGA" ] ; then
echo 'CC = gcc'
@ -154,7 +180,7 @@ if $VALID ; then
echo
fi
done
l=""
for f in ${SRCS}
do
@ -168,7 +194,9 @@ if $VALID ; then
echo
l="${l} ${b}.o"
done
#################################################
# TODO: Rewrite logic
BIN1=""
for f in ${SRCS1}
do
@ -181,20 +209,29 @@ if $VALID ; then
BIN2="${BIN2} `basename $f .c`.o"
done
BIN3=""
for f in ${SRCS3}
do
BIN3="${BIN3} `basename $f .c`.o"
done
#################################################
echo
echo "ntimed: ${BIN1}"
echo " \${CC} \${CFLAGS} -o ntimed ${BIN1} -lm"
echo
echo
echo "ntimed-poll: ${BIN2}"
echo " \${CC} \${CFLAGS} -o ntimed-poll ${BIN2} -lm"
echo
echo "ntimed-sim: ${BIN3}"
echo " \${CC} \${CFLAGS} -o ntimed-sim ${BIN3} -lm"
echo
echo "clean:"
echo " rm -f ${l} ntimed ntimed-poll"
echo " rm -f ${l} ntimed ntimed-poll ntimed-sim"
echo
echo "depend:"
echo " @echo Dependencies already done"
) > Makefile
fi
echo "Makefile generated${msg}"
echo "Makefile generated"

View File

@ -115,6 +115,7 @@ int main(int argc, char **argv)
if (!args.quiet) {
EnableDebug();
EnableTraceDebug();
}
init_libs(&args);
@ -225,11 +226,6 @@ static void init_logic(struct ntimedargs *args)
npeer += NTP_PeerSet_Add(NULL, npl, args->servers[i]);
Put(NULL, OCX_TRACE, "# NTIMED Format poll-server 1.0\n");
Put(NULL, OCX_TRACE, "# Timezone set to %s (UTC%s%.2f)\n",
tza[tzid].abbr,
tza[tzid].offset < 0.0 ? "-" : "+",
fabs(tza[tzid].offset));
Put(NULL, OCX_TRACE, "# Found %d peers\n", npeer);
if (npeer == 0) {
@ -251,6 +247,11 @@ static void init_logic(struct ntimedargs *args)
exit(1);
}
Put(NULL, OCX_TRACE, "# Timezone set to %s (UTC%s%.2f)\n",
tza[tzid].abbr,
tza[tzid].offset < 0.0 ? "-" : "+",
fabs(tza[tzid].offset));
Put(NULL, OCX_TRACE, "# Duration of run set to %d seconds\n", duration);
TODO_ScheduleRel(tdl, mps_end, NULL, duration, 0, "End task");

419
main_amiga_sim.c Normal file
View File

@ -0,0 +1,419 @@
/*
* Copyright (c) 2015 Carsten Larsen
* Copyright (c) 2014 Poul-Henning Kamp
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <math.h>
#include <sys/socket.h>
#ifdef Debug
#undef Debug
#endif
#ifdef AROS
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <proto/socket.h>
#include <sys/socket.h>
#include <bsdsocket/socketbasetags.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef Debug
#undef Debug
#endif
#endif
#include "tz.h"
#include "ntimed.h"
#include "ntimed_platform.h"
#include "mem.h"
#include "ntp.h"
#include "udp.h"
#define PARAM_CLIENT PARAM_INSTANCE
#define PARAM_TABLE_NAME client_param_table
#include "param_instance.h"
#undef PARAM_TABLE_NAME
#undef PARAM_CLIENT
/**********************************************************************/
struct sim_file {
unsigned magic;
#define SIM_FILE_MAGIC 0x7f847bd0
char *filename;
FILE *input;
unsigned n_peer;
struct ntp_peerset *npl;
struct timestamp when;
unsigned t0;
};
static void
simfile_poll(struct ocx *ocx, const struct sim_file *sf, char *buf);
static enum todo_e
simfile_readline(struct ocx *ocx, struct todolist *tdl, void *priv);
static struct sim_file *
SimFile_Open(struct ocx *ocx, const char *fn, struct todolist *tdl,
struct ntp_peerset *npl);
/**********************************************************************/
BOOL started_from_wb;
struct Library *SocketBase = NULL;
struct RDArgs *rdargs = NULL;
struct todolist *tdl = NULL;
#define ARGSFORMAT "P=PARAM/K,B=BUMP/K,T=TRACEFILE/K,SIMFILE/A,QUIET/S"
const char *vers = "\0$VER: ntimed-sim 1.0a";
struct ntimedargs {
char *params;
char *bump;
char *tracefile;
char *simfile;
long quiet;
};
static void init_libs(struct ntimedargs *args);
static void init_logic(struct ntimedargs *args);
static void set_params(char *params);
static void clean_exit();
int main(int argc, char **argv)
{
struct ntimedargs args = { NULL, NULL, NULL, NULL, FALSE };
started_from_wb = (BOOL)argc;
atexit(clean_exit);
rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL);
if (!rdargs)
{
PrintFault(IoErr(), (ARGPTR)argv[0]);
exit(2);
}
if (!args.quiet) {
EnableDebug();
}
init_libs(&args);
if (args.tracefile) {
ArgTracefile(args.tracefile);
}
init_logic(&args);
(void)TODO_Run(NULL, tdl);
return 0;
}
static void init_logic(struct ntimedargs *args)
{
int ch;
struct sim_file *sf;
struct ntp_peerset *npl;
struct ntp_peer *np;
struct combine_delta *cd;
double a, b, c;
tdl = TODO_NewList();
Time_Sim(tdl);
PLL_Init();
npl = NTP_PeerSet_New(NULL);
Param_Register(client_param_table);
NF_Init();
set_params(args->params);
if (args->bump) {
ch = sscanf(args->bump, "%lg,%lg,%lg", &a, &b, &c);
if (ch != 3) {
Put(NULL, OCX_DEBUG, "bad BUMP argument \"when,freq,phase\"\n");
exit(1);
}
}
Time_Sim_Bump(tdl, a, b, c);
Param_Report(NULL, OCX_TRACE);
sf = SimFile_Open(NULL, args->simfile, tdl, npl);
AN(sf);
cd = CD_New();
NTP_PeerSet_Foreach(np, npl) {
NF_New(np);
np->combiner = CD_AddSource(cd, np->hostname, np->ip);
}
}
/**********************************************************************/
static void init_libs(struct ntimedargs *args)
{
#ifdef AROS
if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) {
Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n");
exit(2);
}
if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno,
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) {
Put(NULL, OCX_DIAG, "Error initializing bsdsocket.\n");
exit(2);
}
#endif
}
static void clean_exit()
{
ArgTracefile(NULL);
#ifdef AROS
if (SocketBase != NULL) {
CloseLibrary(SocketBase);
SocketBase = NULL;
}
#endif
if (rdargs) {
FreeArgs(rdargs);
rdargs = NULL;
}
freeall();
}
/**********************************************************************/
static void set_params(char *params)
{
char *token;
const char d[2] = ",";
if (params == NULL)
return;
token = strtok(params, d);
while (token != NULL)
{
Param_Tweak(NULL, token);
token = strtok(NULL, d);
}
}
/**********************************************************************/
static void
simfile_poll(struct ocx *ocx, const struct sim_file *sf, char *buf)
{
char *hostname;
char *ip;
char *pkt;
struct ntp_peer *np;
struct ntp_packet *rxp;
struct ntp_packet *txp;
CHECK_OBJ_NOTNULL(sf, SIM_FILE_MAGIC);
AN(buf);
if (memcmp(buf, "Poll ", 5))
Fail(ocx, 0, "Bad 'Poll' line (%s)\n", buf);
hostname = buf + 5;
ip = strchr(hostname, ' ');
if (ip == NULL)
Fail(ocx, 0, "Bad 'Poll' line (%s)\n", buf);
pkt = strchr(ip + 1, ' ');
if (pkt == NULL)
Fail(ocx, 0, "Bad 'Poll' line (%s)\n", buf);
*ip++ = '\0';
*pkt++ = '\0';
NTP_PeerSet_Foreach(np, sf->npl)
if (!strcmp(np->hostname, hostname) && !strcmp(np->ip, ip))
break;
if (np == NULL)
Fail(ocx, 0, "Peer not found (%s, %s)\n", hostname, ip);
CHECK_OBJ_NOTNULL(np, NTP_PEER_MAGIC);
txp = np->tx_pkt;
INIT_OBJ(txp, NTP_PACKET_MAGIC);
rxp = np->rx_pkt;
if (NTP_Tool_Scan(rxp, pkt))
Fail(ocx, 0, "Cannot parse packet (%s, %s, %s)\n",
hostname, ip, pkt);
TS_Add(&rxp->ntp_origin, Time_Sim_delta);
TS_Add(&rxp->ts_rx, Time_Sim_delta);
txp->ntp_transmit = rxp->ntp_origin;
if (np->filter_func != NULL)
np->filter_func(ocx, np);
}
static enum todo_e
simfile_readline(struct ocx *ocx, struct todolist *tdl, void *priv)
{
struct sim_file *sf;
char buf[BUFSIZ], *p;
struct timestamp t0;
unsigned u1, u2;
double dt;
AN(tdl);
CAST_OBJ_NOTNULL(sf, priv, SIM_FILE_MAGIC);
TB_Now(&t0);
while (1) {
if (fgets(buf, sizeof buf, sf->input) == NULL) {
Debug(ocx, "EOF on SIMFILE (%s)\n", sf->filename);
exit(0);
}
p = strchr(buf, '\r');
if (p != NULL)
*p = '\0';
p = strchr(buf, '\n');
if (p != NULL)
*p = '\0';
if (!strncmp(buf, "Now ", 4)) {
if (sscanf(buf, "Now %u.%u", &u1, &u2) != 2)
Fail(ocx, 0, "Bad 'Now' line (%s)", buf);
if (sf->t0 == 0)
sf->t0 = u1 - t0.sec;
u1 -= sf->t0;
TS_Nanosec(&sf->when, u1, u2);
dt = TS_Diff(&sf->when, &t0);
if (dt >= 1e-3) {
TODO_ScheduleAbs(tdl, simfile_readline, priv,
&sf->when, 0.0, "Readline");
return (TODO_OK);
}
} else if (!strncmp(buf, "Poll ", 5)) {
simfile_poll(ocx, sf, buf);
}
/* We ignore things we don't understand */
}
}
static struct sim_file *
SimFile_Open(struct ocx *ocx, const char *fn, struct todolist *tdl,
struct ntp_peerset *npl)
{
struct sim_file *sf;
char buf[BUFSIZ];
char buf2[BUFSIZ];
char buf3[BUFSIZ];
char *e;
int s;
unsigned fpeer = 0;
AN(fn);
AN(tdl);
AN(npl);
ALLOC_OBJ(sf, SIM_FILE_MAGIC);
AN(sf);
sf->input = fopen(fn, "r");
if (sf->input == NULL)
Fail(ocx, 1, "Could not open SIMFILE (%s)", fn);
sf->filename = strdup(fn);
AN(sf->filename);
sf->npl = npl;
for (s = 0; s < 3; ) {
if (fgets(buf, sizeof buf, sf->input) == NULL)
Fail(ocx, 1, "Premature EOF on SIMFILE (%s)", fn);
e = strchr(buf, '\0');
AN(e);
if (e == buf)
continue;
if (e[-1] == '\n')
*--e = '\0';
Debug(ocx, ">>> %s\n", buf);
switch(s) {
case 0:
if (strcmp(buf, "# NTIMED Format poll-server 1.0"))
Fail(ocx, 0,
"Wrong fileformat in SIMFILE (%s)", fn);
s++;
break;
case 1:
if (sscanf(buf, "# Found %u peers", &sf->n_peer) != 1)
Fail(ocx, 0, "Expected '# Found ... peers' line");
s++;
break;
case 2:
if (sscanf(buf, "# Peer %s %s", buf2, buf3) != 2)
Fail(ocx, 0, "Expected '# Peer' line");
NTP_PeerSet_AddSim(ocx, npl, buf2, buf3);
if (++fpeer == sf->n_peer)
s++;
break;
default:
Debug(ocx, "<%s>\n", buf);
Fail(ocx, 0, "XXX: Wrong state (%d) in open_sim_file", s);
}
}
(void)simfile_readline(NULL, tdl, sf);
return (sf);
}

View File

@ -66,7 +66,6 @@ void Fail(struct ocx *, int err, const char *, ...) \
#define DebugHex(ocx, ptr, len) PutHex(ocx, OCX_DEBUG, ptr, len)
void ArgTracefile(const char *fn);
void EnableDebug();
/* param.c -- Parameters **********************************************/

View File

@ -58,6 +58,8 @@
void amiga_save_time(void);
void amiga_sync_time(int seconds);
void EnableDebug();
void EnableTraceDebug();
#define SetSyncTime amiga_sync_time
#define Time_Amiga_Save amiga_save_time

View File

@ -67,6 +67,8 @@
#include "ntimed.h"
int repeat_trace = 0;
static FILE *debugfile = NULL;
static FILE *tracefile = NULL;
@ -115,6 +117,12 @@ EnableDebug()
debugfile = stdout;
}
void
EnableTraceDebug()
{
repeat_trace = 1;
}
/**********************************************************************
* XXX: The stuff below is generic and really ought to be in ocx.c on
* XXX: its own.
@ -133,6 +141,12 @@ Put(struct ocx *ocx, enum ocx_chan chan, const char *fmt, ...)
if (dst != NULL) {
(void)vfprintf(dst, fmt, ap);
fflush(dst);
if (repeat_trace && chan == OCX_TRACE) {
dst = getdst(OCX_DEBUG);
(void)vfprintf(dst, fmt, ap);
fflush(dst);
}
}
va_end(ap);