1 /*-----------------------------------------------------------------------
2 
3 File  : cio_network.h
4 
5 Author: Stephan Schulz (schulz@eprover.org)
6 
7 Contents
8 
9   Helper code for TCP connections and "message" based communication
10   over TCP (each message corresponds to a transaction request and is
11   packages as a message to allow parsing in whole).
12 
13   Copyright 2011 by the author.
14   This code is released under the GNU General Public Licence.
15   See the file COPYING in the main CLIB directory for details.
16   Run "eprover -h" for contact information.
17 
18 Changes
19 
20 <1> Wed Mar  9 22:24:40 CET 2011
21     New
22 
23 -----------------------------------------------------------------------*/
24 
25 #ifndef CIO_NETWORK
26 
27 #define CIO_NETWORK
28 
29 #include <sys/socket.h>
30 #include <stdint.h>
31 #include <clb_dstrings.h>
32 #include <clb_pqueue.h>
33 
34 /* send_msg(char* msg) -> len, bytes*/
35 /* char* recv_msg()...*/
36 
37 /*---------------------------------------------------------------------*/
38 /*                    Data type declarations                           */
39 /*---------------------------------------------------------------------*/
40 
41 
42 typedef enum
43 {
44    NWIncomplete  = 0,
45    NWError       = 1,
46    NWConnClosed  = 2,
47    NWSuccess     = 3
48 }MsgStatus;
49 
50 /* A single message */
51 
52 typedef struct tcp_msg_cell
53 {
54    DStr_p content; /* Includes 4 bytes initial bytes of total message
55                       lengths in network byte order, and the payload. */
56    int len;     /* Including length field. */
57    int transmission_count; /* How many bytes have been sent/received? */
58    char len_buf[sizeof(uint32_t)];
59 }TCPMsgCell, *TCPMsg_p;
60 
61 
62 
63 /*---------------------------------------------------------------------*/
64 /*                Exported Functions and Variables                     */
65 /*---------------------------------------------------------------------*/
66 
67 
68 #define TCPMsgCellAlloc()    (TCPMsgCell*)SizeMalloc(sizeof(TCPMsgCell))
69 #define TCPMsgCellFree(junk) SizeFree(junk, sizeof(TCPMsgCell))
70 
71 #define TCP_MSG_COMPLETE(msg) ((msg)->len == (msg)->transmission_count)
72 
73 TCPMsg_p  TCPMsgAlloc(void);
74 void   TCPMsgFree(TCPMsg_p junk);
75 
76 
77 TCPMsg_p TCPMsgPack(char* str);
78 char*    TCPMsgUnpack(TCPMsg_p msg);
79 
80 MsgStatus TCPMsgWrite(int sock, TCPMsg_p msg);
81 MsgStatus TCPMsgRead(int sock, TCPMsg_p msg);
82 
83 MsgStatus TCPMsgSend(int sock, TCPMsg_p msg);
84 TCPMsg_p  TCPMsgRecv(int sock, MsgStatus *res);
85 
86 MsgStatus TCPStringSend(int sock, char* str, bool err);
87 char*     TCPStringRecv(int sock, MsgStatus* res, bool err);
88 
89 void      TCPStringSendX(int sock, char* str);
90 char*     TCPStringRecvX(int sock);
91 
92 
93 int  CreateServerSock(int port);
94 void Listen(int sock);
95 
96 int  CreateClientSock(char* host, int port);
97 
98 #endif
99 
100 
101 /*---------------------------------------------------------------------*/
102 /*                        End of File                                  */
103 /*---------------------------------------------------------------------*/
104 
105 
106 
107 
108 
109