amiga-ntimed/main_amiga_client.c

266 lines
6.3 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 "tz.h"
#include "ntp.h"
#include "udp.h"
#define ARGSFORMAT "P=PARAM/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,SYNC/S,SAVE/S,SHOW/S,QUIET/S"
const char *vers = "\0$VER: ntimed-poll 0.9 (15.04.2015)";
#define PARAM_CLIENT PARAM_INSTANCE
#define PARAM_TABLE_NAME client_param_table
#include "param_instance.h"
#undef PARAM_TABLE_NAME
#undef PARAM_CLIENT
void Time_Amiga(struct todolist *);
extern int validtime;
BOOL savetime;
BOOL started_from_wb;
struct Library *BattClockBase;
struct Library *UtilityBase = NULL;
struct Library *SocketBase = NULL;
struct RDArgs *rdargs = NULL;
struct ntp_peerset *nps = NULL;
struct todolist *tdl = NULL;
struct udp_socket *usc = NULL;
struct ntimedargs {
char *params;
char *tracefile;
char *timezone;
char **servers;
long synchronize;
long save;
long show;
long quiet;
};
static void init_libs(struct ntimedargs *args);
static void init_logic(struct ntimedargs *args);
static void clean_exit();
int main(int argc, char **argv)
{
struct ntimedargs args = {
NULL, NULL, NULL, NULL,
FALSE, FALSE, FALSE, FALSE
};
started_from_wb = (argc == 0);
atexit(clean_exit);
if(started_from_wb) {
args.servers = (char *[]) {
"0.pool.ntp.org",
"1.pool.ntp.org",
"2.pool.ntp.org",
"3.pool.ntp.org"
};
args.synchronize = TRUE;
args.save = TRUE;
args.quiet = TRUE;
} else {
rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL);
if (!rdargs)
{
PrintFault(IoErr(), (ARGPTR)argv[0]);
exit(2);
}
}
if (!args.quiet) {
EnableDebug();
if (args.show) {
EnableTraceDebug();
}
}
init_libs(&args);
if (args.timezone) {
ArgTimezone(args.timezone);
}
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 0;
}
static void init_libs(struct ntimedargs *args)
{
if(!(UtilityBase = (struct Library *)OpenLibrary(UTILLIB_NAME, UTILLIB_REV))) {
Put(NULL, OCX_DIAG, "Cannot open utility library.\n");
exit(2);
}
if (args->save) {
BattClockBase = OpenResource(BATTCLOCK_NAME);
}
#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 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;
Put(NULL, OCX_DEBUG, "Timezone set to %s (UTC%s%.2f) %s\n",
tza[tzid].abbr,
tza[tzid].offset < 0.0 ? "-" : "+",
fabs(tza[tzid].offset),
tza[tzid].name);
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");
}
}
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 (UtilityBase != NULL) {
CloseLibrary(UtilityBase);
UtilityBase = NULL;
}
#ifdef AROS
if (SocketBase != NULL) {
CloseLibrary(SocketBase);
SocketBase = NULL;
}
#endif
if (rdargs) {
FreeArgs(rdargs);
rdargs = NULL;
}
freeall();
}