/** * Duplicate a string, safely, allocating space on the heap * @param src the source string which characters copy from * @return the duplicated, allocated string */ char* MQTTStrdup(const char* src) { size_t mlen = strlen(src) + 1; char* temp = malloc(mlen); MQTTStrncpy(temp, src, mlen); return temp; }
/** * Separates an address:port into two separate values * @param uri the input string - hostname:port * @param port the returned port integer * @return the address string */ char* MQTTProtocol_addressPort(const char* uri, int* port) { char* colon_pos = strrchr(uri, ':'); /* reverse find to allow for ':' in IPv6 addresses */ char* buf = (char*)uri; int len; FUNC_ENTRY; if (uri[0] == '[') { /* ip v6 */ if (colon_pos < strrchr(uri, ']')) colon_pos = NULL; /* means it was an IPv6 separator, not for host:port */ } if (colon_pos) { int addr_len = colon_pos - uri; buf = malloc(addr_len + 1); *port = atoi(colon_pos + 1); MQTTStrncpy(buf, uri, addr_len+1); } else *port = DEFAULT_PORT; len = strlen(buf); if (buf[len - 1] == ']') buf[len - 1] = '\0'; FUNC_EXIT; return buf; }