porttcp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * FreeModbus Libary: Win32 Port
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: porttcp.c,v 1.1 2007/09/12 10:15:56 wolti Exp $
  20. */
  21. /*
  22. * Design Notes:
  23. *
  24. * The xMBPortTCPInit function allocates a socket and binds the socket to
  25. * all available interfaces ( bind with INADDR_ANY ). In addition it
  26. * creates an array of event objects which is used to check the state of
  27. * the clients. On event object is used to handle new connections or
  28. * closed ones. The other objects are used on a per client basis for
  29. * processing.
  30. */
  31. /**********************************************************
  32. * Linux TCP support.
  33. * Based on Walter's project.
  34. * Modified by Steven Guo <gotop167@163.com>
  35. ***********************************************************/
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <string.h>
  40. #include <netinet/in.h>
  41. #include <unistd.h>
  42. #include <errno.h>
  43. #include <time.h>
  44. #include "port.h"
  45. /* ----------------------- Modbus includes ----------------------------------*/
  46. #include "mb.h"
  47. #include "mbport.h"
  48. #include "mbctx.h"
  49. /* ----------------------- MBAP Header --------------------------------------*/
  50. #define MB_TCP_UID 6
  51. #define MB_TCP_LEN 4
  52. #define MB_TCP_FUNC 7
  53. /* ----------------------- Defines -----------------------------------------*/
  54. #define MB_TCP_DEFAULT_PORT 502 /* TCP listening port. */
  55. #define MB_TCP_POOL_TIMEOUT 100//50 /* pool timeout for event waiting. */
  56. #define MB_TCP_READ_TIMEOUT 2000 /* Maximum timeout to wait for packets. */
  57. #define MB_TCP_READ_CYCLE 100 /* Time between checking for new data. */
  58. #define MB_TCP_DEBUG 1 /* Set to 1 for additional debug output. */
  59. #define MB_TCP_BUF_SIZE ( 256 + 7 ) /* Must hold a complete Modbus TCP frame. */
  60. #define EV_CONNECTION 0
  61. #define EV_CLIENT 1
  62. #define EV_NEVENTS EV_CLIENT + 1
  63. /* ----------------------- Static variables ---------------------------------*/
  64. SOCKET xListenSocket;
  65. SOCKET xClientSocket = INVALID_SOCKET;
  66. static fd_set allset;
  67. static UCHAR aucTCPBuf[MB_TCP_BUF_SIZE];
  68. static USHORT usTCPBufPos;
  69. static USHORT usTCPFrameBytesLeft;
  70. /* ----------------------- External functions -------------------------------*/
  71. CHAR *WsaError2String( int dwError );
  72. /* ----------------------- Static functions ---------------------------------*/
  73. BOOL prvMBTCPPortAddressToString(SOCKET xSocket, CHAR * szAddr, USHORT usBufSize );
  74. CHAR *prvMBTCPPortFrameToString(UCHAR * pucFrame, USHORT usFrameLen );
  75. static BOOL prvbMBPortAcceptClient( fmodbus_t* ctx );
  76. static void prvvMBPortReleaseClient( fmodbus_t* ctx );
  77. /* ----------------------- Begin implementation -----------------------------*/
  78. BOOL
  79. xMBTCPPortInit(fmodbus_t* ctx, USHORT usTCPPort )
  80. {
  81. USHORT usPort;
  82. struct sockaddr_in serveraddr;
  83. if( usTCPPort == 0 )
  84. {
  85. usPort = MB_TCP_DEFAULT_PORT;
  86. }
  87. else
  88. {
  89. usPort = ( USHORT ) usTCPPort;
  90. }
  91. memset( &serveraddr, 0, sizeof( serveraddr ) );
  92. serveraddr.sin_family = AF_INET;
  93. serveraddr.sin_addr.s_addr = htonl( INADDR_ANY ); //; // inet_addr("192.168.0.175")
  94. serveraddr.sin_port = htons( usPort );
  95. if( ( ctx->xListenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == -1 )
  96. {
  97. //log_info( "Create socket failed.\r\n" );
  98. return FALSE;
  99. }
  100. else if( bind( ctx->xListenSocket, ( struct sockaddr * )&serveraddr, sizeof( serveraddr ) ) == -1 )
  101. {
  102. //log_info( "Bind socket failed.\r\n" );
  103. return FALSE;
  104. }
  105. else if( listen( ctx->xListenSocket, 5 ) == -1 )
  106. {
  107. //log_info( "Listen socket failed.\r\n" );
  108. return FALSE;
  109. }
  110. FD_ZERO( &ctx->allset );
  111. FD_SET( ctx->xListenSocket, &ctx->allset );
  112. return TRUE;
  113. }
  114. void
  115. vMBTCPPortClose( fmodbus_t* ctx )
  116. {
  117. // Close all client sockets.
  118. if( ctx->xClientSocket != SOCKET_ERROR )
  119. {
  120. prvvMBPortReleaseClient( ctx );
  121. }
  122. // Close the listener socket.
  123. if( ctx->xListenSocket != SOCKET_ERROR )
  124. {
  125. close( ctx->xListenSocket );
  126. }
  127. }
  128. void
  129. vMBTCPPortDisable( fmodbus_t* ctx )
  130. {
  131. /* Close all client sockets. */
  132. if( ctx->xClientSocket != SOCKET_ERROR )
  133. {
  134. prvvMBPortReleaseClient( ctx );
  135. }
  136. }
  137. /*! \ingroup port_win32tcp
  138. *
  139. * \brief Pool the listening socket and currently connected Modbus TCP clients
  140. * for new events.
  141. * \internal
  142. *
  143. * This function checks if new clients want to connect or if already connected
  144. * clients are sending requests. If a new client is connected and there are
  145. * still client slots left (The current implementation supports only one)
  146. * then the connection is accepted and an event object for the new client
  147. * socket is activated (See prvbMBPortAcceptClient() ).
  148. * Events for already existing clients in \c FD_READ and \c FD_CLOSE. In case of
  149. * an \c FD_CLOSE the client connection is released (See prvvMBPortReleaseClient() ).
  150. * In case of an \c FD_READ command the existing data is read from the client
  151. * and if a complete frame has been received the Modbus Stack is notified.
  152. *
  153. * \return FALSE in case of an internal I/O error. For example if the internal
  154. * event objects are in an invalid state. Note that this does not include any
  155. * client errors. In all other cases returns TRUE.
  156. */
  157. BOOL
  158. xMBPortTCPPool( fmodbus_t* ctx )
  159. {
  160. int n;
  161. fd_set fread;
  162. struct timeval tval;
  163. int timeout = 0;
  164. tval.tv_sec = 0;
  165. tval.tv_usec = 5000;
  166. int ret;
  167. USHORT usLength;
  168. struct timeval tv2;
  169. struct timezone tz2;
  170. if( ctx->xClientSocket == INVALID_SOCKET )
  171. {
  172. /* Accept to client */
  173. if( ( n = select( ctx->xListenSocket + 1, &ctx->allset, NULL, NULL, NULL ) ) < 0 )
  174. {
  175. if( errno == EINTR )
  176. {
  177. ;
  178. }
  179. else
  180. {
  181. ;
  182. }
  183. }
  184. if( FD_ISSET( ctx->xListenSocket, &ctx->allset ) )
  185. {
  186. ( void )prvbMBPortAcceptClient( ctx );
  187. }
  188. }
  189. while( TRUE )
  190. {
  191. FD_ZERO( &fread );
  192. FD_SET( ctx->xClientSocket, &fread );
  193. if( ( ( ret = select( ctx->xClientSocket + 1, &fread, NULL, NULL, &tval ) ) == SOCKET_ERROR )
  194. || !ret )
  195. {
  196. //gettimeofday(&tv2,&tz2);
  197. //log_info("%s,%d.%d,ret=-1",__func__,tv2.tv_sec,tv2.tv_usec);
  198. usleep(30000); // 30 ms
  199. timeout ++;
  200. //log_info("<FMB>Wait timeout:%d!!",timeout);
  201. if(timeout >= 2000)
  202. {
  203. close( ctx->xClientSocket );
  204. ctx->xClientSocket = INVALID_SOCKET;
  205. //log_info("<FMB>timeout connect lost!!");
  206. return TRUE;
  207. }
  208. continue;
  209. }
  210. timeout = 0;
  211. if( ret > 0 )
  212. {
  213. if( FD_ISSET( ctx->xClientSocket, &fread ) )
  214. {
  215. if( ( ( ret =
  216. recv( ctx->xClientSocket, &ctx->aucTCPBuf[ctx->usTCPBufPos], ctx->usTCPFrameBytesLeft,
  217. 0 ) ) == SOCKET_ERROR ) || ( !ret ) )
  218. {
  219. close( ctx->xClientSocket );
  220. ctx->xClientSocket = INVALID_SOCKET;
  221. return TRUE;
  222. }
  223. ctx->usTCPBufPos += ret;
  224. ctx->usTCPFrameBytesLeft -= ret;
  225. if( ctx->usTCPBufPos >= MB_TCP_FUNC )
  226. {
  227. /* Length is a byte count of Modbus PDU (function code + data) and the
  228. * unit identifier. */
  229. usLength = ctx->aucTCPBuf[MB_TCP_LEN] << 8U;
  230. usLength |= ctx->aucTCPBuf[MB_TCP_LEN + 1];
  231. /* Is the frame already complete. */
  232. if( ctx->usTCPBufPos < ( MB_TCP_UID + usLength ) )
  233. {
  234. ctx->usTCPFrameBytesLeft = usLength + MB_TCP_UID - ctx->usTCPBufPos;
  235. }
  236. /* The frame is complete. */
  237. else if( ctx->usTCPBufPos == ( MB_TCP_UID + usLength ) )
  238. {
  239. ( void )xMBPortEventPost(ctx, EV_FRAME_RECEIVED );
  240. return TRUE;
  241. }
  242. /* This can not happend because we always calculate the number of bytes
  243. * to receive. */
  244. else
  245. {
  246. assert( ctx->usTCPBufPos <= ( MB_TCP_UID + usLength ) );
  247. }
  248. }
  249. }
  250. }
  251. }
  252. return TRUE;
  253. }
  254. /*!
  255. * \ingroup port_win32tcp
  256. * \brief Receives parts of a Modbus TCP frame and if complete notifies
  257. * the protocol stack.
  258. * \internal
  259. *
  260. * This function reads a complete Modbus TCP frame from the protocol stack.
  261. * It starts by reading the header with an initial request size for
  262. * usTCPFrameBytesLeft = MB_TCP_FUNC. If the header is complete the
  263. * number of bytes left can be calculated from it (See Length in MBAP header).
  264. * Further read calls are issued until the frame is complete.
  265. *
  266. * \return \c TRUE if part of a Modbus TCP frame could be processed. In case
  267. * of a communication error the function returns \c FALSE.
  268. */
  269. BOOL
  270. xMBTCPPortGetRequest(fmodbus_t* ctx, UCHAR ** ppucMBTCPFrame, USHORT * usTCPLength )
  271. {
  272. *ppucMBTCPFrame = &ctx->aucTCPBuf[0];
  273. *usTCPLength = ctx->usTCPBufPos;
  274. /* Reset the buffer. */
  275. ctx->usTCPBufPos = 0;
  276. ctx->usTCPFrameBytesLeft = MB_TCP_FUNC;
  277. return TRUE;
  278. }
  279. BOOL
  280. xMBTCPPortSendResponse(fmodbus_t* ctx, const UCHAR * pucMBTCPFrame, USHORT usTCPLength )
  281. {
  282. BOOL bFrameSent = FALSE;
  283. BOOL bAbort = FALSE;
  284. int res;
  285. int iBytesSent = 0;
  286. int iTimeOut = MB_TCP_READ_TIMEOUT;
  287. do
  288. {
  289. res = send( ctx->xClientSocket, &pucMBTCPFrame[iBytesSent], usTCPLength - iBytesSent, 0 );
  290. switch ( res )
  291. {
  292. case -1:
  293. if( iTimeOut > 0 )
  294. {
  295. iTimeOut -= MB_TCP_READ_CYCLE;
  296. usleep( MB_TCP_READ_CYCLE );
  297. }
  298. else
  299. {
  300. bAbort = TRUE;
  301. }
  302. break;
  303. case 0:
  304. prvvMBPortReleaseClient( ctx );
  305. bAbort = TRUE;
  306. break;
  307. default:
  308. iBytesSent += res;
  309. break;
  310. }
  311. }
  312. while( ( iBytesSent != usTCPLength ) && !bAbort );
  313. bFrameSent = iBytesSent == usTCPLength ? TRUE : FALSE;
  314. return bFrameSent;
  315. }
  316. void
  317. prvvMBPortReleaseClient( fmodbus_t* ctx )
  318. {
  319. ( void )recv( ctx->xClientSocket, &ctx->aucTCPBuf[0], MB_TCP_BUF_SIZE, 0 );
  320. ( void )close( ctx->xClientSocket );
  321. ctx->xClientSocket = INVALID_SOCKET;
  322. }
  323. BOOL
  324. prvbMBPortAcceptClient(fmodbus_t* ctx )
  325. {
  326. SOCKET xNewSocket;
  327. BOOL bOkay;
  328. /* Check if we can handle a new connection. */
  329. if( ctx->xClientSocket != INVALID_SOCKET )
  330. {
  331. fprintf( stderr, "can't accept new client. all connections in use.\n" );
  332. bOkay = FALSE;
  333. }
  334. else if( ( xNewSocket = accept( ctx->xListenSocket, NULL, NULL ) ) == INVALID_SOCKET )
  335. {
  336. bOkay = FALSE;
  337. }
  338. else
  339. {
  340. ctx->xClientSocket = xNewSocket;
  341. ctx->usTCPBufPos = 0;
  342. ctx->usTCPFrameBytesLeft = MB_TCP_FUNC;
  343. bOkay = TRUE;
  344. }
  345. return bOkay;
  346. }