/** * Get information about the other end connected to a socket * @param sock the socket to inquire on * @return the peer information */ char* Socket_getpeer(int sock) { struct sockaddr_in6 sa; socklen_t sal = sizeof(sa); int rc; if ((rc = getpeername(sock, (struct sockaddr*)&sa, &sal)) == SOCKET_ERROR) { Socket_error("getpeername", sock); return "unknown"; } return Socket_getaddrname((struct sockaddr*)&sa, sock); }
void* MQTTSPacket_Factory(int sock, char** clientAddr, struct sockaddr* from, uint8_t** wlnid , uint8_t *wlnid_len , int* error) { static MQTTSHeader header; void* pack = NULL; /*struct sockaddr_in cliAddr;*/ int n; char* data = msg; socklen_t len = sizeof(struct sockaddr_in6); *wlnid = NULL ; *wlnid_len = 0 ; FUNC_ENTRY; /* #if !defined(NO_BRIDGE) client = Protocol_getoutboundclient(sock); FUNC_ENTRY; if (client!=NULL) n = recv(sock,msg,512,0); else #endif */ /* max message size from global parameters, as we lose the packet if we don't receive it. Default is * 65535, so the parameter can be used to decrease the memory usage. * The message memory area must be allocated on the heap so that this memory can be not allocated * on reduced-memory systems. */ n = recvfrom(sock, msg, max_packet_size, 0, from, &len); if (n == SOCKET_ERROR) { int en = Socket_error("UDP read error", sock); if (en == EINVAL) Log(LOG_WARNING, 0, "EINVAL"); *error = SOCKET_ERROR; goto exit; } *clientAddr = Socket_getaddrname(from, sock); /* printf("%d bytes of data on socket %d from %s\n",n,sock,*clientAddr); if (n>0) { for (i=0;i<n;i++) { printf("%d ",msg[i]); } printf("\n"); } */ *error = SOCKET_ERROR; // indicate whether an error occurred, or not if (n < 2) goto exit; data = MQTTSPacket_parse_header( &header, data ) ; /* In case of Forwarder Encapsulation packet, Length: 1-octet long, specifies the number of octets up to the end * of the “Wireless Node Id” field (incl. the Length octet itself). Length does not include length of payload * (encapsulated MQTT-SN message itself). */ if (header.type != MQTTS_FRWDENCAP && header.len != n) { *error = UDPSOCKET_INCOMPLETE; goto exit; } else { // Forwarder Encapsulation packet. Extract Wireless Node Id and MQTT-SN message if ( header.type == MQTTS_FRWDENCAP ) { // Skip Crt(1) field data++ ; // Wireless Node Id *wlnid = data ; // Wireless Node Id length is packet length - 3 octet (Length(1) + MsgType(1) + Crt(1)) *wlnid_len = header.len - 3 ; data += *wlnid_len ; // Read encapsulated packet and set header and shift data to beginning of payload data = MQTTSPacket_parse_header( &header, data ) ; } uint8_t ptype = header.type; if (ptype < MQTTS_ADVERTISE || ptype > MQTTS_WILLMSGRESP || new_mqtts_packets[ptype] == NULL) Log(TRACE_MAX, 17, NULL, ptype); else if ((pack = (*new_mqtts_packets[ptype])(header, data)) == NULL) *error = BAD_MQTTS_PACKET; } exit: FUNC_EXIT_RC(*error); return pack; }
void* MQTTSPacket_Factory(int sock, char** clientAddr, struct sockaddr* from, int* error) { static MQTTSHeader header; int ptype; void* pack = NULL; /*struct sockaddr_in cliAddr;*/ int n; char* data = &msg[0]; socklen_t len = sizeof(struct sockaddr_in6); FUNC_ENTRY; /* #if !defined(NO_BRIDGE) client = Protocol_getoutboundclient(sock); FUNC_ENTRY; if (client!=NULL) n = recv(sock,msg,512,0); else #endif */ /* max message size from global parameters, as we lose the packet if we don't receive it. Default is * 65535, so the parameter can be used to decrease the memory usage. * The message memory area must be allocated on the heap so that this memory can be not allocated * on reduced-memory systems. */ n = recvfrom(sock, msg, max_packet_size, 0, from, &len); if (n == SOCKET_ERROR) { int en = Socket_error("UDP read error", sock); if (en == EINVAL) Log(LOG_WARNING, 0, "EINVAL"); *error = SOCKET_ERROR; goto exit; } *clientAddr = Socket_getaddrname(from, sock); /* printf("%d bytes of data on socket %d from %s\n",n,sock,*clientAddr); if (n>0) { for (i=0;i<n;i++) { printf("%d ",msg[i]); } printf("\n"); } */ *error = SOCKET_ERROR; // indicate whether an error occurred, or not if (n < 2) goto exit; if (msg[0] == 1) { ++data; header.len = readInt(&data); } else header.len = *(unsigned char*)data++; header.type = *data++; //printf("header.type is %d, header.len is %d, n is %d\n", header.type, header.len, n); if (header.len != n) { *error = UDPSOCKET_INCOMPLETE; goto exit; } else { ptype = header.type; if (ptype < MQTTS_ADVERTISE || ptype > MQTTS_WILLMSGRESP || new_mqtts_packets[ptype] == NULL) Log(TRACE_MAX, 17, NULL, ptype); else if ((pack = (*new_mqtts_packets[ptype])(header, data)) == NULL) *error = BAD_MQTTS_PACKET; } exit: FUNC_EXIT_RC(*error); return pack; }