Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd4e6d89

Browse files
committed
Correctly resolve addresses in raftable
1 parentd2b6c30 commitd4e6d89

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

‎contrib/raftable/raft/src/raft.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,21 +371,19 @@ int raft_create_udp_socket(raft_t r) {
371371
structaddrinfo*addrs=NULL;
372372
structaddrinfo*a;
373373
charportstr[6];
374-
374+
intrc;
375375
memset(&hint,0,sizeof(hint));
376376
hint.ai_socktype=SOCK_DGRAM;
377377
hint.ai_family=AF_INET;
378378
hint.ai_protocol=getprotobyname("udp")->p_proto;
379379

380380
snprintf(portstr,6,"%d",me->port);
381381

382-
if (getaddrinfo(me->host,portstr,&hint,&addrs))
382+
if ((rc=getaddrinfo(me->host,portstr,&hint,&addrs))!=0)
383383
{
384384
shout(
385-
"cannot convert the host string"
386-
" '%s' to a valid address\n",
387-
me->host
388-
);
385+
"cannot convert the host string '%s'"
386+
" to a valid address: %s\n",me->host,gai_strerror(rc));
389387
return-1;
390388
}
391389

‎contrib/raftable/worker.c‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,53 @@ static void add_peers(WorkerConfig *cfg)
8888
/* Returns the created socket, or -1 if failed. */
8989
staticintcreate_listening_socket(constchar*host,intport) {
9090
intoptval;
91-
structsockaddr_inaddr;
92-
ints=socket(AF_INET,SOCK_STREAM,0);
93-
if (s==-1) {
94-
fprintf(stderr,"cannot create the listening socket: %s\n",strerror(errno));
91+
structaddrinfo*addrs=NULL;
92+
structaddrinfohint;
93+
structaddrinfo*a;
94+
charportstr[6];
95+
intrc;
96+
97+
memset(&hint,0,sizeof(hint));
98+
hint.ai_socktype=SOCK_STREAM;
99+
hint.ai_family=AF_INET;
100+
snprintf(portstr,6,"%d",port);
101+
hint.ai_protocol=getprotobyname("tcp")->p_proto;
102+
103+
if ((rc=getaddrinfo(host,portstr,&hint,&addrs)))
104+
{
105+
elog(WARNING,"failed to resolve address '%s:%d': %s",
106+
host,port,gai_strerror(rc));
95107
return-1;
96108
}
97109

98-
optval=1;
99-
setsockopt(s,IPPROTO_TCP,TCP_NODELAY, (charconst*)&optval,sizeof(optval));
100-
setsockopt(s,SOL_SOCKET,SO_REUSEADDR, (charconst*)&optval,sizeof(optval));
110+
for (a=addrs;a!=NULL;a=a->ai_next)
111+
{
112+
ints=socket(AF_INET,SOCK_STREAM,0);
113+
if (s==-1) {
114+
elog(WARNING,"cannot create the listening socket: %s",strerror(errno));
115+
continue;
116+
}
101117

102-
addr.sin_family=AF_INET;
103-
if (inet_aton(host,&addr.sin_addr)==0) {
104-
fprintf(stderr,"cannot convert the host string '%s' to a valid address\n",host);
105-
return-1;
106-
}
107-
addr.sin_port=htons(port);
108-
fprintf(stderr,"binding tcp %s:%d\n",host,port);
109-
if (bind(s, (structsockaddr*)&addr,sizeof(addr))==-1) {
110-
fprintf(stderr,"cannot bind the listening socket: %s\n",strerror(errno));
111-
return-1;
112-
}
118+
optval=1;
119+
setsockopt(s,IPPROTO_TCP,TCP_NODELAY, (charconst*)&optval,sizeof(optval));
120+
setsockopt(s,SOL_SOCKET,SO_REUSEADDR, (charconst*)&optval,sizeof(optval));
121+
122+
fprintf(stderr,"binding tcp %s:%d\n",host,port);
123+
if (bind(s,a->ai_addr,a->ai_addrlen)<0) {
124+
elog(WARNING,"cannot bind the listening socket: %s",strerror(errno));
125+
close(s);
126+
continue;
127+
}
113128

114-
if (listen(s,LISTEN_QUEUE_SIZE)==-1) {
115-
fprintf(stderr,"failed to listen the socket: %s\n",strerror(errno));
116-
return-1;
129+
if (listen(s,LISTEN_QUEUE_SIZE)==-1) {
130+
elog(WARNING,"failed to listen the socket: %s",strerror(errno));
131+
close(s);
132+
continue;
133+
}
134+
returns;
117135
}
118-
119-
returns;
136+
elog(WARNING,"failed to find proper protocol");
137+
return-1;
120138
}
121139

122140
staticbooladd_socket(intsock)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp