int MQTTSProtocol_handleSubscribes(void* pack, int sock, char* clientAddr, Clients* client) { int rc = 0; MQTTS_Subscribe* sub = (MQTTS_Subscribe*)pack; int isnew; int topicId = 0; char* topicName = NULL , *preDefinedTopicName = NULL; FUNC_ENTRY; Log(LOG_PROTOCOL, 67, NULL, sock, clientAddr, client ? client->clientID : "", sub->msgId, (sub->flags.QoS == 3) ? -1: sub->flags.QoS, sub->flags.topicIdType); // NORMAL (topic name is in subscribe packet) or SHORT topic name if (sub->flags.topicIdType == MQTTS_TOPIC_TYPE_NORMAL || sub->flags.topicIdType == MQTTS_TOPIC_TYPE_SHORT) { topicName = sub->topicName; sub->topicName = NULL; } // Pre-defined topic else if (sub->flags.topicIdType == MQTTS_TOPIC_TYPE_PREDEFINED && client != NULL && sub->topicId != 0) { char *name = MQTTSProtocol_getPreRegisteredTopicName(client, sub->topicId) ; if (name) { preDefinedTopicName = MQTTSProtocol_replaceTopicNamePlaceholders(client , name ) ; } topicName = preDefinedTopicName ; topicId = sub->topicId; } // If topic name not found send SubAck with Rejected - Invalid topic ID if (topicName == NULL) rc = MQTTSPacket_send_subAck(client, sub, 0, sub->flags.QoS, MQTTS_RC_REJECTED_INVALID_TOPIC_ID); else { if (sub->flags.topicIdType == MQTTS_TOPIC_TYPE_NORMAL && !Topics_hasWildcards(topicName)) { char* regTopicName = malloc(strlen(topicName)+1); strcpy(regTopicName, topicName); topicId = (MQTTSProtocol_registerTopic(client, regTopicName))->id; } isnew = SubscriptionEngines_subscribe(bstate->se, client->clientID, topicName, sub->flags.QoS, client->noLocal, (client->cleansession == 0), PRIORITY_NORMAL); if ( (rc = MQTTSPacket_send_subAck(client, sub, topicId, sub->flags.QoS, MQTTS_RC_ACCEPTED)) == 0) if ((client->noLocal == 0) || isnew) MQTTProtocol_processRetaineds(client, topicName,sub->flags.QoS, PRIORITY_NORMAL); } time( &(client->lastContact) ); MQTTSPacket_free_packet(pack); if (preDefinedTopicName) free (preDefinedTopicName); FUNC_EXIT_RC(rc); return rc; }
int MQTTSProtocol_handleRegisters(void* pack, int sock, char* clientAddr, Clients* client) { int rc = 0; MQTTS_Register* registerPack = (MQTTS_Register*)pack; ListElement* elem = NULL; int topicId = 0; FUNC_ENTRY; Log(LOG_PROTOCOL, 51, NULL, sock, clientAddr, client ? client->clientID : "", registerPack->msgId, registerPack->topicId, registerPack->topicName); if ((elem = ListFindItem(client->registrations, registerPack->topicName, registeredTopicNameCompare)) == NULL) { topicId = (MQTTSProtocol_registerTopic(client, registerPack->topicName))->id; registerPack->topicName = NULL; } else topicId = ((Registration*)(elem->content))->id; rc = MQTTSPacket_send_regAck(client, registerPack->msgId, topicId, MQTTS_RC_ACCEPTED); time( &(client->lastContact) ); MQTTSPacket_free_packet(pack); FUNC_EXIT_RC(rc); return rc; }
int MQTTSProtocol_startRegistration(Clients* client, char* topic) { int rc = 0; FUNC_ENTRY; if (client->outbound) rc = MQTTSProtocol_startClientRegistration(client,topic); else { PendingRegistration* pendingReg = malloc(sizeof(PendingRegistration)); Registration* reg; int msgId = MQTTProtocol_assignMsgId(client); char* regTopicName = malloc(strlen(topic)+1); strcpy(regTopicName,topic); reg = MQTTSProtocol_registerTopic(client, regTopicName); pendingReg->msgId = msgId; pendingReg->registration = reg; time(&(pendingReg->sent)); client->pendingRegistration = pendingReg; rc = MQTTSPacket_send_register(client, reg->id, regTopicName, msgId); } FUNC_EXIT_RC(rc); return rc; }