-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs_dnscache.c
188 lines (162 loc) · 4.74 KB
/
bs_dnscache.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
//
// bs_dnscache.c
// MobileCross
//
// Created by super on 2017/8/1.
// Copyright © 2017年 Rex. All rights reserved.
//
#include "bs_dnscache.h"
#include <string.h>
typedef bs_dns *bs_dns_pr;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// 只缓存5个ip
#define BS_HOST_CACHE_COUNT 5
// 根节点不存储dns信息
static bs_dns_pr m_root;
bool_t dns_cache_empty() {
if (m_root == NULL) {
return BS_TRUE;
}
return BS_FALSE;
}
void dns_root_create() {
m_root = (bs_dns_pr)malloc(sizeof(bs_dns));
m_root->hostname = NULL;
m_root->next = NULL;
}
bs_dns* dns_cache_lookup(const char *hostname) {
if (pthread_mutex_lock(&mutex) != 0){
fprintf(stdout, "lock error!\n");
}
if (dns_cache_empty()) {
pthread_mutex_unlock(&mutex);
return NULL;
}
bs_dns_pr dns = m_root->next;
while (dns) {
if (strcmp(dns->hostname, hostname) == 0) {
pthread_mutex_unlock(&mutex);
return dns;
}
dns = dns->next;
}
pthread_mutex_unlock(&mutex);
return NULL;
}
void dns_cache_store(struct in_addr in_addr_ip, const char *hostname) {
if (pthread_mutex_lock(&mutex) != 0){
fprintf(stdout, "lock error!\n");
}
if (dns_cache_empty()) {
dns_root_create();
}
int cacheCount = 0;
bs_dns_pr dns_pre = m_root;
bs_dns_pr dns_next = dns_pre->next;
while (dns_next) {
cacheCount++;
dns_pre = dns_next;
dns_next = dns_next->next;
if (strcmp(hostname, dns_pre->hostname) == 0) {
pthread_mutex_unlock(&mutex);
return;
}
}
if (cacheCount < BS_HOST_CACHE_COUNT) {
// 存储dns信息时候需要加锁,处理线程安全
bs_dns_pr node = (bs_dns_pr)malloc(sizeof(bs_dns));
dns_pre->next = node;
node->in_addr_ip = (struct in_addr *)malloc(sizeof(struct in_addr));
memcpy(node->in_addr_ip, &in_addr_ip, sizeof(struct in_addr));
node->next = NULL;
int len = (int)strlen(hostname) + 1;
if (!(node->hostname = (char*)malloc(sizeof(char) * len))) {
pthread_mutex_unlock(&mutex);
return;
}
memcpy(node->hostname, hostname, len);
}
// 解锁
pthread_mutex_unlock(&mutex);
}
void dns_cache_ipv6_store(struct in6_addr in_addr_ip, const char *hostname) {
if (pthread_mutex_lock(&mutex) != 0){
fprintf(stdout, "lock error!\n");
}
if (dns_cache_empty()) {
dns_root_create();
}
int cacheCount = 0;
bs_dns_pr dns_pre = m_root;
bs_dns_pr dns_next = dns_pre->next;
while (dns_next) {
cacheCount++;
dns_pre = dns_next;
dns_next = dns_next->next;
if (strcmp(hostname, dns_pre->hostname) == 0) {
pthread_mutex_unlock(&mutex);
return;
}
}
if (cacheCount < BS_HOST_CACHE_COUNT) {
// 存储dns信息时候需要加锁,处理线程安全
bs_dns_pr node = (bs_dns_pr)malloc(sizeof(bs_dns));
dns_pre->next = node;
node->in_addr_ip6 = (struct in6_addr *)malloc(sizeof(struct in6_addr));
memcpy(node->in_addr_ip6, &in_addr_ip, sizeof(struct in6_addr));
node->next = NULL;
int len = (int)strlen(hostname) + 1;
if (!(node->hostname = (char*)malloc(sizeof(char) * len))) {
pthread_mutex_unlock(&mutex);
return;
}
memcpy(node->hostname, hostname, len);
}
// 解锁
pthread_mutex_unlock(&mutex);
}
void dns_cache_remove(const char *hostname) {
if (pthread_mutex_lock(&mutex) != 0){
fprintf(stdout, "lock error!\n");
}
if (dns_cache_empty()) {
pthread_mutex_unlock(&mutex);
return;
}
bs_dns_pr dns_pre = m_root;
bs_dns_pr dns_next = m_root->next;
while (dns_next) {
if (strcmp(dns_next->hostname, hostname)) {
dns_pre = dns_next->next;
free(dns_next->hostname);
free(dns_next->in_addr_ip);
free(dns_next->in_addr_ip6);
free(dns_next);
pthread_mutex_unlock(&mutex);
return;
}
dns_pre = dns_next;
dns_next = dns_next->next;
}
pthread_mutex_unlock(&mutex);
}
void dns_cache_clear() {
if (pthread_mutex_lock(&mutex) != 0){
fprintf(stdout, "lock error!\n");
}
if (dns_cache_empty()) {
pthread_mutex_unlock(&mutex);
return;
}
bs_dns_pr dns = m_root;
while (dns) {
bs_dns_pr node = dns;
dns = node->next;
free(node->hostname);
free(node->in_addr_ip);
free(node->in_addr_ip6);
free(node);
}
m_root = NULL;
pthread_mutex_unlock(&mutex);
}