45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
50 #include <arpa/inet.h>
58 unsigned short int port,
int dobind,
int *perrno)
64 mbdyn_host2inet_addr(
struct sockaddr_in *name,
const char *hostname,
unsigned short int port,
int socket_type,
int *perrno)
67 #if defined(HAVE_GETADDRINFO)
68 char portbuf[
sizeof(
"65535") + 1];
69 struct addrinfo hints = { 0 }, *res = NULL;
72 rc = snprintf(portbuf,
sizeof(portbuf),
"%d", (
int)port);
77 hints.ai_family = AF_INET;
78 hints.ai_socktype = socket_type;
79 rc = getaddrinfo(hostname, portbuf, &hints, &res);
85 name->sin_addr = ((
struct sockaddr_in *)res->ai_addr)->sin_addr;
89 #elif defined(HAVE_GETHOSTBYNAME)
90 struct hostent *hostinfo;
93 hostinfo = gethostbyname(hostname);
94 if (hostinfo == NULL) {
99 name->sin_addr = *(
struct in_addr *)hostinfo->h_addr;
100 #elif defined(HAVE_INET_ATON)
102 if (inet_aton(hostname, &addr) == 0) {
106 name->sin_addr = addr.s_addr;
111 name->sin_addr.s_addr = htonl(INADDR_ANY);
119 unsigned short int port,
int socket_type,
int dobind,
int *perrno)
122 struct sockaddr_in tmpname = { 0 };
133 name->sin_family = AF_INET;
134 name->sin_port = htons(port);
136 int rc = mbdyn_host2inet_addr(name, hostname, port, socket_type, perrno);
142 sock = socket(PF_INET, socket_type, 0);
151 rc = bind(sock, (
struct sockaddr *) name,
sizeof(
struct sockaddr_in));
165 int dobind,
int *perrno)
172 int socket_type,
int dobind,
int *perrno)
176 struct sockaddr_un tmpname = { 0 };
188 sock = socket(PF_LOCAL, socket_type, 0);
197 name->sun_family = AF_LOCAL;
198 strncpy(name->sun_path, path,
sizeof(name->sun_path));
200 size = (offsetof(
struct sockaddr_un, sun_path)
201 + strlen(name->sun_path) + 1);
203 size =
sizeof(
struct sockaddr_un);
207 int rc = bind(sock, (
struct sockaddr *)name, size);
int mbdyn_make_named_socket_type(struct sockaddr_un *name, const char *path, int socket_type, int dobind, int *perror)
int mbdyn_make_inet_socket(struct sockaddr_in *name, const char *hostname, unsigned short int port, int dobind, int *perror)
#define MBDYN_DEFAULT_SOCKET_TYPE
int mbdyn_make_inet_socket_type(struct sockaddr_in *name, const char *hostname, unsigned short int port, int socket_type, int dobind, int *perror)
int mbdyn_make_named_socket(struct sockaddr_un *name, const char *path, int dobind, int *perror)