#include "setup.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef _XOPEN_SOURCE_EXTENDED
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#define ENABLE_CURLX_PRINTF
#include "curlx.h"
#include "getpart.h"
#include "util.h"
#include "timeval.h"
#if defined(ENABLE_IPV6) && defined(__MINGW32__)
const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
#endif
extern const char *serverlogfile;
void logmsg(const char *msg, ...)
{
va_list ap;
char buffer[512];
FILE *logfp;
int error;
struct timeval tv;
time_t sec;
struct tm *now;
char timebuf[20];
if (!serverlogfile) {
fprintf(stderr, "Error: serverlogfile not set\n");
return;
}
tv = curlx_tvnow();
sec = tv.tv_sec;
now = localtime(&sec);
snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
now->tm_hour, now->tm_min, now->tm_sec, tv.tv_usec);
va_start(ap, msg);
vsprintf(buffer, msg, ap);
va_end(ap);
logfp = fopen(serverlogfile, "a");
if(logfp) {
fprintf(logfp, "%s %s\n", timebuf, buffer);
fclose(logfp);
}
else {
error = ERRNO;
fprintf(stderr, "fopen() failed with error: %d %s\n",
error, strerror(error));
fprintf(stderr, "Error opening file: %s\n", serverlogfile);
fprintf(stderr, "Msg not logged: %s %s\n", timebuf, buffer);
}
}
#ifdef WIN32
void win32_perror (const char *msg)
{
char buf[512];
DWORD err = SOCKERRNO;
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
LANG_NEUTRAL, buf, sizeof(buf), NULL))
snprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
if (msg)
fprintf(stderr, "%s: ", msg);
fprintf(stderr, "%s\n", buf);
}
#endif
#ifdef USE_WINSOCK
void win32_init(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
perror("Winsock init failed");
logmsg("Error initialising winsock -- aborting");
exit(1);
}
if ( LOBYTE( wsaData.wVersion ) != USE_WINSOCK ||
HIBYTE( wsaData.wVersion ) != USE_WINSOCK ) {
WSACleanup();
perror("Winsock init failed");
logmsg("No suitable winsock.dll found -- aborting");
exit(1);
}
}
void win32_cleanup(void)
{
WSACleanup();
}
#endif
const char *path=".";
char *test2file(long testno)
{
static char filename[256];
snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
return filename;
}