-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbs_socket.c
304 lines (252 loc) · 7.95 KB
/
bs_socket.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* file : bs_socket.c
* author : bushaofeng
* created : 2013-11-18 15:03
* func :
*/
#include "bs_socket.h"
#include "bs_common.h"
#include "bs_type.h"
#include "bs_dnscache.h"
int socket_udp(bool_t is_nonblock);
int socket_tcp(bool_t is_nonblock);
static int socket_create(int family, int sock_type, int is_nonblock);
static int sockaddr_create(struct sockaddr_in* addr, in_addr_t s_addr, int port);
state_t bs_tcp_sock_connect(int *sock, const char *ip, int port, bool_t is_nonblock) {
state_t status = BS_SUCCESS;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
char portStr[8];
snprintf(portStr, 8, "%d", port);
int retryTimes = 3;
while (retryTimes > 0) {
// 偶尔DNS会解析失败,现在给一个重试机制,默认为重试3次
retryTimes -= 1;
status = getaddrinfo(ip, portStr, &hints, &res);
if (status == 0) {
*sock = socket_create(res->ai_family, SOCK_STREAM, is_nonblock);
bs_sock_ignore_sigpipe(*sock);
status = connect(*sock, res->ai_addr, res->ai_addrlen);
break;
} else {
printf("ERROR: getaddrinfo error: %s\n", gai_strerror(status));
}
}
freeaddrinfo(res);
return status;
}
int socket_udp(bool_t is_nonblock){
return socket_create(AF_INET, SOCK_DGRAM, is_nonblock);
}
int socket_tcp(bool_t is_nonblock){
return socket_create(AF_INET, SOCK_STREAM, is_nonblock);
}
state_t bs_sock_keepalive(int sock){
uint32_t len = sizeof(unsigned int);
int optval = 1;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, len);//使用KEEPALIVE
return BS_SUCCESS;
}
state_t bs_sock_nodelay(int sock){
uint32_t len = sizeof(unsigned int);
int optval = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &optval, len);//禁用NAGLE算法,立刻发送
return BS_SUCCESS;
}
state_t bs_sock_optimize(int sock){
uint32_t len = sizeof(unsigned int);
int optval = 1;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, len);//使用KEEPALIVE
optval = 0;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &optval, len);//禁用NAGLE算法,立刻发送
return BS_SUCCESS;
}
state_t bs_sock_bind(int sock, int port){
struct sockaddr_in addr;
sockaddr_create(&addr, htonl(INADDR_ANY), port);
if(bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))==-1){
return BS_HOSTERR;
}
return BS_SUCCESS;
}
state_t bs_sock_connect(int sock, const char* ip, int port){
state_t st;
struct sockaddr_in addr;
st = bs_sock_addr(&addr, ip, port);
if(st != BS_SUCCESS){
return st;
}
st = connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
// if(st==0){
// return BS_SUCCESS;
// }
//
// return errno;
return st;
}
state_t bs_sock_addr(struct sockaddr_in* addr, const char* ip, int port){
struct hostent* host = NULL;
struct in_addr *currentIp;
assert(addr!=NULL && ip!=NULL);
if ((currentIp = dns_cache_lookup(ip)) == NULL) {
if ((host=gethostbyname(ip))==NULL){
return BS_HOSTERR;
}
currentIp = (struct in_addr *)host->h_addr;
dns_cache_store(*currentIp, ip);
}
bzero(addr, sizeof(addr));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr = *currentIp;
return BS_SUCCESS;
}
void bs_sock_ignore_sigpipe(int sock) {
const int set = 1;
#ifdef __ANDROID__
setsockopt(sock, SOL_SOCKET, MSG_NOSIGNAL, (void*)&set, sizeof(int));
#else
setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
#endif
}
struct sockaddr_in bs_int2addr(uint64_t value){
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = value >> 32;
addr.sin_port = value & 0xffff;
return addr;
}
int bs_sock_recvfrom(int sock, struct sockaddr_in* addr, char* buf, size_t size){
int len;
socklen_t sin_size = sizeof(struct sockaddr_in);
assert(addr!=NULL && buf!=NULL);
len = recvfrom(sock, buf, size, 0, (struct sockaddr*)addr, &sin_size);
if(len>0){
buf[len] = '\0';
}
return len;
}
int bs_sock_sendto(int sock, struct sockaddr_in* addr, char* buf, size_t size){
socklen_t sin_size = sizeof(struct sockaddr_in);
assert(addr!=NULL && buf!=NULL);
return sendto(sock, buf, size, 0, (struct sockaddr*)addr, sin_size);
}
state_t bs_sock_localip(char* ip, int size)
{
/*
int inet_sock;
int i;
struct ifreq ifr;
const char* ifr_name[] = {"eth0", "en0"};
inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
memset(ip, 0, size);
for (i=0; i<sizeof(ifr_name)/sizeof(char*); i++) {
strcpy(ifr.ifr_name, ifr_name[i]);
int rv = ioctl(inet_sock, SIOCGIFADDR, &ifr);
if(rv < 0){
//fprintf(stderr, "%s: ioctl error", __FUNCTION__);
continue;
}
char* local = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
bs_strcpy(ip, size, local);
close(inet_sock);
return BS_SUCCESS;
}
fprintf(stderr, "%s: ioctl error", __FUNCTION__);
close(inet_sock);
*/
return BS_INVALID;
/*
char hname[128];
struct hostent *hent;
int i;
gethostname(hname, sizeof(hname));
//hent = gethostent();
hent = gethostbyname(hname);
printf("hostname: %s\naddress list: ", hent->h_name);
for(i = 0; hent->h_addr_list[i]; i++) {
printf("%s\t", inet_ntoa(*(struct in_addr*)(hent->h_addr_list[i])));
}
*/
/*
int MAXINTERFACES=16;
int fd, intrface, retn = 0;
struct ifreq buf[MAXINTERFACES];
struct ifconf ifc;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
{
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
{
intrface = ifc.ifc_len / sizeof(struct ifreq);
while (intrface-- > 0)
{
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
char* local=(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
bs_strcpy(ip, size, local, (uint32_t)strlen(local));
close (fd);
return BS_SUCCESS;
}
}
}
close (fd);
}
*/
return BS_INVALID;
}
static int sockaddr_create(struct sockaddr_in* addr, in_addr_t s_addr, int port){
assert(addr != NULL);
bzero(addr, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = s_addr;
return BS_SUCCESS;
}
static int socket_create(int family, int sock_type, int is_nonblock){
int sock;
if((sock = socket(family, sock_type, 0)) == -1){
return -1;
}
if (sock_type == SOCK_STREAM) {
// 设置发送不延迟
int flag = 0;
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(int)); //使用KEEPALIVE
setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)); // 立刻发送
}
if(is_nonblock){
socket_unblock(sock);
} else{
socket_block(sock);
}
return sock;
}
state_t socket_unblock(int sock){
int flags;
if((flags = fcntl(sock, F_GETFL, 0)) < 0) {
close(sock);
return BS_INVALID;
}
if(fcntl(sock, F_SETFL, flags|O_NONBLOCK) < 0){
close(sock);
return BS_INVALID;
}
return BS_SUCCESS;
}
state_t socket_block(int sock){
int flags;
if((flags = fcntl(sock, F_GETFL, 0)) < 0) {
close(sock);
return BS_INVALID;
}
if(fcntl(sock, F_SETFL, flags & ~O_NONBLOCK) < 0){
close(sock);
return BS_INVALID;
}
return BS_SUCCESS;
}