2005-05-01 01:30:55 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2008-01-22 15:52:54 +01:00
|
|
|
* Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-05-01 01:30:55 +02:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
***************************************************************************/
|
|
|
|
#include "setup.h" /* portability help from the lib directory */
|
|
|
|
|
|
|
|
#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
|
|
|
|
/* This define is "almost" required to build on HPUX 11 */
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2007-07-12 03:07:49 +02:00
|
|
|
#ifdef HAVE_SYS_POLL_H
|
|
|
|
#include <sys/poll.h>
|
2008-01-22 15:52:54 +01:00
|
|
|
#elif defined(HAVE_POLL_H)
|
|
|
|
#include <poll.h>
|
2007-07-12 03:07:49 +02:00
|
|
|
#endif
|
2005-05-01 01:30:55 +02:00
|
|
|
|
2005-05-01 14:56:09 +02:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* make the curlx header define all printf() functions to use the curlx_*
|
|
|
|
versions instead */
|
2005-05-01 01:30:55 +02:00
|
|
|
#include "curlx.h" /* from the private lib dir */
|
|
|
|
#include "getpart.h"
|
|
|
|
#include "util.h"
|
2007-02-17 02:25:32 +01:00
|
|
|
#include "timeval.h"
|
2005-05-01 01:30:55 +02:00
|
|
|
|
2006-09-13 15:41:53 +02:00
|
|
|
#if defined(ENABLE_IPV6) && defined(__MINGW32__)
|
|
|
|
const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
|
|
|
|
#endif
|
|
|
|
|
2005-05-01 01:30:55 +02:00
|
|
|
/* someone else must set this properly */
|
2005-05-01 01:35:51 +02:00
|
|
|
extern const char *serverlogfile;
|
2005-05-01 01:30:55 +02:00
|
|
|
|
|
|
|
void logmsg(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2007-02-16 20:41:25 +01:00
|
|
|
char buffer[512]; /* possible overflow if you pass in a huge string */
|
2005-05-01 01:30:55 +02:00
|
|
|
FILE *logfp;
|
2007-02-16 20:41:25 +01:00
|
|
|
int error;
|
2007-02-17 02:25:32 +01:00
|
|
|
struct timeval tv;
|
|
|
|
time_t sec;
|
|
|
|
struct tm *now;
|
|
|
|
char timebuf[20];
|
2007-02-16 20:41:25 +01:00
|
|
|
|
|
|
|
if (!serverlogfile) {
|
|
|
|
fprintf(stderr, "Error: serverlogfile not set\n");
|
|
|
|
return;
|
|
|
|
}
|
2005-05-01 01:30:55 +02:00
|
|
|
|
2007-02-17 02:25:32 +01:00
|
|
|
tv = curlx_tvnow();
|
|
|
|
sec = tv.tv_sec;
|
|
|
|
now = localtime(&sec); /* not multithread safe but we don't care */
|
2005-05-01 01:30:55 +02:00
|
|
|
|
2005-05-25 14:04:52 +02:00
|
|
|
snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld",
|
|
|
|
now->tm_hour, now->tm_min, now->tm_sec, tv.tv_usec);
|
2005-05-01 01:30:55 +02:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vsprintf(buffer, msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
logfp = fopen(serverlogfile, "a");
|
2005-05-25 14:04:52 +02:00
|
|
|
if(logfp) {
|
|
|
|
fprintf(logfp, "%s %s\n", timebuf, buffer);
|
2005-05-01 01:30:55 +02:00
|
|
|
fclose(logfp);
|
2005-05-25 14:04:52 +02:00
|
|
|
}
|
2007-02-16 20:41:25 +01:00
|
|
|
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);
|
|
|
|
}
|
2005-05-01 01:30:55 +02:00
|
|
|
}
|
2005-05-17 12:22:22 +02:00
|
|
|
|
2006-10-11 18:01:16 +02:00
|
|
|
#ifdef WIN32
|
2005-05-17 12:22:22 +02:00
|
|
|
/* use instead of perror() on generic windows */
|
|
|
|
void win32_perror (const char *msg)
|
|
|
|
{
|
2007-02-16 20:41:25 +01:00
|
|
|
char buf[512];
|
2007-02-16 19:19:35 +01:00
|
|
|
DWORD err = SOCKERRNO;
|
2005-05-17 12:22:22 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2006-10-18 23:05:40 +02:00
|
|
|
#endif /* WIN32 */
|
2005-05-17 12:22:22 +02:00
|
|
|
|
2006-10-18 23:05:40 +02:00
|
|
|
#ifdef USE_WINSOCK
|
2005-05-17 12:22:22 +02:00
|
|
|
void win32_init(void)
|
|
|
|
{
|
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
int err;
|
2006-10-18 23:05:40 +02:00
|
|
|
wVersionRequested = MAKEWORD(USE_WINSOCK, USE_WINSOCK);
|
2005-05-17 12:22:22 +02:00
|
|
|
|
|
|
|
err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
perror("Winsock init failed");
|
2007-02-19 03:03:58 +01:00
|
|
|
logmsg("Error initialising winsock -- aborting");
|
2005-05-17 12:22:22 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2006-10-18 23:05:40 +02:00
|
|
|
if ( LOBYTE( wsaData.wVersion ) != USE_WINSOCK ||
|
|
|
|
HIBYTE( wsaData.wVersion ) != USE_WINSOCK ) {
|
2005-05-17 12:22:22 +02:00
|
|
|
|
|
|
|
WSACleanup();
|
|
|
|
perror("Winsock init failed");
|
2007-02-19 03:03:58 +01:00
|
|
|
logmsg("No suitable winsock.dll found -- aborting");
|
2005-05-17 12:22:22 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void win32_cleanup(void)
|
|
|
|
{
|
|
|
|
WSACleanup();
|
|
|
|
}
|
2006-10-18 23:05:40 +02:00
|
|
|
#endif /* USE_WINSOCK */
|
2005-05-17 12:22:22 +02:00
|
|
|
|
2005-09-15 22:22:43 +02:00
|
|
|
/* set by the main code to point to where the test dir is */
|
|
|
|
const char *path=".";
|
|
|
|
|
|
|
|
char *test2file(long testno)
|
|
|
|
{
|
|
|
|
static char filename[256];
|
|
|
|
snprintf(filename, sizeof(filename), TEST_DATA_PATH, path, testno);
|
|
|
|
return filename;
|
|
|
|
}
|
2007-07-12 03:07:49 +02:00
|
|
|
|
|
|
|
void go_sleep(long ms)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_POLL_FINE
|
|
|
|
/* portable subsecond "sleep" */
|
|
|
|
poll((void *)0, 0, (int)ms);
|
|
|
|
#else
|
|
|
|
/* systems without poll() need other solutions */
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
/* Windows offers a millisecond sleep */
|
|
|
|
Sleep(ms);
|
|
|
|
#elif defined(MSDOS)
|
|
|
|
delay(ms);
|
|
|
|
#else
|
|
|
|
/* Other systems must use select() for this */
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
timeout.tv_sec = ms/1000;
|
|
|
|
ms = ms%1000;
|
|
|
|
timeout.tv_usec = ms * 1000;
|
|
|
|
|
|
|
|
select(0, NULL, NULL, NULL, &timeout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|