summaryrefslogtreecommitdiff
path: root/source/net_sock.h
blob: fb8a970f40ed11a54230bf613e38e8e05f583a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

// net.h -- quake's interface to the networking layer

#define PORT_ANY            -1
#define PORT_ANY_STRING     "-1"
#define PORT_MASTER         27900
#define PORT_SERVER         27910
#define PORT_SERVER_STRING  "27910"

#define MIN_PACKETLEN   512         // don't allow smaller packets
#define MAX_PACKETLEN   4096        // max length of a single packet
#define PACKET_HEADER   10          // two ints and a short (worst case)
#define MAX_PACKETLEN_DEFAULT   1400        // default quake2 limit
#define MAX_PACKETLEN_WRITABLE    ( MAX_PACKETLEN - PACKET_HEADER )
#define MAX_PACKETLEN_WRITABLE_DEFAULT    ( MAX_PACKETLEN_DEFAULT - PACKET_HEADER )

typedef enum {
    NA_BAD,
    NA_LOOPBACK,
    NA_BROADCAST,
    NA_IP
} netadrtype_t;

typedef enum {
    NS_CLIENT,
    NS_SERVER,
    NS_COUNT
} netsrc_t;

typedef enum {
    NET_NONE    = 0,
    NET_CLIENT  = ( 1 << 0 ),
    NET_SERVER  = ( 1 << 1 )
} netflag_t;

typedef enum {
    NET_OK,
    NET_AGAIN,
    NET_CLOSED,
    NET_ERROR,
} neterr_t;

typedef struct {
    netadrtype_t    type;
    uint8_t     ip[4];
    uint16_t    port;
} netadr_t;

static inline qboolean NET_IsEqualAdr( const netadr_t *a, const netadr_t *b ) {
    if( a->type != b->type ) {
        return qfalse;
    }

    switch( a->type ) {
    case NA_LOOPBACK:
        return qtrue;
    case NA_IP:
    case NA_BROADCAST:
        if( *( uint32_t * )a->ip == *( uint32_t * )b->ip && a->port == b->port ) {
            return qtrue;
        }
        // fall through
    default:
        break;
    }

    return qfalse;
}

static inline qboolean NET_IsEqualBaseAdr( const netadr_t *a, const netadr_t *b ) {
    if( a->type != b->type ) {
        return qfalse;
    }

    switch( a->type ) {
    case NA_LOOPBACK:
        return qtrue;
    case NA_IP:
    case NA_BROADCAST:
        if( *( uint32_t * )a->ip == *( uint32_t * )b->ip ) {
            return qtrue;
        }
        // fall through
    default:
        break;
    }

    return qfalse;
}

static inline qboolean NET_IsLanAddress( const netadr_t *adr ) {
    switch( adr->type ) {
    case NA_LOOPBACK:
        return qtrue;
    case NA_IP:
    case NA_BROADCAST:
        if( adr->ip[0] == 127 || adr->ip[0] == 10 ) {
            return qtrue;
        }
        if( *( uint16_t * )adr->ip == MakeShort( 192, 168 ) ||
            *( uint16_t * )adr->ip == MakeShort( 172,  16 ) )
        {
            return qtrue;
        }
        // fall through
    default:
        break;
    }

    return qfalse;
}

void        NET_Init( void );
void        NET_Shutdown( void );

void        NET_Config( netflag_t flag );
qboolean    NET_GetAddress( netsrc_t sock, netadr_t *adr );

qboolean    NET_GetPacket( netsrc_t sock );
qboolean    NET_SendPacket( netsrc_t sock, const netadr_t *to, size_t length, const void *data );
qboolean    NET_GetLoopPacket( netsrc_t sock );

char        *NET_AdrToString( const netadr_t *a );
qboolean    NET_StringToAdr( const char *s, netadr_t *a, int port );

#if USE_CLIENT && USE_SERVER
#define     NET_IsLocalAddress( adr )   ( (adr)->type == NA_LOOPBACK )
#else
#define     NET_IsLocalAddress( adr )   0
#endif

const char  *NET_ErrorString( void );

extern cvar_t       *net_ip;
extern cvar_t       *net_port;

extern netadr_t     net_from;