-
Notifications
You must be signed in to change notification settings - Fork 146
/
sock.c
178 lines (142 loc) · 3.99 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
#define _GNU_SOURCE
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include "debug.h"
#include "sock.h"
ssize_t sock_read (int sock_fd, void *buffer, size_t len)
{
ssize_t nr, tot_read;
char *buf = buffer; // avoid pointer arithmetic on void pointer
tot_read = 0;
while (len !=0 && (nr = read(sock_fd, buf, len)) != 0) {
if (nr < 0) {
if (errno == EINTR) {
continue;
} else {
return -1;
}
}
len -= nr;
buf += nr;
tot_read += nr;
}
return tot_read;
}
ssize_t sock_write (int sock_fd, void *buffer, size_t len)
{
ssize_t nw, tot_written;
const char *buf = buffer; // avoid pointer arithmetic on void pointer
for (tot_written = 0; tot_written < len; ) {
nw = write(sock_fd, buf, len-tot_written);
if (nw <= 0) {
if (nw == -1 && errno == EINTR) {
continue;
} else {
return -1;
}
}
tot_written += nw;
buf += nw;
}
return tot_written;
}
int sock_create_bind (char *port)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int sock_fd = -1, ret = 0;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
ret = getaddrinfo(NULL, port, &hints, &result);
check(ret==0, "getaddrinfo error.");
for (rp = result; rp != NULL; rp = rp->ai_next) {
sock_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock_fd < 0) {
continue;
}
ret = bind(sock_fd, rp->ai_addr, rp->ai_addrlen);
if (ret == 0) {
/* bind success */
break;
}
close(sock_fd);
sock_fd = -1;
}
check(rp != NULL, "creating socket.");
freeaddrinfo(result);
return sock_fd;
error:
if (result) {
freeaddrinfo(result);
}
if (sock_fd > 0) {
close(sock_fd);
}
return -1;
}
int sock_create_connect (char *server_name, char *port)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int sock_fd = -1, ret = 0;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_UNSPEC;
ret = getaddrinfo(server_name, port, &hints, &result);
check(ret==0, "[ERROR] %s", gai_strerror(ret));
for (rp = result; rp != NULL; rp = rp->ai_next) {
sock_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock_fd == -1) {
continue;
}
ret = connect(sock_fd, rp->ai_addr, rp->ai_addrlen);
if (ret == 0) {
/* connection success */
break;
}
close(sock_fd);
sock_fd = -1;
}
check(rp!=NULL, "could not connect.");
freeaddrinfo(result);
return sock_fd;
error:
if (result) {
freeaddrinfo(result);
}
if (sock_fd != -1) {
close(sock_fd);
}
return -1;
}
int sock_set_qp_info(int sock_fd, struct QPInfo *qp_info)
{
int n;
struct QPInfo tmp_qp_info;
tmp_qp_info.lid = htons(qp_info->lid);
tmp_qp_info.qp_num = htonl(qp_info->qp_num);
tmp_qp_info.rank = htonl(qp_info->rank);
n = sock_write(sock_fd, (char *)&tmp_qp_info, sizeof(struct QPInfo));
check(n==sizeof(struct QPInfo), "write qp_info to socket.");
return 0;
error:
return -1;
}
int sock_get_qp_info(int sock_fd, struct QPInfo *qp_info)
{
int n;
struct QPInfo tmp_qp_info;
n = sock_read(sock_fd, (char *)&tmp_qp_info, sizeof(struct QPInfo));
check(n==sizeof(struct QPInfo), "read qp_info from socket.");
qp_info->lid = ntohs(tmp_qp_info.lid);
qp_info->qp_num = ntohl(tmp_qp_info.qp_num);
qp_info->rank = ntohl(tmp_qp_info.rank);
return 0;
error:
return -1;
}