| 1 | /* |
| 2 | * Copyright © Stéphane Raimbault <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <signal.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <modbus.h> |
| 15 | |
| 16 | #if defined(_WIN32) |
| 17 | #include <ws2tcpip.h> |
| 18 | #else |
| 19 | #include <arpa/inet.h> |
| 20 | #ifdef HAVE_NETINET_IN_H |
| 21 | #include <netinet/in.h> |
| 22 | #endif /* HAVE_NETINET_IN_H */ |
| 23 | #include <sys/select.h> |
| 24 | #include <sys/socket.h> |
| 25 | #endif |
| 26 | |
| 27 | #define NB_CONNECTION 5 |
| 28 | |
| 29 | static modbus_t *ctx = NULL; |
| 30 | static modbus_mapping_t *mb_mapping; |
| 31 | |
| 32 | static int server_socket = -1; |
| 33 | |
| 34 | static void close_sigint(int dummy) |
| 35 | { |
| 36 | if (server_socket != -1) { |
| 37 | close(server_socket); |
| 38 | } |
| 39 | modbus_free(ctx); |
| 40 | modbus_mapping_free(mb_mapping); |
| 41 | |
| 42 | exit(dummy); |
| 43 | } |
| 44 | |
| 45 | int main(void) |
| 46 | { |
| 47 | uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH]; |
| 48 | int master_socket; |
| 49 | int rc; |
| 50 | fd_set refset; |
| 51 | fd_set rdset; |
| 52 | /* Maximum file descriptor number */ |
| 53 | int fdmax; |
| 54 | |
| 55 | ctx = modbus_new_tcp("127.0.0.1", 1502); |
| 56 | |
| 57 | mb_mapping = |
| 58 | modbus_mapping_new(MODBUS_MAX_READ_BITS, 0, MODBUS_MAX_READ_REGISTERS, 0); |
| 59 | if (mb_mapping == NULL) { |
| 60 | fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno)); |
| 61 | modbus_free(ctx); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | server_socket = modbus_tcp_listen(ctx, NB_CONNECTION); |
| 66 | if (server_socket == -1) { |
| 67 | fprintf(stderr, "Unable to listen TCP connection\n"); |
| 68 | modbus_free(ctx); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | signal(SIGINT, close_sigint); |
| 73 | |
| 74 | /* Clear the reference set of socket */ |
| 75 | FD_ZERO(&refset); |
| 76 | /* Add the server socket (modbus_tcp_listen already guarantees the |
| 77 | * descriptor is below FD_SETSIZE, but check defensively before FD_SET). */ |
| 78 | if (server_socket >= FD_SETSIZE) { |
| 79 | fprintf(stderr, |
| 80 | "Server socket %d exceeds FD_SETSIZE (%d)\n", |
| 81 | server_socket, |
| 82 | FD_SETSIZE); |
| 83 | close(server_socket); |
| 84 | modbus_free(ctx); |
| 85 | return -1; |
| 86 | } |
| 87 | FD_SET(server_socket, &refset); |
| 88 | |
| 89 | /* Keep track of the max file descriptor */ |
| 90 | fdmax = server_socket; |
| 91 | |
| 92 | for (;;) { |
| 93 | rdset = refset; |
| 94 | if (select(fdmax + 1, &rdset, NULL, NULL, NULL) == -1) { |
| 95 | perror("Server select() failure."); |
| 96 | close_sigint(1); |
| 97 | } |
| 98 | |
| 99 | /* Run through the existing connections looking for data to be |
| 100 | * read */ |
| 101 | for (master_socket = 0; master_socket <= fdmax; master_socket++) { |
| 102 | |
| 103 | if (!FD_ISSET(master_socket, &rdset)) { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | if (master_socket == server_socket) { |
| 108 | /* A client is asking a new connection */ |
| 109 | socklen_t addrlen; |
| 110 | struct sockaddr_in clientaddr; |
| 111 | int newfd; |
| 112 | |
| 113 | /* Handle new connections */ |
| 114 | addrlen = sizeof(clientaddr); |
| 115 | memset(&clientaddr, 0, sizeof(clientaddr)); |
| 116 | newfd = accept(server_socket, (struct sockaddr *) &clientaddr, &addrlen); |
| 117 | if (newfd == -1) { |
| 118 | perror("Server accept() error"); |
| 119 | } else if (newfd >= FD_SETSIZE) { |
| 120 | /* fd_set/FD_SET cannot represent a descriptor >= FD_SETSIZE; |
| 121 | * setting it would corrupt the stack. Refuse the connection |
| 122 | * instead. A real server should use poll()/epoll(). */ |
| 123 | fprintf(stderr, |
| 124 | "Refusing socket %d: exceeds FD_SETSIZE (%d)\n", |
| 125 | newfd, |
| 126 | FD_SETSIZE); |
| 127 | close(newfd); |
| 128 | } else { |
| 129 | FD_SET(newfd, &refset); |
| 130 | |
| 131 | if (newfd > fdmax) { |
| 132 | /* Keep track of the maximum */ |
| 133 | fdmax = newfd; |
| 134 | } |
| 135 | printf("New connection from %s:%d on socket %d\n", |
| 136 | inet_ntoa(clientaddr.sin_addr), |
| 137 | clientaddr.sin_port, |
| 138 | newfd); |
| 139 | } |
| 140 | } else { |
| 141 | modbus_set_socket(ctx, master_socket); |
| 142 | rc = modbus_receive(ctx, query); |
| 143 | if (rc > 0) { |
| 144 | modbus_reply(ctx, query, rc, mb_mapping); |
| 145 | } else if (rc == -1) { |
| 146 | /* This example server in ended on connection closing or |
| 147 | * any errors. */ |
| 148 | printf("Connection closed on socket %d\n", master_socket); |
| 149 | close(master_socket); |
| 150 | |
| 151 | /* Remove from reference set */ |
| 152 | FD_CLR(master_socket, &refset); |
| 153 | |
| 154 | if (master_socket == fdmax) { |
| 155 | fdmax--; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |