2016-02-22 18:36:59 +00:00
|
|
|
/*
|
|
|
|
* Written by Carsten Larsen.
|
|
|
|
* Public domain.
|
|
|
|
*/
|
|
|
|
|
2016-03-05 08:49:08 +00:00
|
|
|
#include "dnstracer.h"
|
|
|
|
#include "getaddrinfo.h"
|
2016-02-22 18:36:59 +00:00
|
|
|
|
|
|
|
#define OPEN_ERROR "Cannot open %s.\n"
|
|
|
|
#define OPEN_VER_ERROR "Cannot open %s (%d.0)\n"
|
2016-03-05 08:49:08 +00:00
|
|
|
#define DOSLIB_NAME "dos.library"
|
|
|
|
#define DOSLIB_REV 37L
|
|
|
|
#define BSDLIB_NAME "bsdsocket.library"
|
|
|
|
#define BSDLIB_REV 03L
|
|
|
|
#define UTILLIB_NAME "utility.library"
|
|
|
|
#define UTILLIB_REV 37L
|
|
|
|
#define TIMER_NAME TIMERNAME
|
|
|
|
#define LIB_ERROR 5
|
2016-02-22 18:36:59 +00:00
|
|
|
|
2016-03-05 08:49:08 +00:00
|
|
|
const char *vers = "\0$VER: " PACKAGE_NAME " " PACKAGE_VERSION " (21.02.2016)";
|
2016-02-22 18:36:59 +00:00
|
|
|
|
|
|
|
#ifdef AOS3
|
|
|
|
int h_errno;
|
|
|
|
#endif
|
|
|
|
|
2016-03-05 08:49:08 +00:00
|
|
|
struct Library* DOSBase = NULL;
|
|
|
|
struct Library* SocketBase = NULL;
|
|
|
|
struct Library* UtilityBase = NULL;
|
|
|
|
struct Device* TimerBase = NULL;
|
|
|
|
struct timerequest* TimeRequest = NULL;
|
|
|
|
|
|
|
|
int create_timer();
|
|
|
|
void delete_timer();
|
2016-02-22 18:36:59 +00:00
|
|
|
|
|
|
|
void amiga_open_error(char *name)
|
|
|
|
{
|
|
|
|
printf(OPEN_ERROR, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void amiga_open_lib_error(char *name, int version)
|
|
|
|
{
|
|
|
|
printf(OPEN_VER_ERROR, name, version);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_libs()
|
|
|
|
{
|
2016-03-05 08:49:08 +00:00
|
|
|
if (TimerBase != NULL) {
|
|
|
|
delete_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UtilityBase != NULL) {
|
|
|
|
CloseLibrary(UtilityBase);
|
|
|
|
UtilityBase = NULL;
|
2016-02-22 18:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SocketBase != NULL) {
|
|
|
|
CloseLibrary(SocketBase);
|
|
|
|
SocketBase = NULL;
|
|
|
|
}
|
2016-03-05 08:49:08 +00:00
|
|
|
|
|
|
|
freeall();
|
|
|
|
|
|
|
|
if (DOSBase != NULL) {
|
|
|
|
CloseLibrary(DOSBase);
|
|
|
|
DOSBase = NULL;
|
|
|
|
}
|
2016-02-22 18:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int open_libs()
|
|
|
|
{
|
2016-03-05 08:49:08 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
2016-02-22 18:36:59 +00:00
|
|
|
atexit(close_libs);
|
|
|
|
InitSemaphore(&GetaddrinfoSemaphore);
|
|
|
|
|
|
|
|
if(!(DOSBase = OpenLibrary((STRPTR)DOSLIB_NAME, DOSLIB_REV))) {
|
|
|
|
amiga_open_lib_error(DOSLIB_NAME, DOSLIB_REV);
|
2016-03-05 08:49:08 +00:00
|
|
|
return LIB_ERROR;
|
2016-02-22 18:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!(SocketBase = OpenLibrary((STRPTR)BSDLIB_NAME, BSDLIB_REV))) {
|
|
|
|
amiga_open_lib_error(BSDLIB_NAME, BSDLIB_REV);
|
2016-03-05 08:49:08 +00:00
|
|
|
return LIB_ERROR;
|
|
|
|
} else {
|
|
|
|
SocketBaseTags(
|
|
|
|
SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), (IPTR) &errno,
|
|
|
|
SBTM_SETVAL(SBTC_HERRNOLONGPTR), (IPTR) &h_errno,
|
|
|
|
SBTM_SETVAL(SBTC_LOGTAGPTR), (IPTR) &prog,
|
|
|
|
TAG_DONE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(UtilityBase = OpenLibrary((STRPTR)UTILLIB_NAME, UTILLIB_REV))) {
|
|
|
|
amiga_open_lib_error(UTILLIB_NAME, UTILLIB_REV);
|
|
|
|
return LIB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create_timer() != 0) {
|
|
|
|
amiga_open_error(TIMER_NAME);
|
|
|
|
return LIB_ERROR;
|
2016-02-22 18:36:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 08:49:08 +00:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
srand(tv.tv_usec);
|
|
|
|
|
2016-02-22 18:36:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-05 08:49:08 +00:00
|
|
|
|
|
|
|
#ifdef AOS3
|
|
|
|
int create_timer()
|
|
|
|
{
|
|
|
|
LONG error;
|
|
|
|
struct MsgPort *port = CreatePort(0, 0);
|
|
|
|
if (port == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
TimeRequest = (struct timerequest*)CreateExtIO(port, sizeof(struct timerequest));
|
|
|
|
if (TimeRequest == NULL)
|
|
|
|
{
|
|
|
|
DeletePort(port);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = OpenDevice(
|
|
|
|
(STRPTR)TIMER_NAME, UNIT_MICROHZ,
|
|
|
|
(struct IORequest*)TimeRequest, 0L
|
|
|
|
);
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
{
|
|
|
|
delete_timer(TimeRequest);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimerBase = (struct Device*)TimeRequest->tr_node.io_Device;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void delete_timer()
|
|
|
|
{
|
|
|
|
struct MsgPort *port;
|
|
|
|
if (TimeRequest == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
port = TimeRequest->tr_node.io_Message.mn_ReplyPort;
|
|
|
|
|
|
|
|
if (port != 0)
|
|
|
|
DeletePort(port);
|
|
|
|
|
|
|
|
CloseDevice((struct IORequest*)TimeRequest);
|
|
|
|
DeleteExtIO((struct IORequest*)TimeRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getsystime(struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct timeval tv1;
|
|
|
|
|
|
|
|
GetSysTime(&tv1);
|
|
|
|
*tv = tv1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int create_timer() { return 0; }
|
|
|
|
void delete_timer() { }
|
|
|
|
#endif
|