]> icculus.org git repositories - divverent/darkplaces.git/blob - lhnet.h
more Nexuiz hud stuff from BlackHC, minor style edits to fit in with surrounding...
[divverent/darkplaces.git] / lhnet.h
1
2 // Written by Forest Hale 2003-06-15 and placed into public domain.
3
4 #ifndef LHNET_H
5 #define LHNET_H
6
7 #define LHNETADDRESSTYPE_NONE 0
8 #define LHNETADDRESSTYPE_LOOP 1
9 #define LHNETADDRESSTYPE_INET4 2
10 #define LHNETADDRESSTYPE_INET6 3
11
12 typedef struct lhnetaddress_loop_s
13 {
14         unsigned short port;
15 }
16 lhnetaddress_loop_t;
17
18 #define LHNETADDRESSTYPE_INET4_FAMILY 2
19 #define LHNETADDRESSTYPE_INET6_FAMILY 10
20
21 // compatible with sockaddr_in
22 typedef struct lhnetaddress_inet4_s
23 {
24         unsigned short family; // 2
25         unsigned short port;
26         unsigned char address[4];
27         unsigned char padding[8]; // to match sockaddr_in
28 }
29 lhnetaddress_inet4_t;
30
31 // compatible with sockaddr_in6
32 typedef struct lhnetaddress_inet6_s
33 {
34         unsigned short family; // 10
35         unsigned short port;
36         unsigned int flowinfo;
37         unsigned short address[8];
38         unsigned int scope_id;
39 }
40 lhnetaddress_inet6_t;
41
42 typedef struct lhnetaddress_s
43 {
44         int addresstype;
45         union
46         {
47                 lhnetaddress_loop_t loop;
48                 lhnetaddress_inet4_t inet4;
49                 lhnetaddress_inet6_t inet6;
50         }
51         addressdata;
52 }
53 lhnetaddress_t;
54
55 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port);
56 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport);
57 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport);
58 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address);
59 int LHNETADDRESS_GetPort(const lhnetaddress_t *address);
60 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port);
61 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2);
62
63 typedef struct lhnetsocket_s
64 {
65         lhnetaddress_t address;
66         int inetsocket;
67         struct lhnetsocket_s *next, *prev;
68 }
69 lhnetsocket_t;
70
71 void LHNET_Init(void);
72 void LHNET_Shutdown(void);
73 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address);
74 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket);
75 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock);
76 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address);
77 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address);
78
79 #endif
80