2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
39 * Implements functions of imdsocket.h.
41 * This file re-implements vmdsock.c functions from original IMD API from scratch,
42 * see imdsocket.h for references to the IMD API.
44 * \author Martin Hoefling, Carsten Kutzner <ckutzne@gwdg.de>
56 #include "gromacs/utility/smalloc.h"
57 #include "gromacs/utility/fatalerror.h"
58 #include "imdsocket.h"
61 #ifdef GMX_NATIVE_WINDOWS
62 #ifdef GMX_HAVE_WINSOCK
63 /*! \brief Define socklen type on Windows. */
64 typedef int socklen_t;
66 /*! \brief Define a function to initialize winsock. */
67 extern int imdsock_winsockinit()
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);
80 /* On UNIX, we can use nice errors from errno.h */
85 /*! \brief Simple error handling. */
86 #ifdef GMX_NATIVE_WINDOWS
87 #define ERR_ARGS __FILE__, __LINE__, NULL
89 #define ERR_ARGS __FILE__, __LINE__, strerror(errno)
93 /*! \brief Currently only 1 client connection is supported. */
94 #define MAXIMDCONNECTIONS 1
97 /*! \brief Print a nice error message on UNIX systems, using errno.h. */
98 static void print_IMD_error(char *file, int line, char *msg)
100 fprintf(stderr, "%s Error in file %s on line %d.\n", IMDstr, file, line);
104 fprintf(stderr, "%s\n", msg);
109 extern IMDSocket* imdsock_create()
111 IMDSocket *sock = NULL;
116 /* Try to create socket: */
117 if ((sock->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
119 print_IMD_error(ERR_ARGS);
132 extern int imdsock_bind(IMDSocket *sock, int port)
138 memset(&(sock->address), 0, sizeof(sock->address));
139 sock->address.sin_family = PF_INET;
140 sock->address.sin_port = htons(port);
142 /* Try to bind to address and port ...*/
143 ret = bind(sock->sockfd, (struct sockaddr *) &sock->address, sizeof(sock->address));
148 print_IMD_error(ERR_ARGS);
155 extern int imd_sock_listen(IMDSocket *sock)
161 /* Try to set to listening state */
162 ret = listen(sock->sockfd, MAXIMDCONNECTIONS);
167 print_IMD_error(ERR_ARGS);
174 extern IMDSocket* imdsock_accept(IMDSocket *sock)
182 length = sizeof(sock->address);
183 ret = accept(sock->sockfd, (struct sockaddr *) &sock->address, &length);
185 /* successful, redirect to distinct clientsocket */
191 newsock->address = sock->address;
192 newsock->sockfd = ret;
199 print_IMD_error(ERR_ARGS);
206 extern int imdsock_getport(IMDSocket *sock, int *port)
210 struct sockaddr_in sin;
215 ret = getsockname(sock->sockfd, (struct sockaddr *) &(sock->address), &len);
218 fprintf(stderr, "%s getsockname failed with error %d.\n", IMDstr, ret);
219 print_IMD_error(ERR_ARGS);
223 *port = ntohs(sock->address.sin_port);
226 gmx_incons("imdsock_getport called without IMD support.");
233 extern int imdsock_write(IMDSocket *sock, const char *buffer, int length)
235 /* No read and write on windows, we have to use send and recv instead... */
236 #ifdef GMX_NATIVE_WINDOWS
237 #ifdef GMX_HAVE_WINSOCK
238 return send(sock->sockfd, (const char *) buffer, length, NOFLAGS);
243 return write(sock->sockfd, buffer, length);
248 extern int imdsock_read(IMDSocket *sock, char *buffer, int length)
251 #ifdef GMX_NATIVE_WINDOWS
252 #ifdef GMX_HAVE_WINSOCK
253 return recv(sock->sockfd, (char *) buffer, length, NOFLAGS);
258 return read(sock->sockfd, buffer, length);
263 extern void imdsock_shutdown(IMDSocket *sock)
268 /* is the socket already NULL? */
275 /* If not, try to properly shut down. */
276 ret = shutdown(sock->sockfd, 1);
281 fprintf(stderr, "%s Failed to shutdown socket. Did the client already disconnect?\n", IMDstr);
282 print_IMD_error(ERR_ARGS);
287 extern int imdsock_destroy(IMDSocket *sock)
297 #ifdef GMX_NATIVE_WINDOWS
298 /* On Windows, this function is called closesocket */
299 #ifdef GMX_HAVE_WINSOCK
300 ret = closesocket(sock->sockfd);
303 ret = close(sock->sockfd);
309 print_IMD_error(ERR_ARGS);
318 extern int imdsock_tryread(IMDSocket *sock, int timeoutsec, int timeoutusec)
325 /* Create new time structure with sec and usec. */
326 struct timeval *tval;
333 /* add the socket to the read set */
334 FD_SET(sock->sockfd, &readfds);
336 /* set the timeout */
337 tval->tv_sec = timeoutsec;
338 tval->tv_usec = timeoutusec;
341 /* check the set for read readiness. */
342 ret = select(sock->sockfd + 1, &readfds, NULL, NULL, tval);
343 /* redo on system interrupt */
345 while (ret < 0 && errno == EINTR);
352 print_IMD_error(ERR_ARGS);