forked from andrewshadura/tnat64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
150 lines (124 loc) · 3.8 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
commmon.c - Common routines for the tnat64 package
*/
#include <config.h>
#include <stdio.h>
#include <netdb.h>
#include <common.h>
#include <stdarg.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
/* Globals */
static int loglevel = MSGERR; /* The default logging level is to only log
error messages */
static char *logfilename = NULL; /* Name of file to which log messages should
be redirected */
static FILE *logfile = NULL; /* File to which messages should be logged */
static int logstamp = 0; /* Timestamp (and pid stamp) messages */
static char *logprogname = NULL;
unsigned int HIDDENSYM resolve_ip(char *host, int showmsg, int allownames)
{
struct hostent *new;
unsigned int hostaddr;
struct in_addr *ip;
if ((hostaddr = inet_addr(host)) == (unsigned int)-1)
{
/* We couldn't convert it as a numerical ip so */
/* try it as a dns name */
if (allownames)
{
#ifdef HAVE_GETHOSTBYNAME
if ((new = gethostbyname(host)) == (struct hostent *)0)
{
#endif
return (-1);
#ifdef HAVE_GETHOSTBYNAME
}
else
{
ip = ((struct in_addr *)*new->h_addr_list);
hostaddr = ip->s_addr;
if (showmsg)
printf("Connecting to %s...\n", inet_ntoa(*ip));
}
#endif
}
else
return (-1);
}
return (hostaddr);
}
/* Set logging options, the options are as follows: */
/* level - This sets the logging threshold, messages with */
/* a higher level (i.e lower importance) will not be */
/* output. For example, if the threshold is set to */
/* MSGWARN a call to log a message of level MSGDEBUG */
/* would be ignored. This can be set to -1 to disable */
/* messages entirely */
/* progname - Program name prefixed to all log messages */
/* filename - This is a filename to which the messages should */
/* be logged instead of to standard error */
/* timestamp - This indicates that messages should be prefixed */
/* with timestamps (and the process id) */
void HIDDENSYM set_log_options(int level, const char *progname, const char *filename, int timestamp)
{
loglevel = level;
if (loglevel < MSGERR)
loglevel = MSGNONE;
if (progname)
{
logprogname = strdup(progname);
}
if (filename)
{
logfilename = strdup(filename);
}
logstamp = timestamp;
}
void HIDDENSYM show_msg(int level, char *fmt, ...)
{
va_list ap;
int saveerr;
char timestring[20];
time_t timestamp;
if ((loglevel == MSGNONE) || (level > loglevel))
return;
if (!logfile)
{
if (logfilename)
{
logfile = fopen(logfilename, "a");
if (logfile == NULL)
{
logfile = stderr;
show_msg(MSGERR, "Could not open log file, %s, %s\n", logfilename, strerror(errno));
}
}
else
logfile = stderr;
}
if (logstamp)
{
timestamp = time(NULL);
strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(×tamp));
fprintf(logfile, "%s ", timestring);
}
fputs(logprogname, logfile);
if (logstamp)
{
fprintf(logfile, "(%d)", getpid());
}
fputs(": ", logfile);
va_start(ap, fmt);
/* Save errno */
saveerr = errno;
vfprintf(logfile, fmt, ap);
fflush(logfile);
errno = saveerr;
va_end(ap);
}