1 /* Copyright (c) 2006 Adam Warrington
2 ** $Id$
3 **
4 ** Permission is hereby granted, free of charge, to any person obtaining a copy
5 ** of this software and associated documentation files (the "Software"), to deal
6 ** in the Software without restriction, including without limitation the rights
7 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 ** copies of the Software, and to permit persons to whom the Software is
9 ** furnished to do so, subject to the following conditions:
10 **
11 ** The above copyright notice and this permission notice shall be included in
12 ** all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 ** SOFTWARE.
21 **
22 ******************************************************************************
23 **
24 ** This file, os_common.h, contains the definitions of the operating system
25 ** specific calls all the operating systems. The operating system specific
26 ** files will include this to get their functionality.
27 */
28 
29 #include "os.h"
30 
31 /* declaration of the operating system specific socket structures. This
32    needs to be included in this file because many of the functions in
33    here will need to operate on these structures
34 
35    because structures are used that are defined in operating system specific
36    locations, we need to include the appropriate headers where those are
37    defined. These headers are platform dependent, so depending on the platform,
38    choose the right one.
39 */
40 #if OS_UNIX
41   /* defines for unix */
42   #include <fcntl.h>
43   #include <time.h>
44   #include <sys/types.h>
45   #include <sys/socket.h>
46   #include <sys/select.h>
47   #include <netinet/in.h>
48   #include <arpa/inet.h>
49   #include <netdb.h>
50   #include <stdlib.h>
51   #include <unistd.h>
52 
53   #include <sys/time.h>
54   #include <string.h>
55 
56   /* this is the unix version of the OsSocket struct */
57   struct OsSocket {
58     int sock;
59   };
60 #endif
61 #if OS_WIN
62   /* defines for win32 */
63   #include <windows.h>
64   #include <winsock.h>
65   typedef int socklen_t;
66 
67   /* this is the win32 version of the OsSocket struct */
68   struct OsSocket {
69     SOCKET sock;
70   };
71 #endif
72 
73 
74 /* This function will send data over a udp socket to address host_addr,
75    and port port. You need to specify the buffer */
76 int LNat_Common_Socket_Udp_Send(OsSocket * s, const char * host_addr, short int port,
77                                 char * buf, int amt, int * amt_sent);
78 
79 /* This function will recieve data over a udp socket form address host_addr,
80    and port port. You need to specify the buffer to store it in, and the
81    amt you are expecting to receive.
82 */
83 int LNat_Common_Socket_Udp_Recv(OsSocket * s, const char * host_addr, short int port,
84                                 char * buf, int amt, int * amt_recv, int timeout_sec);
85 
86 /* function to send the data of length amt, in buffer buf, over a connected
87 socket s. If send is successful, return OK. set the amount actually sent in
88 amt_sent parameter */
89 int LNat_Common_Socket_Send(OsSocket * s, char * buf, int amt, int * amt_sent);
90 
91 /* function to recv the data of length amt, into an already allocated buffer
92 buf, over a connected socket s. If recv is successful, return oK. Set the
93 amount actually recieved in amt_recv parameter */
94 int LNat_Common_Socket_Recv(OsSocket * s, char * buf, int amt,
95                             int * amt_recv, int timeout_sec);
96 
97 
98 /* forward declaration for the sockaddr_in and hostent structures. these are
99    needed for the Initialize_Sockaddr_in function declaration */
100 struct sockaddr_in;
101 struct hostent;
102 
103 /* function to intitialize a sockaddr_in structure */
104 int Common_Initialize_Sockaddr_in(struct sockaddr_in* server,
105                                   struct hostent** hp,
106                                   const char * host_addr,
107                                   short int port);
108 
109 /* This function will call select which will block till timeout,
110    or until there is data ready to be read on the socket */
111 int Select_Till_Readyread(OsSocket * s, int timeout_sec);
112 
113 /* This function will call seelct which will block till timeout,
114    or until the socket is ready to be written to */
115 int Select_Till_Readywrite(OsSocket * s, int timeout_sec);
116 
117 /* get the local ip address from a connected socket */
118 int LNat_Common_Get_Local_Ip(OsSocket * s, char ** local_ip);
119 
120 
121