diff --git a/compiler.h b/compiler.h index d439fc6..8471a1e 100644 --- a/compiler.h +++ b/compiler.h @@ -33,7 +33,6 @@ #include #include #include -#include /* * See also: @@ -51,10 +50,6 @@ typedef uint32_t register_t; #endif -#ifdef AROS -# define HAVE_WAITSELECT -#endif - #ifdef __GNUC__ # if (__GNUC__ == 2 && __GNUC_MINOR__ == 95) # include @@ -75,19 +70,12 @@ typedef uint32_t register_t; # endif #endif -#ifndef _ALIGNBYTES -# define _ALIGNBYTES (sizeof(register_t) - 1) -# define ALIGN(p) (((uintptr_t)(p) + _ALIGNBYTES) & ~_ALIGNBYTES) -#endif - #ifdef __GNUC__ # define HAVE_ASCTIME typedef uint16_t in_port_t; typedef unsigned int sa_family_t; #endif -// Is this needed since we dont use IPv6 ? - // General socket address holding structure, big enough to hold either // struct sockaddr_in or struct sockaddr_in6 data: struct sockaddr_storage { diff --git a/configure b/configure index 4d146a6..0b8bec4 100755 --- a/configure +++ b/configure @@ -172,12 +172,12 @@ if $VALID ; then echo 'all: ntimed ntimed-poll ntimed-sim ntimed-test' echo '' if [ -n "$1" ] && [ $1 = "AMIGA" ] ; then - echo 'CC = gcc' - echo "CFLAGS = -O2 -DAOS3 -DWITHTEST -I. -Wall -Werror" + echo 'CC = m68k-amigaos-gcc' + echo "CFLAGS = -O2 -noixemul -DAOS3 -DWITHTEST -DHAVE_LIBTZ -I. -Ilib -Wall -Werror" echo "LDLIBS = -Llib/aos3 -ltz -lm" elif [ -n "$1" ] && [ $1 = "AROS" ] ; then echo 'CC = gcc' - echo "CFLAGS = -O2 -DAROS -DWITHTEST -I. -Wall -Werror" + echo "CFLAGS = -O2 -DAROS -DWITHTEST -DHAVE_LIBTZ -I. -Ilib -Wall -Werror" echo "LDLIBS = -Llib/aros-i386 -ltz -lm" else echo "CFLAGS = -O2 -Wall -DWITHTEST -Werror" diff --git a/lib/aos3/libtz.a b/lib/aos3/libtz.a index 65d654d..139a697 100644 Binary files a/lib/aos3/libtz.a and b/lib/aos3/libtz.a differ diff --git a/lib/aros-i386/libtz.a b/lib/aros-i386/libtz.a index 84b6f5b..ac37c01 100644 Binary files a/lib/aros-i386/libtz.a and b/lib/aros-i386/libtz.a differ diff --git a/lib/inline_amitcp.h b/lib/inline_amitcp.h new file mode 100644 index 0000000..e011d69 --- /dev/null +++ b/lib/inline_amitcp.h @@ -0,0 +1,201 @@ +#ifndef _INLINE_AMITCP_H +#define _INLINE_AMITCP_H + +#ifndef __INLINE_MACROS_H +#include +#endif + +#ifndef AMITCP_BASE_NAME +#define AMITCP_BASE_NAME lss->lx_BsdSocketBase +#endif + +#define TCP_Accept(s, addr, addrlen) \ + LP3(0x30, LONG, TCP_Accept, LONG, s, d0, struct sockaddr *, addr, a0, int *, addrlen, a1, \ + , AMITCP_BASE_NAME) + +#define TCP_Bind(s, name, namelen) \ + LP3(0x24, LONG, TCP_Bind, LONG, s, d0, const struct sockaddr *, name, a0, LONG, namelen, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_CloseSocket(d) \ + LP1(0x78, LONG, TCP_CloseSocket, LONG, d, d0, \ + , AMITCP_BASE_NAME) + +#define TCP_Connect(s, name, namelen) \ + LP3(0x36, LONG, TCP_Connect, LONG, s, d0, const struct sockaddr *, name, a0, LONG, namelen, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_Dup2Socket(fd1, fd2) \ + LP2(0x108, LONG, TCP_Dup2Socket, LONG, fd1, d0, LONG, fd2, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_Errno() \ + LP0(0xa2, LONG, TCP_Errno, \ + , AMITCP_BASE_NAME) + +#define TCP_GetDTableSize() \ + LP0(0x8a, LONG, TCP_GetDTableSize, \ + , AMITCP_BASE_NAME) + +#define TCP_GetHostByAddr(addr, len, type) \ + LP3(0xd8, struct hostent *, TCP_GetHostByAddr, const UBYTE *, addr, a0, LONG, len, d0, LONG, type, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_GetHostByName(name) \ + LP1(0xd2, struct hostent *, TCP_GetHostByName, const UBYTE *, name, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_GetHostId() \ + LP0(0x120, ULONG, TCP_GetHostId, \ + , AMITCP_BASE_NAME) + +#define TCP_GetHostName(hostname, size) \ + LP2(0x11a, LONG, TCP_GetHostName, STRPTR, hostname, a0, LONG, size, d0, \ + , AMITCP_BASE_NAME) + +#define TCP_GetNetByAddr(net, type) \ + LP2(0xe4, struct netent *, TCP_GetNetByAddr, LONG, net, d0, LONG, type, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_GetNetByName(name) \ + LP1(0xde, struct netent *, TCP_GetNetByName, const UBYTE *, name, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_GetPeerName(s, hostname, namelen) \ + LP3(0x6c, LONG, TCP_GetPeerName, LONG, s, d0, struct sockaddr *, hostname, a0, int *, namelen, a1, \ + , AMITCP_BASE_NAME) + +#define TCP_GetProtoByName(name) \ + LP1(0xf6, struct protoent *, TCP_GetProtoByName, const UBYTE *, name, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_GetProtoByNumber(proto) \ + LP1(0xfc, struct protoent *, TCP_GetProtoByNumber, LONG, proto, d0, \ + , AMITCP_BASE_NAME) + +#define TCP_GetServByName(name, proto) \ + LP2(0xea, struct servent *, TCP_GetServByName, const UBYTE *, name, a0, const UBYTE *, proto, a1, \ + , AMITCP_BASE_NAME) + +#define TCP_GetServByPort(port, proto) \ + LP2(0xf0, struct servent *, TCP_GetServByPort, LONG, port, d0, const UBYTE *, proto, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_GetSockName(s, hostname, namelen) \ + LP3(0x66, LONG, TCP_GetSockName, LONG, s, d0, struct sockaddr *, hostname, a0, int *, namelen, a1, \ + , AMITCP_BASE_NAME) + +#define TCP_GetSockOpt(s, level, optname, optval, optlen) \ + LP5(0x60, LONG, TCP_GetSockOpt, LONG, s, d0, LONG, level, d1, LONG, optname, d2, void *, optval, a0, int *, optlen, a1, \ + , AMITCP_BASE_NAME) + +#define TCP_GetSocketEvents(eventmaskp) \ + LP1(0x12c, LONG, TCP_GetSocketEvents, ULONG *, eventmaskp, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_Inet_Addr(cp) \ + LP1(0xb4, ULONG, TCP_Inet_Addr, const UBYTE *, cp, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_Inet_LnaOf(in) \ + LP1(0xba, ULONG, TCP_Inet_LnaOf, LONG, in, d0, \ + , AMITCP_BASE_NAME) + +#define TCP_Inet_MakeAddr(net, host) \ + LP2(0xc6, ULONG, TCP_Inet_MakeAddr, ULONG, net, d0, ULONG, host, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_Inet_NetOf(in) \ + LP1(0xc0, ULONG, TCP_Inet_NetOf, LONG, in, d0, \ + , AMITCP_BASE_NAME) + +#define TCP_Inet_Network(cp) \ + LP1(0xcc, ULONG, TCP_Inet_Network, const UBYTE *, cp, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_Inet_NtoA(in) \ + LP1(0xae, char *, TCP_Inet_NtoA, ULONG, in, d0, \ + , AMITCP_BASE_NAME) + +#define TCP_IoctlSocket(d, request, argp) \ + LP3(0x72, LONG, TCP_IoctlSocket, LONG, d, d0, ULONG, request, d1, char *, argp, a0, \ + , AMITCP_BASE_NAME) + +#define TCP_Listen(s, backlog) \ + LP2(0x2a, LONG, TCP_Listen, LONG, s, d0, LONG, backlog, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_ObtainSocket(id, domain, type, protocol) \ + LP4(0x90, LONG, TCP_ObtainSocket, LONG, id, d0, LONG, domain, d1, LONG, type, d2, LONG, protocol, d3, \ + , AMITCP_BASE_NAME) + +#define TCP_Recv(s, buf, len, flags) \ + LP4(0x4e, LONG, TCP_Recv, LONG, s, d0, UBYTE *, buf, a0, LONG, len, d1, LONG, flags, d2, \ + , AMITCP_BASE_NAME) + +#define TCP_RecvFrom(s, buf, len, flags, from, fromlen) \ + LP6(0x48, LONG, TCP_RecvFrom, LONG, s, d0, UBYTE *, buf, a0, LONG, len, d1, LONG, flags, d2, struct sockaddr *, from, a1, int *, fromlen, a2, \ + , AMITCP_BASE_NAME) + +#define TCP_RecvMsg(s, msg, flags) \ + LP3(0x114, LONG, TCP_RecvMsg, LONG, s, d0, struct msghdr *, msg, a0, LONG, flags, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_ReleaseCopyOfSocket(fd, id) \ + LP2(0x9c, LONG, TCP_ReleaseCopyOfSocket, LONG, fd, d0, LONG, id, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_ReleaseSocket(fd, id) \ + LP2(0x96, LONG, TCP_ReleaseSocket, LONG, fd, d0, LONG, id, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_Send(s, msg, len, flags) \ + LP4(0x42, LONG, TCP_Send, LONG, s, d0, const UBYTE *, msg, a0, LONG, len, d1, LONG, flags, d2, \ + , AMITCP_BASE_NAME) + +#define TCP_SendMsg(s, msg, flags) \ + LP3(0x10e, LONG, TCP_SendMsg, LONG, s, d0, const struct msghdr *, msg, a0, LONG, flags, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_SendTo(s, msg, len, flags, to, tolen) \ + LP6(0x3c, LONG, TCP_SendTo, LONG, s, d0, const UBYTE *, msg, a0, LONG, len, d1, LONG, flags, d2, const struct sockaddr *, to, a1, LONG, tolen, d3, \ + , AMITCP_BASE_NAME) + +#define TCP_SetErrnoPtr(errno_p, size) \ + LP2(0xa8, LONG, TCP_SetErrnoPtr, void *, errno_p, a0, LONG, size, d0, \ + , AMITCP_BASE_NAME) + +#define TCP_SetSockOpt(s, level, optname, optval, optlen) \ + LP5(0x5a, LONG, TCP_SetSockOpt, LONG, s, d0, LONG, level, d1, LONG, optname, d2, const void *, optval, a0, LONG, optlen, d3, \ + , AMITCP_BASE_NAME) + +#define TCP_SetSocketSignals(SIGINTR, SIGIO, SIGURG) \ + LP3NR(0x84, TCP_SetSocketSignals, ULONG, SIGINTR, d0, ULONG, SIGIO, d1, ULONG, SIGURG, d2, \ + , AMITCP_BASE_NAME) + +#define TCP_ShutDown(s, how) \ + LP2(0x54, LONG, TCP_ShutDown, LONG, s, d0, LONG, how, d1, \ + , AMITCP_BASE_NAME) + +#define TCP_Socket(domain, type, protocol) \ + LP3(0x1e, LONG, TCP_Socket, LONG, domain, d0, LONG, type, d1, LONG, protocol, d2, \ + , AMITCP_BASE_NAME) + +#define TCP_SocketBaseTagList(taglist) \ + LP1(0x126, LONG, TCP_SocketBaseTagList, struct TagItem *, taglist, a0, \ + , AMITCP_BASE_NAME) + +#ifndef NO_INLINE_STDARG +#define TCP_SocketBaseTags(tags...) \ + ({ULONG _tags[] = { tags }; TCP_SocketBaseTagList((struct TagItem *)_tags);}) +#endif + +#define TCP_SyslogA(level, format, ap) \ + LP3NR(0x102, TCP_SyslogA, ULONG, level, d0, const char *, format, a0, va_list, ap, a1, \ + , AMITCP_BASE_NAME) + +#define TCP_WaitSelect(nfds, readfds, writefds, execptfds, timeout, maskp) \ + LP6(0x7e, LONG, TCP_WaitSelect, LONG, nfds, d0, fd_set *, readfds, a0, fd_set *, writefds, a1, fd_set *, execptfds, a2, struct timeval *, timeout, a3, ULONG *, maskp, d1, \ + , AMITCP_BASE_NAME) + +#endif diff --git a/lib/socket_amitcp.h b/lib/socket_amitcp.h new file mode 100644 index 0000000..279cf7c --- /dev/null +++ b/lib/socket_amitcp.h @@ -0,0 +1,114 @@ +/* + * 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. + * + */ + +#ifndef _SOCKET_AMITCP_H +#define _SOCKET_AMITCP_H +//--------------------------------------------------------------------------// +#define AMITCP_BASE_NAME SocketBase +#include "inline_amitcp.h" +//--------------------------------------------------------------------------// +#define socket Socket +#define bind Bind +#define listen Listen +#define accept Accept +#define connect Connect +#define send Send +#define sendto SendTo +#define sendmsg SendMsg +#define recv Recv +#define recvfrom RecvFrom +#define recvmsg RecvMsg +#define shutdown Shutdown +#define setsockopt SetSockOpt +#define getsockopt GetSockOpt +#define getsockname GetSockName +#define getpeername GetPeerName +#define inet_addr Inet_Addr +#define inet_network Inet_Network +#define inet_ntoa Inet_NtoA +#define gethostbyname GetHostByName +#define gethostbyaddr GetHostByAddr +#define getnetbyname GetNetByName +#define getnetbyaddr GetNetByAddr +#define getservbyname GetServByName +#define getservbyport GetServByPort +#define getprotobyname GetProtoByName +#define getprotobynumber GetProtoByNumber +#define getdtablesize GetDTableSize +#define gethostname GetHostName +#define gethostid GetHostId +#define vsyslog SyslogA +#define syslog Syslog +//--------------------------------------------------------------------------// +#define Accept TCP_Accept +#define Bind TCP_Bind +#define CloseSocket TCP_CloseSocket +#define Connect TCP_Connect +#define Dup2Socket TCP_Dup2Socket +#define Errno TCP_Errno +#define GetDTableSize TCP_GetDTableSize +#define GetHostByAddr TCP_GetHostByAddr +#define GetHostByName TCP_GetHostByName +#define GetHostId TCP_GetHostId +#define GetHostName TCP_GetHostName +#define GetNetByAddr TCP_GetNetByAddr +#define GetNetByName TCP_GetNetByName +#define GetPeerName TCP_GetPeerName +#define GetProtoByName TCP_GetProtoByName +#define GetProtoByNumber TCP_GetProtoByNumber +#define GetServByName TCP_GetServByName +#define GetServByPort TCP_GetServByPort +#define GetSockName TCP_GetSockName +#define GetSockOpt TCP_GetSockOpt +#define GetSocketEvents TCP_GetSocketEvents +#define Inet_Addr TCP_Inet_Addr +#define Inet_LnaOf TCP_Inet_LnaOf +#define Inet_MakeAddr TCP_Inet_MakeAddr +#define Inet_NetOf TCP_Inet_NetOf +#define Inet_Network TCP_Inet_Network +#define Inet_NtoA TCP_Inet_NtoA +#define IoctlSocket TCP_IoctlSocket +#define Listen TCP_Listen +#define ObtainSocket TCP_ObtainSocket +#define Recv TCP_Recv +#define RecvFrom TCP_RecvFrom +#define RecvMsg TCP_RecvMsg +#define ReleaseCopyOfSocket TCP_ReleaseCopyOfSocket +#define ReleaseSocket TCP_ReleaseSocket +#define Send TCP_Send +#define SendMsg TCP_SendMsg +#define SendTo TCP_SendTo +#define SetErrnoPtr TCP_SetErrnoPtr +#define SetSockOpt TCP_SetSockOpt +#define SetSocketSignals TCP_SetSocketSignals +#define ShutDown TCP_ShutDown +#define Socket TCP_Socket +#define SocketBaseTagList TCP_SocketBaseTagList +#define SocketBaseTags TCP_SocketBaseTags +#define SyslogA TCP_SyslogA +#define WaitSelect TCP_WaitSelect +//--------------------------------------------------------------------------// +#endif diff --git a/lib/tz.h b/lib/tz.h index ade7422..46a21d3 100644 --- a/lib/tz.h +++ b/lib/tz.h @@ -43,9 +43,16 @@ * ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/include/time.h */ +#ifndef _TZ_HEADER +#define _TZ_HEADER + extern char *tzname[2]; -struct tz_tm { +#define TZVARIABLE "timezone.prefs" +#define TZDIR "LOCALE:Zoneinfo" +#define TZDEFAULT "localtime" + +struct tm { int tm_sec; /* seconds after the minute [0-61] */ int tm_min; /* minutes after the hour [0-59] */ int tm_hour; /* hours since midnight [0-23] */ @@ -59,42 +66,31 @@ struct tz_tm { char *tm_zone; /* timezone abbreviation */ }; -typedef long tz_time_t; +#ifndef HAVE_TIME_T +typedef long time_t; +#endif + typedef struct state *timezone_t; -void tz_tzset(void); -void tz_free(const timezone_t); -timezone_t tz_alloc(const char *); -tz_time_t tz_time(tz_time_t *); -tz_time_t tz_mktime_z(const timezone_t, struct tz_tm *); -tz_time_t tz_mktime(struct tz_tm *); -struct tz_tm *tz_localtime_rz(const timezone_t, const tz_time_t *, struct tz_tm *); -struct tz_tm *tz_localtime_r(const tz_time_t *, struct tz_tm *); -struct tz_tm *tz_localtime(const tz_time_t *); -struct tz_tm *tz_gmtime_r(const tz_time_t *, struct tz_tm *); -struct tz_tm *tz_gmtime(const tz_time_t *); -char *tz_ctime_r(const tz_time_t *, char *); -char *tz_ctime(const tz_time_t *); -char *asctime_r(const struct tz_tm *, char *); -double tz_difftime(tz_time_t, tz_time_t); +void tzset(void); +void tzfree(const timezone_t); +timezone_t tzalloc(const char *); +time_t time(time_t *); +time_t mktime_z(const timezone_t, struct tm *); +time_t mktime(struct tm *); +struct tm *localtime_rz(const timezone_t, const time_t *, struct tm *); +struct tm *localtime_r(const time_t *, struct tm *); +struct tm *localtime(const time_t *); +struct tm *gmtime_r(const time_t *, struct tm *); +struct tm *gmtime(const time_t *); +char *ctime_r(const time_t *, char *); +char *ctime(const time_t *); +double difftime(time_t, time_t); +time_t time(time_t *x); #ifndef HAVE_ASCTIME -char *asctime(const struct tz_tm *); +char *asctime_r(const struct tm *, char *); +char *asctime(const struct tm *); #endif -tz_time_t time(tz_time_t *x); - -#ifdef HAVENOSYSTIME -#include -tz_time_t time(tz_time_t *x) -{ - struct timeval tv; - GetSysTime(&tv); - - if (x) { - *x = (time_t)tv.tv_secs; - } - - return (time_t)tv.tv_secs; -} #endif diff --git a/main_amiga_client.c b/main_amiga_client.c index 966397c..0ab5067 100644 --- a/main_amiga_client.c +++ b/main_amiga_client.c @@ -25,15 +25,13 @@ * */ -//#include - #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 1.01 (18.04.2015)"; +const char *vers = "\0$VER: ntimed-client" AMIGA_VERSION; #define PARAM_CLIENT PARAM_INSTANCE #define PARAM_TABLE_NAME client_param_table @@ -42,12 +40,11 @@ const char *vers = "\0$VER: ntimed-client 1.01 (18.04.2015)"; #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; -int started_from_wb; -struct RDArgs *rdargs = NULL; -struct ntp_peerset *nps = NULL; -struct todolist *tdl = NULL; -struct udp_socket *usc = NULL; struct ntimedargs { char *params; @@ -64,30 +61,19 @@ static void clean_exit(); int main(int argc, char **argv) { - struct ntimedargs args = { - NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE - }; + int result = 0; + struct ntimedargs args = { NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE }; + + atexit(clean_exit); + if((result = amiga_open_libs()) != 0) { + exit(result); + } - 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(5); - } + rdargs = ReadArgs((STRPTR)ARGSFORMAT, (APTR)&args, NULL); + if (!rdargs) + { + PrintFault(IoErr(), (STRPTR)argv[0]); + exit(5); } if (!args.quiet) { @@ -98,8 +84,6 @@ int main(int argc, char **argv) } } - amiga_open_libs(); - if (args.tracefile) { ArgTracefile(args.tracefile); } @@ -115,7 +99,7 @@ int main(int argc, char **argv) NTP_PeerSet_Poll(NULL, nps, usc, tdl); (void)TODO_Run(NULL, tdl); - return 0; + return result; } static void set_params(char *params) @@ -186,6 +170,8 @@ static void init_logic(struct ntimedargs *args) } else { Put(NULL, OCX_DEBUG, "Synchronizing ...\n"); } + + amiga_init_offset(); } static void clean_exit() diff --git a/main_amiga_poll.c b/main_amiga_poll.c index cd50826..311dc6c 100644 --- a/main_amiga_poll.c +++ b/main_amiga_poll.c @@ -31,13 +31,12 @@ #include "udp.h" #define ARGSFORMAT "D=DURATION/N,M=MONITOR/K,T=TRACEFILE/K,SERVERS/M/A,SHOW/S,QUIET/S" -const char *vers = "\0$VER: ntimed-poll 1.01 (18.04.2015)"; +const char *vers = "\0$VER: ntimed-poll" AMIGA_VERSION; -BOOL started_from_wb; -struct RDArgs *rdargs = NULL; -struct udp_socket *usc = NULL; -struct ntp_peerset *npl = NULL; -struct todolist *tdl = NULL; +struct RDArgs *rdargs = NULL; +struct udp_socket *usc = NULL; +struct ntp_peerset *npl = NULL; +struct todolist *tdl = NULL; struct ntimedargs { long *duration; @@ -53,16 +52,18 @@ static void clean_exit(); int main(int argc, char **argv) { + int result = 0; struct ntimedargs args = { NULL, NULL, NULL, NULL, FALSE, FALSE }; - started_from_wb = (BOOL)argc; - atexit(clean_exit); + if((result = amiga_open_libs()) != 0) { + exit(result); + } - rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + rdargs = ReadArgs((STRPTR)ARGSFORMAT, (APTR)&args, NULL); if (!rdargs) { - PrintFault(IoErr(), (ARGPTR)argv[0]); + PrintFault(IoErr(), (STRPTR)argv[0]); exit(5); } @@ -74,8 +75,6 @@ int main(int argc, char **argv) } } - amiga_open_libs(); - if (args.tracefile) { ArgTracefile(args.tracefile); } @@ -85,7 +84,7 @@ int main(int argc, char **argv) NTP_PeerSet_Poll(NULL, npl, usc, tdl); (void)TODO_Run(NULL, tdl); - return 0; + return result; } static void @@ -191,6 +190,8 @@ static void init_logic(struct ntimedargs *args) if (mon != NULL) TODO_ScheduleRel(tdl, mps_mon, mon, 0, 32, "Monitor"); + + amiga_init_offset(); } static void clean_exit() diff --git a/main_amiga_sim.c b/main_amiga_sim.c index 55e5894..335ee88 100644 --- a/main_amiga_sim.c +++ b/main_amiga_sim.c @@ -31,7 +31,7 @@ #include "udp.h" #define ARGSFORMAT "P=PARAM/K,B=BUMP/K,T=TRACEFILE/K,SIMFILE/A,QUIET/S" -const char *vers = "\0$VER: ntimed-sim 1.01 (18.04.2015)"; +const char *vers = "\0$VER: ntimed-sim" AMIGA_VERSION; #define PARAM_CLIENT PARAM_INSTANCE #define PARAM_TABLE_NAME client_param_table @@ -64,9 +64,8 @@ SimFile_Open(struct ocx *ocx, const char *fn, struct todolist *tdl, /**********************************************************************/ -BOOL started_from_wb; -struct RDArgs *rdargs = NULL; -struct todolist *tdl = NULL; +struct RDArgs *rdargs = NULL; +struct todolist *tdl = NULL; struct ntimedargs { char *params; @@ -82,24 +81,25 @@ static void clean_exit(); int main(int argc, char **argv) { + int result = 0; struct ntimedargs args = { NULL, NULL, NULL, NULL, FALSE }; - started_from_wb = (BOOL)argc; atexit(clean_exit); + if((result = amiga_open_libs()) != 0) { + exit(result); + } - rdargs = ReadArgs(ARGS_FORMAT, (APTR)&args, NULL); + rdargs = ReadArgs((STRPTR)ARGSFORMAT, (APTR)&args, NULL); if (!rdargs) { - PrintFault(IoErr(), (ARGPTR)argv[0]); - exit(2); + PrintFault(IoErr(), (STRPTR)argv[0]); + exit(5); } if (!args.quiet) { EnableDebug(); } - amiga_close_libs(); - if (args.tracefile) { ArgTracefile(args.tracefile); } @@ -108,7 +108,7 @@ int main(int argc, char **argv) (void)TODO_Run(NULL, tdl); - return 0; + return result; } static void init_logic(struct ntimedargs *args) @@ -153,6 +153,8 @@ static void init_logic(struct ntimedargs *args) NF_New(np); np->combiner = CD_AddSource(cd, np->hostname, np->ip); } + + amiga_init_offset(); } /**********************************************************************/ diff --git a/main_amiga_test.c b/main_amiga_test.c index 959cd2b..9235fa0 100644 --- a/main_amiga_test.c +++ b/main_amiga_test.c @@ -29,8 +29,7 @@ #include "ntimed.h" #include "ntp.h" -const char *vers = "\0$VER: ntimed-test 1.01 (18.04.2015)"; -BOOL started_from_wb; +const char *vers = "\0$VER: ntimed-test" AMIGA_VERSION; static void clean_exit() { @@ -40,16 +39,17 @@ static void clean_exit() int main(int argc, char * const *argv) { - started_from_wb = (BOOL)argc; - - (void)argc; - (void)argv; + int result = 0; atexit(clean_exit); - amiga_open_libs(); + if((result = amiga_open_libs()) != 0) { + exit(result); + } + EnableDebug(); + amiga_init_offset(); Time_Amiga_Passive(); TS_RunTest(NULL); - return (0); + return result; } diff --git a/net_getaddrinfo.c b/net_getaddrinfo.c index 1f4847d..eb91d32 100644 --- a/net_getaddrinfo.c +++ b/net_getaddrinfo.c @@ -59,6 +59,7 @@ #include #include +#include "ntimed_platform.h" #include "ntimed.h" #include "net_getaddrinfo.h" @@ -504,7 +505,11 @@ int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *node, socklen_ result = EAI_NONAME; goto end; } +#ifndef AOS3 ntoa_address = inet_ntoa(sa_in->sin_addr); +#else + ntoa_address = inet_ntoa((ULONG)&sa_in->sin_addr); +#endif if (nodelen <= strlen(ntoa_address)) { result = EAI_OVERFLOW; goto end; diff --git a/ntimed_platform.h b/ntimed_platform.h index 45eaf0c..7b3eb4c 100644 --- a/ntimed_platform.h +++ b/ntimed_platform.h @@ -24,86 +24,84 @@ * */ -#ifndef NTIMED_PLATFROM_H -#define NTIMED_PLATFROM_H - +#ifndef NTIMED_PLATFORM_H +#define NTIMED_PLATFORM_H +//--------------------------------------------------------------------------// #if defined(AROS) || defined(AOS3) +# define AMIGA_VERSION " 1.02 (10.07.2015)" # ifndef AMIGA # define AMIGA # endif #endif - +//--------------------------------------------------------------------------// +#ifdef HAVE_LIBTZ +# define _TIME_H_ +# define HAVE_TIME_T +# include +# include "tz.h" +#endif +#define HAVE_WAITSELECT +//--------------------------------------------------------------------------// #ifdef AMIGA +# include +# include +# include +# include # include # include # include # include # include #endif - -#ifdef AROS -# ifdef Debug -# undef Debug -# endif -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# ifdef Debug -# undef Debug -# endif -#endif - +//--------------------------------------------------------------------------// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "mem.h" - +//--------------------------------------------------------------------------// +#define OPEN_ERROR "Cannot open %s.\n" +#define OPEN_VER_ERROR "Cannot open %s (%ld.0)\n" +#define DOSLIB_NAME "dos.library" +#define DOSLIB_REV 37L +#define UTILLIB_NAME "utility.library" +#define UTILLIB_REV 37L +#define LOCALELIB_NAME "locale.library" +#define LOCALELIB_REV 37L +#define BSDLIB_NAME "bsdsocket.library" +#define BSDLIB_REV 03L +#define TIMER_NAME TIMERNAME +#define BATTCLOCK_NAME BATTCLOCKNAME +#ifdef TZVARIABLE +# define TZVARIABLE_NAME TZVARIABLE +#endif +//--------------------------------------------------------------------------// #ifdef AOS3 -# define ARGPTR STRPTR -# define ARGS_FORMAT ARGSFORMAT -# define BSDLIB_NAME "bsdsocket.library" -# define BSDLIB_REV 04L -# define UTILLIB_NAME "utility.library" -# define UTILLIB_REV 33L -# define LOCALELIB_NAME "locale.library" -# define LOCALELIB_REV 33L -# define DOSLIB_NAME "dos.library" -# define DOSLIB_REV 36L -# define TIMER_NAME TIMERNAME -# define BATTCLOCK_NAME BATTCLOCKNAME +# define REQ_ERROR "Requires Kickstart 2.04 (37.175) or later.\n" +# include "socket_amitcp.h" +# include #endif - #ifdef AROS -# define ARGPTR CONST_STRPTR -# define ARGS_FORMAT (CONST_STRPTR)ARGSFORMAT -# define BSDLIB_NAME (CONST_STRPTR)"bsdsocket.library" -# define BSDLIB_REV 04L -# define UTILLIB_NAME (CONST_STRPTR)"utility.library" -# define UTILLIB_REV 33L -# define LOCALELIB_NAME (CONST_STRPTR)"locale.library" -# define LOCALELIB_REV 33L -# define DOSLIB_NAME (CONST_STRPTR)"dos.library" -# define DOSLIB_REV 36L -# define TIMER_NAME (CONST_STRPTR)TIMERNAME -# define BATTCLOCK_NAME (CONST_STRPTR)BATTCLOCKNAME -# define APRT IPRT +# define REQ_ERROR "Requires a newer version of AROS.\n" +# include +# include +# define APRT IPRT #endif - +//--------------------------------------------------------------------------// #ifndef AMIGA struct timeval { ULONG tv_secs; ULONG tv_micro; }; #endif - +//--------------------------------------------------------------------------// #ifndef HAVE_POLL #define POLLIN 0x0001 struct pollfd { @@ -114,32 +112,46 @@ struct pollfd { typedef unsigned int nfds_t; int poll(struct pollfd *, nfds_t, int); #endif - -#ifdef AMIGA -void amiga_open_libs(); -void amiga_close_libs(); -void amiga_save_time(void); -void amiga_sync_time(int seconds); -void EnableDebug(); -void EnableTraceDebug(); -#endif - +//--------------------------------------------------------------------------// #ifdef AROS # define settimeofday ntimed_settimeofday # define gettimeofday ntimed_gettimeofday int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); #endif - +//--------------------------------------------------------------------------// #ifdef AMIGA # define Time_Amiga Time_Unix # define Time_Amiga_Passive Time_Unix_Passive # define Time_Amiga_Save amiga_save_time # define SetSyncTime amiga_sync_time #endif - -#ifdef Debug -# undef Debug +//--------------------------------------------------------------------------// +#ifndef _ALIGNBYTES +# define _ALIGNBYTES (sizeof(register_t) - 1) #endif - +#ifndef ALIGN +# define ALIGN(p) (((uintptr_t)(p) + _ALIGNBYTES) & ~_ALIGNBYTES) +#endif +#ifndef SO_TIMESTAMPING +# define SO_TIMESTAMPING 37 +#endif +#ifndef SO_TIMESTAMPNS +# define SO_TIMESTAMPNS 35 +#endif +//--------------------------------------------------------------------------// +int amiga_open_libs(); +void amiga_close_libs(); +void amiga_init_offset(); +void amiga_save_time(); +void amiga_sync_time(int seconds); +void EnableDebug(); +void EnableTraceDebug(); +//--------------------------------------------------------------------------// +extern struct Library *BattClockBase; +extern struct Library *DOSBase; +extern struct Library *UtilityBase; +extern struct Library *LocaleBase; +extern struct Library *SocketBase; +//--------------------------------------------------------------------------// #endif diff --git a/ocx_stdio.c b/ocx_stdio.c index 63eea9e..838610d 100644 --- a/ocx_stdio.c +++ b/ocx_stdio.c @@ -68,7 +68,6 @@ #include "ntimed_platform.h" #include "ntimed.h" -extern BOOL started_from_wb; int repeat_trace = 0; static FILE *debugfile = NULL; @@ -134,16 +133,15 @@ Put(struct ocx *ocx, enum ocx_chan chan, const char *fmt, ...) va_start(ap, fmt); dst = getdst(chan); - if (dst != NULL && !(started_from_wb && dst == stdout)) { + if (dst != NULL && dst == stdout) { (void)vfprintf(dst, fmt, ap); fflush(dst); } dst = getdst(OCX_DEBUG); if ( - dst != NULL && - !(started_from_wb && dst == stdout) && - (repeat_trace && chan == OCX_TRACE)) { + dst != NULL && dst == stdout && + repeat_trace && chan == OCX_TRACE) { (void)vfprintf(dst, fmt, ap); fflush(dst); } diff --git a/time_amiga.c b/time_amiga.c index 49ea106..d800d05 100644 --- a/time_amiga.c +++ b/time_amiga.c @@ -26,7 +26,6 @@ #include "ntimed_platform.h" #include "ntimed.h" -#include "lib/tz.h" #ifdef Debug #undef Debug @@ -46,13 +45,11 @@ #include #include -#ifdef Debug -#undef Debug -#endif - // 2922 is the number of days between 1.1.1970 and 1.1.1978 (2 leap years and 6 normal) #define AMIGAOFFSET 2922 * 24 * 60 * 60 +#define ENVSIZE 256 + int create_timer(); int delete_timer(); void adjust_timeval(struct timeval *tv, double offset); @@ -70,61 +67,70 @@ int validtime = 0; int limited_sync = 0; long offset = 0; -void amiga_open_libs() +#ifdef AOS3 +int errno; +int h_errno; +#endif + +void amiga_open_error(char *name) { - if(!(DOSBase = OpenLibrary(DOSLIB_NAME, DOSLIB_REV))) { - Put(NULL, OCX_DIAG, "Cannot open dos library.\n"); - exit(10); + Put(NULL, OCX_DIAG, OPEN_ERROR, "Locale"); + FPrintf(Output(), (STRPTR)OPEN_ERROR, name); +} + +void amiga_open_lib_error(char *name, int version) +{ + Put(NULL, OCX_DIAG, OPEN_VER_ERROR, name, version); + Put(NULL, OCX_DIAG, REQ_ERROR); + + FPrintf(Output(), (STRPTR)OPEN_VER_ERROR, name, version); + FPrintf(Output(), (STRPTR)REQ_ERROR, NULL); +} + +int amiga_open_libs() +{ + if(!(DOSBase = OpenLibrary((STRPTR)DOSLIB_NAME, DOSLIB_REV))) { + amiga_open_lib_error(DOSLIB_NAME, DOSLIB_REV); + return 5; } - if(!(LocaleBase = OpenLibrary(LOCALELIB_NAME, LOCALELIB_REV))) { - Put(NULL, OCX_DIAG, "Cannot open locale library.\n"); - exit(10); + if(!(LocaleBase = OpenLibrary((STRPTR)LOCALELIB_NAME, LOCALELIB_REV))) { + amiga_open_lib_error(LOCALELIB_NAME, LOCALELIB_REV); + return 5; } if(!(locale = OpenLocale(NULL))) { - Put(NULL, OCX_DIAG, "Cannot open current locale.\n"); - exit(10); + amiga_open_error("Locale"); + return 10; } - if(!(UtilityBase = OpenLibrary(UTILLIB_NAME, UTILLIB_REV))) { - Put(NULL, OCX_DIAG, "Cannot open utility library.\n"); - exit(10); + if(!(UtilityBase = OpenLibrary((STRPTR)UTILLIB_NAME, UTILLIB_REV))) { + amiga_open_lib_error(UTILLIB_NAME, UTILLIB_REV); + return 5; } - BattClockBase = OpenResource(BATTCLOCK_NAME); - -#ifdef AROS - if(!(SocketBase = OpenLibrary(BSDLIB_NAME, BSDLIB_REV))) { - Put(NULL, OCX_DIAG, "No TCP/IP Stack running.\n"); - exit(10); + if(!(BattClockBase = OpenResource((STRPTR)BATTCLOCK_NAME))) { + amiga_open_error(BATTCLOCK_NAME); + return 10; } - 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(10); + if(!(SocketBase = OpenLibrary((STRPTR)BSDLIB_NAME, BSDLIB_REV))) { + amiga_open_lib_error(BSDLIB_NAME, BSDLIB_REV); + return 5; + } + + if(SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (int)&errno, + SBTM_SETVAL(SBTC_HERRNOLONGPTR), (int)&h_errno, TAG_DONE)) { + amiga_open_error(BSDLIB_NAME); + return 10; } -#endif if (create_timer() != 0) { - Put(NULL, OCX_DIAG, "Cannot open timer device.\n"); - exit(10); + amiga_open_error(TIMER_NAME); + return 10; } -#ifdef USETZDATABASE - if (getenv("TZ")) { - tz_time_t rawtime; - time(&rawtime); - struct tz_tm *localtm = tz_localtime(&rawtime); - offset = -localtm->tm_gmtoff + AMIGAOFFSET; - Put(NULL, OCX_DEBUG, "Using TZ GMT offset %i seconds\n", localtm->tm_gmtoff); - } else -#endif - { - offset = -locale->loc_GMTOffset * 60 + AMIGAOFFSET; - Put(NULL, OCX_DEBUG, "Using locale GMT offset %i minutes\n", locale->loc_GMTOffset ); - } + return 0; } void amiga_close_libs() @@ -153,16 +159,69 @@ void amiga_close_libs() DOSBase = NULL; } -#ifdef AROS if (SocketBase != NULL) { CloseLibrary(SocketBase); SocketBase = NULL; } -#endif } /**********************************************************************/ +#ifdef HAVE_LIBTZ +int amiga_get_gmtoffset_attime(time_t locale_time) +{ + struct tm tm; + timezone_t tz; + char *tzvar; + int gmtoffset = 0; + tzvar = AllocVec(ENVSIZE, MEMF_ANY | MEMF_CLEAR); + + GetVar( + (STRPTR)TZVARIABLE_NAME, + (STRPTR)tzvar, + ENVSIZE - 1, + GVF_GLOBAL_ONLY + ); + + tz = tzalloc(tzvar); + + if (tz) { + localtime_rz(tz, &locale_time, &tm); + tzfree(tz); + gmtoffset = tm.tm_gmtoff; + } else { + gmtoffset = locale->loc_GMTOffset * 60; + } + + Put(NULL, OCX_DEBUG, + "Current time zone offset: UTC/GMT %.1f hour\n", + (float)gmtoffset / 60.0 / 60.0); + + FreeVec(tzvar); + return gmtoffset; +} + +int amiga_get_gmtoffset() +{ + time_t now = time(NULL); + return amiga_get_gmtoffset_attime(now); +} +#endif +/**********************************************************************/ +void amiga_init_offset() +{ + int gmt_offset; + +#ifdef HAVE_LIBTZ + gmt_offset = amiga_get_gmtoffset(); +#else + gmt_offset = locale->loc_GMTOffset * 60 * 60; + +#endif + + offset = -gmt_offset + AMIGAOFFSET; +} +/**********************************************************************/ int create_timer() { LONG error; @@ -177,7 +236,11 @@ int create_timer() return 1; } - error = OpenDevice(TIMER_NAME, UNIT_MICROHZ, (struct IORequest*)request, 0L); + error = OpenDevice( + (STRPTR)TIMER_NAME, UNIT_MICROHZ, + (struct IORequest*)request, 0L + ); + if (error != 0) { delete_timer(request); @@ -205,25 +268,7 @@ int delete_timer() return 0; } - /**********************************************************************/ - -#ifdef USETZDATABASE -tz_time_t time(tz_time_t *x) -{ - struct timeval tv; - GetSysTime(&tv); - - if (x) { - *x = (time_t)tv.tv_secs + AMIGAOFFSET; - } - - return (time_t)tv.tv_secs + AMIGAOFFSET; -} -#endif - -/**********************************************************************/ - int gettimeofday(struct timeval *tv, struct timezone *tz) { #ifdef USESYSTIME @@ -247,18 +292,19 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz) validtime = 1; return 0; } - /**********************************************************************/ - void amiga_save_time(void) { struct timeval tv; - ResetBattClock(); - gettimeofday(&tv, NULL); - WriteBattClock((long)tv.tv_secs - offset); -} + int gmt_offset = 0; -/**********************************************************************/ +#ifdef HAVE_LIBTZ + gmt_offset = amiga_get_gmtoffset(); +#endif + + gettimeofday(&tv, NULL); + WriteBattClock((long)tv.tv_secs - offset - gmt_offset); +} void amiga_sync_time(int seconds) { @@ -267,8 +313,6 @@ void amiga_sync_time(int seconds) sync_time.tv_micro = 0; } -/**********************************************************************/ - int amiga_sleep_time(double dur) { ULONG sigs, timersig; @@ -300,9 +344,7 @@ int amiga_sleep_time(double dur) return 0; } - /**********************************************************************/ - void adjust_timeval(struct timeval *tv, double offset) { double d1, d2; @@ -325,7 +367,6 @@ void adjust_timeval(struct timeval *tv, double offset) tv->tv_secs = secs; tv->tv_micro = micro; } - /**********************************************************************/ #ifndef HAVE_POLL int poll(struct pollfd *pfds, nfds_t nfds, int timeout) @@ -387,3 +428,4 @@ int poll(struct pollfd *pfds, nfds_t nfds, int timeout) return ret; } #endif +/**********************************************************************/ diff --git a/time_stuff.c b/time_stuff.c index 6ee1f6e..0a37c46 100644 --- a/time_stuff.c +++ b/time_stuff.c @@ -155,7 +155,8 @@ TS_Format(char *buf, size_t len, const struct timestamp *ts) x += 1; } // i = snprintf(buf, len, "%jd.%09jd", (intmax_t)x, (intmax_t)y); - i = snprintf(buf, len, "%lld.%09lld", x, y); +// i = snprintf(buf, len, "%lld.%09lld", x, y); + i = snprintf(buf, len, "%d.%d", (int32_t)x, (int32_t)y); assert(i < (int)len); } diff --git a/time_unix.c b/time_unix.c index 6240c04..7547a09 100644 --- a/time_unix.c +++ b/time_unix.c @@ -32,13 +32,8 @@ * */ -#include -#include -#include -#include - -#include "ntimed.h" #include "ntimed_platform.h" +#include "ntimed.h" static double adj_offset = 0; static double adj_duration = 0; diff --git a/udp.c b/udp.c index 908148f..67addfe 100644 --- a/udp.c +++ b/udp.c @@ -53,7 +53,19 @@ static int udp_sock(int fam) { int fd; + int i; + fd = socket(fam, SOCK_DGRAM, 0); + if (fd < 0) + return (fd); + +#ifdef SO_TIMESTAMPNS + i = 1; + (void)setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &i, sizeof i); +#elif defined(SO_TIMESTAMP) + i = 1; + (void)setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &i, sizeof i); +#endif return (fd); }