File: network.h

package info (click to toggle)
bzflag 2.0.13.20080902-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 27,564 kB
  • ctags: 34,716
  • sloc: cpp: 139,842; ansic: 14,510; sh: 10,715; makefile: 2,454; perl: 477; php: 428; python: 345; objc: 243; xml: 24
file content (162 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download
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
158
159
160
161
162
/* bzflag
 * Copyright (c) 1993 - 2008 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/*
 * includes platform specific network files and adds missing stuff
 *
 * unfortunately this can include far more than necessary
 */

#ifndef	BZF_NETWORK_H
#define	BZF_NETWORK_H

#include "common.h"

#ifdef _WIN32
# if defined(_MSC_VER)
#   pragma warning(disable: 4786)
# endif
# include <winsock2.h>
# include <ws2tcpip.h>
#endif

/* system headers */
#include <vector>
#include <string>

#if !defined(_WIN32)

#include <sys/time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#ifndef GUSI_20
#  include <sys/param.h>
#endif

#include <net/if.h>
#include <netinet/in.h>

#if defined(__linux__)
/* these are defined in both socket.h and tcp.h without ifdef guards. */
#  undef TCP_NODELAY
#  undef TCP_MAXSEG
#endif

#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_BSTRING_H
#  include <bstring.h>
#endif

#if defined(__linux__) && !defined(_old_linux_)
#  define AddrLen		unsigned int
/* setsockopt incorrectly prototypes the 4th arg without const. */
#  define SSOType		void*
#elif defined(BSD) || defined(sun) || defined(__GLIBC__)
#  define AddrLen		socklen_t
#elif defined (__APPLE__)
#  include <AvailabilityMacros.h>
#  if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
#    define AddrLen		socklen_t
#  endif
#endif

#if defined(sun)
/* setsockopt prototypes the 4th arg as const char*. */
#  define SSOType		const char*
/* connect prototypes the 2nd arg without const */
#  define CNCTType	struct sockaddr
#endif

extern "C" {

#define herror(_x)	bzfherror(_x)

  void			nerror(const char* msg);
  void			bzfherror(const char* msg);
  int			getErrno();

}

/* BeOS net_server has closesocket(), which _must_ be used in place of close() */
#if defined(__BEOS__) && (IPPROTO_TCP != 6)
#  define close(__x) closesocket(__x)
#endif

#else /* !defined(_WIN32) */

#define	MAXHOSTNAMELEN	64

#define EINPROGRESS	WSAEWOULDBLOCK
#define	EWOULDBLOCK	WSAEWOULDBLOCK
#define	ECONNRESET	WSAECONNRESET
#define	EBADMSG		WSAECONNRESET	/* not defined by windows */

/* setsockopt prototypes the 4th arg as const char*. */
#define SSOType		const char*

inline int close(SOCKET s) { return closesocket(s); }
#define	ioctl(__fd, __req, __arg) \
			ioctlsocket(__fd, __req, (u_long*)__arg)
#define	gethostbyaddr(__addr, __len, __type) \
			gethostbyaddr((const char*)__addr, __len, __type)

extern "C" {

  int			inet_aton(const char* cp, struct in_addr* pin);
  void			nerror(const char* msg);
  void			herror(const char* msg);
  int			getErrno();

}

#endif /* !defined(_WIN32) */

#if !defined(AddrLen)
#  define AddrLen		int
#endif

#if !defined(SSOType)
#  define SSOType		const void*
#endif
#if !defined(CNCTType)
#  define CNCTType	const struct sockaddr
#endif

#if !defined(INADDR_NONE)
#  define INADDR_NONE	((in_addr_t)0xffffffff)
#endif

class BzfNetwork {
public:
  static int		setNonBlocking(int fd);
  static int		setBlocking(int fd);
  static bool	parseURL(const std::string& url,
			 std::string& protocol,
			 std::string& hostname,
			 int& port,
			 std::string& pathname);
};

#endif // BZF_NETWORK_H


// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8