| 1 |
|
| /****************************************************************************/
|
| 2 |
|
| /* */
|
| 3 |
|
| /* Program name: MQIsdp protocol C Language implementation */
|
| 4 |
|
| /* */
|
| 5 |
|
| /* Description: This source file contains functions for building, parsing, */
|
| 6 |
|
| /* sending and receiving MQIsdp messgaes. */
|
| 7 |
|
| /* */
|
| 8 |
|
| /* Statement: Licensed Materials - Property of IBM */
|
| 9 |
|
| /* */
|
| 10 |
|
| /* MQSeries SupportPac IA93 */
|
| 11 |
|
| /* (C) Copyright IBM Corp. 2002 */
|
| 12 |
|
| /* */
|
| 13 |
|
| /****************************************************************************/
|
| 14 |
|
| /* Version @(#) IA93/ship/mspscada.c, SupportPacs, S000 1.4 03/11/28 16:43:48 */
|
| 15 |
|
| /* */
|
| 16 |
|
| /* Function: */
|
| 17 |
|
| /* */
|
| 18 |
|
| /* The send functions in this source file talk through to the TCP/IP layer */
|
| 19 |
|
| /* and queues the data if required. Functions also exist to retry sending */
|
| 20 |
|
| /* data and that retry connecting in the event of a connection being broken.*/
|
| 21 |
|
| /* */
|
| 22 |
|
| /****************************************************************************/
|
| 23 |
|
| /* */
|
| 24 |
|
| /* Change history: */
|
| 25 |
|
| /* */
|
| 26 |
|
| /* V1.0 19-02-2003 IRH Initial release */
|
| 27 |
|
| /* V1.2 21-10-2003 IRH Split a line of code in function */
|
| 28 |
|
| /* mspStorePublication into two lines because it was*/
|
| 29 |
|
| /* not understood by all compilers. */
|
| 30 |
|
| /* */
|
| 31 |
|
| /*==========================================================================*/
|
| 32 |
|
| /* Module Name: mspscada.c */
|
| 33 |
|
| #include <mspdmn.h>
|
| 34 |
|
| #include <mspscada.h>
|
| 35 |
|
|
|
| 36 |
|
| /* mspBuildScadaConnectMsg */
|
| 37 |
|
| /* Validate the data passed into the API and build a connect message */
|
| 38 |
|
| /* Returns NULL on error and sets pHconn->apiReturnCode */
|
| 39 |
7 |
| void* mspBuildScadaConnectMsg( HCONNCB *pHconn, long bufLength,
|
| 40 |
|
| void *ipcBuffer, long *msgLength ){
|
| 41 |
7 |
| long fHLength = 0; /* Fixed Header: Depends upon data length */
|
| 42 |
7 |
| long wtLength = 0; /* Will topic length */
|
| 43 |
7 |
| long wtLengthUt = 0; /* Will topic length untrimmed */
|
| 44 |
7 |
| long wmLength = 0; /* Will message length */
|
| 45 |
7 |
| long rLength = 12; /* remaining length. Var Header: 12 bytes long for a connect*/
|
| 46 |
7 |
| long ciLength = 0; /* Client ID length */
|
| 47 |
|
| char *tmpPtr;
|
| 48 |
|
| CONN_PARMS *pMspCp;
|
| 49 |
7 |
| long totalLength = 0;
|
| 50 |
7 |
| char *connectMsg = NULL;
|
| 51 |
7 |
| char *pWillMsg = NULL;
|
| 52 |
7 |
| char *pWillTopic = NULL;
|
| 53 |
7 |
| size_t cleanStartOffset = 0;
|
| 54 |
|
| u_short keepAlive;
|
| 55 |
|
|
|
| 56 |
7 |
| pHconn->apiReturnCode = MQISDP_OK;
|
| 57 |
7 |
| pMspCp = (CONN_PARMS*)ipcBuffer;
|
| 58 |
7 |
| *msgLength = 0;
|
| 59 |
|
|
|
| 60 |
|
| /* Add 2 to the length of the clientId to allow for UTF encoding */
|
| 61 |
7 |
| ciLength = strlen( pMspCp->clientId );
|
| 62 |
7 |
| rLength += ciLength + 2;
|
| 63 |
|
|
|
| 64 |
|
| /* See if a will message is to be sent */
|
| 65 |
7 |
▶ | if ( pMspCp->options & MQISDP_WILL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspCp->options & MQISDP_WILL ) {
|
0 |
7 |
7 |
|
| 66 |
|
|
|
| 67 |
|
| /* Step to the Will Topic Length */
|
| 68 |
0 |
| tmpPtr = (char*)pMspCp + sizeof(CONN_PARMS);
|
| 69 |
|
|
|
| 70 |
|
| /* If a topic > 0 length exists add it to the message, otherwise */
|
| 71 |
|
| /* return an error. Add 2 to the topic length to allow for UTF encoding */
|
| 72 |
0 |
| memcpy( &wtLengthUt, tmpPtr, sizeof(long) );
|
| 73 |
0 |
▶ | if ( wtLengthUt > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( wtLengthUt > 0 ) {
|
0 |
0 |
0 |
|
| 74 |
0 |
| pWillTopic = tmpPtr + sizeof(long);
|
| 75 |
|
| /* Trim any white space off the end of the topic and set the trimmed length accordingly */
|
| 76 |
0 |
| wtLength = mspCharTrim( ' ', wtLengthUt, pWillTopic );
|
| 77 |
0 |
| rLength += wtLength + 2;
|
| 78 |
|
| } else {
|
| 79 |
0 |
| pHconn->apiReturnCode = MQISDP_NO_WILL_TOPIC;
|
| 80 |
0 |
| return NULL;
|
| 81 |
|
| }
|
| 82 |
|
|
|
| 83 |
|
| /* Step over the Will Topic Length and Will Topic to the Will Message Length */
|
| 84 |
0 |
| tmpPtr += wtLengthUt + sizeof(long);
|
| 85 |
|
|
|
| 86 |
|
| /* If a will message > 0 length exists- compare long, so cast long* */
|
| 87 |
|
| /* Add 2 to the message length to allow for UTF encoding */
|
| 88 |
0 |
| memcpy( &wmLength, tmpPtr, sizeof(long) );
|
| 89 |
0 |
▶ | if ( wmLength > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( wmLength > 0 ) {
|
0 |
0 |
0 |
|
| 90 |
0 |
| pWillMsg = tmpPtr + sizeof(long);
|
| 91 |
|
| /* Trim any white space off the end of the message and set the new length accordingly */
|
| 92 |
0 |
| wmLength = mspCharTrim( ' ', wmLength, pWillMsg );
|
| 93 |
0 |
| rLength += wmLength + 2;
|
| 94 |
|
| }
|
| 95 |
|
| }
|
| 96 |
|
|
|
| 97 |
|
| /* Calculate the fixed header size required to hold the remaining length */
|
| 98 |
7 |
▶ | MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
Branch: 4 branches
| Branch | True | False | Total |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
7 |
0 |
7 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
|
| 99 |
|
|
|
| 100 |
7 |
| totalLength = fHLength + rLength;
|
| 101 |
|
|
|
| 102 |
7 |
▶ | if ( fHLength == -1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( fHLength == -1 ) {
|
0 |
7 |
7 |
|
| 103 |
|
| /* Data is bigger than MQIsdp can handle */
|
| 104 |
0 |
| pHconn->apiReturnCode = MQISDP_DATA_TOO_BIG;
|
| 105 |
|
| }
|
| 106 |
|
|
|
| 107 |
7 |
▶ | if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
7 |
0 |
7 |
|
| 108 |
|
| /* Now build up the connect message */
|
| 109 |
7 |
| connectMsg = (char*)mspMalloc( &(pHconn->comParms), (size_t)totalLength );
|
| 110 |
7 |
▶ | if ( connectMsg == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( connectMsg == NULL ) {
|
0 |
7 |
7 |
|
| 111 |
0 |
| pHconn->apiReturnCode = MQISDP_OUT_OF_MEMORY;
|
| 112 |
0 |
| return NULL;
|
| 113 |
|
| }
|
| 114 |
7 |
| tmpPtr = connectMsg;
|
| 115 |
|
|
|
| 116 |
|
| /* Insert the message type */
|
| 117 |
7 |
| *tmpPtr = 0x00 | MSP_CONNECT;
|
| 118 |
7 |
| tmpPtr++;
|
| 119 |
|
|
|
| 120 |
|
| /* Encode the message length */
|
| 121 |
7 |
| mspEncodeFHeaderLength( rLength, tmpPtr );
|
| 122 |
7 |
| tmpPtr = connectMsg + fHLength;
|
| 123 |
|
|
|
| 124 |
|
| /* Variable header */
|
| 125 |
|
| /* Add the protocol name */
|
| 126 |
7 |
| mspUTFEncodeString( MSP_PROTOCOL_NAME_SZ, MSP_PROTOCOL_NAME, tmpPtr );
|
| 127 |
7 |
| tmpPtr += MSP_PROTOCOL_NAME_SZ + 2;
|
| 128 |
|
|
|
| 129 |
|
| /* Add the protocol version */
|
| 130 |
7 |
| *tmpPtr = MSP_PROTOCOL_VERSION_3;
|
| 131 |
7 |
| tmpPtr++;
|
| 132 |
|
|
|
| 133 |
|
| /* Set up the connect options */
|
| 134 |
7 |
| cleanStartOffset = tmpPtr - connectMsg;
|
| 135 |
7 |
| *tmpPtr = 0x00;
|
| 136 |
7 |
▶ | if ( pMspCp->options & MQISDP_WILL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspCp->options & MQISDP_WILL ) {
|
0 |
7 |
7 |
|
| 137 |
0 |
| *tmpPtr |= MSPC_WILL;
|
| 138 |
|
| }
|
| 139 |
7 |
▶ | if ( pMspCp->options & MQISDP_WILL_RETAIN ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspCp->options & MQISDP_WILL_RETAIN ) {
|
0 |
7 |
7 |
|
| 140 |
0 |
| *tmpPtr |= MSPC_WILL_RETAIN;
|
| 141 |
|
| }
|
| 142 |
7 |
▶ | if ( pMspCp->options & MQISDP_CLEAN_START ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspCp->options & MQISDP_CLEAN_START ) {
|
3 |
4 |
7 |
|
| 143 |
3 |
| pHconn->ctrlFlags |= MSP_CLEAN_SESSION;
|
| 144 |
3 |
| *tmpPtr |= MSPC_CLEAN_START;
|
| 145 |
|
| /* If we are using clean start then the protocol library */
|
| 146 |
|
| /* should not attempt to reconnect on behalf of the application */
|
| 147 |
|
| /* because the protocol library does not know what subscriptions*/
|
| 148 |
|
| /* to register on behalf of the application. */
|
| 149 |
3 |
| pHconn->reconnect.connRetries = -1;
|
| 150 |
|
| } else {
|
| 151 |
|
| /* Switch off the clean session control flag */
|
| 152 |
4 |
| pHconn->ctrlFlags &= MSP_CLEAN_SESSION_OFF;
|
| 153 |
|
| }
|
| 154 |
|
| /* QoS 0 is the default, so only handle QoS 1 and 2 */
|
| 155 |
7 |
▶ | if ( pMspCp->options & MQISDP_QOS_2 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspCp->options & MQISDP_QOS_2 ) {
|
0 |
7 |
7 |
|
| 156 |
0 |
| *tmpPtr |= MSPC_QOS_2;
|
| 157 |
7 |
▶ | } else if ( pMspCp->options & MQISDP_QOS_1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( pMspCp->options & MQISDP_QOS_1 ) {
|
0 |
7 |
7 |
|
| 158 |
0 |
| *tmpPtr |= MSPC_QOS_1;
|
| 159 |
|
| }
|
| 160 |
7 |
| tmpPtr ++;
|
| 161 |
|
|
|
| 162 |
|
| /* Now add the KeepAliveTimer */
|
| 163 |
7 |
| keepAlive = htons( (u_short)pMspCp->keepAliveTime );
|
| 164 |
7 |
| memcpy( tmpPtr, &keepAlive, sizeof(u_short) );
|
| 165 |
7 |
| tmpPtr += sizeof(u_short);
|
| 166 |
|
|
|
| 167 |
|
| /* Payload */
|
| 168 |
|
| /* First the ClientID */
|
| 169 |
7 |
| mspUTFEncodeString( (u_short)ciLength, pMspCp->clientId, tmpPtr );
|
| 170 |
7 |
| tmpPtr += ciLength + 2;
|
| 171 |
|
|
|
| 172 |
|
| /* Next the will topic and message, if required */
|
| 173 |
7 |
▶ | if ( pMspCp->options & MQISDP_WILL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspCp->options & MQISDP_WILL ) {
|
0 |
7 |
7 |
|
| 174 |
0 |
▶ | if ( wtLength > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( wtLength > 0 ) {
|
0 |
0 |
0 |
|
| 175 |
|
| /* Add the will topic */
|
| 176 |
0 |
| mspUTFEncodeString( (u_short)wtLength, pWillTopic, tmpPtr );
|
| 177 |
0 |
| tmpPtr += wtLength + 2;
|
| 178 |
|
| }
|
| 179 |
0 |
▶ | if ( wmLength > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( wmLength > 0 ) {
|
0 |
0 |
0 |
|
| 180 |
|
| /* Add the will message */
|
| 181 |
0 |
| mspUTFEncodeString( (u_short)wmLength, pWillMsg, tmpPtr );
|
| 182 |
0 |
| tmpPtr += wmLength + 2;
|
| 183 |
|
| }
|
| 184 |
|
| }
|
| 185 |
|
|
|
| 186 |
|
| /* Take a copy of the MQIsdp connect message incase a reconnect is required */
|
| 187 |
|
| /* Turn clean start off for a reconnect message */
|
| 188 |
7 |
| pHconn->reconnect.connMsgSz = totalLength;
|
| 189 |
7 |
| pHconn->reconnect.connectMsg = (char*)mspMalloc( &(pHconn->comParms), totalLength );
|
| 190 |
7 |
▶ | if ( pHconn->reconnect.connectMsg == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->reconnect.connectMsg == NULL ) {
|
0 |
7 |
7 |
|
| 191 |
0 |
| mspFree( &(pHconn->comParms), connectMsg, totalLength );
|
| 192 |
0 |
| pHconn->apiReturnCode = MQISDP_OUT_OF_MEMORY;
|
| 193 |
0 |
| return NULL;
|
| 194 |
|
| }
|
| 195 |
7 |
| memcpy( pHconn->reconnect.connectMsg, connectMsg, totalLength );
|
| 196 |
7 |
| *(pHconn->reconnect.connectMsg + cleanStartOffset) &= MSPC_CLEAN_START_OFF;
|
| 197 |
|
|
|
| 198 |
7 |
| mspLog( LOGSCADA, &(pHconn->comParms), "CONNECT\n" );
|
| 199 |
|
|
|
| 200 |
7 |
| *msgLength = totalLength;
|
| 201 |
|
| }
|
| 202 |
|
|
|
| 203 |
7 |
| return connectMsg;
|
| 204 |
|
| }
|
| 205 |
|
|
|
| 206 |
|
| /* mspBuildScadaDisconnectMsg */
|
| 207 |
|
| /* Validate the data passed into the API and build a disconnect message */
|
| 208 |
|
| /* Returns NULL on error and sets pHconn->apiReturnCode */
|
| 209 |
4 |
| void* mspBuildScadaDisconnectMsg( HCONNCB *pHconn, long bufLength,
|
| 210 |
|
| void *ipcBuffer, long *msgLength ){
|
| 211 |
4 |
| long totalLength = 2;
|
| 212 |
4 |
| char *discMsg = NULL;
|
| 213 |
|
|
|
| 214 |
4 |
| pHconn->apiReturnCode = MQISDP_OK;
|
| 215 |
4 |
| *msgLength = 0;
|
| 216 |
|
|
|
| 217 |
|
| /* Now build up the disconnect message */
|
| 218 |
4 |
| discMsg = (char*)mspMalloc( &(pHconn->comParms), (size_t)totalLength );
|
| 219 |
|
|
|
| 220 |
4 |
▶ | if ( discMsg != NULL) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( discMsg != NULL) {
|
4 |
0 |
4 |
|
| 221 |
|
| /* Insert the message type */
|
| 222 |
4 |
| *discMsg = 0x00 | (char)MSP_DISCONNECT;
|
| 223 |
4 |
| *(discMsg+1) = 0x00;
|
| 224 |
|
|
|
| 225 |
4 |
| mspLog( LOGSCADA, &(pHconn->comParms), "DISCONNECT\n" );
|
| 226 |
|
|
|
| 227 |
4 |
| *msgLength = totalLength;
|
| 228 |
|
| } else {
|
| 229 |
0 |
| pHconn->apiReturnCode = MQISDP_OUT_OF_MEMORY;
|
| 230 |
|
| }
|
| 231 |
|
|
|
| 232 |
4 |
| return discMsg;
|
| 233 |
|
| }
|
| 234 |
|
|
|
| 235 |
|
| /* mspBuildScadaSubscribeMsg */
|
| 236 |
|
| /* Validate the data passed into the API and build a subscribe message */
|
| 237 |
|
| /* Returns NULL on error and sets pHconn->apiReturnCode */
|
| 238 |
0 |
| void* mspBuildScadaSubscribeMsg( HCONNCB *pHconn, long bufLength,
|
| 239 |
|
| void *ipcBuffer, long *msgLength ){
|
| 240 |
0 |
| long fHLength = 0; /* Fixed Header: Depends upon data length */
|
| 241 |
0 |
| long rLength = 2; /* remaining length. Var Header: 2 bytes long for a subscribe*/
|
| 242 |
|
| long tLength; /* Topic length */
|
| 243 |
|
| long tLengthUt; /* Topic length untrimmed */
|
| 244 |
|
| SUB_PARMS *pMspSp;
|
| 245 |
|
| char *endPtr;
|
| 246 |
|
| char *tmpPtr;
|
| 247 |
|
| char *topicPtr;
|
| 248 |
0 |
| char *subMsg = NULL;
|
| 249 |
0 |
| long totalLength = 0;
|
| 250 |
|
| long topicQoS;
|
| 251 |
|
| u_short msgId;
|
| 252 |
|
|
|
| 253 |
0 |
| pHconn->apiReturnCode = MQISDP_OK;
|
| 254 |
0 |
| pMspSp = (SUB_PARMS*)ipcBuffer;
|
| 255 |
0 |
| endPtr = (char*)pMspSp + pMspSp->strucLength;
|
| 256 |
0 |
| *msgLength = 0;
|
| 257 |
|
|
|
| 258 |
0 |
| tmpPtr = (char*)pMspSp + sizeof(SUB_PARMS);
|
| 259 |
|
|
|
| 260 |
0 |
▶ | while ( tmpPtr < endPtr ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( tmpPtr < endPtr ) {
|
0 |
0 |
0 |
|
| 261 |
|
| /* Get the topic length and check we are in bounds */
|
| 262 |
0 |
▶ | if ( tmpPtr <= (endPtr - sizeof(long)) ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( tmpPtr <= (endPtr - sizeof(long)) ) {
|
0 |
0 |
0 |
|
| 263 |
0 |
| memcpy( &tLengthUt, tmpPtr, sizeof(long) );
|
| 264 |
0 |
| tLength = mspCharTrim( ' ', tLengthUt, tmpPtr + sizeof(long) );
|
| 265 |
|
| /* Add the topic length + 2 for UTF encoding + 1 for the QoS */
|
| 266 |
0 |
| rLength += tLength + 3;
|
| 267 |
|
| } else {
|
| 268 |
0 |
| pHconn->apiReturnCode = MQISDP_INVALID_STRUC_LENGTH;
|
| 269 |
0 |
| break;
|
| 270 |
|
| }
|
| 271 |
|
|
|
| 272 |
|
| /* Step over the topic name length, topic name and QoS - check we are in bounds */
|
| 273 |
0 |
| tmpPtr += sizeof(long) + tLengthUt + sizeof(long);
|
| 274 |
0 |
▶ | if ( tmpPtr > endPtr ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( tmpPtr > endPtr ) {
|
0 |
0 |
0 |
|
| 275 |
0 |
| pHconn->apiReturnCode = MQISDP_INVALID_STRUC_LENGTH;
|
| 276 |
0 |
| break;
|
| 277 |
|
| }
|
| 278 |
|
| }
|
| 279 |
|
|
|
| 280 |
|
| /* Calculate the fixed header size required to hold the remaining length */
|
| 281 |
0 |
▶ | MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
Branch: 4 branches
| Branch | True | False | Total |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
|
| 282 |
|
|
|
| 283 |
0 |
| totalLength = fHLength + rLength;
|
| 284 |
|
|
|
| 285 |
|
| /* Check we have enough room to hold this data */
|
| 286 |
0 |
▶ | if ( pHconn->outQ.numBytesQueued + totalLength + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->outQ.numBytesQueued + totalLength + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
0 |
0 |
0 |
|
| 287 |
0 |
| pHconn->apiReturnCode = MQISDP_Q_FULL;
|
| 288 |
|
| }
|
| 289 |
|
|
|
| 290 |
0 |
▶ | if ( fHLength == -1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( fHLength == -1 ) {
|
0 |
0 |
0 |
|
| 291 |
|
| /* Data is bigger than MQIsdp can handle */
|
| 292 |
0 |
| pHconn->apiReturnCode = MQISDP_DATA_TOO_BIG;
|
| 293 |
|
| }
|
| 294 |
|
|
|
| 295 |
0 |
▶ | if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
0 |
0 |
0 |
|
| 296 |
|
|
|
| 297 |
|
| /* Now build up the subscribe message */
|
| 298 |
0 |
| subMsg = (char*)mspMalloc( &(pHconn->comParms), (size_t)totalLength );
|
| 299 |
0 |
▶ | if ( subMsg == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( subMsg == NULL ) {
|
0 |
0 |
0 |
|
| 300 |
0 |
| pHconn->apiReturnCode = MQISDP_OUT_OF_MEMORY;
|
| 301 |
0 |
| return NULL;
|
| 302 |
|
| }
|
| 303 |
0 |
| tmpPtr = subMsg;
|
| 304 |
|
|
|
| 305 |
|
| /* Insert the message type and QoS (always QoS 1) */
|
| 306 |
0 |
| *tmpPtr = 0x00 | (char)MSP_SUBSCRIBE | (char)MSPF_QOS_1 ;
|
| 307 |
0 |
| tmpPtr++;
|
| 308 |
|
|
|
| 309 |
|
| /* Encode the message length */
|
| 310 |
0 |
| mspEncodeFHeaderLength( rLength, tmpPtr );
|
| 311 |
0 |
| tmpPtr = subMsg + fHLength;
|
| 312 |
|
|
|
| 313 |
|
| /* Variable header */
|
| 314 |
|
| /* Now add the message ID in network byte order (big-endian) */
|
| 315 |
0 |
| msgId = htons( pHconn->nextMsgId );
|
| 316 |
0 |
| memcpy( tmpPtr, &msgId, sizeof(u_short) );
|
| 317 |
0 |
| tmpPtr += sizeof(u_short);
|
| 318 |
|
|
|
| 319 |
|
| /* Payload */
|
| 320 |
|
| /* Add topics and QoS */
|
| 321 |
|
| /* topicPtr points at the SUB_PARMS structure */
|
| 322 |
|
| /* tmpPtr points at the MQIsdp message */
|
| 323 |
0 |
| topicPtr = (char*)pMspSp + sizeof(SUB_PARMS);
|
| 324 |
|
|
|
| 325 |
0 |
▶ | while ( topicPtr < endPtr ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( topicPtr < endPtr ) {
|
0 |
0 |
0 |
|
| 326 |
|
| /* Get the topic length supplied in SUB_PARMS */
|
| 327 |
0 |
| memcpy( &tLengthUt, topicPtr, sizeof(long) );
|
| 328 |
|
|
|
| 329 |
|
| /* Get the length once spaces have been trimmed */
|
| 330 |
0 |
| tLength = mspCharTrim( ' ', tLengthUt, topicPtr + sizeof(long) );
|
| 331 |
0 |
| topicPtr += sizeof(long);
|
| 332 |
0 |
| mspUTFEncodeString( (u_short)tLength, topicPtr, tmpPtr );
|
| 333 |
|
|
|
| 334 |
|
| /* Move the pointer in the MQIsdp message and the SUB_PARMS structure */
|
| 335 |
0 |
| tmpPtr += tLength + 2;
|
| 336 |
0 |
| topicPtr += tLengthUt;
|
| 337 |
|
|
|
| 338 |
|
| /* Add the QoS to the message */
|
| 339 |
0 |
| memcpy( &topicQoS, topicPtr, sizeof(long) );
|
| 340 |
0 |
▶ | if ( topicQoS & MQISDP_QOS_2 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( topicQoS & MQISDP_QOS_2 ) {
|
0 |
0 |
0 |
|
| 341 |
0 |
| *tmpPtr = MSPS_QOS_2;
|
| 342 |
0 |
▶ | } else if ( topicQoS & MQISDP_QOS_1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( topicQoS & MQISDP_QOS_1 ) {
|
0 |
0 |
0 |
|
| 343 |
0 |
| *tmpPtr = MSPS_QOS_1;
|
| 344 |
|
| } else {
|
| 345 |
0 |
| *tmpPtr = MSPS_QOS_0;
|
| 346 |
|
| }
|
| 347 |
|
|
|
| 348 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "SUBSCRIBE,topic:<%.*s>,QoS:%d\n",
|
| 349 |
0 |
| tLength, topicPtr - tLengthUt, *tmpPtr );
|
| 350 |
|
|
|
| 351 |
0 |
| tmpPtr++;
|
| 352 |
0 |
| topicPtr += sizeof(long);
|
| 353 |
|
| }
|
| 354 |
|
|
|
| 355 |
0 |
| *msgLength = totalLength;
|
| 356 |
|
| }
|
| 357 |
|
|
|
| 358 |
0 |
| return subMsg;
|
| 359 |
|
| }
|
| 360 |
|
|
|
| 361 |
|
| /* mspBuildScadaUnsubscribeMsg */
|
| 362 |
|
| /* Validate the data passed into the API and build a unsubscribe message */
|
| 363 |
|
| /* Returns NULL on error and sets pHconn->apiReturnCode */
|
| 364 |
0 |
| void* mspBuildScadaUnsubscribeMsg( HCONNCB *pHconn, long bufLength,
|
| 365 |
|
| void *ipcBuffer, long *msgLength ){
|
| 366 |
0 |
| long fHLength = 0; /* Fixed Header: Depends upon data length */
|
| 367 |
0 |
| long rLength = 2; /* remaining length. Var Header: 2 bytes long for a subscribe*/
|
| 368 |
|
| long tLength; /* Topic length */
|
| 369 |
|
| long tLengthUt; /* Topic length untrimmed */
|
| 370 |
|
| UNSUB_PARMS *pMspUp;
|
| 371 |
|
| char *endPtr;
|
| 372 |
|
| char *tmpPtr;
|
| 373 |
|
| char *topicPtr;
|
| 374 |
0 |
| char *unsubMsg = NULL;
|
| 375 |
0 |
| long totalLength = 0;
|
| 376 |
|
| u_short msgId;
|
| 377 |
|
|
|
| 378 |
0 |
| pHconn->apiReturnCode = MQISDP_OK;
|
| 379 |
0 |
| pMspUp = (UNSUB_PARMS*)ipcBuffer;
|
| 380 |
0 |
| endPtr = (char*)pMspUp + pMspUp->strucLength;
|
| 381 |
0 |
| *msgLength = 0;
|
| 382 |
|
|
|
| 383 |
0 |
| tmpPtr = (char*)pMspUp + sizeof(UNSUB_PARMS);
|
| 384 |
|
|
|
| 385 |
0 |
▶ | while ( tmpPtr < endPtr ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( tmpPtr < endPtr ) {
|
0 |
0 |
0 |
|
| 386 |
|
| /* Get the topic length and check we are in bounds */
|
| 387 |
0 |
▶ | if ( tmpPtr <= (endPtr - sizeof(long)) ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( tmpPtr <= (endPtr - sizeof(long)) ) {
|
0 |
0 |
0 |
|
| 388 |
0 |
| memcpy( &tLengthUt, tmpPtr, sizeof(long) );
|
| 389 |
0 |
| tLength = mspCharTrim( ' ', tLengthUt, tmpPtr + sizeof(long) );
|
| 390 |
|
|
|
| 391 |
|
| /* Add the topic length + 2 for UTF encoding */
|
| 392 |
0 |
| rLength += tLength + 2;
|
| 393 |
|
| } else {
|
| 394 |
0 |
| pHconn->apiReturnCode = MQISDP_INVALID_STRUC_LENGTH;
|
| 395 |
0 |
| break;
|
| 396 |
|
| }
|
| 397 |
|
|
|
| 398 |
|
| /* Step over the topic name length and topic name - check we are in bounds */
|
| 399 |
0 |
| tmpPtr += sizeof(long) + tLengthUt;
|
| 400 |
0 |
▶ | if ( tmpPtr > endPtr ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( tmpPtr > endPtr ) {
|
0 |
0 |
0 |
|
| 401 |
0 |
| pHconn->apiReturnCode = MQISDP_INVALID_STRUC_LENGTH;
|
| 402 |
0 |
| break;
|
| 403 |
|
| }
|
| 404 |
|
| }
|
| 405 |
|
|
|
| 406 |
|
| /* Calculate the fixed header size required to hold the remaining length */
|
| 407 |
0 |
▶ | MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
Branch: 4 branches
| Branch | True | False | Total |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
|
| 408 |
|
|
|
| 409 |
0 |
| totalLength = fHLength + rLength;
|
| 410 |
|
|
|
| 411 |
|
| /* Check we have enough room to hold this data */
|
| 412 |
0 |
▶ | if ( pHconn->outQ.numBytesQueued + totalLength + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->outQ.numBytesQueued + totalLength + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
0 |
0 |
0 |
|
| 413 |
0 |
| pHconn->apiReturnCode = MQISDP_Q_FULL;
|
| 414 |
|
| }
|
| 415 |
|
|
|
| 416 |
0 |
▶ | if ( fHLength == -1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( fHLength == -1 ) {
|
0 |
0 |
0 |
|
| 417 |
|
| /* Data is bigger than MQIsdp can handle */
|
| 418 |
0 |
| pHconn->apiReturnCode = MQISDP_DATA_TOO_BIG;
|
| 419 |
|
| }
|
| 420 |
|
|
|
| 421 |
0 |
▶ | if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
0 |
0 |
0 |
|
| 422 |
|
|
|
| 423 |
|
| /* Now build up the unsubscribe message */
|
| 424 |
0 |
| unsubMsg = (char*)mspMalloc( &(pHconn->comParms), (size_t)totalLength );
|
| 425 |
0 |
▶ | if ( unsubMsg == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( unsubMsg == NULL ) {
|
0 |
0 |
0 |
|
| 426 |
0 |
| pHconn->apiReturnCode = MQISDP_OUT_OF_MEMORY;
|
| 427 |
0 |
| return NULL;
|
| 428 |
|
| }
|
| 429 |
0 |
| tmpPtr = unsubMsg;
|
| 430 |
|
|
|
| 431 |
|
| /* Insert the message type and QoS (always QoS 1) */
|
| 432 |
0 |
| *tmpPtr = 0x00 | (char)MSP_UNSUBSCRIBE | (char)MSPF_QOS_1 ;
|
| 433 |
0 |
| tmpPtr++;
|
| 434 |
|
|
|
| 435 |
|
| /* Encode the message length */
|
| 436 |
0 |
| mspEncodeFHeaderLength( rLength, tmpPtr );
|
| 437 |
0 |
| tmpPtr = unsubMsg + fHLength;
|
| 438 |
|
|
|
| 439 |
|
| /* Variable header */
|
| 440 |
|
| /* Now add the message ID in network byte order (big-endian) */
|
| 441 |
0 |
| msgId = htons( pHconn->nextMsgId );
|
| 442 |
0 |
| memcpy( tmpPtr, &msgId, sizeof(u_short) );
|
| 443 |
0 |
| tmpPtr += sizeof(u_short);
|
| 444 |
|
|
|
| 445 |
|
| /* Payload */
|
| 446 |
|
| /* Add topics */
|
| 447 |
|
| /* topicPtr points at the UNSUB_PARMS structure */
|
| 448 |
|
| /* tmpPtr points at the MQIsdp message */
|
| 449 |
0 |
| topicPtr = (char*)pMspUp + sizeof(UNSUB_PARMS);
|
| 450 |
|
|
|
| 451 |
0 |
▶ | while ( topicPtr < endPtr ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( topicPtr < endPtr ) {
|
0 |
0 |
0 |
|
| 452 |
|
| /* Get the topic length supplied in SUB_PARMS */
|
| 453 |
0 |
| memcpy( &tLengthUt, topicPtr, sizeof(long) );
|
| 454 |
|
| /* Get the length once spaces have been trimmed */
|
| 455 |
0 |
| tLength = mspCharTrim( ' ', tLengthUt, topicPtr + sizeof(long) );
|
| 456 |
0 |
| topicPtr += sizeof(long);
|
| 457 |
|
|
|
| 458 |
0 |
| mspUTFEncodeString( (u_short)tLength, topicPtr, tmpPtr );
|
| 459 |
|
| /* Move the pointer in the MQIsdp message and the SUB_PARMS structure */
|
| 460 |
0 |
| tmpPtr += tLength + 2;
|
| 461 |
0 |
| topicPtr += tLengthUt;
|
| 462 |
|
|
|
| 463 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "UNSUBSCRIBE,topic:<%.*s>\n",
|
| 464 |
0 |
| tLength, topicPtr-tLengthUt );
|
| 465 |
|
| }
|
| 466 |
|
|
|
| 467 |
0 |
| *msgLength = totalLength;
|
| 468 |
|
| }
|
| 469 |
|
|
|
| 470 |
0 |
| return unsubMsg;
|
| 471 |
|
| }
|
| 472 |
|
|
|
| 473 |
|
| /* mspBuildScadaPublishMsg */
|
| 474 |
|
| /* Validate the data passed into the API and build a publish message */
|
| 475 |
|
| /* Returns NULL on error and sets pHconn->apiReturnCode */
|
| 476 |
3 |
| void* mspBuildScadaPublishMsg( HCONNCB *pHconn, long bufLength,
|
| 477 |
|
| void *ipcBuffer, long *msgLength ){
|
| 478 |
3 |
| long fHLength = 0; /* Fixed Header: Depends upon data length */
|
| 479 |
3 |
| long rLength = 0; /* remaining length. */
|
| 480 |
|
| long dLength; /* data length */
|
| 481 |
|
| PUB_PARMS *pMspPp;
|
| 482 |
|
| char *tmpPtr;
|
| 483 |
3 |
| char *pubMsg = NULL;
|
| 484 |
3 |
| long totalLength = 0;
|
| 485 |
|
|
|
| 486 |
3 |
| pHconn->apiReturnCode = MQISDP_OK;
|
| 487 |
3 |
| pMspPp = (PUB_PARMS*)ipcBuffer;
|
| 488 |
|
|
|
| 489 |
3 |
| *msgLength = 0;
|
| 490 |
|
|
|
| 491 |
|
| /* Variable header */
|
| 492 |
|
| /* This has a UTF encoded topic name and a msgId if the QoS is 1 or 2 */
|
| 493 |
|
|
|
| 494 |
|
| /* Get the topic length */
|
| 495 |
3 |
| dLength = mspCharTrim( ' ', pMspPp->topicLength, pMspPp->topic );
|
| 496 |
3 |
| rLength += dLength + 2; /* Add topic length plus 2 (For the UTF encoding) to the message length */
|
| 497 |
3 |
▶ | if ( (pMspPp->options & MQISDP_QOS_1) || (pMspPp->options & MQISDP_QOS_2) ) {
|
Branch: 2 branches
| Branch | True | False | Total |
| if ( (pMspPp->options & MQISDP_QOS_1) || (pMspPp->options & MQISDP_QOS_2) ) {
|
1 |
2 |
3 |
| if ( (pMspPp->options & MQISDP_QOS_1) || (pMspPp->options & MQISDP_QOS_2) ) {
|
0 |
1 |
1 |
|
| 498 |
2 |
| rLength += 2; /* Also allow for a 16 bit msg ID for QoS > 0 messages */
|
| 499 |
|
| }
|
| 500 |
|
|
|
| 501 |
|
| /* Payload */
|
| 502 |
3 |
▶ | if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
3 |
0 |
3 |
|
| 503 |
3 |
| rLength += pMspPp->dataLength;
|
| 504 |
|
| }
|
| 505 |
|
|
|
| 506 |
|
| /* Calculate the fixed header size required to hold the remaining length */
|
| 507 |
3 |
▶ | MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
Branch: 4 branches
| Branch | True | False | Total |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
2 |
1 |
3 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
1 |
1 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
1 |
0 |
1 |
| MSP_CALC_FHEADER_LENGTH( rLength, fHLength );
|
0 |
0 |
0 |
|
| 508 |
|
|
|
| 509 |
3 |
| totalLength = fHLength + rLength;
|
| 510 |
|
|
|
| 511 |
|
| /* Check we have enough room to hold this data */
|
| 512 |
3 |
▶ | if ( pHconn->outQ.numBytesQueued + totalLength + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->outQ.numBytesQueued + totalLength + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
1 |
2 |
3 |
|
| 513 |
1 |
| pHconn->apiReturnCode = MQISDP_Q_FULL;
|
| 514 |
|
| }
|
| 515 |
|
|
|
| 516 |
3 |
▶ | if ( fHLength == -1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( fHLength == -1 ) {
|
0 |
3 |
3 |
|
| 517 |
|
| /* Data is bigger than MQIsdp can handle */
|
| 518 |
0 |
| pHconn->apiReturnCode = MQISDP_DATA_TOO_BIG;
|
| 519 |
|
| }
|
| 520 |
|
|
|
| 521 |
3 |
▶ | if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->apiReturnCode == MQISDP_OK ) {
|
2 |
1 |
3 |
|
| 522 |
|
|
|
| 523 |
|
| /* Now build up the publish message */
|
| 524 |
2 |
| pubMsg = (char*)mspMalloc( &(pHconn->comParms), (size_t)totalLength );
|
| 525 |
2 |
▶ | if ( pubMsg == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pubMsg == NULL ) {
|
0 |
2 |
2 |
|
| 526 |
0 |
| pHconn->apiReturnCode = MQISDP_OUT_OF_MEMORY;
|
| 527 |
0 |
| return NULL;
|
| 528 |
|
| }
|
| 529 |
2 |
| tmpPtr = pubMsg;
|
| 530 |
|
|
|
| 531 |
|
| /* Insert the message type, QoS and retain */
|
| 532 |
2 |
| *tmpPtr = 0x00 | (char)MSP_PUBLISH;
|
| 533 |
2 |
▶ | if ( pMspPp->options & MQISDP_QOS_2 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspPp->options & MQISDP_QOS_2 ) {
|
0 |
2 |
2 |
|
| 534 |
0 |
| *tmpPtr |= (char)MSPF_QOS_2;
|
| 535 |
2 |
▶ | } else if ( pMspPp->options & MQISDP_QOS_1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( pMspPp->options & MQISDP_QOS_1 ) {
|
1 |
1 |
2 |
|
| 536 |
1 |
| *tmpPtr |= (char)MSPF_QOS_1;
|
| 537 |
|
| }
|
| 538 |
|
|
|
| 539 |
2 |
▶ | if ( pMspPp->options & MQISDP_RETAIN ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pMspPp->options & MQISDP_RETAIN ) {
|
0 |
2 |
2 |
|
| 540 |
0 |
| *tmpPtr |= (char)MSPF_RETAIN;
|
| 541 |
|
| }
|
| 542 |
|
|
|
| 543 |
2 |
| tmpPtr++;
|
| 544 |
|
|
|
| 545 |
|
| /* Encode the message length */
|
| 546 |
2 |
| mspEncodeFHeaderLength( rLength, tmpPtr );
|
| 547 |
2 |
| tmpPtr = pubMsg + fHLength;
|
| 548 |
|
|
|
| 549 |
|
| /* Variable header */
|
| 550 |
|
| /* Add the topic name - dataPtr is still pointing at the topic */
|
| 551 |
2 |
| mspUTFEncodeString( (u_short)dLength, pMspPp->topic, tmpPtr );
|
| 552 |
2 |
| tmpPtr += dLength + 2;
|
| 553 |
|
|
|
| 554 |
|
| /* If the QoS is not 0 then add a msg id */
|
| 555 |
2 |
▶ | if ( (*pubMsg & MSP_FH_GET_QOS) != 0x00 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( (*pubMsg & MSP_FH_GET_QOS) != 0x00 ) {
|
1 |
1 |
2 |
|
| 556 |
1 |
| u_short msgId = htons( pHconn->nextMsgId );
|
| 557 |
1 |
| memcpy( tmpPtr, &msgId, sizeof(u_short) );
|
| 558 |
1 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBLISH sent,topic:<%.*s>,QoS:%d,msgid:%d\n",
|
| 559 |
1 |
| dLength, pMspPp->topic, (*pubMsg & MSP_FH_GET_QOS)>>1 , pHconn->nextMsgId );
|
| 560 |
1 |
| tmpPtr += sizeof(u_short);
|
| 561 |
|
| } else {
|
| 562 |
1 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBLISH sent,topic:<%.*s>,QoS:%d\n",
|
| 563 |
1 |
| dLength, pMspPp->topic, (*pubMsg & MSP_FH_GET_QOS)>>1 );
|
| 564 |
|
| }
|
| 565 |
|
|
|
| 566 |
|
| /* Payload */
|
| 567 |
2 |
| memcpy( tmpPtr, pMspPp->data, pMspPp->dataLength );
|
| 568 |
|
|
|
| 569 |
2 |
| *msgLength = totalLength;
|
| 570 |
|
| }
|
| 571 |
|
|
|
| 572 |
3 |
| return pubMsg;
|
| 573 |
|
| }
|
| 574 |
|
|
|
| 575 |
|
| /* mspRecieveScadaMessage */
|
| 576 |
|
| /* Receives a MQIsdp message buffer and handles it as appropriate for the message type */
|
| 577 |
|
| /* Returns NULL on error and sets pHconn->apiReturnCode */
|
| 578 |
|
| /* A note on identifiers: */
|
| 579 |
|
| /* When sending data this implementation ensures that the wmqtt message id is */
|
| 580 |
|
| /* incremented each time, so that a wmqtt message id can uniquely identify a message */
|
| 581 |
|
| /* that was previously sent to the broker. */
|
| 582 |
|
| /* When receiving data the wmqtt message id is not unique enough because if data is */
|
| 583 |
|
| /* queued up for receiving then when a wmqtt connection is established there is no */
|
| 584 |
|
| /* guarantee that wmqtt msg ids received on the new connection will not clash with wmqtt*/
|
| 585 |
|
| /* message ids received on previous connections. COnsequently we generate a our own */
|
| 586 |
|
| /* internal message id whenever a publication is received. */
|
| 587 |
0 |
| int mspReceiveScadaMessage( HCONNCB *pHconn, long bytesRead, char *pReadBuffer ) {
|
| 588 |
0 |
| long rlBytes = 0; /* How many bytes does the remaining length take up? */
|
| 589 |
0 |
| int rLength = 0; /* What is the remaining length? */
|
| 590 |
0 |
| int l = 0;
|
| 591 |
0 |
| int rc = 0;
|
| 592 |
|
| RPQ *pRpqEntry;
|
| 593 |
|
| IPQ *pIpqEntry;
|
| 594 |
|
| u_short wmqttMsgId;
|
| 595 |
|
|
|
| 596 |
0 |
| switch ( pReadBuffer[0] & MSP_GET_MSG_TYPE ) {
|
| 597 |
0 |
| case MSP_PUBLISH:
|
| 598 |
|
| /* Should the publication be released to the application yet? */
|
| 599 |
|
| /* The last byte of the buffer is reserved for this flag. It is part of the */
|
| 600 |
|
| /* buffer so that the persistence will store it with the message. */
|
| 601 |
0 |
▶ | if ( pReadBuffer[0] & MSPF_QOS_2 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pReadBuffer[0] & MSPF_QOS_2 ) {
|
0 |
0 |
0 |
|
| 602 |
0 |
| pReadBuffer[bytesRead-1] = 0x00; /* Not yet released to the application */
|
| 603 |
|
| } else {
|
| 604 |
0 |
| pReadBuffer[bytesRead-1] = MQISDP_RELEASED;
|
| 605 |
|
| }
|
| 606 |
|
|
|
| 607 |
0 |
| pRpqEntry = mspStorePublication( pHconn, bytesRead, pReadBuffer, &wmqttMsgId );
|
| 608 |
0 |
▶ | if ( pRpqEntry == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pRpqEntry == NULL ) {
|
0 |
0 |
0 |
|
| 609 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBLISH received, unable to store\n" );
|
| 610 |
|
| } else {
|
| 611 |
|
| /* Add the message to the persistence store (QoS > 0 pubs only) if there is one */
|
| 612 |
0 |
▶ | if ( !(pRpqEntry->options & MQISDP_QOS_0) && (pHconn->persistFuncs != NULL) ) {
|
Branch: 2 branches
| Branch | True | False | Total |
| if ( !(pRpqEntry->options & MQISDP_QOS_0) && (pHconn->persistFuncs != NULL) ) {
|
0 |
0 |
0 |
| if ( !(pRpqEntry->options & MQISDP_QOS_0) && (pHconn->persistFuncs != NULL) ) {
|
0 |
0 |
0 |
|
| 613 |
|
| /* Increment the received message id */
|
| 614 |
0 |
▶ | MSP_SET_NEXT_RCVID( pHconn->nextRcvId );
|
Branch: 1 branch
| Branch | True | False | Total |
| MSP_SET_NEXT_RCVID( pHconn->nextRcvId );
|
0 |
0 |
0 |
|
| 615 |
0 |
| pRpqEntry->rcvId = pHconn->nextRcvId;
|
| 616 |
|
|
|
| 617 |
|
| /* Use the rcvId when persisting the data - this is separate from the */
|
| 618 |
|
| /* protocol id, which isn't sufficiently unique. */
|
| 619 |
0 |
| rc = pHconn->persistFuncs->addReceivedMessage( pHconn->persistFuncs->pUserData,
|
| 620 |
|
| pRpqEntry->rcvId, bytesRead, pReadBuffer );
|
| 621 |
|
| }
|
| 622 |
0 |
▶ | if ( rc != 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc != 0 ) {
|
0 |
0 |
0 |
|
| 623 |
|
| /* The persistence failed, so undo the mspStorePublication */
|
| 624 |
0 |
| mspDelFromHash( pHconn, pHconn->inQ.rpHash, wmqttMsgId );
|
| 625 |
0 |
| mspDeleteRPMFromList( pHconn, pRpqEntry );
|
| 626 |
|
| } else {
|
| 627 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBLISH received,topic:<%.*s>,QoS:%d,msgid:%d\n",
|
| 628 |
0 |
| pRpqEntry->topicLength, pRpqEntry->buffer, (pRpqEntry->options)/8,
|
| 629 |
|
| wmqttMsgId );
|
| 630 |
|
|
|
| 631 |
|
| /* Send the response using the message id of the received message */
|
| 632 |
0 |
| mspSendPublishResponse( pHconn, pRpqEntry, wmqttMsgId );
|
| 633 |
|
| }
|
| 634 |
|
| }
|
| 635 |
0 |
| break;
|
| 636 |
0 |
| case MSP_PUBREC:
|
| 637 |
0 |
▶ | if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
0 |
0 |
0 |
|
| 638 |
0 |
▶ | && rlBytes + rLength <= bytesRead - 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| && rlBytes + rLength <= bytesRead - 1 ) {
|
0 |
0 |
0 |
|
| 639 |
0 |
| memcpy( &wmqttMsgId, pReadBuffer + 1 + rlBytes, sizeof(u_short) );
|
| 640 |
|
| /* Swap the bytes from network to host order */
|
| 641 |
0 |
| wmqttMsgId = ntohs( wmqttMsgId );
|
| 642 |
|
|
|
| 643 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBREC received\n" );
|
| 644 |
|
|
|
| 645 |
|
| /* Now send a PUBREL to get the sent publication released. */
|
| 646 |
|
| /* The PUBLISH message will be deleted from the IPQ by mspSendScadaMessage when */
|
| 647 |
|
| /* the PUBREL has been successfully sent. */
|
| 648 |
|
| /* The persistence of the PUBREL is handled by mspSendScadaMessage. */
|
| 649 |
|
| /* Send the response using the message id of the received message */
|
| 650 |
0 |
| mspSendPubReceivedResponse( pHconn, wmqttMsgId );
|
| 651 |
|
| }
|
| 652 |
0 |
| break;
|
| 653 |
0 |
| case MSP_PUBREL:
|
| 654 |
0 |
▶ | if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
0 |
0 |
0 |
|
| 655 |
0 |
▶ | && rlBytes + rLength <= bytesRead - 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| && rlBytes + rLength <= bytesRead - 1 ) {
|
0 |
0 |
0 |
|
| 656 |
0 |
| memcpy( &wmqttMsgId, pReadBuffer + 1 + rlBytes, sizeof(u_short) );
|
| 657 |
|
| /* Swap the bytes from network to host order */
|
| 658 |
0 |
| wmqttMsgId = ntohs( wmqttMsgId );
|
| 659 |
0 |
| pRpqEntry = (RPQ*)mspReadFromHash( pHconn->inQ.rpHash, wmqttMsgId );
|
| 660 |
|
| /* If pPubData is NULL then this must be a duplicate PUBREL message. In this */
|
| 661 |
|
| /* case just send the PUBCOMP as the message has already been released. */
|
| 662 |
0 |
▶ | if ( pRpqEntry != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pRpqEntry != NULL ) {
|
0 |
0 |
0 |
|
| 663 |
|
|
|
| 664 |
0 |
▶ | if ( pHconn->persistFuncs != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->persistFuncs != NULL ) {
|
0 |
0 |
0 |
|
| 665 |
|
| /* Use the rcvId when persisting the data - this is separate from the protocol id,*/
|
| 666 |
|
| /* which isn't sufficiently unique. */
|
| 667 |
0 |
| rc = pHconn->persistFuncs->updReceivedMessage( pHconn->persistFuncs->pUserData, pRpqEntry->rcvId );
|
| 668 |
|
| }
|
| 669 |
|
|
|
| 670 |
0 |
▶ | if ( rc == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) {
|
0 |
0 |
0 |
|
| 671 |
|
| /* Mark the publication as having been released */
|
| 672 |
0 |
| pRpqEntry->readyToPublish = MQISDP_RELEASED;
|
| 673 |
0 |
▶ | if ( pHconn->inQ.rtpEntries == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->inQ.rtpEntries == 0 ) {
|
0 |
0 |
0 |
|
| 674 |
|
| /* If the number of publications available to receive */
|
| 675 |
|
| /* changes from 0 to 1 then signal the receive semaphore. */
|
| 676 |
0 |
| mspSignalSemaphore( pHconn->ipcCb.receiveSemaphore );
|
| 677 |
|
| }
|
| 678 |
0 |
| pHconn->inQ.rtpEntries++;
|
| 679 |
|
|
|
| 680 |
|
| /* Free the data in the hash */
|
| 681 |
0 |
| mspDelFromHash( pHconn, pHconn->inQ.rpHash, wmqttMsgId );
|
| 682 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBREL received,msgId:%ld. Releasing publication.\n", wmqttMsgId );
|
| 683 |
|
| }
|
| 684 |
|
| } else {
|
| 685 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBREL received,msgId:%ld. Already released.\n", wmqttMsgId );
|
| 686 |
|
| /* Leave rc == 0 so that the PUBCOMP still gets sent */
|
| 687 |
|
| }
|
| 688 |
|
|
|
| 689 |
|
| /* If the persistence update was a success then send the PUBCOMP */
|
| 690 |
0 |
▶ | if ( rc == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) {
|
0 |
0 |
0 |
|
| 691 |
|
| /* Now send a PUBCOMP to complete the QoS 2 flow */
|
| 692 |
|
| /* Send the response using the message id of the received message */
|
| 693 |
0 |
| mspSendPubReleaseResponse( pHconn, wmqttMsgId );
|
| 694 |
|
| }
|
| 695 |
|
| }
|
| 696 |
0 |
| break;
|
| 697 |
0 |
| case MSP_PUBACK:
|
| 698 |
0 |
▶ | if ( l == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( l == 0 ) {
|
0 |
0 |
0 |
|
| 699 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBACK received\n" );
|
| 700 |
0 |
| l=1;
|
| 701 |
|
| }
|
| 702 |
|
| case MSP_PUBCOMP:
|
| 703 |
0 |
▶ | if ( l == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( l == 0 ) {
|
0 |
0 |
0 |
|
| 704 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBCOMP received\n" );
|
| 705 |
0 |
| l=1;
|
| 706 |
|
| }
|
| 707 |
|
| case MSP_SUBACK:
|
| 708 |
0 |
▶ | if ( l == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( l == 0 ) {
|
0 |
0 |
0 |
|
| 709 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "SUBACK received\n" );
|
| 710 |
0 |
| l=1;
|
| 711 |
|
| }
|
| 712 |
|
| case MSP_UNSUBACK:
|
| 713 |
0 |
▶ | if ( l == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( l == 0 ) {
|
0 |
0 |
0 |
|
| 714 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "UNSUBACK received\n" );
|
| 715 |
0 |
| l=1;
|
| 716 |
|
| }
|
| 717 |
0 |
▶ | if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
0 |
0 |
0 |
|
| 718 |
0 |
▶ | && rlBytes + rLength <= bytesRead - 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| && rlBytes + rLength <= bytesRead - 1 ) {
|
0 |
0 |
0 |
|
| 719 |
0 |
| memcpy( &wmqttMsgId, pReadBuffer + 1 + rlBytes, sizeof(u_short) );
|
| 720 |
|
| /* Swap the bytes from network to host order */
|
| 721 |
0 |
| wmqttMsgId = ntohs( wmqttMsgId );
|
| 722 |
0 |
| pIpqEntry = mspReadFromHash( pHconn->outQ.ipHash, wmqttMsgId );
|
| 723 |
|
| /* We have received the ACK for the particular message id, so the message */
|
| 724 |
|
| /* can be removed from the In Progress Queue and from the hash table */
|
| 725 |
0 |
| mspDelFromHash( pHconn, pHconn->outQ.ipHash, wmqttMsgId );
|
| 726 |
0 |
| mspDeleteIPMFromList( pHconn, pIpqEntry );
|
| 727 |
|
|
|
| 728 |
|
| /* Delete the message from the persistence store, if there is one */
|
| 729 |
|
| /* This doesn't check the return code, because there is not a lot that can */
|
| 730 |
|
| /* be done if the persistence fails to delete a sent message. */
|
| 731 |
|
| /* At worst at application restart QoS 1 messages would be duplicated. QoS 2 */
|
| 732 |
|
| /* will not because it will just be the PUBREL message that will be retried. */
|
| 733 |
0 |
▶ | if ( pHconn->persistFuncs != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->persistFuncs != NULL ) {
|
0 |
0 |
0 |
|
| 734 |
0 |
| pHconn->persistFuncs->delSentMessage( pHconn->persistFuncs->pUserData, wmqttMsgId );
|
| 735 |
|
| }
|
| 736 |
|
| }
|
| 737 |
0 |
| break;
|
| 738 |
0 |
| case MSP_PINGREQ:
|
| 739 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PINGREQ received\n" );
|
| 740 |
0 |
| mspSendPingResponse( pHconn );
|
| 741 |
0 |
| break;
|
| 742 |
0 |
| case MSP_PINGRESP:
|
| 743 |
|
| /* When we receive a ping response do nothing */
|
| 744 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PINGRESP received\n" );
|
| 745 |
0 |
| break;
|
| 746 |
0 |
| case MSP_CONNACK:
|
| 747 |
|
| /* Check the response code in the CONNACK */
|
| 748 |
0 |
▶ | if ( pReadBuffer[3] == MSP_CONN_ACCEPTED ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pReadBuffer[3] == MSP_CONN_ACCEPTED ) {
|
0 |
0 |
0 |
|
| 749 |
0 |
| pHconn->connState = MQISDP_CONNECTED;
|
| 750 |
0 |
| pHconn->reconnect.timeForNextConnect = 0;
|
| 751 |
0 |
▶ | if ( pHconn->reconnect.connRetries > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->reconnect.connRetries > 0 ) {
|
0 |
0 |
0 |
|
| 752 |
|
| /* If connRetries < 0 then we are running under clean */
|
| 753 |
|
| /* start, so the protocol library should not reconnect */
|
| 754 |
|
| /* on behalf of the application */
|
| 755 |
0 |
| pHconn->reconnect.connRetries = 0;
|
| 756 |
|
| }
|
| 757 |
|
| } else {
|
| 758 |
0 |
| pHconn->connState = MQISDP_DISCONNECTED;
|
| 759 |
0 |
| mspTCPDisconnect( &(pHconn->tcpParms.sockfd) );
|
| 760 |
|
| /* Stop any retry being attempted */
|
| 761 |
0 |
| pHconn->reconnect.connRetries = pHconn->retryCount + 1;
|
| 762 |
|
|
|
| 763 |
0 |
| switch ( pReadBuffer[4] ) {
|
| 764 |
0 |
| case MSP_CONN_REFUSED_VERSION:
|
| 765 |
0 |
| pHconn->tcpParms.lastError = MQISDP_PROTOCOL_VERSION_ERROR | MSP_CONN_ERROR;
|
| 766 |
0 |
| break;
|
| 767 |
0 |
| case MSP_CONN_REFUSED_ID:
|
| 768 |
0 |
| pHconn->tcpParms.lastError = MQISDP_CLIENT_ID_ERROR | MSP_CONN_ERROR;
|
| 769 |
0 |
| break;
|
| 770 |
0 |
| case MSP_CONN_REFUSED_BROKER:
|
| 771 |
0 |
| pHconn->tcpParms.lastError = MQISDP_BROKER_UNAVAILABLE | MSP_CONN_ERROR;
|
| 772 |
0 |
| break;
|
| 773 |
|
| }
|
| 774 |
|
| }
|
| 775 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "CONNACK\n" );
|
| 776 |
0 |
| break;
|
| 777 |
0 |
| default:
|
| 778 |
0 |
| mspLog( LOGERROR, &(pHconn->comParms), "Unrecognised MQIsdp fixed header:\n" );
|
| 779 |
0 |
| mspLogHex( LOGERROR, &(pHconn->comParms), 1, pReadBuffer );
|
| 780 |
|
| }
|
| 781 |
|
|
|
| 782 |
0 |
| return 0;
|
| 783 |
|
| }
|
| 784 |
|
|
|
| 785 |
|
| /* Encode the message length according to the SCADA algorithm */
|
| 786 |
9 |
| int mspEncodeFHeaderLength( int l, char *ptr ) {
|
| 787 |
|
| char d;
|
| 788 |
|
|
|
| 789 |
|
| do
|
| 790 |
|
| {
|
| 791 |
9 |
| d = l % 128;
|
| 792 |
9 |
| l = l / 128;
|
| 793 |
|
| /* if there are more digits to encode, set the top bit of this digit */
|
| 794 |
9 |
▶ | if ( l > 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( l > 0 ) {
|
0 |
9 |
9 |
|
| 795 |
0 |
| d = d | 0x80;
|
| 796 |
|
| }
|
| 797 |
9 |
| *ptr = d;
|
| 798 |
9 |
| ptr++;
|
| 799 |
9 |
▶ | } while ( l > 0 );
|
Branch: 1 branch
| Branch | True | False | Total |
| } while ( l > 0 );
|
0 |
9 |
9 |
|
| 800 |
|
|
|
| 801 |
9 |
| return 0;
|
| 802 |
|
| }
|
| 803 |
|
|
|
| 804 |
|
| /* Decode the message length according to the SCADA algorithm */
|
| 805 |
|
| /* numBytes is the length of the buffer */
|
| 806 |
|
| /* rlLength is the count of the bytes used to encode the remaining length */
|
| 807 |
|
| /* l is the length decoded */
|
| 808 |
|
| /* ptr is the buffer containing the encoded length */
|
| 809 |
|
| /* Returns 0 on success, otherwise -1 */
|
| 810 |
3 |
| int mspDecodeFHeaderLength( long numBytes, long *rlLength, int *l, char *ptr ) {
|
| 811 |
|
| char d;
|
| 812 |
3 |
| int multiplier = 1;
|
| 813 |
|
|
|
| 814 |
3 |
| *l = 0;
|
| 815 |
3 |
| *rlLength = 0;
|
| 816 |
|
|
|
| 817 |
|
| do {
|
| 818 |
3 |
| (*rlLength)++; /* Increment the byte count for the remaining length */
|
| 819 |
3 |
| d = *ptr; /* point at the next byte */
|
| 820 |
3 |
| *l += (d & 127) * multiplier;
|
| 821 |
3 |
| multiplier *= 128;
|
| 822 |
3 |
| ptr++;
|
| 823 |
3 |
▶ | } while ( ((d & 128) != 0) && (*rlLength <= numBytes) );
|
Branch: 2 branches
| Branch | True | False | Total |
| } while ( ((d & 128) != 0) && (*rlLength <= numBytes) );
|
0 |
3 |
3 |
| } while ( ((d & 128) != 0) && (*rlLength <= numBytes) );
|
0 |
0 |
0 |
|
| 824 |
|
|
|
| 825 |
|
| /* The length is encoded in more bytes than we have been given */
|
| 826 |
3 |
▶ | if ( (d & 128) != 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( (d & 128) != 0 ) {
|
0 |
3 |
3 |
|
| 827 |
0 |
| *rlLength = 0;
|
| 828 |
0 |
| *l = 0;
|
| 829 |
0 |
| return -1;
|
| 830 |
|
| }
|
| 831 |
|
|
|
| 832 |
3 |
| return 0;
|
| 833 |
|
| }
|
| 834 |
|
|
|
| 835 |
|
|
|
| 836 |
|
| /* A UTF encoded string is preceeded by a big-endian 16 bit length, */
|
| 837 |
|
| /* so the outBuf must be 16 bits bigger than the data being encoded */
|
| 838 |
16 |
| int mspUTFEncodeString( u_short bufLen, char *buf, char *outBuf ) {
|
| 839 |
|
| u_short netShort;
|
| 840 |
|
|
|
| 841 |
|
| /* Use the socket function htons to get the length in big-endian order */
|
| 842 |
16 |
| netShort = htons( bufLen );
|
| 843 |
|
|
|
| 844 |
16 |
| memcpy( outBuf, &netShort, sizeof(u_short) );
|
| 845 |
16 |
| memcpy( outBuf + 2, buf, (size_t)bufLen );
|
| 846 |
|
|
|
| 847 |
16 |
| return 0;
|
| 848 |
|
| }
|
| 849 |
|
|
|
| 850 |
|
| /* A UTF encoded string is preceeded by a big-endian 16 bit length, */
|
| 851 |
|
| /* To decode the string: */
|
| 852 |
|
| /* 1. Convert the first 16 bytes into an unsigned short in the platform endian */
|
| 853 |
|
| /* 2. Move the UTF string pointer forward 16 bits to point to a conventional string */
|
| 854 |
0 |
| int mspUTFDecodeString( u_short *bufLen, char *bufToDecode, char **ppBuffer ) {
|
| 855 |
|
|
|
| 856 |
|
| /* Use the socket function ntohs to get the length in platform endian order */
|
| 857 |
0 |
| memcpy( bufLen, bufToDecode, sizeof(u_short) );
|
| 858 |
|
| /* Swap the bytes, if appriopriate for the platform */
|
| 859 |
0 |
| *bufLen = ntohs( *bufLen );
|
| 860 |
|
|
|
| 861 |
0 |
| *ppBuffer = bufToDecode + 2;
|
| 862 |
|
|
|
| 863 |
0 |
| return 0;
|
| 864 |
|
| }
|
| 865 |
|
|
|
| 866 |
|
| /* mspSendScadaMessage */
|
| 867 |
|
| /* pHconn - Connection handle for the current application */
|
| 868 |
|
| /* msgLen - Length of the message to send */
|
| 869 |
|
| /* msgData - The data to send in MQIsdp wire format */
|
| 870 |
|
| /* retryFlag - (0/1) Is this message being retried? */
|
| 871 |
|
| /* initFlag - (0/1) Is the state being initialised from persistence? If so */
|
| 872 |
|
| /* then the state must not be written back to the persistence */
|
| 873 |
|
| /* we are initialising from !. */
|
| 874 |
|
| /* Returns as follows: */
|
| 875 |
|
| /* 0 - Successful send */
|
| 876 |
|
| /* 1 - Unsuccessful send, data queued (if appropriate ) */
|
| 877 |
|
| /* MQISDP_PERSISTENCE_FAILED - Unable to queue the data - persistence error */
|
| 878 |
|
| /* MQISDP_OUT_OF_MEMORY - Unable to queue the data - out of memory */
|
| 879 |
20 |
| int mspSendScadaMessage( HCONNCB *pHconn, long msgLen, char *msgData,
|
| 880 |
|
| short msgId, int retryFlag, int initFlag ) {
|
| 881 |
20 |
| long storeMsg = 0;
|
| 882 |
20 |
| int tcpRc = 1;
|
| 883 |
20 |
| int rc = 0;
|
| 884 |
20 |
| IPQ *ipqEntry = NULL;
|
| 885 |
20 |
| IPQ *pTmpIpqEntry = NULL;
|
| 886 |
|
|
|
| 887 |
|
| /* Store messages with QoS greater than 0 */
|
| 888 |
20 |
▶ | switch ( *msgData & MSP_FH_GET_QOS ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| switch ( *msgData & MSP_FH_GET_QOS ) {
|
7 |
13 |
20 |
|
| 889 |
7 |
| case MSPF_QOS_2:
|
| 890 |
|
| case MSPF_QOS_1:
|
| 891 |
7 |
| storeMsg = 1;
|
| 892 |
7 |
| break;
|
| 893 |
13 |
| default:
|
| 894 |
13 |
| break;
|
| 895 |
|
| }
|
| 896 |
|
|
|
| 897 |
|
| /* The message needs to be put on the InProgressQ if: */
|
| 898 |
|
| /* 1. It has a QoS > 0 or */
|
| 899 |
|
| /* 2. It is a PUBREL message (This implementation assigns */
|
| 900 |
|
| /* PUBREL message a QoS > 0). */
|
| 901 |
|
| /* */
|
| 902 |
|
| /* PUBLISH (QoS > 0) and PUBREL messages are driven by the */
|
| 903 |
|
| /* client end of the MQIsdp connection. PUBLISH must be */
|
| 904 |
|
| /* retried in the event of a TCP/IP error and PUBREL must */
|
| 905 |
|
| /* be retried until a PUBCOMP is received. */
|
| 906 |
20 |
▶ | if ( storeMsg == 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( storeMsg == 1 ) {
|
7 |
13 |
20 |
|
| 907 |
|
|
|
| 908 |
|
| /* If we are not retrying then queue up the data, otherwise it is already */
|
| 909 |
|
| /* queued. */
|
| 910 |
7 |
▶ | if ( retryFlag == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( retryFlag == 0 ) {
|
7 |
0 |
7 |
|
| 911 |
|
|
|
| 912 |
7 |
▶ | if ( pHconn->outQ.numBytesQueued + msgLen + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->outQ.numBytesQueued + msgLen + sizeof(IPQ) > MSP_DEFAULT_MAX_OUTQ_SZ ) {
|
0 |
7 |
7 |
|
| 913 |
0 |
| rc = MQISDP_OUT_OF_MEMORY;
|
| 914 |
|
| } else {
|
| 915 |
7 |
| ipqEntry = mspAddIPMToList( pHconn, msgLen, msgData, msgId );
|
| 916 |
7 |
▶ | if ( ipqEntry == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( ipqEntry == NULL ) {
|
0 |
7 |
7 |
|
| 917 |
0 |
| rc = MQISDP_OUT_OF_MEMORY;
|
| 918 |
|
| } else {
|
| 919 |
|
| /* Add the message into the persistence. If this fails, then */
|
| 920 |
|
| /* undo previous in memory queue and abort the send. */
|
| 921 |
7 |
▶ | if ( initFlag == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( initFlag == 0 ) {
|
1 |
6 |
7 |
|
| 922 |
1 |
▶ | if ( pHconn->persistFuncs != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->persistFuncs != NULL ) {
|
1 |
0 |
1 |
|
| 923 |
1 |
▶ | if ( (*msgData & MSP_GET_MSG_TYPE) == MSP_PUBREL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( (*msgData & MSP_GET_MSG_TYPE) == MSP_PUBREL ) {
|
0 |
1 |
1 |
|
| 924 |
0 |
| rc = pHconn->persistFuncs->updSentMessage( pHconn->persistFuncs->pUserData,
|
| 925 |
|
| msgId, msgLen, msgData );
|
| 926 |
|
| } else {
|
| 927 |
1 |
| rc = pHconn->persistFuncs->addSentMessage( pHconn->persistFuncs->pUserData,
|
| 928 |
|
| msgId, msgLen, msgData );
|
| 929 |
|
| }
|
| 930 |
|
| }
|
| 931 |
|
| } else {
|
| 932 |
6 |
| ipqEntry->msgStatus = MQISDP_RETRYING;
|
| 933 |
|
| }
|
| 934 |
|
|
|
| 935 |
|
| /* If the persistence failed then undo the previous update */
|
| 936 |
7 |
▶ | if ( rc != 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc != 0 ) {
|
0 |
7 |
7 |
|
| 937 |
0 |
| rc = MQISDP_PERSISTENCE_FAILED;
|
| 938 |
|
| /* Set the msgData field to be NULL to stop mspDeleteIPMFromList */
|
| 939 |
|
| /* freeing the message buffer that this function is trying to send. */
|
| 940 |
|
| /* It will be freed further down... */
|
| 941 |
0 |
| ipqEntry->msgData = NULL;
|
| 942 |
0 |
| mspDeleteIPMFromList( pHconn, ipqEntry );
|
| 943 |
0 |
| ipqEntry = NULL;
|
| 944 |
|
| } else {
|
| 945 |
|
| /* Delete a previous entry if it exists */
|
| 946 |
|
| /* This is only ever the case for QoS 2 publications we send */
|
| 947 |
|
| /* We don't want to delete the PUBLISH message until we know the PUBREL has been queued */
|
| 948 |
7 |
| pTmpIpqEntry = mspReadFromHash( pHconn->outQ.ipHash, msgId );
|
| 949 |
7 |
▶ | if ( pTmpIpqEntry != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pTmpIpqEntry != NULL ) {
|
0 |
7 |
7 |
|
| 950 |
0 |
| mspDeleteIPMFromList( pHconn, pTmpIpqEntry );
|
| 951 |
|
| }
|
| 952 |
7 |
| mspAddToHash( pHconn, pHconn->outQ.ipHash, msgId, ipqEntry );
|
| 953 |
|
| }
|
| 954 |
|
| }
|
| 955 |
|
| }
|
| 956 |
|
| } else {
|
| 957 |
|
| /* Retry, so flip on the DUP flag. */
|
| 958 |
0 |
| *msgData |= MSPF_DUPLICATE;
|
| 959 |
|
| }
|
| 960 |
|
| }
|
| 961 |
|
|
|
| 962 |
|
| /* Write the data to TCP/IP */
|
| 963 |
20 |
▶ | if ( rc == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) {
|
20 |
0 |
20 |
|
| 964 |
20 |
▶ | if ( pHconn->tcpParms.sockfd != MSP_INVALID_SOCKET ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->tcpParms.sockfd != MSP_INVALID_SOCKET ) {
|
0 |
20 |
20 |
|
| 965 |
0 |
| tcpRc = mspTCPWrite( pHconn, msgLen, msgData );
|
| 966 |
0 |
▶ | if ( tcpRc == -1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( tcpRc == -1 ) {
|
0 |
0 |
0 |
|
| 967 |
0 |
| rc = 1;
|
| 968 |
0 |
| pHconn->connState = MQISDP_DISCONNECTED;
|
| 969 |
0 |
| mspTCPDisconnect( &(pHconn->tcpParms.sockfd) );
|
| 970 |
|
| } else {
|
| 971 |
|
| /* A successful write, so update the time for the next poll */
|
| 972 |
0 |
| pHconn->timeForNextPoll = time(NULL) + pHconn->keepAliveTime;
|
| 973 |
|
| }
|
| 974 |
|
| } else {
|
| 975 |
20 |
| rc = 1;
|
| 976 |
|
| }
|
| 977 |
|
| }
|
| 978 |
|
|
|
| 979 |
|
| /* If it the message QoS is 0 or the persistence failed then free up */
|
| 980 |
|
| /* storage used by the message. Don't free data if retrying. Only */
|
| 981 |
|
| /* QoS 1 & 2 data is retried and it will be freed when an ACK is */
|
| 982 |
|
| /* received from the broker. */
|
| 983 |
20 |
▶ | if ( ((storeMsg != 1) || (ipqEntry == NULL)) && !retryFlag ) {
|
Branch: 3 branches
| Branch | True | False | Total |
| if ( ((storeMsg != 1) || (ipqEntry == NULL)) && !retryFlag ) {
|
7 |
13 |
20 |
| if ( ((storeMsg != 1) || (ipqEntry == NULL)) && !retryFlag ) {
|
0 |
7 |
7 |
| if ( ((storeMsg != 1) || (ipqEntry == NULL)) && !retryFlag ) {
|
13 |
0 |
13 |
|
| 984 |
13 |
| mspFree( &(pHconn->comParms), msgData, msgLen );
|
| 985 |
|
| }
|
| 986 |
|
|
|
| 987 |
20 |
| return rc;
|
| 988 |
|
| }
|
| 989 |
|
|
|
| 990 |
|
| /* mspStorePublication */
|
| 991 |
|
| /* A received message is stored in a RIPQ structure. These structures */
|
| 992 |
|
| /* are then chained together to form a queue, with the newest message */
|
| 993 |
|
| /* received being added to the end of the queue. For Qos 2 messages the */
|
| 994 |
|
| /* pointer to the RIPQ structure is added to a hash table with the */
|
| 995 |
|
| /* message id being the hash key. This allows entries to be quickly found */
|
| 996 |
|
| /* when PUBREL messages are received. */
|
| 997 |
3 |
| RPQ* mspStorePublication( HCONNCB *pHconn, long bytesRead, char *pReadBuffer, u_short *wmqttMsgId ) {
|
| 998 |
3 |
| RPQ *pRpqEntry = NULL;
|
| 999 |
|
| long rlBytes; /* How many bytes does the Remaining length occupy */
|
| 1000 |
|
| int rLength; /* The actual remaining length */
|
| 1001 |
|
| char *tmpPtr;
|
| 1002 |
3 |
| char *topicPtr = NULL;
|
| 1003 |
3 |
| u_short topicLength = 0; /* @ V1.2 */
|
| 1004 |
|
|
|
| 1005 |
3 |
| pRpqEntry = (RPQ*)mspMalloc( &(pHconn->comParms), sizeof(RPQ) );
|
| 1006 |
3 |
▶ | if ( pRpqEntry == NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pRpqEntry == NULL ) {
|
0 |
3 |
3 |
|
| 1007 |
0 |
| return NULL;
|
| 1008 |
|
| }
|
| 1009 |
|
|
|
| 1010 |
3 |
▶ | if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspDecodeFHeaderLength( bytesRead - 1, &rlBytes, &rLength, pReadBuffer + 1 ) != -1
|
3 |
0 |
3 |
|
| 1011 |
3 |
▶ | && rlBytes + rLength <= bytesRead - 1 /* Sanity check */ ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| && rlBytes + rLength <= bytesRead - 1 /* Sanity check */ ) {
|
0 |
3 |
3 |
|
| 1012 |
|
|
|
| 1013 |
0 |
| pRpqEntry->options = 0;
|
| 1014 |
0 |
| *wmqttMsgId = 0;
|
| 1015 |
0 |
| pRpqEntry->topicLength = 0;
|
| 1016 |
|
|
|
| 1017 |
|
| /* Get the options from the fixed header - byte 1 */
|
| 1018 |
0 |
▶ | if ( pReadBuffer[0] & MSPF_RETAIN ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pReadBuffer[0] & MSPF_RETAIN ) {
|
0 |
0 |
0 |
|
| 1019 |
0 |
| pRpqEntry->options |= MQISDP_RETAIN;
|
| 1020 |
|
| }
|
| 1021 |
|
|
|
| 1022 |
0 |
▶ | if ( pReadBuffer[0] & MSPF_DUPLICATE ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pReadBuffer[0] & MSPF_DUPLICATE ) {
|
0 |
0 |
0 |
|
| 1023 |
0 |
| pRpqEntry->options |= MQISDP_DUPLICATE;
|
| 1024 |
|
| }
|
| 1025 |
|
|
|
| 1026 |
|
| /* Store some options to do with the message */
|
| 1027 |
0 |
▶ | if ( pReadBuffer[0] & MSPF_QOS_2 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pReadBuffer[0] & MSPF_QOS_2 ) {
|
0 |
0 |
0 |
|
| 1028 |
0 |
| pRpqEntry->options |= MQISDP_QOS_2;
|
| 1029 |
0 |
▶ | } else if ( pReadBuffer[0] & MSPF_QOS_1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( pReadBuffer[0] & MSPF_QOS_1 ) {
|
0 |
0 |
0 |
|
| 1030 |
0 |
| pRpqEntry->options |= MQISDP_QOS_1;
|
| 1031 |
|
| } else {
|
| 1032 |
0 |
| pRpqEntry->options |= MQISDP_QOS_0;
|
| 1033 |
|
| }
|
| 1034 |
|
| /* The flag indicating whether the publication should be released to the application */
|
| 1035 |
|
| /* is the last byte in the buffer. The QoS is not used to determine this because this */
|
| 1036 |
|
| /* function may be rebuilding QoS 2 publications from the persistence which have */
|
| 1037 |
|
| /* already been released. */
|
| 1038 |
0 |
| pRpqEntry->readyToPublish = pReadBuffer[bytesRead-1];
|
| 1039 |
|
|
|
| 1040 |
|
| /* Decode the variable header - step over fixed header and remaining length bytes */
|
| 1041 |
0 |
| tmpPtr = pReadBuffer + rlBytes + 1;
|
| 1042 |
|
|
|
| 1043 |
0 |
| mspUTFDecodeString( &topicLength, tmpPtr, &topicPtr ); /* @ V1.2 */
|
| 1044 |
0 |
| pRpqEntry->topicLength = topicLength; /* @ V1.2 */
|
| 1045 |
0 |
| tmpPtr += pRpqEntry->topicLength + 2;
|
| 1046 |
|
|
|
| 1047 |
0 |
▶ | if ( (pReadBuffer[0] & MSPF_QOS_2) || (pReadBuffer[0] & MSPF_QOS_1) ) {
|
Branch: 2 branches
| Branch | True | False | Total |
| if ( (pReadBuffer[0] & MSPF_QOS_2) || (pReadBuffer[0] & MSPF_QOS_1) ) {
|
0 |
0 |
0 |
| if ( (pReadBuffer[0] & MSPF_QOS_2) || (pReadBuffer[0] & MSPF_QOS_1) ) {
|
0 |
0 |
0 |
|
| 1048 |
|
| /* Sanity check */
|
| 1049 |
0 |
▶ | if (tmpPtr + sizeof(u_short) > pReadBuffer + bytesRead) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (tmpPtr + sizeof(u_short) > pReadBuffer + bytesRead) {
|
0 |
0 |
0 |
|
| 1050 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry, sizeof(RPQ) );
|
| 1051 |
0 |
| return NULL;
|
| 1052 |
|
| }
|
| 1053 |
|
| /* Get the message id */
|
| 1054 |
0 |
| memcpy( wmqttMsgId, tmpPtr, sizeof(u_short) );
|
| 1055 |
0 |
| (*wmqttMsgId) = ntohs( *wmqttMsgId );
|
| 1056 |
|
| /* The buffer length to hold the topic and data is: */
|
| 1057 |
|
| /* remaining length - 2 (for the msg Id) - 2 (for the UTF encoding of the topic) */
|
| 1058 |
0 |
| pRpqEntry->bufferLength = rLength - 4;
|
| 1059 |
0 |
| tmpPtr += 2;
|
| 1060 |
|
| } else {
|
| 1061 |
|
| /* The buffer length to hold the topic and data is: */
|
| 1062 |
|
| /* remaining length - 2 (for the UTF encoding of the topic) */
|
| 1063 |
0 |
| pRpqEntry->bufferLength = rLength - 2;
|
| 1064 |
|
| }
|
| 1065 |
|
|
|
| 1066 |
|
| /* Sanity check the two upcoming memcpy's */
|
| 1067 |
0 |
▶ | if (topicPtr + pRpqEntry->topicLength > pReadBuffer + bytesRead
|
Branch: 1 branch
| Branch | True | False | Total |
| if (topicPtr + pRpqEntry->topicLength > pReadBuffer + bytesRead
|
0 |
0 |
0 |
|
| 1068 |
0 |
▶ | || pRpqEntry->bufferLength - pRpqEntry->topicLength < 0
|
Branch: 1 branch
| Branch | True | False | Total |
| || pRpqEntry->bufferLength - pRpqEntry->topicLength < 0
|
0 |
0 |
0 |
|
| 1069 |
0 |
▶ | || tmpPtr + (pRpqEntry->bufferLength - pRpqEntry->topicLength) > pReadBuffer + bytesRead) {
|
Branch: 1 branch
| Branch | True | False | Total |
| || tmpPtr + (pRpqEntry->bufferLength - pRpqEntry->topicLength) > pReadBuffer + bytesRead) {
|
0 |
0 |
0 |
|
| 1070 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry, sizeof(RPQ) );
|
| 1071 |
0 |
| return NULL;
|
| 1072 |
|
| }
|
| 1073 |
|
|
|
| 1074 |
|
| /* We have no more room to store publications, so free any storage */
|
| 1075 |
|
| /* we have allocated and return NULL. */
|
| 1076 |
0 |
▶ | if ( pRpqEntry->bufferLength + sizeof(RPQ) > MSP_DEFAULT_MAX_INQ_SZ ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pRpqEntry->bufferLength + sizeof(RPQ) > MSP_DEFAULT_MAX_INQ_SZ ) {
|
0 |
0 |
0 |
|
| 1077 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry, sizeof(RPQ) );
|
| 1078 |
0 |
| return NULL;
|
| 1079 |
|
| }
|
| 1080 |
|
|
|
| 1081 |
0 |
| pRpqEntry->buffer = (char*)mspMalloc( &(pHconn->comParms), pRpqEntry->bufferLength );
|
| 1082 |
0 |
▶ | if ( pRpqEntry->buffer != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pRpqEntry->buffer != NULL ) {
|
0 |
0 |
0 |
|
| 1083 |
|
| /* Copy the topic first */
|
| 1084 |
0 |
| memcpy( pRpqEntry->buffer, topicPtr, pRpqEntry->topicLength );
|
| 1085 |
|
|
|
| 1086 |
|
| /* Copy the data next */
|
| 1087 |
0 |
| memcpy( pRpqEntry->buffer + pRpqEntry->topicLength, tmpPtr,
|
| 1088 |
0 |
| pRpqEntry->bufferLength - pRpqEntry->topicLength );
|
| 1089 |
|
|
|
| 1090 |
|
| /* Add QoS 2 messages to the hash table so that they can be easily looked */
|
| 1091 |
|
| /* up later when a PUBREL message is received. If a duplicate is sent then */
|
| 1092 |
|
| /* check if we have already received it. */
|
| 1093 |
|
| /* QoS2 pubs that are readyToPublish have been fully acknowledged, so don't*/
|
| 1094 |
|
| /* add then to the hash table for later look up. */
|
| 1095 |
0 |
▶ | if ( (pReadBuffer[0] & MSPF_QOS_2) && (pRpqEntry->readyToPublish == 0) ) {
|
Branch: 2 branches
| Branch | True | False | Total |
| if ( (pReadBuffer[0] & MSPF_QOS_2) && (pRpqEntry->readyToPublish == 0) ) {
|
0 |
0 |
0 |
| if ( (pReadBuffer[0] & MSPF_QOS_2) && (pRpqEntry->readyToPublish == 0) ) {
|
0 |
0 |
0 |
|
| 1096 |
0 |
▶ | switch ( pReadBuffer[0] & MSPF_DUPLICATE ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| switch ( pReadBuffer[0] & MSPF_DUPLICATE ) {
|
0 |
0 |
0 |
|
| 1097 |
0 |
| case MSPF_DUPLICATE: /* DUP flag is set.... */
|
| 1098 |
0 |
▶ | if ( mspGetHashEntry( pHconn->inQ.rpHash, *wmqttMsgId ) != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspGetHashEntry( pHconn->inQ.rpHash, *wmqttMsgId ) != NULL ) {
|
0 |
0 |
0 |
|
| 1099 |
|
| /* ....but we have already received it */
|
| 1100 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry->buffer, pRpqEntry->bufferLength );
|
| 1101 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry, sizeof(RPQ) );
|
| 1102 |
0 |
| pRpqEntry = NULL;
|
| 1103 |
0 |
| break;
|
| 1104 |
|
| }
|
| 1105 |
|
| /* ....else we haven't received it, so drop through to default processing */
|
| 1106 |
|
| default: /* DUP flag not set */
|
| 1107 |
0 |
| mspAddRPMToList( pHconn, pRpqEntry );
|
| 1108 |
0 |
▶ | if ( mspAddToHash( pHconn, pHconn->inQ.rpHash, *wmqttMsgId, pRpqEntry ) != 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspAddToHash( pHconn, pHconn->inQ.rpHash, *wmqttMsgId, pRpqEntry ) != 0 ) {
|
0 |
0 |
0 |
|
| 1109 |
|
| /* Failed to store the publication */
|
| 1110 |
0 |
| mspDeleteRPMFromList( pHconn, pRpqEntry);
|
| 1111 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry->buffer, pRpqEntry->bufferLength );
|
| 1112 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry, sizeof(RPQ) );
|
| 1113 |
0 |
| pRpqEntry = NULL;
|
| 1114 |
|
| }
|
| 1115 |
0 |
| break;
|
| 1116 |
|
| }
|
| 1117 |
|
| } else {
|
| 1118 |
|
| /* Add the message to the receive queue for the client */
|
| 1119 |
0 |
| mspAddRPMToList( pHconn, pRpqEntry );
|
| 1120 |
|
| }
|
| 1121 |
|
|
|
| 1122 |
0 |
▶ | if ( pRpqEntry != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pRpqEntry != NULL ) {
|
0 |
0 |
0 |
|
| 1123 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "Storing publication,msgId:%ld. Releasing:%s\n",
|
| 1124 |
0 |
▶ | *wmqttMsgId, (pRpqEntry->readyToPublish==MQISDP_RELEASED)?"TRUE":"FALSE" );
|
Branch: 1 branch
| Branch | True | False | Total |
| *wmqttMsgId, (pRpqEntry->readyToPublish==MQISDP_RELEASED)?"TRUE":"FALSE" );
|
0 |
0 |
0 |
|
| 1125 |
|
| }
|
| 1126 |
|
| } else {
|
| 1127 |
|
| /* Failed to store the publication */
|
| 1128 |
0 |
| mspFree( &(pHconn->comParms), pRpqEntry, sizeof(RPQ) );
|
| 1129 |
0 |
| pRpqEntry = NULL;
|
| 1130 |
|
| }
|
| 1131 |
|
| } else {
|
| 1132 |
|
| /* Failed to store the publication */
|
| 1133 |
3 |
| mspFree( &(pHconn->comParms), pRpqEntry, sizeof(RPQ) );
|
| 1134 |
3 |
| pRpqEntry = NULL;
|
| 1135 |
|
| }
|
| 1136 |
|
|
|
| 1137 |
3 |
| return pRpqEntry;
|
| 1138 |
|
| }
|
| 1139 |
|
|
|
| 1140 |
|
| /* mspSendPublishResponse */
|
| 1141 |
|
| /* Send a MQIsdp publish response message as appropriate for the QoS */
|
| 1142 |
|
| /* Returns 0 on success, 1 otherwise */
|
| 1143 |
0 |
| int mspSendPublishResponse( HCONNCB *pHconn, RPQ* pRpqEntry, u_short wmqttMsgId ) {
|
| 1144 |
|
| char *pScadaMsg;
|
| 1145 |
|
| u_short netMsgId;
|
| 1146 |
0 |
| int rc = 1;
|
| 1147 |
|
|
|
| 1148 |
0 |
▶ | if ( pRpqEntry->options & MQISDP_QOS_2 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pRpqEntry->options & MQISDP_QOS_2 ) {
|
0 |
0 |
0 |
|
| 1149 |
|
| /* Send a PUBREC */
|
| 1150 |
0 |
| pScadaMsg = mspMalloc( &(pHconn->comParms), 4 );
|
| 1151 |
0 |
▶ | if ( pScadaMsg != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pScadaMsg != NULL ) {
|
0 |
0 |
0 |
|
| 1152 |
0 |
| pScadaMsg[0] = 0x00 | MSP_PUBREC; /* No other options */
|
| 1153 |
0 |
| pScadaMsg[1] = 0x02; /* 2 bytes to follow */
|
| 1154 |
0 |
| netMsgId = htons( wmqttMsgId );
|
| 1155 |
0 |
| memcpy( pScadaMsg+2, &netMsgId, sizeof(u_short) );
|
| 1156 |
|
|
|
| 1157 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBREC sent\n" );
|
| 1158 |
|
|
|
| 1159 |
0 |
▶ | if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, wmqttMsgId, 0, 0 ) <= 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, wmqttMsgId, 0, 0 ) <= 1 ) {
|
0 |
0 |
0 |
|
| 1160 |
0 |
| rc = 0;
|
| 1161 |
|
| }
|
| 1162 |
|
| }
|
| 1163 |
0 |
▶ | } else if ( pRpqEntry->options & MQISDP_QOS_1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( pRpqEntry->options & MQISDP_QOS_1 ) {
|
0 |
0 |
0 |
|
| 1164 |
|
| /* Send a PUBACK */
|
| 1165 |
0 |
| pScadaMsg = mspMalloc( &(pHconn->comParms), 4 );
|
| 1166 |
0 |
▶ | if ( pScadaMsg != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pScadaMsg != NULL ) {
|
0 |
0 |
0 |
|
| 1167 |
0 |
| pScadaMsg[0] = 0x00 | MSP_PUBACK; /* No other options */
|
| 1168 |
0 |
| pScadaMsg[1] = 0x02; /* 2 bytes to follow */
|
| 1169 |
0 |
| netMsgId = htons( wmqttMsgId );
|
| 1170 |
0 |
| memcpy( pScadaMsg+2, &netMsgId, sizeof(u_short) );
|
| 1171 |
|
|
|
| 1172 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBACK sent\n" );
|
| 1173 |
|
|
|
| 1174 |
0 |
▶ | if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, wmqttMsgId, 0, 0 ) <= 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, wmqttMsgId, 0, 0 ) <= 1 ) {
|
0 |
0 |
0 |
|
| 1175 |
0 |
| rc = 0;
|
| 1176 |
|
| }
|
| 1177 |
|
| }
|
| 1178 |
|
| } else {
|
| 1179 |
|
| /* Nothing to do for QoS 0 */
|
| 1180 |
0 |
| rc = 0;
|
| 1181 |
|
| }
|
| 1182 |
|
|
|
| 1183 |
0 |
| return rc;
|
| 1184 |
|
| }
|
| 1185 |
|
|
|
| 1186 |
|
| /* mspSendPingResponse */
|
| 1187 |
|
| /* Send a MQIsdp PINGRESP message after receiving a PINGREQ */
|
| 1188 |
|
| /* Returns 0 on success, 1 otherwise */
|
| 1189 |
0 |
| int mspSendPingResponse( HCONNCB *pHconn ) {
|
| 1190 |
|
| char *pScadaMsg;
|
| 1191 |
0 |
| int rc = 1;
|
| 1192 |
|
|
|
| 1193 |
|
| /* Send a PINGRESP */
|
| 1194 |
0 |
| pScadaMsg = mspMalloc( &(pHconn->comParms), 2 );
|
| 1195 |
0 |
▶ | if ( pScadaMsg != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pScadaMsg != NULL ) {
|
0 |
0 |
0 |
|
| 1196 |
0 |
| pScadaMsg[0] = 0x00 | (char)MSP_PINGRESP; /* No other options */
|
| 1197 |
0 |
| pScadaMsg[1] = 0x00; /* 0 bytes to follow */
|
| 1198 |
|
|
|
| 1199 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PINGRESP sent\n" );
|
| 1200 |
|
|
|
| 1201 |
0 |
▶ | if ( mspSendScadaMessage( pHconn , 2, pScadaMsg, 0, 0, 0 ) <= 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspSendScadaMessage( pHconn , 2, pScadaMsg, 0, 0, 0 ) <= 1 ) {
|
0 |
0 |
0 |
|
| 1202 |
0 |
| rc = 0;
|
| 1203 |
|
| }
|
| 1204 |
|
| }
|
| 1205 |
|
|
|
| 1206 |
0 |
| return rc;
|
| 1207 |
|
| }
|
| 1208 |
|
|
|
| 1209 |
|
| /* mspSendPingRequest */
|
| 1210 |
|
| /* Send a MQIsdp PINGREQ message to keepalive the MQIsdp connection */
|
| 1211 |
|
| /* Returns 0 on success, 1 otherwise */
|
| 1212 |
0 |
| int mspSendPingRequest( HCONNCB *pHconn ) {
|
| 1213 |
|
| char *pScadaMsg;
|
| 1214 |
0 |
| int rc = 1;
|
| 1215 |
|
|
|
| 1216 |
|
| /* Send a PINGREQ */
|
| 1217 |
0 |
| pScadaMsg = mspMalloc( &(pHconn->comParms), 2 );
|
| 1218 |
0 |
▶ | if ( pScadaMsg != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pScadaMsg != NULL ) {
|
0 |
0 |
0 |
|
| 1219 |
0 |
| pScadaMsg[0] = 0x00 | (char)MSP_PINGREQ; /* No other options */
|
| 1220 |
0 |
| pScadaMsg[1] = 0x00; /* 0 bytes to follow */
|
| 1221 |
|
|
|
| 1222 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PINGREQ sent\n" );
|
| 1223 |
|
|
|
| 1224 |
0 |
▶ | if ( mspSendScadaMessage( pHconn , 2, pScadaMsg, 0, 0, 0 ) <= 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspSendScadaMessage( pHconn , 2, pScadaMsg, 0, 0, 0 ) <= 1 ) {
|
0 |
0 |
0 |
|
| 1225 |
0 |
| rc = 0;
|
| 1226 |
|
| }
|
| 1227 |
|
| }
|
| 1228 |
|
|
|
| 1229 |
0 |
| return rc;
|
| 1230 |
|
| }
|
| 1231 |
|
|
|
| 1232 |
|
| /* mspSendPubReceivedResponse */
|
| 1233 |
|
| /* Send a MQIsdp publish release message for QoS 2 publications */
|
| 1234 |
|
| /* Returns 0 on success, 1 otherwise */
|
| 1235 |
0 |
| int mspSendPubReceivedResponse( HCONNCB *pHconn, u_short msgId ) {
|
| 1236 |
|
| char *pScadaMsg;
|
| 1237 |
0 |
| int rc = 1;
|
| 1238 |
|
|
|
| 1239 |
|
| /* Send a PUBREL */
|
| 1240 |
0 |
| pScadaMsg = mspMalloc( &(pHconn->comParms), 4 );
|
| 1241 |
0 |
▶ | if ( pScadaMsg != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pScadaMsg != NULL ) {
|
0 |
0 |
0 |
|
| 1242 |
0 |
| pScadaMsg[0] = 0x00 | MSP_PUBREL; /* No other options */
|
| 1243 |
|
| /* Ensure PUBREL gets retried. QoS 1 has for PUBREL no meaning in the protocol */
|
| 1244 |
|
| /* spec. The QoS is used by this implementation to identify messages to retry */
|
| 1245 |
0 |
| pScadaMsg[0] |= (char)MSPF_QOS_1;
|
| 1246 |
0 |
| pScadaMsg[1] = 0x02; /* 2 bytes to follow */
|
| 1247 |
0 |
| *(u_short*)(pScadaMsg+2) = htons( msgId );
|
| 1248 |
|
|
|
| 1249 |
0 |
▶ | if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, msgId, 0, 0 ) <= 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, msgId, 0, 0 ) <= 1 ) {
|
0 |
0 |
0 |
|
| 1250 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBREL sent, msgid:%d\n", msgId );
|
| 1251 |
0 |
| rc = 0;
|
| 1252 |
|
| }
|
| 1253 |
|
| }
|
| 1254 |
|
|
|
| 1255 |
0 |
| return rc;
|
| 1256 |
|
| }
|
| 1257 |
|
|
|
| 1258 |
|
| /* mspSendPubReleaseResponse */
|
| 1259 |
|
| /* Send a MQIsdp publish complete message for QoS 2 publications */
|
| 1260 |
|
| /* Returns 0 on success, 1 otherwise */
|
| 1261 |
0 |
| int mspSendPubReleaseResponse( HCONNCB *pHconn, u_short msgId ) {
|
| 1262 |
|
| char *pScadaMsg;
|
| 1263 |
0 |
| int rc = 1;
|
| 1264 |
|
|
|
| 1265 |
|
| /* Send a PUBCOMP */
|
| 1266 |
0 |
| pScadaMsg = mspMalloc( &(pHconn->comParms), 4 );
|
| 1267 |
0 |
▶ | if ( pScadaMsg != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pScadaMsg != NULL ) {
|
0 |
0 |
0 |
|
| 1268 |
0 |
| pScadaMsg[0] = 0x00 | MSP_PUBCOMP; /* No other options */
|
| 1269 |
0 |
| pScadaMsg[1] = 0x02; /* 2 bytes to follow */
|
| 1270 |
0 |
| *(u_short*)(pScadaMsg+2) = htons( msgId );
|
| 1271 |
|
|
|
| 1272 |
0 |
| mspLog( LOGSCADA, &(pHconn->comParms), "PUBCOMP sent, msgid:%d\n", msgId );
|
| 1273 |
|
|
|
| 1274 |
0 |
▶ | if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, msgId, 0, 0 ) <= 1 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( mspSendScadaMessage( pHconn , 4, pScadaMsg, msgId, 0, 0 ) <= 1 ) {
|
0 |
0 |
0 |
|
| 1275 |
0 |
| rc = 0;
|
| 1276 |
|
| }
|
| 1277 |
|
| }
|
| 1278 |
|
|
|
| 1279 |
0 |
| return rc;
|
| 1280 |
|
| }
|
| 1281 |
|
|
|
| 1282 |
|
| /* Attempts to reconnect a broken TCP/IP connection. The original SCADA */
|
| 1283 |
|
| /* connect message is sent to establish the MQIsdp connection. */
|
| 1284 |
|
| /* Returns 0 on success, 1 otherwise */
|
| 1285 |
3 |
| int mspMQIsdpReconnect( HCONNCB *pHconn ) {
|
| 1286 |
3 |
| int rc = 1;
|
| 1287 |
|
| char *connMsg;
|
| 1288 |
|
|
|
| 1289 |
3 |
▶ | if ( pHconn->tcpParms.sockfd != MSP_INVALID_SOCKET ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( pHconn->tcpParms.sockfd != MSP_INVALID_SOCKET ) {
|
0 |
3 |
3 |
|
| 1290 |
|
| /* Disconnect TCP/IP before trying the reconnect. This ensures that a WMQtt connect */
|
| 1291 |
|
| /* message is sent on a different TCP/IP connection each time to avoid confusion */
|
| 1292 |
0 |
| mspTCPDisconnect( &(pHconn->tcpParms.sockfd) );
|
| 1293 |
|
| }
|
| 1294 |
|
|
|
| 1295 |
|
| /* mspTCPInitialise will also reconnect TCP/IP */
|
| 1296 |
3 |
| rc = mspTCPInitialise( pHconn );
|
| 1297 |
|
|
|
| 1298 |
3 |
▶ | if ( rc == 0 ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) {
|
0 |
3 |
3 |
|
| 1299 |
0 |
| pHconn->connState = MQISDP_CONNECTING;
|
| 1300 |
|
| /* Take a copy of the message to send */
|
| 1301 |
0 |
| connMsg = (char*)mspMalloc( &(pHconn->comParms), pHconn->reconnect.connMsgSz );
|
| 1302 |
0 |
▶ | if ( connMsg != NULL ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( connMsg != NULL ) {
|
0 |
0 |
0 |
|
| 1303 |
0 |
| memcpy( connMsg, pHconn->reconnect.connectMsg, pHconn->reconnect.connMsgSz );
|
| 1304 |
|
|
|
| 1305 |
0 |
| mspSendScadaMessage( pHconn, pHconn->reconnect.connMsgSz, connMsg, 0, 0, 0 );
|
| 1306 |
|
| }
|
| 1307 |
|
| }
|
| 1308 |
|
|
|
| 1309 |
3 |
| return rc;
|
| 1310 |
|
| }
|
| 1311 |
|
|
|