Skip to content

Commit 8b1740f

Browse files
committed
session REFACTOR do not mix return value and socket
1 parent 62ffe7c commit 8b1740f

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/session_client.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
18411841
API int
18421842
nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
18431843
{
1844-
int sock;
1844+
int ret, sock;
18451845
char *host = NULL;
18461846
uint16_t port, idx;
18471847

@@ -1852,11 +1852,11 @@ nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session)
18521852
return -1;
18531853
}
18541854

1855-
sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, &client_opts.ch_bind_lock, timeout,
1856-
&host, &port, &idx);
1857-
if (sock < 1) {
1855+
ret = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, &client_opts.ch_bind_lock, timeout,
1856+
&host, &port, &idx, &sock);
1857+
if (ret < 1) {
18581858
free(host);
1859-
return sock;
1859+
return ret;
18601860
}
18611861

18621862
/* configure keepalives */

src/session_p.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,13 @@ int nc_sock_listen_inet(const char *address, uint16_t port);
948948
* @param[out] host Host of the remote peer. Can be NULL.
949949
* @param[out] port Port of the new connection. Can be NULL.
950950
* @param[out] idx Index of the bind that was accepted. Can be NULL.
951-
* @return Accepted socket of the new connection, -1 on error.
951+
* @param[out] sock Accepted socket, if any.
952+
* @return -1 on error.
953+
* @return 0 on timeout.
954+
* @return 1 if a socket was accepted.
952955
*/
953956
int nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t *bind_lock, int timeout,
954-
char **host, uint16_t *port, uint16_t *idx);
957+
char **host, uint16_t *port, uint16_t *idx, int *sock);
955958

956959
/**
957960
* @brief Gets an endpoint structure based on its name.

src/session_server.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,14 @@ sock_host_inet6(const struct sockaddr_in6 *addr, char **host, uint16_t *port)
502502

503503
int
504504
nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t *bind_lock, int timeout, char **host,
505-
uint16_t *port, uint16_t *idx)
505+
uint16_t *port, uint16_t *idx, int *sock)
506506
{
507507
uint16_t i, j, pfd_count, client_port;
508508
char *client_address;
509509
struct pollfd *pfd;
510510
struct sockaddr_storage saddr;
511511
socklen_t saddr_len = sizeof(saddr);
512-
int ret, client_sock, sock = -1, flags;
512+
int ret, client_sock, server_sock = -1, flags;
513513

514514
pfd = malloc(bind_count * sizeof *pfd);
515515
NC_CHECK_ERRMEM_RET(!pfd, -1);
@@ -525,7 +525,7 @@ nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t
525525
if (binds[i].pollin) {
526526
binds[i].pollin = 0;
527527
/* leftover pollin */
528-
sock = binds[i].sock;
528+
server_sock = binds[i].sock;
529529
break;
530530
}
531531
pfd[pfd_count].fd = binds[i].sock;
@@ -535,7 +535,7 @@ nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t
535535
++pfd_count;
536536
}
537537

538-
if (sock == -1) {
538+
if (server_sock == -1) {
539539
/* poll for a new connection */
540540
ret = nc_poll(pfd, pfd_count, timeout);
541541
if (ret < 1) {
@@ -558,7 +558,7 @@ nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t
558558

559559
if (!ret) {
560560
/* the last socket with an event, use it */
561-
sock = pfd[j].fd;
561+
server_sock = pfd[j].fd;
562562
break;
563563
} else {
564564
/* just remember the event for next time */
@@ -568,15 +568,15 @@ nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t
568568
}
569569
}
570570
free(pfd);
571-
if (sock == -1) {
571+
if (server_sock == -1) {
572572
ERRINT;
573573
/* UNLOCK */
574574
pthread_mutex_unlock(bind_lock);
575575
return -1;
576576
}
577577

578578
/* accept connection */
579-
client_sock = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
579+
client_sock = accept(server_sock, (struct sockaddr *)&saddr, &saddr_len);
580580
if (client_sock < 0) {
581581
ERR(NULL, "Accept failed (%s).", strerror(errno));
582582
/* UNLOCK */
@@ -628,7 +628,9 @@ nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, pthread_mutex_t
628628
}
629629
/* UNLOCK */
630630
pthread_mutex_unlock(bind_lock);
631-
return client_sock;
631+
632+
*sock = client_sock;
633+
return 1;
632634

633635
fail:
634636
close(client_sock);
@@ -2302,12 +2304,12 @@ nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session)
23022304
goto cleanup;
23032305
}
23042306

2305-
ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, &server_opts.bind_lock, timeout, &host, &port, &bind_idx);
2306-
if (ret < 0) {
2307+
ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, &server_opts.bind_lock, timeout, &host,
2308+
&port, &bind_idx, &sock);
2309+
if (ret < 1) {
23072310
msgtype = (!ret ? NC_MSG_WOULDBLOCK : NC_MSG_ERROR);
23082311
goto cleanup;
23092312
}
2310-
sock = ret;
23112313

23122314
/* configure keepalives */
23132315
if (nc_sock_configure_ka(sock, &server_opts.endpts[bind_idx].ka)) {

0 commit comments

Comments
 (0)