e322523845888111cb29d45f7837d1bb891cfa86
[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 #include "gmxpre.h"
50
51 #include "config.h"
52
53 #include <errno.h>
54 #include <string.h>
55
56 #include "gromacs/utility/smalloc.h"
57 #include "gromacs/utility/fatalerror.h"
58 #include "imdsocket.h"
59 #include "imd.h"
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 <unistd.h>
82 #endif
83
84
85 /*! \brief Simple error handling. */
86 #ifdef GMX_NATIVE_WINDOWS
87 #define ERR_ARGS __FILE__, __LINE__, NULL
88 #else
89 #define ERR_ARGS __FILE__, __LINE__, strerror(errno)
90 #endif
91
92
93 /*! \brief Currently only 1 client connection is supported. */
94 #define MAXIMDCONNECTIONS 1
95
96
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)
99 {
100     fprintf(stderr, "%s Error in file %s on line %d.\n", IMDstr, file, line);
101
102     if (NULL != msg)
103     {
104         fprintf(stderr, "%s\n", msg);
105     }
106 }
107
108
109 extern IMDSocket* imdsock_create()
110 {
111     IMDSocket *sock = NULL;
112
113
114 #ifdef GMX_IMD
115     snew(sock, 1);
116     /* Try to create socket: */
117     if ((sock->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
118     {
119         print_IMD_error(ERR_ARGS);
120         sfree(sock);
121
122         return NULL;
123     }
124     else
125 #endif
126     {
127         return sock;
128     }
129 }
130
131
132 extern int imdsock_bind(IMDSocket *sock, int port)
133 {
134     int ret;
135
136
137 #ifdef GMX_IMD
138     memset(&(sock->address), 0, sizeof(sock->address));
139     sock->address.sin_family = PF_INET;
140     sock->address.sin_port   = htons(port);
141
142     /* Try to bind to address and port ...*/
143     ret = bind(sock->sockfd, (struct sockaddr *) &sock->address, sizeof(sock->address));
144 #else
145     ret = -1;
146 #endif
147
148     if (ret)
149     {
150         print_IMD_error(ERR_ARGS);
151     }
152
153     return ret;
154 }
155
156
157 extern int imd_sock_listen(IMDSocket *sock)
158 {
159     int ret;
160
161
162 #ifdef GMX_IMD
163     /* Try to set to listening state */
164     ret = listen(sock->sockfd, MAXIMDCONNECTIONS);
165 #else
166     ret = -1;
167 #endif
168
169     if (ret)
170     {
171         print_IMD_error(ERR_ARGS);
172     }
173
174     return ret;
175 }
176
177
178 extern IMDSocket* imdsock_accept(IMDSocket *sock)
179 {
180     int       ret;
181
182 #ifdef GMX_IMD
183     socklen_t length;
184
185
186     length = sizeof(sock->address);
187     ret    = accept(sock->sockfd, (struct sockaddr *) &sock->address, &length);
188
189     /* successful, redirect to distinct clientsocket */
190     if (ret >= 0)
191     {
192         IMDSocket *newsock;
193
194         snew(newsock, 1);
195         newsock->address    = sock->address;
196         newsock->sockfd     = ret;
197
198         return newsock;
199     }
200     else
201 #endif
202     {
203         print_IMD_error(ERR_ARGS);
204
205         return NULL;
206     }
207 }
208
209
210 extern int imdsock_getport(IMDSocket *sock, int *port)
211 {
212     int                ret;
213 #ifdef GMX_IMD
214     struct sockaddr_in sin;
215     socklen_t          len;
216
217
218     len = sizeof(sin);
219     ret = getsockname(sock->sockfd, (struct sockaddr *) &(sock->address), &len);
220     if (ret)
221     {
222         fprintf(stderr, "%s getsockname failed with error %d.\n", IMDstr, ret);
223         print_IMD_error(ERR_ARGS);
224     }
225     else
226     {
227         *port = ntohs(sock->address.sin_port);
228     }
229 #else
230     gmx_incons("imdsock_getport called without IMD support.");
231 #endif
232
233     return ret;
234 }
235
236
237 extern int imdsock_write(IMDSocket *sock, const char *buffer, int length)
238 {
239     /* No read and write on windows, we have to use send and recv instead... */
240 #ifdef GMX_NATIVE_WINDOWS
241 #ifdef GMX_HAVE_WINSOCK
242     return send(sock->sockfd, (const char *) buffer, length, NOFLAGS);
243 #else
244     return -1;
245 #endif
246 #else
247     return write(sock->sockfd, buffer, length);
248 #endif
249 }
250
251
252 extern int imdsock_read(IMDSocket *sock, char *buffer, int length)
253 {
254     /* See above... */
255 #ifdef GMX_NATIVE_WINDOWS
256 #ifdef GMX_HAVE_WINSOCK
257     return recv(sock->sockfd, (char *) buffer, length, NOFLAGS);
258 #else
259     return -1;
260 #endif
261 #else
262     return read(sock->sockfd, buffer, length);
263 #endif
264 }
265
266
267 extern void imdsock_shutdown(IMDSocket *sock)
268 {
269     int ret = -1;
270
271
272     /* is the socket already NULL? */
273     if (sock == NULL)
274     {
275         return;
276     }
277
278 #ifdef GMX_IMD
279     /* If not, try to properly shut down. */
280     ret = shutdown(sock->sockfd, 1);
281 #endif
282
283     if (ret == -1)
284     {
285         fprintf(stderr, "%s Failed to shutdown socket. Did the client already disconnect?\n", IMDstr);
286         print_IMD_error(ERR_ARGS);
287     }
288 }
289
290
291 extern int imdsock_destroy(IMDSocket *sock)
292 {
293     int ret = -1;
294
295
296     if (sock == NULL)
297     {
298         return 1;
299     }
300
301 #ifdef GMX_NATIVE_WINDOWS
302     /* On Windows, this function is called closesocket */
303 #ifdef GMX_HAVE_WINSOCK
304     ret = closesocket(sock->sockfd);
305 #endif
306 #else
307     ret = close(sock->sockfd);
308 #endif
309
310     if (ret == -1)
311     {
312         sfree(sock);
313         print_IMD_error(ERR_ARGS);
314
315         return 0;
316     }
317
318     return 1;
319 }
320
321
322 extern int imdsock_tryread(IMDSocket *sock, int timeoutsec, int timeoutusec)
323 {
324     int             ret = -1;
325
326
327 #ifdef GMX_IMD
328     fd_set          readfds;
329     /* Create new time structure with sec and usec. */
330     struct timeval *tval;
331
332
333     snew(tval, 1);
334
335     /* clear the set */
336     FD_ZERO(&readfds);
337     /* add the socket to the read set */
338     FD_SET(sock->sockfd, &readfds);
339
340     /* set the timeout */
341     tval->tv_sec  = timeoutsec;
342     tval->tv_usec = timeoutusec;
343     do
344     {
345         /* check the set for read readiness. */
346         ret = select(sock->sockfd + 1, &readfds, NULL, NULL, tval);
347         /* redo on system interrupt */
348     }
349     while (ret < 0 && errno == EINTR);
350
351     sfree(tval);
352 #endif
353
354     if (ret < 0)
355     {
356         print_IMD_error(ERR_ARGS);
357     }
358
359     return ret;
360 }