You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.1 KiB

/*
*
*/
#include "UDPTCPNetwork.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h> /* close() */
#include <string.h> /* memset() */
using namespace std;
char dnsip[NET_HOSTLEN];
int UDPTCPNetwork_init = 0;
//
// convert host and port to sockaddr_in6
//
int dns_filladdr (string host, string port, int ai_family, struct sockaddr_storage *sAddr) {
struct addrinfo hints, *res;
int err;
bzero (&hints, sizeof (struct addrinfo));
hints.ai_family = ai_family;
hints.ai_socktype = SOCK_DGRAM;
if ((err = getaddrinfo (host.c_str(), port.c_str(), &hints, &res)) < 0) {
fprintf (stdout, "dns_filladdr (getaddrinfo):%s\n", gai_strerror (err));
return -1;
}
memcpy (sAddr, res->ai_addr, res->ai_addrlen);
freeaddrinfo (res);
return 1;
};
//
// convert int to char*
//
char* itoa(char* buffer, int number, int size) {
snprintf (buffer, size, "%d", number);
return buffer;
};
void UDPTCPNetwork_Startup() {
if (UDPTCPNetwork_init != 0) return;
printf ("%s\n", __FUNCTION__);
#ifdef BUILD_WINDOWS
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return;
}
#endif
UDPTCPNetwork_init = 1;
};
int file_is_readable (const char *fname) {
int f;
if ((f = open(fname, O_RDONLY)) == -1) return 0;
close (f);
return 1;
};
std::string string_to_upper(std::string s) {
for (int i = 0; i < s.length(); i++) s[i] = toupper(s[i]);
return s;
};
std::string string_to_lower(std::string s) {
for (int i = 0; i < s.length(); i++) s[i] = tolower(s[i]);
return s;
};
#define LEN_TEXT 128
#define DEBUG_TEXT1LEN 2048
#define DEBUG_TEXT2LEN (DEBUG_TEXT1LEN+LEN_TEXT)
void _debug (char *srcfile, int srcline, char *srcfunc, char *fmt,...) {
va_list args;
char texttime[LEN_TEXT];
char text1[DEBUG_TEXT1LEN];
char text2[DEBUG_TEXT2LEN];
pid_t pid = gettid();
int tms;
struct timeval tv;
struct tm tmp;
struct tm *tmptr = NULL;
va_start (args, fmt);
vsnprintf (text1, (DEBUG_TEXT1LEN-1), fmt, args);
va_end (args);
texttime[0] = 0;
text1[DEBUG_TEXT1LEN-1] = 0;
text2[DEBUG_TEXT2LEN-1] = 0;
gettimeofday (&tv, NULL);
tmptr = localtime_r(&tv.tv_sec, &tmp);
if (tmptr != NULL)
strftime(texttime, LEN_TEXT, "%H:%M:%S", &tmp);
snprintf (text2, DEBUG_TEXT2LEN-1, "%s.%03d %d %s:%d %s %s", texttime, tv.tv_usec/1000, pid,
srcfile, srcline, srcfunc, text1);
printf ("%s", text2);
/*
if (type == 0 || (type & _debuglevel) != 0) {
FILE *f;
printf ("%s\n", text2);
f= fopen(DEBUG_FILE, "a");
if (f) {
fprintf (f, "%s\n", text2);
fclose (f);
}
}
*/
};
void _errorexit (char *srcfile, int srcline, char *srcfunc, char *fmt,...) {
va_list args;
va_start (args, fmt);
_debug (srcfile, srcline, srcfunc, fmt, args);
va_end (args);
exit (1);
}