1
0
mirror of https://frontier.innolan.net/rainlance/amiga-ntimed.git synced 2025-11-22 23:02:32 +00:00
Files
amiga-ntimed/main_amiga.c
2015-03-13 15:56:07 +01:00

315 lines
7.0 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 "atimed.h"
#include "ntimed.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";
#define ARG_TEMPLATE "P=PARAM/K,T=TRACEFILE/K,Z=TIMEZONE/K,SERVERS/M,SYNC/S,SAVE/S,QUIET/S"
enum
{
ARG_PARAM,
ARG_TRACE,
ARG_ZONE,
ARG_SERVERS,
ARG_SYNC,
ARG_SAVE,
ARG_QUIET,
ARG_LAST
};
extern int errno;
BOOL started_from_wb;
struct Library *BattClockBase;
struct Library *UtilityBase = NULL;
struct Library *SocketBase = NULL;
struct RDArgs *rda = NULL;
struct ntp_peerset *nps = NULL;
struct todolist *tdl = NULL;
struct combine_delta *cd = NULL;
struct udp_socket *usc = NULL;
int saveclock = 0;
int tracefile = 0;
static void set_params(const char *params);
static void clean_exit();
static enum todo_e __match_proto__(todo_f)
sync_end(struct ocx *ocx, struct todolist *tdl, void *priv)
{
(void)tdl;
(void)priv;
Put(ocx, OCX_TRACE, "# Sync done\n");
return(TODO_FAIL);
}
int main(int argc, char **argv)
{
IPTR args[ARG_LAST];
const char **servers;
struct ntp_peer *np;
int i;
int sync = 0;
int npeer = 0;
atexit(clean_exit);
started_from_wb = (BOOL)argc;
args[ARG_PARAM] = (IPTR)NULL;
args[ARG_TRACE] = (IPTR)NULL;
args[ARG_ZONE] = (IPTR)NULL;
args[ARG_SERVERS] = (IPTR)NULL;
args[ARG_SYNC] = (IPTR)NULL;
args[ARG_SAVE] = (IPTR)NULL;
args[ARG_QUIET] = (IPTR)NULL;
rda = (struct RDArgs*)ReadArgs((CONST_STRPTR)ARG_TEMPLATE, (ARGSPTR)args, NULL);
if (!rda)
{
PrintFault(IoErr(), (CONST_STRPTR)argv[0]);
exit(1);
}
if(!(UtilityBase = (struct Library *)OpenLibrary((CONST_STRPTR)"utility.library", 33))) {
PrintFault(IoErr(), (CONST_STRPTR)argv[0]);
exit(1);
}
if (args[ARG_SYNC]) {
sync = 1;
}
if (!args[ARG_QUIET]) {
EnableDebug();
}
if (args[ARG_SAVE]) {
saveclock = 1;
BattClockBase = OpenResource((CONST_STRPTR)BATTCLOCKNAME);
}
#ifdef AROS
if(!(SocketBase = OpenLibrary((CONST_STRPTR)"bsdsocket.library", 4))) {
PrintFault(IoErr(), (CONST_STRPTR)argv[0]);
exit(1);
}
if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR)&errno,
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR)&h_errno, TAG_DONE)) {
PrintFault(IoErr(), (CONST_STRPTR)argv[0]);
exit(1);
}
#endif
// *** INITIALIZE LOGIC ***
tdl = TODO_NewList();
Time_Amiga(tdl);
PLL_Init();
nps = NTP_PeerSet_New(NULL);
Param_Register(client_param_table);
NF_Init();
if (args[ARG_ZONE]) {
ArgTimezone((const char*)args[ARG_ZONE]);
}
if (args[ARG_PARAM]) {
set_params((const char*)args[ARG_PARAM]);
}
if (args[ARG_TRACE]) {
ArgTracefile((const char*)args[ARG_TRACE]);
tracefile = 1;
}
servers = (const char**)args[ARG_SERVERS];
for (i = 0; servers[i]; i++)
npeer += NTP_PeerSet_Add(NULL, nps, servers[i]);
if (npeer == 0) {
Fail(NULL, 0, "No NTP peers found");
}
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) {
Fail(NULL, errno, "Could not open UDP socket");
}
cd = CD_New();
NTP_PeerSet_Foreach(np, nps) {
NF_New(np);
np->combiner = CD_AddSource(cd, np->hostname, np->ip);
}
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);
if (sync) {
TODO_ScheduleRel(tdl, sync_end, NULL, 15.0, 0, "End sync task");
Put(NULL, OCX_DEBUG, "Synchronizing local time to server(s)\n");
} else {
Put(NULL, OCX_DEBUG, "Synchronizing ...\n");
}
NTP_PeerSet_Poll(NULL, nps, usc, tdl);
(void)TODO_Run(NULL, tdl);
return 0;
}
static void set_params(const char *params)
{
char *token;
const char d[2] = ",";
token = strtok((char*)params, d);
while (token != NULL)
{
Param_Tweak(NULL, token);
token = strtok(NULL, d);
}
}
static void clean_exit()
{
if (saveclock) {
Time_Amiga_Save();
Put(NULL, OCX_DEBUG, "Saved time to hardware clock\n");
}
// Free peer set && peers && NTP filters
if(nps) {
}
// Free combine delta
if(cd) {
}
// Free todo list
if(tdl) {
}
// Free UDP socket
if(usc) {
UDP_Socket_Destroy(usc);
usc = NULL;
}
if(tracefile) {
ArgTracefile(NULL);
}
if (rda) {
FreeArgs(rda);
rda = NULL;
}
if (UtilityBase != NULL) {
CloseLibrary(UtilityBase);
}
#ifdef AROS
if (SocketBase != NULL) {
CloseLibrary(SocketBase);
}
#endif
}