Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inet.cpp
Go to the documentation of this file.
1 #include "config.h"
2 #if defined (_WIN32)
3 #define in_addr_t unsigned long
4 #ifndef NOMINMAX
5 #define NOMINMAX
6 #endif //tells VCC not to generate min/max macros
7 #include <windows.h>
8 #else
9 #define SOCKET_ERROR -1
10 #include <sys/time.h>
11 #include <sys/types.h>
12 //#if !defined(__APPLE__) && !defined(__CYGWIN__) && !defined(BSD) && !defined(__FreeBSD__) && !defined(SOLARIS)
13 //#include <error.h>
14 //#endif
15 #include <netdb.h>
16 #include <string.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 
27 {
28 #ifdef _WIN32
29  unsigned long datato;
30  if ( 0 == ioctlsocket( socket, FIONREAD, &datato ) )
31  return datato > 0;
32 #else
33  struct timeval tv;
34  tv.tv_sec = 0;
35  tv.tv_usec = 00000;
36  int selres = 0;
37  fd_set rfds;
38  FD_ZERO( &rfds );
39  FD_SET( socket, &rfds );
40  if ( ( selres = select( socket+1, &rfds, NULL, NULL, &tv ) ) ) {
41  if (selres == -1)
42  return false;
43  return true;
44  }
45 #endif
46  return false;
47 }
48 void INET_close( int socket )
49 {
50 #ifdef _WIN32
51  closesocket( socket );
52 #else
53  close( socket );
54 #endif
55 }
57 {
58 #ifdef _WIN32
59  WSACleanup();
60 #endif
61 }
62 int INET_Recv( int socket, char *data, int bytestoread )
63 {
64  return recv( socket, data, bytestoread, 0 );
65 }
66 bool INET_Read( int socket, char *data, int bytestoread )
67 {
68  int bytes_read = 0;
69  int ret;
70  while (bytes_read < bytestoread) {
71  ret = recv( socket, data+bytes_read, bytestoread-bytes_read, 0 );
72  if (ret == 0 || SOCKET_ERROR == ret)
73  return false;
74  bytes_read += ret;
75  }
76  return true;
77 }
78 
79 int INET_Write( int socket, int bytestowrite, const char *data )
80 {
81  return send( socket, data, bytestowrite, 0 );
82 }
84 {
85 #ifdef _WIN32
86  WORD wVersionRequested = MAKEWORD( 1, 1 );
87  WSADATA wsaData;
88  WSAStartup( wVersionRequested, &wsaData );
89 #endif
90 }
91 char INET_fgetc( int socket )
92 {
93  char myc = '\0';
94  INET_Read( socket, &myc, sizeof (char) );
95  return myc;
96 }
97 
98 bool INET_getHostByName( const char *hostname, unsigned short port, sockaddr_in &connexto )
99 {
100  bool gotaddr = false;
101  connexto.sin_port = htons( port );
102  connexto.sin_family = AF_INET;
103  if (hostname == NULL)
104  connexto.sin_addr.s_addr = INADDR_ANY;
105  hostent *addrs = gethostbyname( hostname );
106  if (addrs) {
107  if (addrs->h_addr_list[0]) {
108  memset( &connexto.sin_addr, 0, sizeof (in_addr) );
109  if ( addrs->h_length > (int) sizeof (in_addr) )
110  addrs->h_length = sizeof (in_addr);
111  memcpy( &connexto.sin_addr, addrs->h_addr_list[0], addrs->h_length );
112  gotaddr = true;
113  }
114  } else {
115  in_addr_t tmp = inet_addr( hostname );
116  memcpy( &connexto.sin_addr, &tmp, sizeof (in_addr_t) < sizeof (in_addr) ? sizeof (in_addr_t) : sizeof (in_addr) );
117  if (*( (int*) &tmp ) != -1)
118  gotaddr = true;
119  }
120  return gotaddr;
121 }
122 int INET_listen( unsigned short port, const char *hostname )
123 {
124  int listenqueue = 5;
125  int hServerSocket; //so signal can be caught;
126  struct sockaddr_in Address; //Internet socket address stuct
127 #if defined (_WIN32) || defined (__CYGWIN__) || defined (MAC_OS_X_VERSION_10_3) || defined (MAC_OS_X_VERSION_10_2) \
128  || defined (MAC_OS_X_VERSION_10_1)
129  int
130 #else
131  socklen_t
132 #endif
133  nAddressSize = sizeof (struct sockaddr_in);
134  hServerSocket = socket( AF_INET, SOCK_STREAM, 0 );
135  int sockerr = SOCKET_ERROR;
136 #ifdef _WIN32
137  sockerr = INVALID_SOCKET;
138 #endif
139  if (hServerSocket == sockerr) {
140 #ifdef _WIN32
141  int err = WSAGetLastError();
142  printf( "\nCould not make a socket %d\n", err );
143 #endif
144 
145  return -1;
146  }
147  INET_getHostByName( hostname, port, Address );
148  Address.sin_addr.s_addr = INADDR_ANY;
149  Address.sin_family = AF_INET;
150  if (bind( hServerSocket, (struct sockaddr*) &Address, sizeof (Address) )
151  == SOCKET_ERROR) {
152  printf( "\nCould not connect to host\n" );
153  printf( "%d Error ", errno );
154  return -1;
155  }
156  getsockname( hServerSocket, (struct sockaddr*) &Address, &nAddressSize );
157  if (listen( hServerSocket, listenqueue ) == SOCKET_ERROR) {
158  printf( "\nCould not listen\n" );
159  return -1;
160  }
161  return hServerSocket;
162  //get the connected socket
163 }
164 int INET_Accept( int hServerSocket )
165 {
166  sockaddr_in Address;
167 #if defined (_WIN32) || defined (__CYGWIN__) || defined (MAC_OS_X_VERSION_10_3) || defined (MAC_OS_X_VERSION_10_2) \
168  || defined (MAC_OS_X_VERSION_10_1)
169  int
170 #else
171  socklen_t
172 #endif
173  nAddressSize = sizeof (Address);
174  return accept( hServerSocket, (struct sockaddr*) &Address, &nAddressSize );
175 }
176 
177 int INET_AcceptFrom( unsigned short port, const char *hostname )
178 {
179  int hServerSocket = INET_listen( port, hostname );
180  int hSocket = INET_Accept( hServerSocket );
181  INET_close( hServerSocket );
182  return hSocket;
183 }
184 int INET_ConnectTo( const char *hostname, unsigned short port )
185 {
186  sockaddr_in connexto;
187  int aftype = AF_INET;
188 #ifdef _WIN32
189  aftype = PF_INET;
190 #endif
191  int retval = -1;
192  int my_socket = -1;
193  if ( INET_getHostByName( hostname, port, connexto ) ) {
194  my_socket = socket( aftype, SOCK_STREAM, 0 );
195  retval = true;
196  if (connect( my_socket, (struct sockaddr*) &connexto, sizeof (connexto) ) != -1) {
197  return my_socket;
198  } else {
199  retval = -1;
200 #ifdef _WIN32
201  fprintf( stderr, "Socket Error %d", WSAGetLastError() );
202 #endif
203  }
204  }
205  return retval;
206 }
207