287 lines
6.4 KiB
C
287 lines
6.4 KiB
C
/*
|
|
* Copyright (c) 2015 Carsten Larsen
|
|
* 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>
|
|
|
|
#include <resources/battclock.h>
|
|
#include <clib/battclock_protos.h>
|
|
#include <clib/utility_protos.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
|
|
|
|
const char *vers = "\0$VER: ntimed 1.0a";
|
|
|
|
BOOL tracefile;
|
|
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 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
|
|
};
|
|
|
|
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.timezone) {
|
|
ArgTimezone(args.timezone);
|
|
}
|
|
|
|
if (args.tracefile) {
|
|
ArgTracefile(args.tracefile);
|
|
tracefile = 1;
|
|
}
|
|
|
|
init_logic(&args);
|
|
|
|
if (args.synchronize) {
|
|
SetSyncTime(10);
|
|
}
|
|
|
|
NTP_PeerSet_Poll(NULL, nps, usc, tdl);
|
|
(void)TODO_Run(NULL, tdl);
|
|
|
|
if (args.save) {
|
|
Time_Amiga_Save();
|
|
Put(NULL, OCX_DEBUG, "Saved time to hardware clock\n");
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) {
|
|
Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n");
|
|
exit(2);
|
|
}
|
|
|
|
#ifdef AROS
|
|
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()
|
|
{
|
|
ArgTracefile(NULL);
|
|
|
|
if(usc) {
|
|
UDP_Socket_Destroy(usc);
|
|
usc = NULL;
|
|
}
|
|
|
|
if (UtilityBase != NULL) {
|
|
CloseLibrary(UtilityBase);
|
|
UtilityBase = NULL;
|
|
}
|
|
|
|
if (SocketBase != NULL) {
|
|
CloseLibrary(SocketBase);
|
|
SocketBase = NULL;
|
|
}
|
|
|
|
if (rdargs) {
|
|
FreeArgs(rdargs);
|
|
rdargs = NULL;
|
|
}
|
|
|
|
freeall();
|
|
}
|
|
|