In this article I will present to you a useful function for getting the host name and service of a socket.
Below, is the header containing the interface of the function:
/* * Copyright (C) 2014 Efstathios Chatzikyriakidis (stathis.chatzikyriakidis@gmail.com). * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #ifndef _SOCKET_HELPER_UTILITIES_H_ #define _SOCKET_HELPER_UTILITIES_H_ #include <sys/socket.h> #include "shared-types.h" /* * function prototypes. */ bool_t get_host_and_service (const int socket_descriptor, socklen_t address_length, const string_t const host, socklen_t host_length, const string_t const service, socklen_t service_length, const unsigned int flags); #endif // _SOCKET_HELPER_UTILITIES_H_
Next, follows the implementation of the function:
/* * Copyright (C) 2014 Efstathios Chatzikyriakidis (stathis.chatzikyriakidis@gmail.com). * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <netdb.h> #include <stdlib.h> #include "socket-helper-utilities.h" /* * functions. */ bool_t get_host_and_service (const int socket_descriptor, socklen_t address_length, const string_t const host, socklen_t host_length, const string_t const service, socklen_t service_length, const unsigned int flags) { struct sockaddr * address = (struct sockaddr *) malloc (address_length); if (!address) { return false; } if (getsockname (socket_descriptor, address, &address_length) < 0) { free(address); return false; } const int return_value = getnameinfo (address, address_length, host, host_length, service, service_length, flags); free(address); if (return_value != 0) { return false; } return true; }
Here is an example of using the function in a server application:
... code before socklen_t server_address_length; const int server_socket_descriptor = create_server_socket (options->server_address, options->server_port, AI_PASSIVE | AI_NUMERICSERV, AF_UNSPEC, IPPROTO_TCP, SOCK_STREAM, &server_address_length); if (server_socket_descriptor < 0) { log_error (_(failed_to_connect_server_socket_message)); return false; } char host[NI_MAXHOST], service[NI_MAXSERV]; if (!get_host_and_service (server_socket_descriptor, server_address_length, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV)) { log_error (_(failed_to_get_server_host_name_and_service_message)); return false; } log_info ("%s [%s - %s]", _(server_listens_on_following_socket_message), host, service); ... code after
Happy Hacking!