199 lines
4.6 KiB
C
199 lines
4.6 KiB
C
/*
|
|
* 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 "ntimed_platform.h"
|
|
#include "ntimed.h"
|
|
#include "ntp.h"
|
|
#include "udp.h"
|
|
|
|
#define ARGSFORMAT "P=PARAM/K,T=TRACEFILE/K,SERVERS/M/A,SYNC/S,SAVE/S,SHOW/S,QUIET/S"
|
|
const char *vers = "\0$VER: ntimed-client" AMIGA_VERSION;
|
|
|
|
#define PARAM_CLIENT PARAM_INSTANCE
|
|
#define PARAM_TABLE_NAME client_param_table
|
|
#include "param_instance.h"
|
|
#undef PARAM_TABLE_NAME
|
|
#undef PARAM_CLIENT
|
|
|
|
extern int validtime;
|
|
struct RDArgs *rdargs = NULL;
|
|
struct ntp_peerset *nps = NULL;
|
|
struct todolist *tdl = NULL;
|
|
struct udp_socket *usc = NULL;
|
|
int savetime;
|
|
|
|
struct ntimedargs {
|
|
char *params;
|
|
char *tracefile;
|
|
char **servers;
|
|
long synchronize;
|
|
long save;
|
|
long show;
|
|
long quiet;
|
|
};
|
|
|
|
static void init_logic(struct ntimedargs *args);
|
|
static void clean_exit();
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int result = 0;
|
|
struct ntimedargs args = { NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE };
|
|
|
|
atexit(clean_exit);
|
|
if((result = amiga_open_libs()) != 0) {
|
|
exit(result);
|
|
}
|
|
|
|
rdargs = ReadArgs((STRPTR)ARGSFORMAT, (APTR)&args, NULL);
|
|
if (!rdargs)
|
|
{
|
|
PrintFault(IoErr(), (STRPTR)argv[0]);
|
|
exit(5);
|
|
}
|
|
|
|
if (!args.quiet) {
|
|
EnableDebug();
|
|
|
|
if (args.show) {
|
|
EnableTraceDebug();
|
|
}
|
|
}
|
|
|
|
if (args.tracefile) {
|
|
ArgTracefile(args.tracefile);
|
|
}
|
|
|
|
init_logic(&args);
|
|
|
|
if (args.synchronize) {
|
|
SetSyncTime(10);
|
|
}
|
|
|
|
savetime = (BOOL)args.save;
|
|
|
|
NTP_PeerSet_Poll(NULL, nps, usc, tdl);
|
|
(void)TODO_Run(NULL, tdl);
|
|
|
|
return result;
|
|
}
|
|
|
|
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 init_logic(struct ntimedargs *args)
|
|
{
|
|
struct ntp_peer *np;
|
|
struct combine_delta *cd;
|
|
|
|
int i;
|
|
int npeer = 0;
|
|
|
|
tdl = TODO_NewList();
|
|
Time_Amiga(tdl);
|
|
|
|
PLL_Init();
|
|
|
|
nps = NTP_PeerSet_New(NULL);
|
|
|
|
Param_Register(client_param_table);
|
|
NF_Init();
|
|
|
|
set_params(args->params);
|
|
|
|
for (i = 0; args->servers[i]; i++)
|
|
npeer += NTP_PeerSet_Add(NULL, nps, args->servers[i]);
|
|
|
|
if (npeer == 0) {
|
|
Put(NULL, OCX_DEBUG, "No NTP peers found\n");
|
|
exit(1);
|
|
}
|
|
|
|
Put(NULL, OCX_TRACE, "# NTIMED Format client 1.0\n");
|
|
Put(NULL, OCX_TRACE, "# Found %d peers\n", npeer);
|
|
Param_Report(NULL, OCX_TRACE);
|
|
|
|
usc = UdpTimedSocket(NULL);
|
|
if (usc == NULL) {
|
|
Put(NULL, OCX_DEBUG, "Could not open UDP socket\n");
|
|
exit(1);
|
|
}
|
|
|
|
cd = CD_New();
|
|
|
|
NTP_PeerSet_Foreach(np, nps) {
|
|
NF_New(np);
|
|
np->combiner = CD_AddSource(cd, np->hostname, np->ip);
|
|
}
|
|
|
|
if (args->synchronize) {
|
|
Put(NULL, OCX_DEBUG, "Synchronizing local clock to server(s)\n");
|
|
|
|
} else {
|
|
Put(NULL, OCX_DEBUG, "Synchronizing ...\n");
|
|
}
|
|
|
|
amiga_init_offset();
|
|
}
|
|
|
|
static void clean_exit()
|
|
{
|
|
if (savetime && validtime) {
|
|
Time_Amiga_Save();
|
|
Put(NULL, OCX_DEBUG, "Saved to real-time clock\n");
|
|
}
|
|
|
|
ArgTracefile(NULL);
|
|
|
|
if(usc) {
|
|
UDP_Socket_Destroy(usc);
|
|
usc = NULL;
|
|
}
|
|
|
|
if (rdargs) {
|
|
FreeArgs(rdargs);
|
|
rdargs = NULL;
|
|
}
|
|
|
|
amiga_close_libs();
|
|
freeall();
|
|
}
|