static int build_http_request(const char* domainName, const char* path, int ocspReqSz, byte* buf, int bufSize) { return XSNPRINTF((char*)buf, bufSize, "POST %s HTTP/1.1\r\n" "Host: %s\r\n" "Content-Length: %d\r\n" "Content-Type: application/ocsp-request\r\n" "\r\n", path, domainName, ocspReqSz); }
int awsiot_test(MQTTCtx *mqttCtx) { int rc = MQTT_CODE_SUCCESS, i; switch (mqttCtx->stat) { case WMQ_BEGIN: { PRINTF("AwsIoT Client: QoS %d, Use TLS %d", mqttCtx->qos, mqttCtx->use_tls); /* Aws IoT requires TLS */ if (!mqttCtx->use_tls) { return MQTT_CODE_ERROR_BAD_ARG; } } case WMQ_NET_INIT: { mqttCtx->stat = WMQ_NET_INIT; /* Initialize Network */ rc = MqttClientNet_Init(&mqttCtx->net); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Net Init: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); if (rc != MQTT_CODE_SUCCESS) { goto exit; } /* setup tx/rx buffers */ mqttCtx->tx_buf = (byte*)WOLFMQTT_MALLOC(MAX_BUFFER_SIZE); mqttCtx->rx_buf = (byte*)WOLFMQTT_MALLOC(MAX_BUFFER_SIZE); } case WMQ_INIT: { mqttCtx->stat = WMQ_INIT; /* Initialize MqttClient structure */ rc = MqttClient_Init(&mqttCtx->client, &mqttCtx->net, mqtt_message_cb, mqttCtx->tx_buf, MAX_BUFFER_SIZE, mqttCtx->rx_buf, MAX_BUFFER_SIZE, mqttCtx->cmd_timeout_ms); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Init: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); if (rc != MQTT_CODE_SUCCESS) { goto exit; } mqttCtx->client.ctx = mqttCtx; } case WMQ_TCP_CONN: { mqttCtx->stat = WMQ_TCP_CONN; /* Connect to broker */ rc = MqttClient_NetConnect(&mqttCtx->client, mqttCtx->host, mqttCtx->port, DEFAULT_CON_TIMEOUT_MS, mqttCtx->use_tls, mqtt_aws_tls_cb); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Socket Connect: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); if (rc != MQTT_CODE_SUCCESS) { goto exit; } } case WMQ_MQTT_CONN: { mqttCtx->stat = WMQ_MQTT_CONN; XMEMSET(&mqttCtx->connect, 0, sizeof(MqttConnect)); mqttCtx->connect.keep_alive_sec = mqttCtx->keep_alive_sec; mqttCtx->connect.clean_session = mqttCtx->clean_session; mqttCtx->connect.client_id = mqttCtx->client_id; /* Last will and testament sent by broker to subscribers of topic when broker connection is lost */ XMEMSET(&mqttCtx->lwt_msg, 0, sizeof(mqttCtx->lwt_msg)); mqttCtx->connect.lwt_msg = &mqttCtx->lwt_msg; mqttCtx->connect.enable_lwt = mqttCtx->enable_lwt; if (mqttCtx->enable_lwt) { /* Send client id in LWT payload */ mqttCtx->lwt_msg.qos = mqttCtx->qos; mqttCtx->lwt_msg.retain = 0; mqttCtx->lwt_msg.topic_name = AWSIOT_PUBLISH_TOPIC"lwt"; mqttCtx->lwt_msg.buffer = (byte*)mqttCtx->client_id; mqttCtx->lwt_msg.total_len = (word16)XSTRLEN(mqttCtx->client_id); } /* Optional authentication */ mqttCtx->connect.username = mqttCtx->username; mqttCtx->connect.password = mqttCtx->password; /* Send Connect and wait for Connect Ack */ rc = MqttClient_Connect(&mqttCtx->client, &mqttCtx->connect); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Connect: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); if (rc != MQTT_CODE_SUCCESS) { goto disconn; } /* Validate Connect Ack info */ PRINTF("MQTT Connect Ack: Return Code %u, Session Present %d", mqttCtx->connect.ack.return_code, (mqttCtx->connect.ack.flags & MQTT_CONNECT_ACK_FLAG_SESSION_PRESENT) ? 1 : 0 ); /* Build list of topics */ mqttCtx->topics[0].topic_filter = mqttCtx->topic_name; mqttCtx->topics[0].qos = mqttCtx->qos; /* Subscribe Topic */ XMEMSET(&mqttCtx->subscribe, 0, sizeof(MqttSubscribe)); mqttCtx->subscribe.packet_id = mqtt_get_packetid(); mqttCtx->subscribe.topic_count = sizeof(mqttCtx->topics)/sizeof(MqttTopic); mqttCtx->subscribe.topics = mqttCtx->topics; } case WMQ_SUB: { mqttCtx->stat = WMQ_SUB; rc = MqttClient_Subscribe(&mqttCtx->client, &mqttCtx->subscribe); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Subscribe: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); if (rc != MQTT_CODE_SUCCESS) { goto disconn; } /* show subscribe results */ for (i = 0; i < mqttCtx->subscribe.topic_count; i++) { mqttCtx->topic = &mqttCtx->subscribe.topics[i]; PRINTF(" Topic %s, Qos %u, Return Code %u", mqttCtx->topic->topic_filter, mqttCtx->topic->qos, mqttCtx->topic->return_code); } /* Publish Topic */ XSNPRINTF(mqttCtx->buffer.pubMsg, sizeof(mqttCtx->buffer.pubMsg), "{\"state\":{\"reported\":{\"hardware\":{\"type\":\"%s\",\"firmware_version\":\"%s\"}}}}", APP_HARDWARE, APP_FIRMWARE_VERSION); XMEMSET(&mqttCtx->publish, 0, sizeof(MqttPublish)); mqttCtx->publish.retain = 0; mqttCtx->publish.qos = mqttCtx->qos; mqttCtx->publish.duplicate = 0; mqttCtx->publish.topic_name = AWSIOT_PUBLISH_TOPIC; mqttCtx->publish.packet_id = mqtt_get_packetid(); mqttCtx->publish.buffer = (byte*)mqttCtx->buffer.pubMsg; mqttCtx->publish.total_len = (word32)XSTRLEN(mqttCtx->buffer.pubMsg); } case WMQ_PUB: { mqttCtx->stat = WMQ_PUB; rc = MqttClient_Publish(&mqttCtx->client, &mqttCtx->publish); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Publish: Topic %s, %s (%d)", mqttCtx->publish.topic_name, MqttClient_ReturnCodeToString(rc), rc); if (rc != MQTT_CODE_SUCCESS) { goto disconn; } /* Read Loop */ PRINTF("MQTT Waiting for message..."); MqttClientNet_CheckForCommand_Enable(&mqttCtx->net); } case WMQ_WAIT_MSG: { mqttCtx->stat = WMQ_WAIT_MSG; do { /* Try and read packet */ rc = MqttClient_WaitMessage(&mqttCtx->client, mqttCtx->cmd_timeout_ms); /* check for test mode or stop */ if (mStopRead || mqttCtx->test_mode) { rc = MQTT_CODE_SUCCESS; break; } /* check return code */ if (rc == MQTT_CODE_CONTINUE) { return rc; } else if (rc == MQTT_CODE_ERROR_TIMEOUT) { /* Check to see if command data (stdin) is available */ rc = MqttClientNet_CheckForCommand(&mqttCtx->net, mqttCtx->rx_buf, MAX_BUFFER_SIZE); if (rc > 0) { /* Publish Topic */ XSNPRINTF(mqttCtx->buffer.pubMsg, sizeof(mqttCtx->buffer.pubMsg), "{\"state\":{\"reported\":{\"msg\":\"%s\"}}}", mqttCtx->rx_buf); mqttCtx->stat = WMQ_PUB; XMEMSET(&mqttCtx->publish, 0, sizeof(MqttPublish)); mqttCtx->publish.retain = 0; mqttCtx->publish.qos = mqttCtx->qos; mqttCtx->publish.duplicate = 0; mqttCtx->publish.topic_name = AWSIOT_PUBLISH_TOPIC; mqttCtx->publish.packet_id = mqtt_get_packetid(); mqttCtx->publish.buffer = (byte*)mqttCtx->buffer.pubMsg; mqttCtx->publish.total_len = (word32)XSTRLEN(mqttCtx->buffer.pubMsg); rc = MqttClient_Publish(&mqttCtx->client, &mqttCtx->publish); PRINTF("MQTT Publish: Topic %s, %s (%d)", mqttCtx->publish.topic_name, MqttClient_ReturnCodeToString(rc), rc); } /* Keep Alive */ else { rc = MqttClient_Ping(&mqttCtx->client); if (rc == MQTT_CODE_CONTINUE) { return rc; } else if (rc != MQTT_CODE_SUCCESS) { PRINTF("MQTT Ping Keep Alive Error: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); break; } } } else if (rc != MQTT_CODE_SUCCESS) { /* There was an error */ PRINTF("MQTT Message Wait: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); break; } } while (1); /* Check for error */ if (rc != MQTT_CODE_SUCCESS) { goto disconn; } } case WMQ_DISCONNECT: { PRINTF("MQTT Exiting..."); /* Disconnect */ rc = MqttClient_Disconnect(&mqttCtx->client); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Disconnect: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); if (rc != MQTT_CODE_SUCCESS) { goto disconn; } } case WMQ_NET_DISCONNECT: { mqttCtx->stat = WMQ_NET_DISCONNECT; rc = MqttClient_NetDisconnect(&mqttCtx->client); if (rc == MQTT_CODE_CONTINUE) { return rc; } PRINTF("MQTT Socket Disconnect: %s (%d)", MqttClient_ReturnCodeToString(rc), rc); } case WMQ_DONE: { mqttCtx->stat = WMQ_DONE; rc = mqttCtx->return_code; goto exit; } case WMQ_UNSUB: /* not used */ default: rc = MQTT_CODE_ERROR_STAT; goto exit; } /* switch */ disconn: mqttCtx->stat = WMQ_NET_DISCONNECT; mqttCtx->return_code = rc; return MQTT_CODE_CONTINUE; exit: /* Free resources */ if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf); if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf); /* Cleanup network */ MqttClientNet_DeInit(&mqttCtx->net); return rc; }
static int tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port) { struct sockaddr_storage addr; int sockaddr_len = sizeof(struct sockaddr_in); XMEMSET(&addr, 0, sizeof(addr)); #ifdef HAVE_GETADDRINFO { struct addrinfo hints; struct addrinfo* answer = NULL; char strPort[8]; XMEMSET(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; XSNPRINTF(strPort, sizeof(strPort), "%d", port); strPort[7] = '\0'; if (getaddrinfo(ip, strPort, &hints, &answer) < 0 || answer == NULL) { CYASSL_MSG("no addr info for OCSP responder"); return -1; } sockaddr_len = answer->ai_addrlen; XMEMCPY(&addr, answer->ai_addr, sockaddr_len); freeaddrinfo(answer); } #else /* HAVE_GETADDRINFO */ { struct hostent* entry = gethostbyname(ip); struct sockaddr_in *sin = (struct sockaddr_in *)&addr; if (entry) { sin->sin_family = AF_INET; sin->sin_port = htons(port); XMEMCPY(&sin->sin_addr.s_addr, entry->h_addr_list[0], entry->h_length); } else { CYASSL_MSG("no addr info for OCSP responder"); return -1; } } #endif /* HAVE_GETADDRINFO */ *sockfd = socket(addr.ss_family, SOCK_STREAM, 0); if (*sockfd < 0) { CYASSL_MSG("bad socket fd, out of fds?"); return -1; } if (connect(*sockfd, (struct sockaddr *)&addr, sockaddr_len) != 0) { CYASSL_MSG("OCSP responder tcp connect failed"); return -1; } return 0; }