first commit
[libutp-c.git] / utp.h
blob7140b99ca07db69e98ffaaca43e8382a302741f7
1 #ifndef __UTP_H__
2 #define __UTP_H__
4 #include "utypes.h"
6 #ifdef WIN32
7 #define _CRT_SECURE_NO_DEPRECATE
8 #define WIN32_LEAN_AND_MEAN
9 #include <windows.h>
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12 #pragma comment(lib,"ws2_32.lib")
13 #else
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #endif
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
25 struct UTPSocket;
27 /* Used to set sockopt on a uTP socket to set the version of uTP
28 to use for outgoing connections. This can only be called before
29 the uTP socket is connected */
30 #define SO_UTPVERSION 99
32 enum {
33 /* socket has reveived syn-ack (notification only for outgoing connection completion)
34 this implies writability */
35 UTP_STATE_CONNECT = 1,
37 /* socket is able to send more data */
38 UTP_STATE_WRITABLE = 2,
40 /* connection closed */
41 UTP_STATE_EOF = 3,
43 /* socket is being destroyed, meaning all data has been sent if possible.
44 it is not valid to refer to the socket after this state change occurs */
45 UTP_STATE_DESTROYING = 4
48 /* Callbacks called by a uTP socket (register with UTP_SetCallbacks) */
50 /* The uTP socket layer calls this when bytes have been received from the network. */
51 typedef void UTPOnReadProc(void *userdata, const byte *bytes, size_t count);
53 /* The uTP socket layer calls this to fill the outgoing buffer with bytes.
54 The uTP layer takes responsibility that those bytes will be delivered. */
55 typedef void UTPOnWriteProc(void *userdata, byte *bytes, size_t count);
57 /* The uTP socket layer calls this to retrieve number of bytes currently in read buffer */
58 typedef size_t UTPGetRBSize(void *userdata);
60 /* The uTP socket layer calls this whenever the socket becomes writable. */
61 typedef void UTPOnStateChangeProc(void *userdata, int state);
63 /* The uTP socket layer calls this when an error occurs on the socket.
64 These errors currently include ECONNREFUSED, ECONNRESET and ETIMEDOUT, but
65 could eventually include any BSD socket error. */
66 typedef void UTPOnErrorProc(void *userdata, int errcode);
68 /* The uTP socket layer calls this to report overhead statistics */
69 typedef void UTPOnOverheadProc(void *userdata, bool send, size_t count, int type);
71 struct UTPFunctionTable {
72 UTPOnReadProc *on_read;
73 UTPOnWriteProc *on_write;
74 UTPGetRBSize *get_rb_size;
75 UTPOnStateChangeProc *on_state;
76 UTPOnErrorProc *on_error;
77 UTPOnOverheadProc *on_overhead;
81 /* The uTP socket layer calls this when a new incoming uTP connection is established
82 this implies writability */
83 typedef void UTPGotIncomingConnection(void *userdata, struct UTPSocket* s);
85 /* The uTP socket layer calls this to send UDP packets */
86 typedef void SendToProc(void *userdata, const byte *p, size_t len, const struct sockaddr *to, socklen_t tolen);
89 /* Functions which can be called with a uTP socket */
91 /* Create a uTP socket */
92 struct UTPSocket *UTP_Create(SendToProc *send_to_proc, void *send_to_userdata,
93 const struct sockaddr *addr, socklen_t addrlen);
95 /* Setup the callbacks - must be done before connect or on incoming connection */
96 void UTP_SetCallbacks(struct UTPSocket *socket, struct UTPFunctionTable *func, void *userdata);
98 /* Valid options include SO_SNDBUF, SO_RCVBUF and SO_UTPVERSION */
99 bool UTP_SetSockopt(struct UTPSocket *socket, int opt, int val);
101 /* Try to connect to a specified host. */
102 void UTP_Connect(struct UTPSocket *socket);
104 /* Process a UDP packet from the network. This will process a packet for an existing connection,
105 or create a new connection and call incoming_proc. Returns true if the packet was processed
106 in some way, false if the packet did not appear to be uTP. */
107 bool UTP_IsIncomingUTP(UTPGotIncomingConnection *incoming_proc,
108 SendToProc *send_to_proc, void *send_to_userdata,
109 const byte *buffer, size_t len, const struct sockaddr *to, socklen_t tolen);
111 /* Process an ICMP received UDP packet. */
112 bool UTP_HandleICMP(const byte* buffer, size_t len, const struct sockaddr *to, socklen_t tolen);
114 /* Write bytes to the uTP socket.
115 Returns true if the socket is still writable. */
116 bool UTP_Write(struct UTPSocket *socket, size_t count);
118 /* Notify the uTP socket of buffer drain */
119 void UTP_RBDrained(struct UTPSocket *socket);
121 /* Call periodically to process timeouts and other periodic events */
122 void UTP_CheckTimeouts(void);
124 /* Retrieves the peer address of the specified socket, stores this address in the
125 sockaddr structure pointed to by the addr argument, and stores the length of this
126 address in the object pointed to by the addrlen argument. */
127 void UTP_GetPeerName(struct UTPSocket *socket, struct sockaddr *addr, socklen_t *addrlen);
129 void UTP_GetDelays(struct UTPSocket *socket, int32 *ours, int32 *theirs, uint32 *age);
131 size_t UTP_GetPacketSize(struct UTPSocket *socket);
133 #ifdef _DEBUG
134 struct UTPStats {
135 uint64 _nbytes_recv; /* total bytes received */
136 uint64 _nbytes_xmit; /* total bytes transmitted */
137 uint32 _rexmit; /* retransmit counter */
138 uint32 _fastrexmit; /* fast retransmit counter */
139 uint32 _nxmit; /* transmit counter */
140 uint32 _nrecv; /* receive counter (total) */
141 uint32 _nduprecv; /* duplicate receive counter */
144 /* Get stats for UTP socket */
145 void UTP_GetStats(struct UTPSocket *socket, UTPStats *stats);
146 #endif
148 /* Close the UTP socket.
149 It is not valid to issue commands for this socket after it is closed.
150 This does not actually destroy the socket until outstanding data is sent, at which
151 point the socket will change to the UTP_STATE_DESTROYING state. */
152 void UTP_Close(struct UTPSocket *socket);
154 struct UTPGlobalStats {
155 uint32 _nraw_recv[5]; /* total packets recieved less than 300/600/1200/MTU bytes fpr all connections (global) */
156 uint32 _nraw_send[5]; /* total packets sent less than 300/600/1200/MTU bytes for all connections (global) */
159 void UTP_GetGlobalStats(struct UTPGlobalStats *stats);
161 #ifdef __cplusplus
163 #endif
165 #endif /* __UTP_H__ */