forked from zhao-gang/shadowsocks-tiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
209 lines (167 loc) · 4.36 KB
/
log.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) 2014 Zhao, Gang <[email protected]>
* This is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*/
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "log.h"
static int _pr_addrinfo(int level, struct addrinfo *info,
const char *fmt, va_list ap)
{
unsigned short port;
int offset;
char log[1024];
char addr[INET6_ADDRSTRLEN];
struct addrinfo *ai = info;
void *addrptr;
vsprintf(log, fmt, ap);
strcat(log, ":");
offset = strlen(log);
while (ai) {
if (ai->ai_family == AF_INET) {
addrptr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
port = ntohs(((struct sockaddr_in *)ai->ai_addr)->sin_port);
} else {
addrptr = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
port = ntohs(((struct sockaddr_in6 *)ai->ai_addr)->sin6_port);
}
if (inet_ntop(ai->ai_family, addrptr, addr,
INET6_ADDRSTRLEN) == NULL) {
return errno;
}
if (ai->ai_socktype == SOCK_STREAM)
sprintf(log + offset, " %s:%d(tcp)", addr, port);
else if (ai->ai_socktype == SOCK_DGRAM)
sprintf(log + offset, " %s:%d(udp)", addr, port);
ai = ai->ai_next;
}
strcat(log, "\n");
syslog(level, "%s", log);
return 0;
}
void pr_ai_debug(struct addrinfo *info, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = _pr_addrinfo(LOG_DEBUG, info, fmt, ap);
if (ret != 0)
pr_warn("%s: %s\n", __func__, strerror(ret));
va_end(ap);
}
void pr_ai_info(struct addrinfo *info, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = _pr_addrinfo(LOG_INFO, info, fmt, ap);
if (ret != 0)
pr_warn("%s: %s\n", __func__, strerror(ret));
va_end(ap);
}
void pr_ai_notice(struct addrinfo *info, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = _pr_addrinfo(LOG_NOTICE, info, fmt, ap);
if (ret != 0)
pr_warn("%s: %s\n", __func__, strerror(ret));
va_end(ap);
}
static int get_sock_addr(int sockfd, char *str, int *port, const char *type)
{
void *addrptr;
struct sockaddr_storage ss_addr;
int len = sizeof(struct sockaddr_storage);
if (strcmp(type, "peer") == 0) {
if (getpeername(sockfd, (struct sockaddr *)&ss_addr,
(void *)&len) == -1)
goto err;
} else if (strcmp(type, "sock") == 0) {
if (getsockname(sockfd, (struct sockaddr *)&ss_addr,
(void *)&len) == -1)
goto err;
}
if (ss_addr.ss_family == AF_INET) {
addrptr = &((struct sockaddr_in *)&ss_addr)->sin_addr;
*port = ntohs(((struct sockaddr_in *)&ss_addr)->sin_port);
} else if (ss_addr.ss_family == AF_INET6) {
addrptr = &((struct sockaddr_in6 *)&ss_addr)->sin6_addr;
*port = ntohs(((struct sockaddr_in6 *)&ss_addr)->sin6_port);
} else {
pr_exit("%s: unsupported address family\n", __func__);
}
if (inet_ntop(ss_addr.ss_family, addrptr, str,
INET6_ADDRSTRLEN) == NULL)
goto err;
return 0;
err:
return -1;
}
static void sock_print(int sockfd, int level, const char *fmt, va_list ap)
{
int offset, port;
char *type;
char str[INET6_ADDRSTRLEN] = {'\0'};
char log[1024];
if (get_sock_addr(sockfd, str, &port, "peer") == 0)
type = "peer";
else if (get_sock_addr(sockfd, str, &port, "sock") == 0)
type = "sock";
else
type = "sockfd";
vsprintf(log, fmt, ap);
offset = strlen(log);
if (strcmp(type, "peer") == 0)
sprintf(log + offset, " (peer)%s:%d\n", str, port);
else if (strcmp(type, "sock") == 0)
sprintf(log + offset, " %s:%d\n", str, port);
else if (strcmp(type, "sockfd") == 0)
sprintf(log + offset, " (sockfd)%d\n", sockfd);
syslog(level, "%s", log);
}
void sock_debug(int sockfd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sock_print(sockfd, LOG_DEBUG, fmt, ap);
va_end(ap);
}
void sock_info(int sockfd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sock_print(sockfd, LOG_INFO, fmt, ap);
va_end(ap);
}
void sock_notice(int sockfd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sock_print(sockfd, LOG_NOTICE, fmt, ap);
va_end(ap);
}
void sock_warn(int sockfd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sock_print(sockfd, LOG_WARNING, fmt, ap);
va_end(ap);
}
void sock_err(int sockfd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
sock_print(sockfd, LOG_ERR, fmt, ap);
va_end(ap);
}