MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
sock.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbc/sock.c,v 1.12 2017/01/12 14:43:43 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #ifdef USE_SOCKET
35 
36 #include <stdio.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <stddef.h>
42 #include <ctype.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netdb.h>
49 #include <sys/un.h>
50 #include <arpa/inet.h>
51 #include <errno.h>
52 #include <sys/poll.h>
53 
54 #include "sock.h"
55 
56 int
57 mbdyn_make_inet_socket(struct sockaddr_in *name, const char *hostname,
58  unsigned short int port, int dobind, int *perrno)
59 {
60  return mbdyn_make_inet_socket_type(name, hostname, port, MBDYN_DEFAULT_SOCKET_TYPE, dobind, perrno);
61 }
62 
63 int
64 mbdyn_host2inet_addr(struct sockaddr_in *name, const char *hostname, unsigned short int port, int socket_type, int *perrno)
65 {
66  if (hostname) {
67 #if defined(HAVE_GETADDRINFO)
68  char portbuf[sizeof("65535") + 1];
69  struct addrinfo hints = { 0 }, *res = NULL;
70  int rc;
71 
72  rc = snprintf(portbuf, sizeof(portbuf), "%d", (int)port);
73  if (rc > STRLENOF("65535")) {
74  return -4;
75  }
76 
77  hints.ai_family = AF_INET;
78  hints.ai_socktype = socket_type;
79  rc = getaddrinfo(hostname, portbuf, &hints, &res);
80  if (rc != 0) {
81  *perrno = errno;
82  return -3;
83  }
84 
85  name->sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
86 
87  freeaddrinfo(res);
88 
89 #elif defined(HAVE_GETHOSTBYNAME)
90  struct hostent *hostinfo;
91 
92  /* TODO: use getnameinfo() if available */
93  hostinfo = gethostbyname(hostname);
94  if (hostinfo == NULL) {
95  *perrno = h_errno;
96  return -3;
97  }
98 
99  name->sin_addr = *(struct in_addr *)hostinfo->h_addr;
100 #elif defined(HAVE_INET_ATON)
101  struct in_addr addr;
102  if (inet_aton(hostname, &addr) == 0) {
103  *perrno = errno;
104  return -3;
105  }
106  name->sin_addr = addr.s_addr;
107 #else
108  return -3;
109 #endif
110  } else {
111  name->sin_addr.s_addr = htonl(INADDR_ANY);
112  }
113 
114  return 0;
115 }
116 
117 int
118 mbdyn_make_inet_socket_type(struct sockaddr_in *name, const char *hostname,
119  unsigned short int port, int socket_type, int dobind, int *perrno)
120 {
121  int sock = -1;
122  struct sockaddr_in tmpname = { 0 };
123 
124  if (name == NULL) {
125  name = &tmpname;
126  }
127 
128  if (perrno) {
129  *perrno = 0;
130  }
131 
132  /* Give the socket a name. */
133  name->sin_family = AF_INET;
134  name->sin_port = htons(port);
135 
136  int rc = mbdyn_host2inet_addr(name, hostname, port, socket_type, perrno);
137  if (rc != 0) {
138  return rc;
139  }
140 
141  /* Create the socket. */
142  sock = socket(PF_INET, socket_type, 0);
143  if (sock < 0) {
144  if (perrno) {
145  *perrno = errno;
146  }
147  return -1;
148  }
149 
150  if (dobind) {
151  rc = bind(sock, (struct sockaddr *) name, sizeof(struct sockaddr_in));
152  if (rc < 0) {
153  if (perrno) {
154  *perrno = errno;
155  }
156  return -2;
157  }
158  }
159 
160  return sock;
161 }
162 
163 int
164 mbdyn_make_named_socket(struct sockaddr_un *name, const char *path,
165  int dobind, int *perrno)
166 {
167  return mbdyn_make_named_socket_type(name, path, MBDYN_DEFAULT_SOCKET_TYPE, dobind, perrno);
168 }
169 
170 int
171 mbdyn_make_named_socket_type(struct sockaddr_un *name, const char *path,
172  int socket_type, int dobind, int *perrno)
173 {
174  int sock = -1;
175 
176  struct sockaddr_un tmpname = { 0 };
177  socklen_t size;
178 
179  if (name == NULL) {
180  name = &tmpname;
181  }
182 
183  if (perrno) {
184  *perrno = 0;
185  }
186 
187  /* Create the socket. */
188  sock = socket(PF_LOCAL, socket_type, 0);
189  if (sock < 0) {
190  if (perrno) {
191  *perrno = errno;
192  }
193  return -1;
194  }
195 
196  /* Give the socket a name. */
197  name->sun_family = AF_LOCAL;
198  strncpy(name->sun_path, path, sizeof(name->sun_path));
199 #ifdef HAVE_OFFSETOF
200  size = (offsetof(struct sockaddr_un, sun_path)
201  + strlen(name->sun_path) + 1);
202 #else /* HAVE_OFFSETOF */
203  size = sizeof(struct sockaddr_un);
204 #endif /* !HAVE_OFFSETOF */
205 
206  if (dobind) {
207  int rc = bind(sock, (struct sockaddr *)name, size);
208  if (rc < 0) {
209  if (perrno) {
210  *perrno = errno;
211  }
212  return -2;
213  }
214  }
215 
216  return sock;
217 }
218 
219 #endif /* USE_SOCKET */
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
Definition: sock.h:73
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)
#define STRLENOF(s)
Definition: mbdyn.h:166
unsigned short int port
Definition: autopilot.c:143
const char * path
Definition: autopilot.c:141