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 File Reference
#include "config.h"
#include <sys/time.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define SOCKET_ERROR   -1
 

Functions

bool INET_BytesToRead (int socket)
 
void INET_close (int socket)
 
void INET_cleanup ()
 
int INET_Recv (int socket, char *data, int bytestoread)
 
bool INET_Read (int socket, char *data, int bytestoread)
 
int INET_Write (int socket, int bytestowrite, const char *data)
 
void INET_startup ()
 
char INET_fgetc (int socket)
 
bool INET_getHostByName (const char *hostname, unsigned short port, sockaddr_in &connexto)
 
int INET_listen (unsigned short port, const char *hostname)
 
int INET_Accept (int hServerSocket)
 
int INET_AcceptFrom (unsigned short port, const char *hostname)
 
int INET_ConnectTo (const char *hostname, unsigned short port)
 

Macro Definition Documentation

#define SOCKET_ERROR   -1

Definition at line 9 of file inet.cpp.

Referenced by INET_listen(), and INET_Read().

Function Documentation

int INET_Accept ( int  hServerSocket)

Definition at line 164 of file inet.cpp.

Referenced by INET_AcceptFrom().

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 }
int INET_AcceptFrom ( unsigned short  port,
const char *  hostname 
)

Definition at line 177 of file inet.cpp.

References INET_Accept(), INET_close(), and INET_listen().

Referenced by main().

178 {
179  int hServerSocket = INET_listen( port, hostname );
180  int hSocket = INET_Accept( hServerSocket );
181  INET_close( hServerSocket );
182  return hSocket;
183 }
bool INET_BytesToRead ( int  socket)

Definition at line 26 of file inet.cpp.

Referenced by ForkedProcess(), ForwardBytes(), Music::Listen(), main(), and my_bytestoread().

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 }
void INET_cleanup ( )

Definition at line 56 of file inet.cpp.

Referenced by main(), and Music::~Music().

57 {
58 #ifdef _WIN32
59  WSACleanup();
60 #endif
61 }
void INET_close ( int  socket)

Definition at line 48 of file inet.cpp.

Referenced by FinishOlde(), INET_AcceptFrom(), main(), and Music::~Music().

49 {
50 #ifdef _WIN32
51  closesocket( socket );
52 #else
53  close( socket );
54 #endif
55 }
int INET_ConnectTo ( const char *  hostname,
unsigned short  port 
)

Definition at line 184 of file inet.cpp.

References fprintf, INET_getHostByName(), and VsnetOSS::socket().

Referenced by mmoc::connectTo(), main(), and Music::Music().

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 }
char INET_fgetc ( int  socket)

Definition at line 91 of file inet.cpp.

References INET_Read().

Referenced by Music::Listen(), main(), my_getchar(), and my_getstring().

92 {
93  char myc = '\0';
94  INET_Read( socket, &myc, sizeof (char) );
95  return myc;
96 }
bool INET_getHostByName ( const char *  hostname,
unsigned short  port,
sockaddr_in &  connexto 
)

Definition at line 98 of file inet.cpp.

References VsnetOSS::memcpy(), and sizeof().

Referenced by INET_ConnectTo(), and INET_listen().

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 }
int INET_listen ( unsigned short  port,
const char *  hostname 
)

Definition at line 122 of file inet.cpp.

References INET_getHostByName(), VsnetOSS::socket(), and SOCKET_ERROR.

Referenced by INET_AcceptFrom().

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 }
bool INET_Read ( int  socket,
char *  data,
int  bytestoread 
)

Definition at line 66 of file inet.cpp.

References VsnetOSS::recv(), and SOCKET_ERROR.

Referenced by INET_fgetc(), and mmoc::listenThread().

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 }
int INET_Recv ( int  socket,
char *  data,
int  bytestoread 
)

Definition at line 62 of file inet.cpp.

References VsnetOSS::recv().

Referenced by ForwardBytes(), and mmoc::listenThread().

63 {
64  return recv( socket, data, bytestoread, 0 );
65 }
void INET_startup ( )

Definition at line 83 of file inet.cpp.

Referenced by main(), mmoc::mmoc(), and Music::Music().

84 {
85 #ifdef _WIN32
86  WORD wVersionRequested = MAKEWORD( 1, 1 );
87  WSADATA wsaData;
88  WSAStartup( wVersionRequested, &wsaData );
89 #endif
90 }
int INET_Write ( int  socket,
int  bytestowrite,
const char *  data 
)

Definition at line 79 of file inet.cpp.

Referenced by FinishOlde(), ForwardBytes(), ForwardFileBytes(), main(), Music::Music(), music_finished(), mmoc::send(), and Music::~Music().

80 {
81  return send( socket, data, bytestowrite, 0 );
82 }