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