| 1 |
|
| /****************************************************************************/
|
| 2 |
|
| /* */
|
| 3 |
|
| /* Program name: MQIsdp protocol C Language implementation */
|
| 4 |
|
| /* */
|
| 5 |
|
| /* Description: This source file contains the TCP/IP interface code. All */
|
| 6 |
|
| /* platform differences with regard to TCP/IP imlementations */
|
| 7 |
|
| /* are handled in here. */
|
| 8 |
|
| /* */
|
| 9 |
|
| /* Statement: Licensed Materials - Property of IBM */
|
| 10 |
|
| /* */
|
| 11 |
|
| /* MQSeries SupportPac IA93 */
|
| 12 |
|
| /* (C) Copyright IBM Corp. 2002 */
|
| 13 |
|
| /* */
|
| 14 |
|
| /****************************************************************************/
|
| 15 |
|
| /* Version @(#) IA93/ship/msptcp.c, SupportPacs, S000 1.3 03/11/28 16:44:03 */
|
| 16 |
|
| /* */
|
| 17 |
|
| /* Function: */
|
| 18 |
|
| /* */
|
| 19 |
|
| /* The main functions that are exposed to callers are mspTCPReadMsg, */
|
| 20 |
|
| /* mspTCPWrite, mspTCPInitialise and mspTCPDisconnect. */
|
| 21 |
|
| /* All low level socket functions are wrapped e.g. socket() is wrapped by */
|
| 22 |
|
| /* msp_socket() to assist with masking platform differences. */
|
| 23 |
|
| /* */
|
| 24 |
|
| /****************************************************************************/
|
| 25 |
|
| /* */
|
| 26 |
|
| /* Change history: */
|
| 27 |
|
| /* */
|
| 28 |
|
| /* V1.0 19-02-2003 IRH Initial release */
|
| 29 |
|
| /* */
|
| 30 |
|
| /*==========================================================================*/
|
| 31 |
|
| /* Module Name: msptcp.c */
|
| 32 |
|
| #include <mspdmn.h>
|
| 33 |
|
| #include <mspscada.h>
|
| 34 |
|
|
|
| 35 |
|
| #ifndef WIN32
|
| 36 |
|
| #ifdef MSP_SINGLE_THREAD
|
| 37 |
|
| #include <signal.h>
|
| 38 |
0 |
| static void connect_alarm(int s) { return; }
|
| 39 |
|
| #endif
|
| 40 |
|
| #endif
|
| 41 |
|
|
|
| 42 |
|
| /* Prototypes for wrapper functions of the standard socket functions */
|
| 43 |
|
| static int msp_socket( int af, int type, int protocol, int *pError );
|
| 44 |
|
| static int msp_inet_addr( const char *sp, struct in_addr *inadrp );
|
| 45 |
|
| static int msp_connect(int s, struct sockaddr *destaddr, int addrlen );
|
| 46 |
|
| static int msp_close( int s );
|
| 47 |
|
| static int msp_send( int s, void *pBuf, int len, int flags, int *pError );
|
| 48 |
|
| static int msp_recv( int s, void *pBuf, int len, int flags, int *pError );
|
| 49 |
|
| static int mspTcpGetLastError( void );
|
| 50 |
|
|
|
| 51 |
|
| /* Do DNS resolution */
|
| 52 |
|
| /* Returns the dotted ip address (a.b.c.d) as a string on success */
|
| 53 |
|
| /* Returns NULL error if the hostname cannot be resolved */
|
| 54 |
|
| /* pHconn->tcpParms.lastError contains the error code which is */
|
| 55 |
|
| /* returned if the application uses MQIsdp_status() to query the */
|
| 56 |
|
| /* connection state. */
|
| 57 |
7 |
| char* mspTCPGetHostByName( HCONNCB *pHconn, char *pHostName ) {
|
| 58 |
|
| struct hostent *pHostEntry;
|
| 59 |
|
| struct in_addr sinAddr;
|
| 60 |
|
|
|
| 61 |
7 |
| pHostEntry = gethostbyname( pHostName );
|
| 62 |
|
|
|
| 63 |
7 |
▶ | if ( pHostEntry != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHostEntry != NULL ) {
|
6 |
1 |
7 |
|
| 64 |
6 |
| memcpy( &sinAddr, pHostEntry->h_addr_list[0], pHostEntry->h_length );
|
| 65 |
6 |
| return inet_ntoa( sinAddr );
|
| 66 |
|
| } else {
|
| 67 |
1 |
▶ | if ( pHconn->tcpParms.lastError == 0L ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->tcpParms.lastError == 0L ) {
|
1 |
0 |
1 |
|
| 68 |
1 |
| pHconn->tcpParms.lastError = MSP_TCP_HOST_ERROR | MQISDP_HOSTNAME_NOT_FOUND;
|
| 69 |
|
| }
|
| 70 |
1 |
| return NULL;
|
| 71 |
|
| }
|
| 72 |
|
| }
|
| 73 |
|
|
|
| 74 |
|
| /* Create a socket and connect to the IP address and port specified */
|
| 75 |
|
| /* Returns 0 on success, 1 on error */
|
| 76 |
|
| /* pHconn->tcpParms.lastError contains the specific error code, */
|
| 77 |
|
| /* which is interpreted by the MQIsdp_status() API call. */
|
| 78 |
8 |
| int mspTCPConnect( HCONNCB *pHconn, u_short port, char *ipAddr ) {
|
| 79 |
|
| int connrc;
|
| 80 |
8 |
| int rc = 0;
|
| 81 |
|
| struct sockaddr_in servAddr;
|
| 82 |
|
|
|
| 83 |
8 |
| pHconn->tcpParms.sockfd = msp_socket( AF_INET, SOCK_STREAM, 0, &connrc );
|
| 84 |
|
|
|
| 85 |
8 |
▶ | if ( pHconn->tcpParms.sockfd != MSP_INVALID_SOCKET ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->tcpParms.sockfd != MSP_INVALID_SOCKET ) {
|
8 |
0 |
8 |
|
| 86 |
|
|
|
| 87 |
8 |
| servAddr.sin_family = AF_INET;
|
| 88 |
8 |
| servAddr.sin_port = htons(port);
|
| 89 |
|
|
|
| 90 |
8 |
| msp_inet_addr( ipAddr, &(servAddr.sin_addr) );
|
| 91 |
|
|
|
| 92 |
8 |
▶ | if ( msp_connect( pHconn->tcpParms.sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr) ) < 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( msp_connect( pHconn->tcpParms.sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr) ) < 0 ) {
|
8 |
0 |
8 |
|
| 93 |
8 |
| connrc = mspTcpGetLastError();
|
| 94 |
|
| /* Close the socket */
|
| 95 |
8 |
| mspTCPDisconnect( &(pHconn->tcpParms.sockfd) );
|
| 96 |
|
|
|
| 97 |
8 |
| mspLog( LOGTCPIP, &(pHconn->comParms), "mspTCPConnect:connect error:%ld - %s(%d)\n",
|
| 98 |
|
| connrc, ipAddr, port );
|
| 99 |
|
| /* Only set a last error if it is zero otherwise don't overwrite anything */
|
| 100 |
8 |
▶ | if ( pHconn->tcpParms.lastError == 0L ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->tcpParms.lastError == 0L ) {
|
5 |
3 |
8 |
|
| 101 |
5 |
| pHconn->tcpParms.lastError = connrc | MSP_TCP_CONN_ERROR;
|
| 102 |
|
| }
|
| 103 |
8 |
| rc = 1;
|
| 104 |
|
| }
|
| 105 |
|
| } else {
|
| 106 |
|
| /* Only set a last error if it is zero otherwise don't overwrite anything */
|
| 107 |
0 |
▶ | if ( pHconn->tcpParms.lastError == 0L ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->tcpParms.lastError == 0L ) {
|
0 |
0 |
0 |
|
| 108 |
0 |
| pHconn->tcpParms.lastError = connrc | MSP_TCP_SOCK_ERROR;
|
| 109 |
0 |
| mspLog( LOGERROR, &(pHconn->comParms), "mspTCPConnect:socket error:%ld\n", connrc );
|
| 110 |
|
| }
|
| 111 |
0 |
| rc = 1;
|
| 112 |
|
| }
|
| 113 |
|
|
|
| 114 |
8 |
▶ | if ( rc == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) {
|
0 |
8 |
8 |
|
| 115 |
0 |
| pHconn->tcpParms.lastError = 0L;
|
| 116 |
0 |
| mspLog( LOGNORMAL, &(pHconn->comParms), "mspTCPConnect:connect success - %s(%d)\n", ipAddr, port );
|
| 117 |
|
| }
|
| 118 |
|
|
|
| 119 |
8 |
| return rc;
|
| 120 |
|
| }
|
| 121 |
|
|
|
| 122 |
|
| /* This function loops through all the servers provided on the connect call */
|
| 123 |
|
| /* and connects to each in the order supplied until a successful connection is made */
|
| 124 |
|
| /* or the list of servers is exhausted. */
|
| 125 |
|
| /* If a successful connection is made then all other potential servers are removed */
|
| 126 |
|
| /* from the list. If mspTCPInitialise is called when reconnecting then there will */
|
| 127 |
|
| /* only be one server in the list (as all others have been removed), so that is the */
|
| 128 |
|
| /* one it will use. */
|
| 129 |
|
| /* Returns 0 on success, otherwise 1 */
|
| 130 |
8 |
| int mspTCPInitialise( HCONNCB *pHconn ) {
|
| 131 |
8 |
| int rc = 1;
|
| 132 |
|
|
|
| 133 |
8 |
| rc = mspTCPConnect( pHconn, pHconn->tcpParms.brokerPort, pHconn->tcpParms.brokerAddress );
|
| 134 |
|
|
|
| 135 |
8 |
▶ | if ( rc == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) {
|
0 |
8 |
8 |
|
| 136 |
|
| /* Send a message to the receive task to tell it what the socket descriptor is */
|
| 137 |
0 |
| mspInitReceiveTask( pHconn );
|
| 138 |
|
| }
|
| 139 |
|
|
|
| 140 |
8 |
| return rc;
|
| 141 |
|
| }
|
| 142 |
|
|
|
| 143 |
|
| /* Close the TCP/IP socket */
|
| 144 |
|
| /* Returns 0 */
|
| 145 |
19 |
| int mspTCPDisconnect( int *pSockfd ) {
|
| 146 |
|
|
|
| 147 |
19 |
▶ | if ( *pSockfd != MSP_INVALID_SOCKET ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( *pSockfd != MSP_INVALID_SOCKET ) {
|
8 |
11 |
19 |
|
| 148 |
8 |
| msp_close( *pSockfd );
|
| 149 |
8 |
| *pSockfd = MSP_INVALID_SOCKET;
|
| 150 |
|
| }
|
| 151 |
|
|
|
| 152 |
19 |
| return 0;
|
| 153 |
|
| }
|
| 154 |
|
|
|
| 155 |
|
| /* Write data to the socket */
|
| 156 |
|
| /* Returns 0 on success, -1 on error */
|
| 157 |
0 |
| int mspTCPWrite( HCONNCB *pHconn, size_t msgLen, char *msgData ) {
|
| 158 |
|
|
|
| 159 |
|
| size_t nleft;
|
| 160 |
0 |
| int nwritten = 0;
|
| 161 |
|
| char *ptr;
|
| 162 |
0 |
| int mspErrno = 0;
|
| 163 |
|
|
|
| 164 |
0 |
| ptr = msgData;
|
| 165 |
0 |
| nleft = msgLen;
|
| 166 |
0 |
▶ | while ( nleft > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( nleft > 0 ) {
|
0 |
0 |
0 |
|
| 167 |
0 |
▶ | if ( (nwritten = msp_send( pHconn->tcpParms.sockfd, ptr, nleft, 0, &mspErrno )) <= 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( (nwritten = msp_send( pHconn->tcpParms.sockfd, ptr, nleft, 0, &mspErrno )) <= 0 ) {
|
0 |
0 |
0 |
|
| 168 |
0 |
▶ | if ( mspErrno == EINTR ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspErrno == EINTR ) {
|
0 |
0 |
0 |
|
| 169 |
0 |
| nwritten = 0;
|
| 170 |
|
| } else {
|
| 171 |
0 |
| pHconn->tcpParms.lastError = mspTcpGetLastError() | MSP_TCP_SEND_ERROR;
|
| 172 |
0 |
| mspLog( LOGERROR, &(pHconn->comParms), "TCP/IP send error %ld\n",
|
| 173 |
0 |
| (pHconn->tcpParms.lastError & MSP_GET_LAST_ERROR) );
|
| 174 |
0 |
| return -1;
|
| 175 |
|
| }
|
| 176 |
|
| }
|
| 177 |
0 |
| nleft -= nwritten;
|
| 178 |
0 |
| ptr += nwritten;
|
| 179 |
|
| }
|
| 180 |
|
|
|
| 181 |
0 |
| mspLog( LOGTCPIP, &(pHconn->comParms), "TCP/IP output sent: %ld bytes\n", nwritten );
|
| 182 |
0 |
| mspLogHex( LOGTCPIP, &(pHconn->comParms), nwritten, msgData );
|
| 183 |
|
|
|
| 184 |
0 |
| return 0;
|
| 185 |
|
| }
|
| 186 |
|
|
|
| 187 |
|
| /* Read data from the socket, increasing the size of the supplied receive buffer if required. */
|
| 188 |
|
| /* Multiple messages may be waiting to be read from the socket, so a bulk read of all data */
|
| 189 |
|
| /* available isn't done. This function initially reads two bytes (Fixed header and first */
|
| 190 |
|
| /* length byte), then reads 1 byte at a time up to the end of the remaining length field. */
|
| 191 |
|
| /* the remaining length has been calculated the remaining part of the message can be read in */
|
| 192 |
|
| /* a single read operation. */
|
| 193 |
|
| /* Returns 0 on success, -1 on error */
|
| 194 |
0 |
| int mspTCPReadMsg( int sockfd, MSPCMN *pComParms, int *pLastError,
|
| 195 |
|
| long *msgLen, long *bufLen, char **buffer ) {
|
| 196 |
|
|
|
| 197 |
0 |
| int nread = -1;
|
| 198 |
0 |
| long rlLength = 2; /* Read the first 2 bytes of data */
|
| 199 |
|
| int nleft;
|
| 200 |
|
| char *ptr;
|
| 201 |
0 |
| int rc = 0;
|
| 202 |
0 |
| int mspErrno = 0;
|
| 203 |
|
|
|
| 204 |
0 |
| ptr = *buffer;
|
| 205 |
0 |
| *msgLen = 0;
|
| 206 |
|
|
|
| 207 |
|
| /* Keep reading until we either read without being interrupted or there is an error */
|
| 208 |
|
| /* rlLength indicates how much data to receive */
|
| 209 |
|
| /* This first loop reads the MQIsdp fixed header and remaining length bytes */
|
| 210 |
0 |
▶ | while ( rlLength > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( rlLength > 0 ) {
|
0 |
0 |
0 |
|
| 211 |
0 |
▶ | if ( (nread = msp_recv( sockfd, ptr, rlLength, 0, &mspErrno )) == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( (nread = msp_recv( sockfd, ptr, rlLength, 0, &mspErrno )) == 0 ) {
|
0 |
0 |
0 |
|
| 212 |
|
| /* If the socket is readable, but 0 bytes are read then this */
|
| 213 |
|
| /* indicates that the remote end has closed the socket */
|
| 214 |
0 |
| *msgLen = 0;
|
| 215 |
0 |
▶ | if ( pLastError != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pLastError != NULL ) {
|
0 |
0 |
0 |
|
| 216 |
0 |
| *pLastError = MQISDP_SOCKET_CLOSED | MSP_CONN_ERROR ;
|
| 217 |
|
| }
|
| 218 |
0 |
| rlLength = 0;
|
| 219 |
0 |
| rc = -1;
|
| 220 |
0 |
| break;
|
| 221 |
0 |
▶ | } else if ( (nread < 0) && (mspErrno != EINTR) ) {
|
Branch: 2 branches
| Branch | True | False | Total |
| } else if ( (nread < 0) && (mspErrno != EINTR) ) {
|
0 |
0 |
0 |
| } else if ( (nread < 0) && (mspErrno != EINTR) ) {
|
0 |
0 |
0 |
|
| 222 |
|
| /* Something else has gone wrong.... */
|
| 223 |
0 |
| *msgLen = 0;
|
| 224 |
0 |
▶ | if ( pLastError != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pLastError != NULL ) {
|
0 |
0 |
0 |
|
| 225 |
0 |
| *pLastError = mspErrno | MSP_TCP_RECV_ERROR;
|
| 226 |
|
| }
|
| 227 |
0 |
| mspLog( LOGERROR, pComParms, "TCP/IP recv error %ld\n", mspErrno );
|
| 228 |
0 |
| rlLength = 0;
|
| 229 |
0 |
| rc = -1;
|
| 230 |
0 |
| break;
|
| 231 |
|
| } else {
|
| 232 |
|
| /* Record how much data we have read */
|
| 233 |
0 |
| *msgLen += nread;
|
| 234 |
0 |
| rlLength = 1;
|
| 235 |
|
|
|
| 236 |
|
| /* The first byte is the MQIsdp fixed header */
|
| 237 |
0 |
▶ | if ( *msgLen > 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( *msgLen > 1 ) {
|
0 |
0 |
0 |
|
| 238 |
|
| /* Move to the last byte read and check the highest order bit */
|
| 239 |
|
| /* If it is off then we have read all the remaining length bytes */
|
| 240 |
0 |
| ptr += nread - 1;
|
| 241 |
0 |
▶ | if ( !(*ptr & 128) ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( !(*ptr & 128) ) {
|
0 |
0 |
0 |
|
| 242 |
0 |
| rlLength = 0;
|
| 243 |
|
| }
|
| 244 |
|
| }
|
| 245 |
0 |
| ptr++;
|
| 246 |
|
| }
|
| 247 |
|
| }
|
| 248 |
|
|
|
| 249 |
|
| /* Now decode the remaining length bytes */
|
| 250 |
|
| /* The number of bytes used will be stored in rlLength */
|
| 251 |
|
| /* The decoded length will be stored in nleft */
|
| 252 |
0 |
▶ | if ( (*msgLen == 0) || (mspDecodeFHeaderLength( *msgLen-1, &rlLength, &nleft, *buffer+1 ) < 0) ) {
|
Branch: 2 branches
| Branch | True | False | Total |
| if ( (*msgLen == 0) || (mspDecodeFHeaderLength( *msgLen-1, &rlLength, &nleft, *buffer+1 ) < 0) ) {
|
0 |
0 |
0 |
| if ( (*msgLen == 0) || (mspDecodeFHeaderLength( *msgLen-1, &rlLength, &nleft, *buffer+1 ) < 0) ) {
|
0 |
0 |
0 |
|
| 253 |
|
| /* We are unable to decode the data length */
|
| 254 |
0 |
| *msgLen = 0;
|
| 255 |
0 |
| return -1;
|
| 256 |
|
| }
|
| 257 |
|
|
|
| 258 |
|
| /* Work out the message length we have received */
|
| 259 |
|
| /* Fixed header (1) + # of remaining length bytes (rlLength) */
|
| 260 |
|
| /* + the data length following (nleft) */
|
| 261 |
0 |
| *msgLen = nleft + rlLength + 1;
|
| 262 |
|
|
|
| 263 |
|
| /* Take a look at the fixed header. If it is a message of type publish then */
|
| 264 |
|
| /* allocate an additional byte that indicates if it has been released or not. */
|
| 265 |
|
| /* This additional byte is used by the persistence. */
|
| 266 |
0 |
▶ | if ( ((*buffer)[0] & MSP_GET_MSG_TYPE) == MSP_PUBLISH ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( ((*buffer)[0] & MSP_GET_MSG_TYPE) == MSP_PUBLISH ) {
|
0 |
0 |
0 |
|
| 267 |
0 |
| (*msgLen)++;
|
| 268 |
|
| }
|
| 269 |
|
|
|
| 270 |
0 |
▶ | if ( *msgLen > *bufLen ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( *msgLen > *bufLen ) {
|
0 |
0 |
0 |
|
| 271 |
0 |
| *buffer = (char*)mspRealloc( pComParms, *buffer, *msgLen, *bufLen );
|
| 272 |
0 |
| *bufLen = *msgLen;
|
| 273 |
0 |
▶ | if ( *buffer == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( *buffer == NULL ) {
|
0 |
0 |
0 |
|
| 274 |
0 |
| *bufLen = 0;
|
| 275 |
0 |
| *msgLen = 0;
|
| 276 |
0 |
| return -1;
|
| 277 |
|
| }
|
| 278 |
|
| }
|
| 279 |
|
|
|
| 280 |
0 |
| ptr = *buffer + rlLength + 1;
|
| 281 |
0 |
▶ | while ( nleft > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( nleft > 0 ) {
|
0 |
0 |
0 |
|
| 282 |
0 |
▶ | if ( (nread = msp_recv( sockfd, ptr, nleft, 0, &mspErrno )) <= 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( (nread = msp_recv( sockfd, ptr, nleft, 0, &mspErrno )) <= 0 ) {
|
0 |
0 |
0 |
|
| 283 |
|
| /* If we have been interrupted spin around and retry the recv */
|
| 284 |
0 |
▶ | if ( mspErrno != EINTR ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspErrno != EINTR ) {
|
0 |
0 |
0 |
|
| 285 |
|
| /* Something else has gone wrong.... */
|
| 286 |
0 |
▶ | if ( pLastError != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pLastError != NULL ) {
|
0 |
0 |
0 |
|
| 287 |
0 |
| *pLastError = mspErrno | MSP_TCP_RECV_ERROR;
|
| 288 |
|
| }
|
| 289 |
0 |
| mspLog( LOGERROR, pComParms, "TCP/IP recv error %ld\n", mspErrno );
|
| 290 |
0 |
| *bufLen = 0;
|
| 291 |
0 |
| *msgLen = 0;
|
| 292 |
0 |
| rc = -1;
|
| 293 |
0 |
| break;
|
| 294 |
|
| }
|
| 295 |
|
| } else {
|
| 296 |
0 |
| ptr += nread;
|
| 297 |
0 |
| nleft -= nread;
|
| 298 |
|
| }
|
| 299 |
|
| }
|
| 300 |
|
|
|
| 301 |
0 |
| mspLog( LOGTCPIP, pComParms, "TCP/IP input received: %ld bytes\n", *msgLen );
|
| 302 |
0 |
| mspLogHex( LOGTCPIP, pComParms, *msgLen, *buffer );
|
| 303 |
|
|
|
| 304 |
0 |
| return rc;
|
| 305 |
|
| }
|
| 306 |
|
|
|
| 307 |
|
| /* Initialise the TCP/IP stack */
|
| 308 |
|
| /* This function is called once when the send task starts up */
|
| 309 |
7 |
| int mspTCPInit( void ) {
|
| 310 |
|
| #ifdef WIN32
|
| 311 |
|
| WORD winsockVer = 0x0202;
|
| 312 |
|
| WSADATA wsd;
|
| 313 |
|
|
|
| 314 |
|
| WSAStartup( winsockVer, &wsd );
|
| 315 |
|
| #endif
|
| 316 |
|
|
|
| 317 |
7 |
| return 0;
|
| 318 |
|
| }
|
| 319 |
|
|
|
| 320 |
|
| /* Terminate the TCP/IP stack */
|
| 321 |
|
| /* This function is called once when the send task shuts down */
|
| 322 |
0 |
| int mspTCPTerm( void ) {
|
| 323 |
|
| #ifdef WIN32
|
| 324 |
|
| WSACleanup();
|
| 325 |
|
| #endif
|
| 326 |
|
|
|
| 327 |
0 |
| return 0;
|
| 328 |
|
| }
|
| 329 |
|
|
|
| 330 |
|
| /*##############################################################################*/
|
| 331 |
|
| /* Below are wrapper functions for the standard TCP/IP socket functions. */
|
| 332 |
|
| /* Specifics of different TCP/IP stacks aree handled in these wrapper functions */
|
| 333 |
|
| /*##############################################################################*/
|
| 334 |
|
| /* mspSelect */
|
| 335 |
|
| /* Inputs: */
|
| 336 |
|
| /* sockfd - Socket descriptor */
|
| 337 |
|
| /* mSecs - millisecond timeout */
|
| 338 |
|
| /* If sockfd is an invalid socket msp_select will sleep for mSecs milliseconds */
|
| 339 |
|
| /* If mSecs is -1 then msp_select will block infinitely on sockfd, unless */
|
| 340 |
|
| /* sockfd is invalid. In this case it will return immediately. */
|
| 341 |
53 |
| int msp_select( int sockfd, long mSecs ) {
|
| 342 |
|
| #ifndef MSP_FUSION_SOCKETS
|
| 343 |
|
| fd_set rset;
|
| 344 |
53 |
| int maxfdp1 = 0;
|
| 345 |
|
| struct timeval timeout;
|
| 346 |
53 |
| struct timeval *pTimeout = &timeout;
|
| 347 |
|
|
|
| 348 |
53 |
▶ | if ( mSecs < 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mSecs < 0 ) {
|
0 |
53 |
53 |
|
| 349 |
0 |
| pTimeout = NULL;
|
| 350 |
|
| } else {
|
| 351 |
53 |
| timeout.tv_sec = (int)mSecs / 1000; /* seconds */
|
| 352 |
53 |
| timeout.tv_usec = (mSecs % 1000) * 1000; /* microseconds */
|
| 353 |
|
| }
|
| 354 |
|
|
|
| 355 |
53 |
▶ | if ( sockfd != MSP_INVALID_SOCKET ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( sockfd != MSP_INVALID_SOCKET ) {
|
0 |
53 |
53 |
|
| 356 |
|
| /* Initialise the descriptor set */
|
| 357 |
0 |
▶ | FD_ZERO( &rset );
|
Branch: 1 branch
| Branch | True | False | Total |
| FD_ZERO( &rset );
|
0 |
0 |
0 |
|
| 358 |
|
| /* Add the current socket descriptor */
|
| 359 |
0 |
| FD_SET( (u_int)sockfd, &rset );
|
| 360 |
0 |
| maxfdp1 = sockfd + 1;
|
| 361 |
|
| }
|
| 362 |
|
|
|
| 363 |
|
| #ifdef MSP_KN_SOCKETS
|
| 364 |
|
| return kn_select( maxfdp1, &rset, NULL, NULL, pTimeout );
|
| 365 |
|
| #else /* MSP_KN_SOCKETS */
|
| 366 |
|
| #if defined(WIN32)
|
| 367 |
|
| /* If there are no descriptors to wait on select will fail on Windows */
|
| 368 |
|
| if ( maxfdp1 == 0 ) {
|
| 369 |
|
| Sleep( mSecs );
|
| 370 |
|
| /* Return 0 because we have timed out rather than receiving data */
|
| 371 |
|
| return 0;
|
| 372 |
|
| }
|
| 373 |
|
| #endif /* WIN32 */
|
| 374 |
53 |
| return select( maxfdp1, &rset, NULL, NULL, pTimeout );
|
| 375 |
|
| #endif /* MSP_KN_SOCKETS */
|
| 376 |
|
| #else /* MSP_FUSION_SOCKETS */
|
| 377 |
|
| struct sel sel_arr[1];
|
| 378 |
|
| int err;
|
| 379 |
|
|
|
| 380 |
|
| if ( sockfd == MSP_INVALID_SOCKET ) {
|
| 381 |
|
| if( mSecs > 0 ) {
|
| 382 |
|
| DELAY( mSecs );
|
| 383 |
|
| }
|
| 384 |
|
| return 0;
|
| 385 |
|
| } else {
|
| 386 |
|
| sel_arr[0].se_fd = sockfd;
|
| 387 |
|
| sel_arr[0].se_inflags = READ_NOTIFY;
|
| 388 |
|
| return nselect( sel_arr, 1, &mSecs, usel_nilpfi, (u32)0, &err );
|
| 389 |
|
| }
|
| 390 |
|
| #endif /* MSP_FUSION_SOCKETS */
|
| 391 |
|
| /* return 0; - unreachable statement */
|
| 392 |
|
| }
|
| 393 |
|
|
|
| 394 |
8 |
| static int msp_socket( int af, int type, int protocol, int *pError ) {
|
| 395 |
|
| #ifdef MSP_FUSION_SOCKETS
|
| 396 |
|
| return socket( AF_INET, SOCK_STREAM, 0, pError );
|
| 397 |
|
| #else
|
| 398 |
|
| int sockRc;
|
| 399 |
|
| #ifdef MSP_KN_SOCKETS
|
| 400 |
|
| sockRc = kn_socket( AF_INET, SOCK_STREAM, 0 );
|
| 401 |
|
| #else
|
| 402 |
8 |
| sockRc = socket( AF_INET, SOCK_STREAM, 0 );
|
| 403 |
|
| #endif
|
| 404 |
8 |
| *pError = mspTcpGetLastError();
|
| 405 |
8 |
| return sockRc;
|
| 406 |
|
| #endif
|
| 407 |
|
| }
|
| 408 |
|
|
|
| 409 |
8 |
| static int msp_inet_addr( const char *sp, struct in_addr *inadrp ) {
|
| 410 |
|
| #ifdef MSP_KN_SOCKETS
|
| 411 |
|
| return kn_inet_addr( sp, inadrp);
|
| 412 |
|
| #else
|
| 413 |
|
| /* AIX and SOLARIS only support inet_addr */
|
| 414 |
|
| /* On LINUX inet_addr or inet_aton works */
|
| 415 |
|
| #if defined( WIN32 ) || defined(UNIX) || defined( MSP_FUSION_SOCKETS )
|
| 416 |
|
| long inetAddr;
|
| 417 |
8 |
| inetAddr = inet_addr( sp );
|
| 418 |
8 |
▶ | if ( inetAddr != INADDR_NONE ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( inetAddr != INADDR_NONE ) {
|
8 |
0 |
8 |
|
| 419 |
8 |
| memcpy( inadrp, &inetAddr, sizeof(inadrp) );
|
| 420 |
8 |
| return 0;
|
| 421 |
|
| }
|
| 422 |
0 |
| return 1;
|
| 423 |
|
| #else
|
| 424 |
|
| return inet_aton( sp, inadrp );
|
| 425 |
|
| #endif
|
| 426 |
|
| #endif
|
| 427 |
|
| }
|
| 428 |
|
|
|
| 429 |
8 |
| static int msp_connect(int s, struct sockaddr *destaddr, int addrlen ) {
|
| 430 |
|
| #ifdef MSP_KN_SOCKETS
|
| 431 |
|
| return kn_connect( s, destaddr, addrlen );
|
| 432 |
|
| #else
|
| 433 |
|
| #ifdef WIN32
|
| 434 |
|
| if ( connect( s, destaddr, addrlen ) == SOCKET_ERROR )
|
| 435 |
|
| return -1;
|
| 436 |
|
| else
|
| 437 |
|
| return 0;
|
| 438 |
|
| #elif defined(MSP_SINGLE_THREAD) && defined(SIGALRM)
|
| 439 |
|
| /* This is a frig to prevent blocking in connect() for several
|
| 440 |
|
| minutes if the remote host is not reachable */
|
| 441 |
|
| int rc;
|
| 442 |
|
| struct sigaction act, oact;
|
| 443 |
8 |
| memset(&act, 0, sizeof(act));
|
| 444 |
8 |
| act.sa_handler = connect_alarm;
|
| 445 |
|
| /* Note: don't set sa_flags = SA_RESTART */
|
| 446 |
8 |
| sigaction(SIGALRM, &act, &oact);
|
| 447 |
8 |
| alarm(15);
|
| 448 |
8 |
| rc = connect( s, destaddr, addrlen );
|
| 449 |
8 |
| alarm(0);
|
| 450 |
8 |
| sigaction(SIGALRM, &oact, NULL);
|
| 451 |
8 |
▶ | if (rc < 0 && errno == EINTR) errno = ETIMEDOUT;
|
Branch: 2 branches
| Branch | True | False | Total |
| if (rc < 0 && errno == EINTR) errno = ETIMEDOUT;
|
8 |
0 |
8 |
| if (rc < 0 && errno == EINTR) errno = ETIMEDOUT;
|
0 |
8 |
8 |
|
| 452 |
8 |
| return rc;
|
| 453 |
|
| #else
|
| 454 |
|
| return connect( s, destaddr, addrlen );
|
| 455 |
|
| #endif
|
| 456 |
|
| #endif
|
| 457 |
|
| }
|
| 458 |
|
|
|
| 459 |
16 |
| static int mspTcpGetLastError( void ) {
|
| 460 |
|
| #ifdef MSP_KN_SOCKETS
|
| 461 |
|
| return kn_errno();
|
| 462 |
|
| #else
|
| 463 |
|
| #ifdef WIN32
|
| 464 |
|
| return WSAGetLastError();
|
| 465 |
|
| #else
|
| 466 |
16 |
| return errno;
|
| 467 |
|
| #endif
|
| 468 |
|
| #endif
|
| 469 |
|
| }
|
| 470 |
|
|
|
| 471 |
8 |
| static int msp_close( int s ) {
|
| 472 |
|
| #ifdef MSP_KN_SOCKETS
|
| 473 |
|
| return kn_close( s )
|
| 474 |
|
| #else
|
| 475 |
|
| #ifdef WIN32
|
| 476 |
|
| return closesocket( s );
|
| 477 |
|
| #else
|
| 478 |
8 |
| return close( s );
|
| 479 |
|
| #endif
|
| 480 |
|
| #endif
|
| 481 |
|
| }
|
| 482 |
|
|
|
| 483 |
0 |
| static int msp_send( int s, void *pBuf, int len, int flags, int *pError ) {
|
| 484 |
|
| #ifdef MSP_FUSION_SOCKETS
|
| 485 |
|
| return send( s, pBuf, len, flags, pError );
|
| 486 |
|
| #else
|
| 487 |
|
| int sockRc;
|
| 488 |
|
| #ifdef MSP_KN_SOCKETS
|
| 489 |
|
| sockRc = kn_send( s, pBuf, len, flags );
|
| 490 |
|
| #else
|
| 491 |
0 |
| sockRc = send( s, pBuf, len, flags );
|
| 492 |
|
| #endif
|
| 493 |
0 |
| *pError = mspTcpGetLastError();
|
| 494 |
0 |
| return sockRc;
|
| 495 |
|
| #endif
|
| 496 |
|
| }
|
| 497 |
|
|
|
| 498 |
0 |
| static int msp_recv( int s, void *pBuf, int len, int flags, int *pError ) {
|
| 499 |
|
| #ifdef MSP_FUSION_SOCKETS
|
| 500 |
|
| return recv( s, pBuf, len, flags, pError );
|
| 501 |
|
| #else
|
| 502 |
|
| int sockRc;
|
| 503 |
|
| #ifdef MSP_KN_SOCKETS
|
| 504 |
|
| sockRc = kn_recv( s, pBuf, len, flags );
|
| 505 |
|
| #else
|
| 506 |
0 |
| sockRc = recv( s, pBuf, len, flags );
|
| 507 |
|
| #endif
|
| 508 |
0 |
| *pError = mspTcpGetLastError();
|
| 509 |
0 |
| return sockRc;
|
| 510 |
|
| #endif
|
| 511 |
|
| }
|