Merge release-4-6 into release-5-0
[alexxy/gromacs.git] / src / gromacs / imd / imdsocket.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 /*! \internal \file
37  *
38  * \brief
39  * Implements functions of imdsocket.h.
40  *
41  * This file re-implements vmdsock.c functions from original IMD API from scratch,
42  * see imdsocket.h for references to the IMD API.
43  *
44  * \author Martin Hoefling, Carsten Kutzner <ckutzne@gwdg.de>
45  *
46  * \ingroup module_imd
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53
54 #include <string.h>
55 #include "gromacs/utility/smalloc.h"
56 #include "gmx_fatal.h"
57 #include "imdsocket.h"
58 #include "imd.h"
59
60
61 #ifdef GMX_NATIVE_WINDOWS
62 #ifdef GMX_HAVE_WINSOCK
63 /*! \brief Define socklen type on Windows. */
64 typedef int socklen_t;
65
66 /*! \brief Define a function to initialize winsock. */
67 extern int imdsock_winsockinit()
68 {
69     int ret = -1;
70
71
72     WSADATA wsd;
73
74     /* We use winsock 1.1 compatibility for now. Though I guess no one will try on Windows 95. */
75     ret = WSAStartup(MAKEWORD(1, 1), &wsd);
76     return ret;
77 }
78 #endif
79 #else
80 /* On UNIX, we can use nice errors from errno.h */
81 #include <errno.h>
82 #include <unistd.h>
83 #endif
84
85
86 /*! \brief Simple error handling. */
87 #ifdef GMX_NATIVE_WINDOWS
88 #define ERR_ARGS __FILE__, __LINE__, NULL
89 #else
90 #define ERR_ARGS __FILE__, __LINE__, strerror(errno)
91 #endif
92
93
94 /*! \brief Currently only 1 client connection is supported. */
95 #define MAXIMDCONNECTIONS 1
96
97
98 /*! \brief Print a nice error message on UNIX systems, using errno.h. */
99 static void print_IMD_error(char *file, int line, char *msg)
100 {
101     fprintf(stderr, "%s Error in file %s on line %d.\n", IMDstr, file, line);
102
103     if (NULL != msg)
104     {
105         fprintf(stderr, "%s\n", msg);
106     }
107 }
108
109
110 extern IMDSocket* imdsock_create()
111 {
112     IMDSocket *sock = NULL;
113
114
115 #ifdef GMX_IMD
116     snew(sock, 1);
117     /* Try to create socket: */
118     if ((sock->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
119     {
120         print_IMD_error(ERR_ARGS);
121         sfree(sock);
122
123         return NULL;
124     }
125     else
126 #endif
127     {
128         return sock;
129     }
130 }
131
132
133 extern int imdsock_bind(IMDSocket *sock, int port)
134 {
135     int ret = -1;
136
137
138 #ifdef GMX_IMD
139     memset(&(sock->address), 0, sizeof(sock->address));
140     sock->address.sin_family = PF_INET;
141     sock->address.sin_port   = htons(port);
142
143     /* Try to bind to address and port ...*/
144     ret = bind(sock->sockfd, (struct sockaddr *) &sock->address, sizeof(sock->address));
145 #endif
146
147     if (ret)
148     {
149         print_IMD_error(ERR_ARGS);
150     }
151
152     return ret;
153 }
154
155
156 extern int imd_sock_listen(IMDSocket *sock)
157 {
158     int ret = -1;
159
160
161 #ifdef GMX_IMD
162     /* Try to set to listening state */
163     ret = listen(sock->sockfd, MAXIMDCONNECTIONS);
164 #endif
165
166     if (ret)
167     {
168         print_IMD_error(ERR_ARGS);
169     }
170
171     return ret;
172 }
173
174
175 extern IMDSocket* imdsock_accept(IMDSocket *sock)
176 {
177     int       ret = -1;
178
179 #ifdef GMX_IMD
180     socklen_t length;
181
182
183     length = sizeof(sock->address);
184     ret    = accept(sock->sockfd, (struct sockaddr *) &sock->address, &length);
185
186     /* successful, redirect to distinct clientsocket */
187     if (ret >= 0)
188     {
189         IMDSocket *newsock;
190
191         snew(newsock, 1);
192         newsock->address    = sock->address;
193         newsock->sockfd     = ret;
194
195         return newsock;
196     }
197     else
198 #endif
199     {
200         print_IMD_error(ERR_ARGS);
201
202         return NULL;
203     }
204 }
205
206
207 extern int imdsock_getport(IMDSocket *sock, int *port)
208 {
209     int                ret = -1;
210 #ifdef GMX_IMD
211     struct sockaddr_in sin;
212     socklen_t          len;
213
214
215     len = sizeof(sin);
216     ret = getsockname(sock->sockfd, (struct sockaddr *) &(sock->address), &len);
217     if (ret)
218     {
219         fprintf(stderr, "%s getsockname failed with error %d.\n", IMDstr, ret);
220         print_IMD_error(ERR_ARGS);
221     }
222     else
223     {
224         *port = ntohs(sock->address.sin_port);
225     }
226 #else
227     gmx_incons("imdsock_getport called without IMD support.");
228 #endif
229
230     return ret;
231 }
232
233
234 extern int imdsock_write(IMDSocket *sock, const char *buffer, int length)
235 {
236     /* No read and write on windows, we have to use send and recv instead... */
237 #ifdef GMX_NATIVE_WINDOWS
238 #ifdef GMX_HAVE_WINSOCK
239     return send(sock->sockfd, (const char *) buffer, length, NOFLAGS);
240 #else
241     return -1;
242 #endif
243 #else
244     return write(sock->sockfd, buffer, length);
245 #endif
246 }
247
248
249 extern int imdsock_read(IMDSocket *sock, char *buffer, int length)
250 {
251     /* See above... */
252 #ifdef GMX_NATIVE_WINDOWS
253 #ifdef GMX_HAVE_WINSOCK
254     return recv(sock->sockfd, (char *) buffer, length, NOFLAGS);
255 #else
256     return -1;
257 #endif
258 #else
259     return read(sock->sockfd, buffer, length);
260 #endif
261 }
262
263
264 extern void imdsock_shutdown(IMDSocket *sock)
265 {
266     int ret = -1;
267
268
269     /* is the socket already NULL? */
270     if (sock == NULL)
271     {
272         return;
273     }
274
275 #ifdef GMX_IMD
276     /* If not, try to properly shut down. */
277     ret = shutdown(sock->sockfd, 1);
278 #endif
279
280     if (ret == -1)
281     {
282         fprintf(stderr, "%s Failed to shutdown socket. Did the client already disconnect?\n", IMDstr);
283         print_IMD_error(ERR_ARGS);
284     }
285 }
286
287
288 extern int imdsock_destroy(IMDSocket *sock)
289 {
290     int ret = -1;
291
292
293     if (sock == NULL)
294     {
295         return 1;
296     }
297
298 #ifdef GMX_NATIVE_WINDOWS
299     /* On Windows, this function is called closesocket */
300 #ifdef GMX_HAVE_WINSOCK
301     ret = closesocket(sock->sockfd);
302 #endif
303 #else
304     ret = close(sock->sockfd);
305 #endif
306
307     if (ret == -1)
308     {
309         sfree(sock);
310         print_IMD_error(ERR_ARGS);
311
312         return 0;
313     }
314
315     return 1;
316 }
317
318
319 extern int imdsock_tryread(IMDSocket *sock, int timeoutsec, int timeoutusec)
320 {
321     int             ret = -1;
322
323
324 #ifdef GMX_IMD
325     fd_set          readfds;
326     /* Create new time structure with sec and usec. */
327     struct timeval *tval;
328
329
330     snew(tval, 1);
331
332     /* clear the set */
333     FD_ZERO(&readfds);
334     /* add the socket to the read set */
335     FD_SET(sock->sockfd, &readfds);
336
337     /* set the timeout */
338     tval->tv_sec  = timeoutsec;
339     tval->tv_usec = timeoutusec;
340     do
341     {
342         /* check the set for read readiness. */
343         ret = select(sock->sockfd + 1, &readfds, NULL, NULL, tval);
344         /* redo on system interrupt */
345     }
346     while (ret < 0 && errno == EINTR);
347
348     sfree(tval);
349 #endif
350
351     if (ret < 0)
352     {
353         print_IMD_error(ERR_ARGS);
354     }
355
356     return ret;
357 }