vmdsock.C

Go to the documentation of this file.
00001 
00007 #if ( defined(DUMMY_VMDSOCK) )
00008 
00009 #include "vmdsock.h"
00010 
00011 int vmdsock_init(void) { return -1; }
00012 void * vmdsock_create(void) { return 0; }
00013 int  vmdsock_connect(void *v, const char *host, int port) { return 0; }
00014 int vmdsock_bind(void * v, int port) { return 0; }
00015 int vmdsock_listen(void * v) { return 0; }
00016 void *vmdsock_accept(void * v) { return 0; }
00017 int  vmdsock_write(void * v, const void *buf, int len) { return 0; }
00018 int  vmdsock_read(void * v, void *buf, int len) { return 0; }
00019 void vmdsock_destroy(void * v) { return; }
00020 int vmdsock_selread(void *v, int sec) { return 0; }
00021 int vmdsock_selwrite(void *v, int sec) { return 0; }
00022 
00023 #else
00024 
00025 #define VMDSOCKINTERNAL
00026 
00027 #include <sys/types.h>
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 #if defined(WIN32) && !defined(__CYGWIN__)
00033 #include <winsock.h>
00034 #else
00035 #include <strings.h>
00036 #include <arpa/inet.h>
00037 #include <fcntl.h>
00038 
00039 #include <unistd.h>   /* for Linux */
00040 #include <sys/socket.h>
00041 #include <netdb.h>
00042 #endif
00043 
00044 #include <errno.h>
00045 
00046 #include "vmdsock.h"
00047 
00048 #ifdef NAMD_SOCKLEN_T
00049 typedef NAMD_SOCKLEN_T socklen_t;
00050 #endif
00051 
00052 int vmdsock_init(void) {
00053 #if defined(WIN32) && !defined(__CYGWIN__)
00054   int rc = 0;
00055 
00056 /*
00057 ** XXX static and global variables are unsafe for shared memory builds.
00058 ** The global and static vars should be eliminated.
00059 */
00060   static int initialized=0;
00061 
00062   if (!initialized) {
00063     WSADATA wsdata;
00064     rc = WSAStartup(MAKEWORD(1,1), &wsdata);
00065     if (rc == 0)
00066       initialized = 1;
00067   }
00068 
00069   return rc;
00070 #else   
00071   return 0;
00072 #endif
00073 }
00074 
00075 
00076 void * vmdsock_create(void) {
00077   vmdsocket * s;
00078 
00079   s = (vmdsocket *) malloc(sizeof(vmdsocket));
00080   if (s != NULL)
00081     memset(s, 0, sizeof(vmdsocket)); 
00082 
00083   if ((s->sd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
00084     printf("Failed to open socket.");
00085     free(s);
00086     return NULL;
00087   }
00088 
00089   return (void *) s;
00090 }
00091 
00092 int  vmdsock_connect(void *v, const char *host, int port) {
00093   vmdsocket *s = (vmdsocket *) v;
00094   char address[1030];
00095   struct hostent *h;
00096 
00097   h=gethostbyname(host);
00098   if (h == NULL) 
00099     return -1;
00100   sprintf(address, "%d.%d.%d.%d",
00101     (unsigned char) h->h_addr_list[0][0],
00102     (unsigned char) h->h_addr_list[0][1],
00103     (unsigned char) h->h_addr_list[0][2],
00104     (unsigned char) h->h_addr_list[0][3]);
00105 
00106   memset(&(s->addr), 0, sizeof(s->addr)); 
00107   s->addr.sin_family = PF_INET;
00108   s->addr.sin_addr.s_addr = inet_addr(address);
00109   s->addr.sin_port = htons(port);  
00110 
00111   return connect(s->sd, (struct sockaddr *) &s->addr, sizeof(s->addr)); 
00112 }
00113 
00114 int vmdsock_bind(void * v, int port) {
00115   vmdsocket *s = (vmdsocket *) v;
00116   memset(&(s->addr), 0, sizeof(s->addr)); 
00117   s->addr.sin_family = PF_INET;
00118   s->addr.sin_port = htons(port);
00119 
00120   return bind(s->sd, (struct sockaddr *) &s->addr, sizeof(s->addr));
00121 }
00122 
00123 int vmdsock_listen(void * v) {
00124   vmdsocket *s = (vmdsocket *) v;
00125   return listen(s->sd, 5);
00126 }
00127 
00128 void *vmdsock_accept(void * v) {
00129   int rc;
00130   vmdsocket *new_s = NULL, *s = (vmdsocket *) v;
00131   socklen_t len;
00132 
00133   len = sizeof(s->addr);
00134   rc = accept(s->sd, (struct sockaddr *) &s->addr, &len);
00135   if (rc >= 0) {
00136     new_s = (vmdsocket *) malloc(sizeof(vmdsocket));
00137     if (new_s != NULL) {
00138       *new_s = *s;
00139       new_s->sd = rc;
00140     }
00141   }
00142   return (void *)new_s;
00143 }
00144 
00145 int  vmdsock_write(void * v, const void *buf, int len) {
00146   vmdsocket *s = (vmdsocket *) v;
00147 #if defined(WIN32) && !defined(__CYGWIN__)
00148   return send(s->sd, (const char*) buf, len, 0);  // windows lacks the write() call
00149 #else
00150   return write(s->sd, buf, len);
00151 #endif
00152 }
00153 
00154 int  vmdsock_read(void * v, void *buf, int len) {
00155   vmdsocket *s = (vmdsocket *) v;
00156 #if defined(WIN32) && !defined(__CYGWIN__)
00157   return recv(s->sd, (char*) buf, len, 0); // windows lacks the read() call
00158 #else
00159   return read(s->sd, buf, len);
00160 #endif
00161 
00162 }
00163 
00164 void vmdsock_destroy(void * v) {
00165   vmdsocket * s = (vmdsocket *) v;
00166   if (s == NULL)
00167     return;
00168 
00169 #if defined(WIN32) && !defined(__CYGWIN__)
00170   closesocket(s->sd);
00171 #else
00172   close(s->sd);
00173 #endif
00174   free(s);  
00175 }
00176 
00177 int vmdsock_selread(void *v, int sec) {
00178   vmdsocket *s = (vmdsocket *)v;
00179   fd_set rfd;
00180   struct timeval tv;
00181   int rc;
00182  
00183   FD_ZERO(&rfd);
00184   FD_SET(s->sd, &rfd);
00185   memset((void *)&tv, 0, sizeof(struct timeval));
00186   tv.tv_sec = sec;
00187   do {
00188     rc = select(s->sd+1, &rfd, NULL, NULL, &tv);
00189   } while (rc < 0 && errno == EINTR);
00190   return rc;
00191 
00192 }
00193   
00194 int vmdsock_selwrite(void *v, int sec) {
00195   vmdsocket *s = (vmdsocket *)v;
00196   fd_set wfd;
00197   struct timeval tv;
00198   int rc;
00199  
00200   FD_ZERO(&wfd);
00201   FD_SET(s->sd, &wfd);
00202   memset((void *)&tv, 0, sizeof(struct timeval));
00203   tv.tv_sec = sec;
00204   do {
00205     rc = select(s->sd + 1, NULL, &wfd, NULL, &tv);
00206   } while (rc < 0 && errno == EINTR);
00207   return rc;
00208 }
00209 
00210 #endif
00211 

Generated on Sat Nov 18 01:17:16 2017 for NAMD by  doxygen 1.4.7