-
Notifications
You must be signed in to change notification settings - Fork 2
/
sock.c
216 lines (184 loc) · 4.37 KB
/
sock.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
//License: Apache License 2.0
//See LICENSE for details
//Authors (in alphabet order of last name):
// - Qijiang Fan <fqj1994@gmail.com>
// - Bin He <binhe22@gmail.com>
// - Cheng Zhang <chengzhang@hustunique.com>
// - Shiwei Zhou <shwzhou@hustunique.com>
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "sock.h"
/*****************************************
* Function: sock_daemon_connect
*****************************************/
int sock_daemon_connect(int port)
{
struct addrinfo *res, *t;
struct addrinfo hints = {
.ai_flags = AI_PASSIVE,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM
};
char *service;
int n;
int sockfd = -1, connfd;
if (asprintf(&service, "%d", port) < 0) {
fprintf(stderr, "asprintf failed\n");
return -1;
}
n = getaddrinfo(NULL, service, &hints, &res);
if (n < 0) {
fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
free(service);
return -1;
}
for (t = res; t; t = t->ai_next) {
sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
if (sockfd >= 0) {
n = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &n,
sizeof n);
if (!bind(sockfd, t->ai_addr, t->ai_addrlen))
break;
close(sockfd);
sockfd = -1;
}
}
freeaddrinfo(res);
free(service);
if (sockfd < 0) {
fprintf(stderr, "couldn't listen to port %d\n", port);
return -1;
}
listen(sockfd, 1);
connfd = accept(sockfd, NULL, 0);
close(sockfd);
if (connfd < 0) {
fprintf(stderr, "accept() failed\n");
return -1;
}
return connfd;
}
/*****************************************
* Function: sock_client_connect
*****************************************/
int sock_client_connect(const char *server_name, int port)
{
struct addrinfo *res, *t;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM
};
char *service;
int n;
int sockfd = -1;
if (asprintf(&service, "%d", port) < 0) {
fprintf(stderr, "asprintf failed\n");
return -1;
}
n = getaddrinfo(server_name, service, &hints, &res);
if (n < 0) {
fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), server_name,
port);
free(service);
return -1;
}
for (t = res; t; t = t->ai_next) {
sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
if (sockfd >= 0) {
if (!connect(sockfd, t->ai_addr, t->ai_addrlen))
break;
close(sockfd);
sockfd = -1;
}
}
freeaddrinfo(res);
free(service);
if (sockfd < 0) {
//fprintf(stderr, "couldn't connect to %s:%d\n", server_name,
// port);
return -1;
}
return sockfd;
}
/*****************************************
* Function: sock_recv
*****************************************/
static int sock_recv(int sock_fd, size_t size, void *buf)
{
int rc;
retry_after_signal:
rc = recv(sock_fd, buf, size, MSG_WAITALL);
if (rc != size) {
fprintf(stderr, "recv failed: %s, rc=%d\n", strerror(errno),
rc);
if ((errno == EINTR) && (rc != 0))
goto retry_after_signal; /* Interrupted system call */
if (rc)
return rc;
else
return -1;
}
return 0;
}
/*****************************************
* Function: sock_send
*****************************************/
static int sock_send(int sock_fd, size_t size, const void *buf)
{
int rc;
retry_after_signal:
rc = send(sock_fd, buf, size, 0);
if (rc != size) {
fprintf(stderr, "send failed: %s, rc=%d\n", strerror(errno),
rc);
if ((errno == EINTR) && (rc != 0))
goto retry_after_signal; /* Interrupted system call */
if (rc)
return rc;
else
return -1;
}
return 0;
}
/*****************************************
* Function: sock_sync_data
*****************************************/
int sock_sync_data(int sock_fd,
int is_daemon,
size_t size, const void *out_buf, void *in_buf)
{
int rc;
if (!is_daemon) {
rc = sock_send(sock_fd, size, out_buf);
if (rc)
return rc;
rc = sock_recv(sock_fd, size, in_buf);
if (rc)
return rc;
} else {
rc = sock_recv(sock_fd, size, in_buf);
if (rc)
return rc;
rc = sock_send(sock_fd, size, out_buf);
if (rc)
return rc;
}
return 0;
}
/*****************************************
* Function: sock_sync_ready
*****************************************/
int sock_sync_ready(int sock_fd, int is_daemon)
{
char cm_buf = 'a';
return sock_sync_data(sock_fd, is_daemon, sizeof(cm_buf), &cm_buf,
&cm_buf);
}