amiga-dnstracer/dnstracer_amiga.c

167 lines
3.4 KiB
C

/*
* Written by Carsten Larsen.
* Public domain.
*/
#include "dnstracer.h"
#include "getaddrinfo.h"
#define OPEN_ERROR "Cannot open %s.\n"
#define OPEN_VER_ERROR "Cannot open %s (%d.0)\n"
#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
const char *vers = "\0$VER: " PACKAGE_NAME " " PACKAGE_VERSION " (21.02.2016)";
#ifdef AOS3
int h_errno;
#endif
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();
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()
{
if (TimerBase != NULL) {
delete_timer();
}
if (UtilityBase != NULL) {
CloseLibrary(UtilityBase);
UtilityBase = NULL;
}
if (SocketBase != NULL) {
CloseLibrary(SocketBase);
SocketBase = NULL;
}
freeall();
if (DOSBase != NULL) {
CloseLibrary(DOSBase);
DOSBase = NULL;
}
}
int open_libs()
{
struct timeval tv;
atexit(close_libs);
InitSemaphore(&GetaddrinfoSemaphore);
if(!(DOSBase = OpenLibrary((STRPTR)DOSLIB_NAME, DOSLIB_REV))) {
amiga_open_lib_error(DOSLIB_NAME, DOSLIB_REV);
return LIB_ERROR;
}
if(!(SocketBase = OpenLibrary((STRPTR)BSDLIB_NAME, BSDLIB_REV))) {
amiga_open_lib_error(BSDLIB_NAME, BSDLIB_REV);
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;
}
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
return 0;
}
#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