int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersion) #endif #endif { int rc, port; char* addr; FUNC_ENTRY; aClient->good = 1; addr = MQTTProtocol_addressPort(ip_address, &port); #if defined(__GNUC__) && defined(__linux__) if (timeout < 0) rc = -1; else rc = Socket_new(addr, port, &(aClient->net.socket), timeout); #else rc = Socket_new(addr, port, &(aClient->net.socket)); #endif if (rc == EINPROGRESS || rc == EWOULDBLOCK) aClient->connect_state = 1; /* TCP connect called - wait for connect completion */ else if (rc == 0) { /* TCP connect completed. If SSL, send SSL connect */ #if defined(OPENSSL) if (ssl) { if (SSLSocket_setSocketForSSL(&aClient->net, aClient->sslopts) == 1) { rc = SSLSocket_connect(aClient->net.ssl, aClient->net.socket); if (rc == -1) aClient->connect_state = 2; /* SSL connect called - wait for completion */ } else rc = SOCKET_ERROR; } #endif if (rc == 0) { /* Now send the MQTT connect packet */ if ((rc = MQTTPacket_send_connect(aClient, MQTTVersion)) == 0) aClient->connect_state = 3; /* MQTT Connect sent - wait for CONNACK */ else aClient->connect_state = 0; } } if (addr != ip_address) free(addr); FUNC_EXIT_RC(rc); return rc; }
Clients* MQTTSProtocol_connect(char* ip_address, char* clientID, int cleansession, int try_private, int keepalive, willMessages* willMessage) { /* outgoing connection */ int rc, port; char* addr; Clients* newc = malloc(sizeof(Clients)); FUNC_ENTRY; newc->clientID = clientID; newc->cleansession = cleansession; newc->outbound = 1; newc->connected = 0; newc->ping_outstanding = 0; newc->keepAliveInterval = keepalive; newc->msgID = 0; newc->outboundMsgs = ListInitialize(); newc->inboundMsgs = ListInitialize(); newc->queuedMsgs = ListInitialize(); newc->registrations = ListInitialize(); newc->will = willMessage; newc->good = 1; newc->connect_state = 0; newc->noLocal = try_private; /* try private connection first */ time(&(newc->lastContact)); newc->discardedMsgs = 0; newc->pendingRegistration = NULL; newc->protocol = PROTOCOL_MQTTS; addr = MQTTProtocol_addressPort(ip_address, &port); newc->addr = malloc(strlen(ip_address)); strcpy(newc->addr,ip_address); rc = Socket_new_type(addr, port, &(newc->socket), SOCK_DGRAM); if (rc == EINPROGRESS || rc == EWOULDBLOCK) newc->connect_state = 1; // UDP connect called - improbable, but possible on obscure platforms else if (rc == 0) { Log(LOG_PROTOCOL, 69, NULL, newc->clientID, newc->socket, newc->noLocal); newc->connect_state = 2; rc = MQTTSPacket_send_connect(newc); } ListAppend(bstate->clients, newc, sizeof(Clients) + strlen(newc->clientID)+1 + 3*sizeof(List)); FUNC_EXIT; return newc; }
void MQTTSProtocol_reconnect(char* ip_address, Clients* client) { int port, rc; char *address = NULL; Clients* oldc = NULL; FUNC_ENTRY; oldc = TreeRemoveKeyIndex(bstate->disconnected_clients, client->clientID, 1); if (oldc == NULL) oldc = TreeRemoveKeyIndex(bstate->clients, client->clientID, 1); client->good = 1; client->ping_outstanding = 0; client->connect_state = 0; client->connected = 0; if (client->cleansession) client->msgID = 0; if (client->addr) free(client->addr); client->addr = malloc(strlen(ip_address)); strcpy(client->addr, ip_address); address = MQTTProtocol_addressPort(ip_address, &port); rc = Socket_new_type(address, port, &(client->socket), SOCK_DGRAM); if (rc == EINPROGRESS || rc == EWOULDBLOCK) client->connect_state = 1; /* UDP connect called - improbable, but possible on obscure platforms */ else if (rc == 0) { client->connect_state = 2; // TCP connect completed, in which case send the MQTT connect packet MQTTSPacket_send_connect(client); time(&(client->lastContact)); } TreeAdd(bstate->clients, client, sizeof(Clients) + strlen(client->clientID)+1 + 3*sizeof(List)); FUNC_EXIT; }
/** * MQTT outgoing connect processing for a client * @param ip_address the TCP address:port to connect to * @param clientID the MQTT client id to use * @param cleansession MQTT cleansession flag * @param keepalive MQTT keepalive timeout in seconds * @param willMessage pointer to the will message to be used, if any * @param username MQTT 3.1 username, or NULL * @param password MQTT 3.1 password, or NULL * @return the new client structure */ int MQTTProtocol_connect(char* ip_address, Clients* aClient) { int rc, port; char* addr; FUNC_ENTRY; aClient->good = 1; time(&(aClient->lastContact)); addr = MQTTProtocol_addressPort(ip_address, &port); rc = Socket_new(addr, port, &(aClient->socket)); if (rc == EINPROGRESS || rc == EWOULDBLOCK) aClient->connect_state = 1; /* TCP connect called */ else if (rc == 0) { if ((rc = MQTTPacket_send_connect(aClient)) == 0) aClient->connect_state = 2; /* TCP connect completed, in which case send the MQTT connect packet */ else aClient->connect_state = 0; } FUNC_EXIT_RC(rc); return rc; }
Clients* MQTTSProtocol_create_multicast(char* ip_address, char* clientID, int loopback) { /* outgoing connection */ int i, port, rc; char* addr; Clients* newc = NULL; char* intface = NULL; int ipv6 = 0; FUNC_ENTRY; newc = malloc(sizeof(Clients)); memset(newc, '\0', sizeof(Clients)); newc->clientID = clientID; newc->outbound = 1; newc->good = 1; newc->connected = 1; newc->outboundMsgs = ListInitialize(); newc->inboundMsgs = ListInitialize(); for (i = 0; i < PRIORITY_MAX; ++i) newc->queuedMsgs[i] = ListInitialize(); newc->registrations = ListInitialize(); newc->protocol = PROTOCOL_MQTTS_MULTICAST; /* if there is a space in the ip_address string, it means we have address plus interface specified */ if ((intface = strchr(ip_address, ' ')) != NULL) { *intface = '\0'; ++intface; } addr = MQTTProtocol_addressPort(ip_address, &port); newc->addr = malloc(strlen(ip_address) + 1); strcpy(newc->addr, ip_address); ipv6 = (newc->addr[0] == '['); rc = Socket_new_udp(&(newc->socket), ipv6); if (setsockopt(newc->socket, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&loopback, sizeof(loopback)) == SOCKET_ERROR) Socket_error("set bridge IP_MULTICAST_LOOP", newc->socket); if (intface) { if (ipv6) { int index = 0; if ((index = if_nametoindex(intface)) == 0) Socket_error("get interface index", newc->socket); else if (setsockopt(newc->socket, IPPROTO_IPV6, IPV6_MULTICAST_IF, (const char*)&index, sizeof(index)) == SOCKET_ERROR) Socket_error("set bridge IP_MULTICAST_IF", newc->socket); } else { struct in_addr interface_addr; #if defined(WIN32) if ((rc = win_inet_pton(AF_INET, intface, &interface_addr)) == SOCKET_ERROR) Socket_error("WSAStringToAddress interface", newc->socket); else { #else /* get address of the interface */ struct ifreq ifreq; strncpy(ifreq.ifr_name, intface, IFNAMSIZ); if (ioctl(newc->socket, SIOCGIFADDR, &ifreq) == SOCKET_ERROR) Socket_error("get interface address", newc->socket); else { memcpy(&interface_addr, &((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr, sizeof(struct in_addr)); #endif if (setsockopt(newc->socket, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr)) == SOCKET_ERROR) Socket_error("set bridge IP_MULTICAST_IF", newc->socket); } } } TreeAdd(bstate->disconnected_mqtts_clients, newc, sizeof(Clients) + strlen(newc->clientID)+1 + 3*sizeof(List)); FUNC_EXIT; return newc; } Clients* MQTTSProtocol_connect(char* ip_address, char* clientID, int cleansession, int try_private, int keepalive, willMessages* willMessage) { /* outgoing connection */ int rc, port; char* addr; Clients* newc = NULL; FUNC_ENTRY; newc = TreeRemoveKeyIndex(bstate->disconnected_clients, clientID, 1); /* must be a dummy client */ if (newc == NULL) newc = TreeRemoveKeyIndex(bstate->clients, clientID, 1); if (newc) { free(clientID); newc->connected = newc->ping_outstanding = newc->connect_state = newc->msgID = newc->discardedMsgs = 0; } else { int i; newc = malloc(sizeof(Clients)); memset(newc, '\0', sizeof(Clients)); newc->outboundMsgs = ListInitialize(); newc->inboundMsgs = ListInitialize(); for (i = 0; i < PRIORITY_MAX; ++i) newc->queuedMsgs[i] = ListInitialize(); newc->clientID = clientID; } newc->cleansession = cleansession; newc->outbound = newc->good = 1; newc->keepAliveInterval = keepalive; newc->registrations = ListInitialize(); newc->will = willMessage; newc->noLocal = try_private; /* try private connection first */ time(&(newc->lastContact)); newc->pendingRegistration = NULL; newc->protocol = PROTOCOL_MQTTS; addr = MQTTProtocol_addressPort(ip_address, &port); newc->addr = malloc(strlen(ip_address)); strcpy(newc->addr, ip_address); rc = Socket_new_type(addr, port, &(newc->socket), SOCK_DGRAM); if (rc == EINPROGRESS || rc == EWOULDBLOCK) newc->connect_state = 1; /* UDP connect called - improbable, but possible on obscure platforms */ else if (rc == 0) { newc->connect_state = 2; rc = MQTTSPacket_send_connect(newc); } TreeAdd(bstate->clients, newc, sizeof(Clients) + strlen(newc->clientID)+1 + 3*sizeof(List)); FUNC_EXIT; return newc; }