-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommon_net.c
146 lines (121 loc) · 2.83 KB
/
common_net.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
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <public.h>
#include <common_net.h>
int
make_socket_non_blocking(int sfd)
{
int flags, ret;
flags = fcntl(sfd, F_GETFL, 0);
if (-1 == flags)
{
ERROR_MSG("fcntl\n");
goto error;
}
flags |= O_NONBLOCK;
ret = fcntl(sfd, F_SETFL, flags);
if (-1 == ret)
{
ERROR_MSG("fcntl\n");
goto error;
}
return 0;
error:
return -1;
}
static int
create_and_bind(const char *port)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int s, sfd;
memset(&hints, 0, sizeof(struct addrinfo));
/* this protocol family can't be supported by SSL */
//hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_family = AF_INET; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
hints.ai_flags = AI_PASSIVE; /* All interfaces */
s = getaddrinfo(NULL, port, &hints, &result);
if (s)
{
ERROR_MSG("getaddrinfo: %s\n", gai_strerror(s));
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (-1 == sfd)
continue;
s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
if (0 == s)
{
/* We managed to bind successfully! */
break;
}
close(sfd);
}
if (NULL == rp)
{
ERROR_MSG("Could not bind\n");
return -1;
}
freeaddrinfo(result);
return sfd;
}
int
start_listen(const char *str_port)
{
int lsfd, ret;
lsfd = create_and_bind(str_port);
if (-1 == lsfd) {
ERROR_MSG("create_and_bind\n");
goto error;
}
ret = make_socket_non_blocking(lsfd);
if (-1 == ret) {
ERROR_MSG("make_socket_non_blocking\n");
goto error;
}
ret = listen(lsfd, SOMAXCONN);
if (-1 == ret) {
ERROR_MSG("listen\n");
goto error;
}
return lsfd;
error:
if (lsfd) close(lsfd);
return -1;
}
int
connect_srv(const char *ip, unsigned short port)
{
int sock = 0, ret;
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
ERROR_MSG("sock ERROR\n");
goto error;
}
memset((void *)&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
//addr.sin_family = AF_UNSPEC;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);
ret = connect(sock, (struct sockaddr *)&addr, sizeof addr);
if (ret < 0)
{
ERROR_MSG("connect ERROR\n");
goto error;
}
return sock;
error:
if (sock) close(sock);
return -1;
}