/* Hint that a hostname may be found at an address
* See header file for detailed comment.
*/
-extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, struct sockaddr *addr) {
+extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr) {
if(!mesh || !node || !addr)
return;
- char *addr_str = malloc(MAX_ADDRESS_LENGTH*sizeof(char));
- memset(addr_str, 0, MAX_ADDRESS_LENGTH*sizeof(char));
+ char *host = NULL, *port = NULL, *str = NULL;
+ sockaddr2str((const sockaddr_t *)addr, &host, &port);
- char *port_str = malloc(MAX_PORT_LENGTH*sizeof(char));
- memset(port_str, 0, MAX_PORT_LENGTH*sizeof(char));
-
- // extra byte for a space, and one to make sure string is null-terminated
- int full_addr_len = MAX_ADDRESS_LENGTH + MAX_PORT_LENGTH + 2;
-
- char *full_addr_str = malloc(full_addr_len*sizeof(char));
- memset(full_addr_str, 0, full_addr_len*sizeof(char));
-
- // get address and port number
- if(!get_ip_str(addr, addr_str, MAX_ADDRESS_LENGTH))
- goto fail;
- if(!get_port_str(addr, port_str, MAX_ADDRESS_LENGTH))
- goto fail;
-
- // append_config_file expects an address, a space, and then a port number
- strcat(full_addr_str, addr_str);
- strcat(full_addr_str, " ");
- strcat(full_addr_str, port_str);
-
- append_config_file(mesh, node->name, "Address", full_addr_str);
+ if(host && port) {
+ xasprintf(&str, "%s %s", host, port);
+ append_config_file(mesh, node->name, "Address", str);
+ }
-fail:
- free(addr_str);
- free(port_str);
- free(full_addr_str);
+ free(str);
+ free(host);
+ free(port);
// @TODO do we want to fire off a connection attempt right away?
}
memcpy(&value, bitfield, size);
return value;
}
-
-/* Write IP address from sockaddr to string.
- * Returns NULL on error.
- */
-char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
-{
- switch(sa->sa_family) {
- case AF_INET:
- inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
- s, maxlen);
- break;
-
- case AF_INET6:
- inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
- s, maxlen);
- break;
-
- default:
- strncpy(s, "Unknown AF", maxlen);
- return NULL;
- }
-
- return s;
-}
-
-/* Write port from sockaddr to string.
- * Returns NULL on error.
- */
-char *get_port_str(const struct sockaddr *sa, char *s, size_t maxlen)
-{
- switch(sa->sa_family) {
- case AF_INET:
- snprintf(s, maxlen, "%d",
- ntohs(((struct sockaddr_in*)sa)->sin_port));
- break;
-
- case AF_INET6:
- snprintf(s, maxlen, "%d",
- ntohs(((struct sockaddr_in6*)sa)->sin6_port));
- break;
-
- default:
- strncpy(s, "Unknown AF", maxlen);
- return NULL;
- }
-
- return s;
-}