sockets.h



This is one of the most important file of the project. In this file we define macros to overwrite systems' specific functions. So we can write generic code.


/* The following code is C code only */

#ifdef __cplusplus
extern "C" {
#endif



#ifndef SOCKETS_H

  #include "depend.h"


  /**************************************************/
  /* Error codes */
  /**************************************************/

  #define SCK_OK 0
  #define SCK_CREATE_ERROR -1
  #define SCK_CONNECT_ERROR -2
  #define SCK_GET_IP_ERROR -3
  #define SCK_TIMEOUT_CONNECT -4
  #define SCK_SOCK_INIT_DLL_ERR -6 // Windows only
  #define SCK_SOCK_INIT_DLL_CHECK_ERR -7 // Windows only
  #define SCK_SET_NON_BLOCK_ERR -8 // Windows only
  #define SCK_SET_BLOCK_ERR -9 // Windows only

  /**************************************************/
  /* OS specific header files */
  /**************************************************/

  #if OS == UNIX_OS
        #include <stdio.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <string.h>
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <netdb.h>
        #include <netinet/in.h>
        #include <sys/param.h>
        #include <arpa/inet.h>
  #endif

  #if OS == WINDOWS_OS
        #include <winsock.h>
        #include <windows.h>
  #endif

  /**************************************************/
  /* OS specific types, functions and return values */
  /**************************************************/

  /************************************************************************/
  /* CLOSE_SOCKET(x): */
  /* close the socket 'x'. */
  /* WRITE_TO_SOCKET(a,b,c,d): write 'c' bytes from the buffer 'b' to */
  /* the socket 'a'. The parameter 'd' is only used under Windows. */
  /* 'd' represents options. */
  /* READ_FROM_SOCKET(a,b,c,d): read a maximum of 'c' bytes fom the */
  /* socket 'a'. The bytes extracted from the socket are saved into */
  /* the buffer pointed by 'b'. The parameter 'd' is only used under */
  /* Windows. 'd' represents options. */
  /* CONNECT_TO_SERVER(a, b, c): open a TCP connexion from socket 'a' to */
  /* server which Internet address is 'b'. */
  /* CREATE_SOCKET(a, b, c): Create a Internet socket. */
  /* SCK_ERROR_READ: return value for READ_FROM_SOCKET() in case of an */
  /* error. */
  /* SCK_ERROR_WRITE: return value for WRITE_TO_SOCKET() in case of an */
  /* error. */
  /* SCK_ERROR_SOCKET: return value for CREATE_SOCKET() in case of an */
  /* error. */
  /* SCK_ERROR_CONNECT: return value for in case of an error. */
  /************************************************************************/

  #if OS == UNIX_OS
          typedef struct sockaddr_in SOCK_IN;
        typedef int SOCK;

        #define CLOSE_SOCKET(x) close(x)
        #define WRITE_TO_SOCKET(a,b,c,d) write(a, (void*)b, c)
        #define READ_FROM_SOCKET(a,b,c,d) read(a, (void*)b, c)
        #define CONNECT_TO_SERVER(a, b, c) connect(a, (struct sockaddr*)b, c)
        #define CREATE_SOCKET(a, b, c) socket(a, b, c);

        #define SCK_ERROR_READ -1
        #define SCK_ERROR_WRITE -1
        #define SCK_ERROR_SOCKET -1
        #define SCK_ERROR_CONNECT -1
  #endif

  #if OS == WINDOWS_OS
        typedef SOCKADDR_IN SOCK_IN;
        typedef SOCKET SOCK;

        #define CLOSE_SOCKET(x) closesocket(x)
        #define WRITE_TO_SOCKET(a,b,c,d) send(a, (const char FAR*)b, c, d)
        #define READ_FROM_SOCKET(a,b,c,d) recv(a, (char FAR*)b, c, d)
        #define CONNECT_TO_SERVER(a, b, c) connect(a, (struct sockaddr FAR*)b, c)
        #define CREATE_SOCKET(a, b, c) socket(a, b, c);

        #define SCK_ERROR_READ SOCKET_ERROR
        #define SCK_ERROR_WRITE SOCKET_ERROR
    #define SCK_ERROR_SOCKET INVALID_SOCKET
        #define SCK_ERROR_CONNECT SOCKET_ERROR
  #endif

  /**************************************************/
  /* OS generic interface */
  /**************************************************/

  SOCK open_tcp_connexion (SOCK_IN*, char*, int, int);
  int Get_Sock_Adress (char*, int, SOCK_IN*);

  #define SOCKETS_H


#endif /* SOCKETS_H */


#ifdef __cplusplus
}
#endif