Commit f7799a4fddc9c4598f4b5795d1ca6bca1210272b
1 parent
ca928d50
Rename 'socket' variables to 's' to avoid warning on Windows
Showing
8 changed files
with
47 additions
and
47 deletions
doc/modbus_set_socket.txt
| @@ -9,7 +9,7 @@ modbus_set_socket - set socket of the context | @@ -9,7 +9,7 @@ modbus_set_socket - set socket of the context | ||
| 9 | 9 | ||
| 10 | SYNOPSIS | 10 | SYNOPSIS |
| 11 | -------- | 11 | -------- |
| 12 | -*int modbus_set_socket(modbus_t *'ctx', int 'socket');* | 12 | +*int modbus_set_socket(modbus_t *'ctx', int 's');* |
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | DESCRIPTION | 15 | DESCRIPTION |
src/modbus-tcp.c
| @@ -470,7 +470,7 @@ static int _modbus_tcp_flush(modbus_t *ctx) | @@ -470,7 +470,7 @@ static int _modbus_tcp_flush(modbus_t *ctx) | ||
| 470 | /* Listens for any request from one or many modbus masters in TCP */ | 470 | /* Listens for any request from one or many modbus masters in TCP */ |
| 471 | int modbus_tcp_listen(modbus_t *ctx, int nb_connection) | 471 | int modbus_tcp_listen(modbus_t *ctx, int nb_connection) |
| 472 | { | 472 | { |
| 473 | - int new_socket; | 473 | + int new_s; |
| 474 | int yes; | 474 | int yes; |
| 475 | struct sockaddr_in addr; | 475 | struct sockaddr_in addr; |
| 476 | modbus_tcp_t *ctx_tcp; | 476 | modbus_tcp_t *ctx_tcp; |
| @@ -488,15 +488,15 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) | @@ -488,15 +488,15 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) | ||
| 488 | } | 488 | } |
| 489 | #endif | 489 | #endif |
| 490 | 490 | ||
| 491 | - new_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | ||
| 492 | - if (new_socket == -1) { | 491 | + new_s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 492 | + if (new_s == -1) { | ||
| 493 | return -1; | 493 | return -1; |
| 494 | } | 494 | } |
| 495 | 495 | ||
| 496 | yes = 1; | 496 | yes = 1; |
| 497 | - if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEADDR, | 497 | + if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, |
| 498 | (char *) &yes, sizeof(yes)) == -1) { | 498 | (char *) &yes, sizeof(yes)) == -1) { |
| 499 | - close(new_socket); | 499 | + close(new_s); |
| 500 | return -1; | 500 | return -1; |
| 501 | } | 501 | } |
| 502 | 502 | ||
| @@ -505,17 +505,17 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) | @@ -505,17 +505,17 @@ int modbus_tcp_listen(modbus_t *ctx, int nb_connection) | ||
| 505 | /* If the modbus port is < to 1024, we need the setuid root. */ | 505 | /* If the modbus port is < to 1024, we need the setuid root. */ |
| 506 | addr.sin_port = htons(ctx_tcp->port); | 506 | addr.sin_port = htons(ctx_tcp->port); |
| 507 | addr.sin_addr.s_addr = INADDR_ANY; | 507 | addr.sin_addr.s_addr = INADDR_ANY; |
| 508 | - if (bind(new_socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) { | ||
| 509 | - close(new_socket); | 508 | + if (bind(new_s, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
| 509 | + close(new_s); | ||
| 510 | return -1; | 510 | return -1; |
| 511 | } | 511 | } |
| 512 | 512 | ||
| 513 | - if (listen(new_socket, nb_connection) == -1) { | ||
| 514 | - close(new_socket); | 513 | + if (listen(new_s, nb_connection) == -1) { |
| 514 | + close(new_s); | ||
| 515 | return -1; | 515 | return -1; |
| 516 | } | 516 | } |
| 517 | 517 | ||
| 518 | - return new_socket; | 518 | + return new_s; |
| 519 | } | 519 | } |
| 520 | 520 | ||
| 521 | int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) | 521 | int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) |
| @@ -526,7 +526,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) | @@ -526,7 +526,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) | ||
| 526 | struct addrinfo ai_hints; | 526 | struct addrinfo ai_hints; |
| 527 | const char *node; | 527 | const char *node; |
| 528 | const char *service; | 528 | const char *service; |
| 529 | - int new_socket; | 529 | + int new_s; |
| 530 | modbus_tcp_pi_t *ctx_tcp_pi; | 530 | modbus_tcp_pi_t *ctx_tcp_pi; |
| 531 | 531 | ||
| 532 | if (ctx == NULL) { | 532 | if (ctx == NULL) { |
| @@ -567,7 +567,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) | @@ -567,7 +567,7 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) | ||
| 567 | return -1; | 567 | return -1; |
| 568 | } | 568 | } |
| 569 | 569 | ||
| 570 | - new_socket = -1; | 570 | + new_s = -1; |
| 571 | for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { | 571 | for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { |
| 572 | int s; | 572 | int s; |
| 573 | 573 | ||
| @@ -609,22 +609,22 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) | @@ -609,22 +609,22 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection) | ||
| 609 | continue; | 609 | continue; |
| 610 | } | 610 | } |
| 611 | 611 | ||
| 612 | - new_socket = s; | 612 | + new_s = s; |
| 613 | break; | 613 | break; |
| 614 | } | 614 | } |
| 615 | freeaddrinfo(ai_list); | 615 | freeaddrinfo(ai_list); |
| 616 | 616 | ||
| 617 | - if (new_socket < 0) { | 617 | + if (new_s < 0) { |
| 618 | return -1; | 618 | return -1; |
| 619 | } | 619 | } |
| 620 | 620 | ||
| 621 | - return new_socket; | 621 | + return new_s; |
| 622 | } | 622 | } |
| 623 | 623 | ||
| 624 | /* On success, the function return a non-negative integer that is a descriptor | 624 | /* On success, the function return a non-negative integer that is a descriptor |
| 625 | for the accepted socket. On error, socket is set to -1, -1 is returned and errno | 625 | for the accepted socket. On error, socket is set to -1, -1 is returned and errno |
| 626 | is set appropriately. */ | 626 | is set appropriately. */ |
| 627 | -int modbus_tcp_accept(modbus_t *ctx, int *socket) | 627 | +int modbus_tcp_accept(modbus_t *ctx, int *s) |
| 628 | { | 628 | { |
| 629 | struct sockaddr_in addr; | 629 | struct sockaddr_in addr; |
| 630 | socklen_t addrlen; | 630 | socklen_t addrlen; |
| @@ -637,14 +637,14 @@ int modbus_tcp_accept(modbus_t *ctx, int *socket) | @@ -637,14 +637,14 @@ int modbus_tcp_accept(modbus_t *ctx, int *socket) | ||
| 637 | addrlen = sizeof(addr); | 637 | addrlen = sizeof(addr); |
| 638 | #ifdef HAVE_ACCEPT4 | 638 | #ifdef HAVE_ACCEPT4 |
| 639 | /* Inherit socket flags and use accept4 call */ | 639 | /* Inherit socket flags and use accept4 call */ |
| 640 | - ctx->s = accept4(*socket, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC); | 640 | + ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC); |
| 641 | #else | 641 | #else |
| 642 | - ctx->s = accept(*socket, (struct sockaddr *)&addr, &addrlen); | 642 | + ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen); |
| 643 | #endif | 643 | #endif |
| 644 | 644 | ||
| 645 | if (ctx->s == -1) { | 645 | if (ctx->s == -1) { |
| 646 | - close(*socket); | ||
| 647 | - *socket = -1; | 646 | + close(*s); |
| 647 | + *s = -1; | ||
| 648 | return -1; | 648 | return -1; |
| 649 | } | 649 | } |
| 650 | 650 | ||
| @@ -656,7 +656,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *socket) | @@ -656,7 +656,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *socket) | ||
| 656 | return ctx->s; | 656 | return ctx->s; |
| 657 | } | 657 | } |
| 658 | 658 | ||
| 659 | -int modbus_tcp_pi_accept(modbus_t *ctx, int *socket) | 659 | +int modbus_tcp_pi_accept(modbus_t *ctx, int *s) |
| 660 | { | 660 | { |
| 661 | struct sockaddr_storage addr; | 661 | struct sockaddr_storage addr; |
| 662 | socklen_t addrlen; | 662 | socklen_t addrlen; |
| @@ -667,10 +667,10 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *socket) | @@ -667,10 +667,10 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *socket) | ||
| 667 | } | 667 | } |
| 668 | 668 | ||
| 669 | addrlen = sizeof(addr); | 669 | addrlen = sizeof(addr); |
| 670 | - ctx->s = accept(*socket, (void *)&addr, &addrlen); | 670 | + ctx->s = accept(*s, (void *)&addr, &addrlen); |
| 671 | if (ctx->s == -1) { | 671 | if (ctx->s == -1) { |
| 672 | - close(*socket); | ||
| 673 | - *socket = -1; | 672 | + close(*s); |
| 673 | + *s = -1; | ||
| 674 | } | 674 | } |
| 675 | 675 | ||
| 676 | if (ctx->debug) { | 676 | if (ctx->debug) { |
src/modbus-tcp.h
| @@ -53,11 +53,11 @@ MODBUS_BEGIN_DECLS | @@ -53,11 +53,11 @@ MODBUS_BEGIN_DECLS | ||
| 53 | 53 | ||
| 54 | EXPORT modbus_t* modbus_new_tcp(const char *ip_address, int port); | 54 | EXPORT modbus_t* modbus_new_tcp(const char *ip_address, int port); |
| 55 | EXPORT int modbus_tcp_listen(modbus_t *ctx, int nb_connection); | 55 | EXPORT int modbus_tcp_listen(modbus_t *ctx, int nb_connection); |
| 56 | -EXPORT int modbus_tcp_accept(modbus_t *ctx, int *socket); | 56 | +EXPORT int modbus_tcp_accept(modbus_t *ctx, int *s); |
| 57 | 57 | ||
| 58 | EXPORT modbus_t* modbus_new_tcp_pi(const char *node, const char *service); | 58 | EXPORT modbus_t* modbus_new_tcp_pi(const char *node, const char *service); |
| 59 | EXPORT int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection); | 59 | EXPORT int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection); |
| 60 | -EXPORT int modbus_tcp_pi_accept(modbus_t *ctx, int *socket); | 60 | +EXPORT int modbus_tcp_pi_accept(modbus_t *ctx, int *s); |
| 61 | 61 | ||
| 62 | MODBUS_END_DECLS | 62 | MODBUS_END_DECLS |
| 63 | 63 |
src/modbus.c
| @@ -1565,14 +1565,14 @@ int modbus_set_error_recovery(modbus_t *ctx, | @@ -1565,14 +1565,14 @@ int modbus_set_error_recovery(modbus_t *ctx, | ||
| 1565 | return 0; | 1565 | return 0; |
| 1566 | } | 1566 | } |
| 1567 | 1567 | ||
| 1568 | -int modbus_set_socket(modbus_t *ctx, int socket) | 1568 | +int modbus_set_socket(modbus_t *ctx, int s) |
| 1569 | { | 1569 | { |
| 1570 | if (ctx == NULL) { | 1570 | if (ctx == NULL) { |
| 1571 | errno = EINVAL; | 1571 | errno = EINVAL; |
| 1572 | return -1; | 1572 | return -1; |
| 1573 | } | 1573 | } |
| 1574 | 1574 | ||
| 1575 | - ctx->s = socket; | 1575 | + ctx->s = s; |
| 1576 | return 0; | 1576 | return 0; |
| 1577 | } | 1577 | } |
| 1578 | 1578 |
src/modbus.h
| @@ -156,7 +156,7 @@ typedef enum | @@ -156,7 +156,7 @@ typedef enum | ||
| 156 | 156 | ||
| 157 | EXPORT int modbus_set_slave(modbus_t* ctx, int slave); | 157 | EXPORT int modbus_set_slave(modbus_t* ctx, int slave); |
| 158 | EXPORT int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery); | 158 | EXPORT int modbus_set_error_recovery(modbus_t *ctx, modbus_error_recovery_mode error_recovery); |
| 159 | -EXPORT int modbus_set_socket(modbus_t *ctx, int socket); | 159 | +EXPORT int modbus_set_socket(modbus_t *ctx, int s); |
| 160 | EXPORT int modbus_get_socket(modbus_t *ctx); | 160 | EXPORT int modbus_get_socket(modbus_t *ctx); |
| 161 | 161 | ||
| 162 | EXPORT int modbus_get_response_timeout(modbus_t *ctx, struct timeval *timeout); | 162 | EXPORT int modbus_get_response_timeout(modbus_t *ctx, struct timeval *timeout); |
tests/bandwidth-server-one.c
| @@ -36,7 +36,7 @@ enum { | @@ -36,7 +36,7 @@ enum { | ||
| 36 | 36 | ||
| 37 | int main(int argc, char *argv[]) | 37 | int main(int argc, char *argv[]) |
| 38 | { | 38 | { |
| 39 | - int socket = -1; | 39 | + int s = -1; |
| 40 | modbus_t *ctx = NULL; | 40 | modbus_t *ctx = NULL; |
| 41 | modbus_mapping_t *mb_mapping = NULL; | 41 | modbus_mapping_t *mb_mapping = NULL; |
| 42 | int rc; | 42 | int rc; |
| @@ -59,8 +59,8 @@ int main(int argc, char *argv[]) | @@ -59,8 +59,8 @@ int main(int argc, char *argv[]) | ||
| 59 | 59 | ||
| 60 | if (use_backend == TCP) { | 60 | if (use_backend == TCP) { |
| 61 | ctx = modbus_new_tcp("127.0.0.1", 1502); | 61 | ctx = modbus_new_tcp("127.0.0.1", 1502); |
| 62 | - socket = modbus_tcp_listen(ctx, 1); | ||
| 63 | - modbus_tcp_accept(ctx, &socket); | 62 | + s = modbus_tcp_listen(ctx, 1); |
| 63 | + modbus_tcp_accept(ctx, &s); | ||
| 64 | 64 | ||
| 65 | } else { | 65 | } else { |
| 66 | ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1); | 66 | ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1); |
| @@ -92,8 +92,8 @@ int main(int argc, char *argv[]) | @@ -92,8 +92,8 @@ int main(int argc, char *argv[]) | ||
| 92 | printf("Quit the loop: %s\n", modbus_strerror(errno)); | 92 | printf("Quit the loop: %s\n", modbus_strerror(errno)); |
| 93 | 93 | ||
| 94 | modbus_mapping_free(mb_mapping); | 94 | modbus_mapping_free(mb_mapping); |
| 95 | - if (socket != -1) { | ||
| 96 | - close(socket); | 95 | + if (s != -1) { |
| 96 | + close(s); | ||
| 97 | } | 97 | } |
| 98 | /* For RTU, skipped by TCP (no TCP connect) */ | 98 | /* For RTU, skipped by TCP (no TCP connect) */ |
| 99 | modbus_close(ctx); | 99 | modbus_close(ctx); |
tests/random-test-server.c
| @@ -26,7 +26,7 @@ | @@ -26,7 +26,7 @@ | ||
| 26 | 26 | ||
| 27 | int main(void) | 27 | int main(void) |
| 28 | { | 28 | { |
| 29 | - int socket = -1; | 29 | + int s = -1; |
| 30 | modbus_t *ctx; | 30 | modbus_t *ctx; |
| 31 | modbus_mapping_t *mb_mapping; | 31 | modbus_mapping_t *mb_mapping; |
| 32 | 32 | ||
| @@ -41,8 +41,8 @@ int main(void) | @@ -41,8 +41,8 @@ int main(void) | ||
| 41 | return -1; | 41 | return -1; |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | - socket = modbus_tcp_listen(ctx, 1); | ||
| 45 | - modbus_tcp_accept(ctx, &socket); | 44 | + s = modbus_tcp_listen(ctx, 1); |
| 45 | + modbus_tcp_accept(ctx, &s); | ||
| 46 | 46 | ||
| 47 | for (;;) { | 47 | for (;;) { |
| 48 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; | 48 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| @@ -60,8 +60,8 @@ int main(void) | @@ -60,8 +60,8 @@ int main(void) | ||
| 60 | 60 | ||
| 61 | printf("Quit the loop: %s\n", modbus_strerror(errno)); | 61 | printf("Quit the loop: %s\n", modbus_strerror(errno)); |
| 62 | 62 | ||
| 63 | - if (socket != -1) { | ||
| 64 | - close(socket); | 63 | + if (s != -1) { |
| 64 | + close(s); | ||
| 65 | } | 65 | } |
| 66 | modbus_mapping_free(mb_mapping); | 66 | modbus_mapping_free(mb_mapping); |
| 67 | modbus_close(ctx); | 67 | modbus_close(ctx); |
tests/unit-test-server.c
| @@ -32,7 +32,7 @@ enum { | @@ -32,7 +32,7 @@ enum { | ||
| 32 | 32 | ||
| 33 | int main(int argc, char*argv[]) | 33 | int main(int argc, char*argv[]) |
| 34 | { | 34 | { |
| 35 | - int socket = -1; | 35 | + int s = -1; |
| 36 | modbus_t *ctx; | 36 | modbus_t *ctx; |
| 37 | modbus_mapping_t *mb_mapping; | 37 | modbus_mapping_t *mb_mapping; |
| 38 | int rc; | 38 | int rc; |
| @@ -124,11 +124,11 @@ int main(int argc, char*argv[]) | @@ -124,11 +124,11 @@ int main(int argc, char*argv[]) | ||
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | if (use_backend == TCP) { | 126 | if (use_backend == TCP) { |
| 127 | - socket = modbus_tcp_listen(ctx, 1); | ||
| 128 | - modbus_tcp_accept(ctx, &socket); | 127 | + s = modbus_tcp_listen(ctx, 1); |
| 128 | + modbus_tcp_accept(ctx, &s); | ||
| 129 | } else if (use_backend == TCP_PI) { | 129 | } else if (use_backend == TCP_PI) { |
| 130 | - socket = modbus_tcp_pi_listen(ctx, 1); | ||
| 131 | - modbus_tcp_pi_accept(ctx, &socket); | 130 | + s = modbus_tcp_pi_listen(ctx, 1); |
| 131 | + modbus_tcp_pi_accept(ctx, &s); | ||
| 132 | } else { | 132 | } else { |
| 133 | rc = modbus_connect(ctx); | 133 | rc = modbus_connect(ctx); |
| 134 | if (rc == -1) { | 134 | if (rc == -1) { |
| @@ -187,8 +187,8 @@ int main(int argc, char*argv[]) | @@ -187,8 +187,8 @@ int main(int argc, char*argv[]) | ||
| 187 | printf("Quit the loop: %s\n", modbus_strerror(errno)); | 187 | printf("Quit the loop: %s\n", modbus_strerror(errno)); |
| 188 | 188 | ||
| 189 | if (use_backend == TCP) { | 189 | if (use_backend == TCP) { |
| 190 | - if (socket != -1) { | ||
| 191 | - close(socket); | 190 | + if (s != -1) { |
| 191 | + close(s); | ||
| 192 | } | 192 | } |
| 193 | } | 193 | } |
| 194 | modbus_mapping_free(mb_mapping); | 194 | modbus_mapping_free(mb_mapping); |