summaryrefslogtreecommitdiff
path: root/pr/src/md/mac/macsocket.h
blob: b9194c521d4d5df3b2ad7c5de9de0e49b65c7363 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape Portable Runtime (NSPR).
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

#ifndef macksocket_h___
#define macksocket_h___

// macsock.h
// Interface visible to xp code
// C socket type definitions and routines
// from sys/socket.h
#include <Files.h>
#include <OpenTptInternet.h>	// All the internet typedefs
#include <utime.h>				// For timeval
/*
 * sleep and delay conflict with the same in unistd.h from Metrowerks.  OT
 * defines them as 
 *
 *    extern pascal void		OTDelay(UInt32 seconds);
 *    extern pascal void		OTIdle(void);
 *
 *    #define sleep(x)	OTDelay(x)
 *    #define delay(x)	OTDelay(x)
 */

#undef sleep
#undef delay

#pragma once

#include "prio.h"

struct sockaddr {
	unsigned char	sa_len;			/* total length */
	unsigned char	sa_family;		/* address family */
	char	sa_data[14];		/* actually longer; address value */
};

// from netinet/in.h
struct in_addr {
	unsigned long s_addr;
};

struct sockaddr_in {
	unsigned char	sin_len;
	unsigned char	sin_family;		// AF_INET
	unsigned short	sin_port;
	struct	in_addr sin_addr;
	char		sin_zero[8];
};

struct	hostent {
	char	*h_name;	/* official name of host */
	char	**h_aliases;	/* alias list */
	int	h_addrtype;	/* host address type */
	int	h_length;	/* length of address */
	char	**h_addr_list;	/* list of addresses from name server */
#define	h_addr	h_addr_list[0]	/* address, for backward compatiblity */
};

// Necessary network defines, found by grepping unix headers when XP code would not compile
#define FIONBIO 1
#define SOCK_STREAM 1
#define SOCK_DGRAM 		2
#define IPPROTO_TCP 	INET_TCP		// Default TCP protocol
#define IPPROTO_UDP		INET_UDP		// Default UDP protocol
#define INADDR_ANY		kOTAnyInetAddress
#define SOL_SOCKET		XTI_GENERIC		// Any type of socket
#define SO_REUSEADDR	IP_REUSEADDR
#define SO_BROADCAST	IP_BROADCAST
#define MSG_PEEK		0x2				// Just look at a message waiting, donŐt actually read it.

typedef unsigned long u_long;

/* ldap.h has its own definition of fd_set */
/* select support */
#if !defined(FD_SET)
#define	NBBY    8
typedef long    fd_mask;
#define NFDBITS (sizeof(fd_mask) * NBBY)	/* bits per mask */

#ifndef howmany
#define howmany(x, y)   (((x)+((y)-1))/(y))
#endif
#define FD_SETSIZE 64
typedef	struct fd_set{
    fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
} fd_set;

#define	FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
#define	FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
#define	FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
#define	FD_ZERO(p)      memset (p, 0, sizeof(*(p)))
#endif /* !FD_SET */


#ifdef __cplusplus
extern "C" {
#endif

extern unsigned long inet_addr(const char *cp);
extern char *inet_ntoa(struct in_addr in);

inline unsigned long htonl(unsigned long hostlong) {return hostlong;}
inline unsigned long ntohl(unsigned long netlong) {return netlong;}
inline unsigned short ntohs(unsigned short netshort) {return netshort;}
inline unsigned short htons(unsigned short hostshort) {return hostshort;}


// UNIX look-alike routines
// They make sure that the arguments passed in are valid, and then
//
extern struct hostent *macsock_gethostbyaddr(const void *addr, int addrlen, int type);

extern int macsock_socket(int domain, int type, int protocol);
extern int macsock_ioctl(int sID, unsigned int request, void *value);
extern int macsock_connect(int sID, struct sockaddr *name, int namelen);
extern int macsock_write(int sID, const void *buffer, unsigned buflen);
extern int macsock_read(int sID, void *buf, unsigned nbyte);
extern int macsock_close(int sID);

extern int macsock_accept(int sID, struct sockaddr *addr, int *addrlen);
extern int macsock_bind(int sID, const struct sockaddr *name, int namelen);
extern int macsock_listen(int sID, int backlog);

extern int macsock_shutdown(int sID, int how);
extern int macsock_getpeername(int sID, struct sockaddr *name, int *namelen);
extern int macsock_getsockname(int sID, struct sockaddr *name, int *namelen);
extern int macsock_getsockopt(int sID, int level, int optname, void *optval,int *optlen);
extern int macsock_setsockopt(int sID, int level, int optname, const void *optval,int optlen);
extern int macsock_socketavailable(int sID, size_t *bytesAvailable);
extern int macsock_dup(int sID);

extern int macsock_send(int sID, const void *msg, int len, int flags);
extern int macsock_sendto(int sID, const void *msg, int len, int flags, struct sockaddr *toAddr, int toLen);
extern int macsock_recvfrom(int sID, void *buf, int len, int flags, struct sockaddr *from, int *fromLen);
extern int macsock_recv(int sID, void *buf, int len, int flags);

extern int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);


#define macsock_gethostbyaddr PR_GetHostByAddr
#define macsock_socket PR_Socket
#define macsock_connect PR_Connect
#define macsock_write PR_Write
#define macsock_read PR_Read
#define macsock_close PR_Close
#define macsock_accept PR_Accept
#define macsock_bind PR_Bind
#define macsock_listen PR_Listen
#define macsock_shutdown PR_Shutdown
#define macsock_getpeername PR_GetPeerName
#define macsock_getsockname PR_GetSockName
#define macsock_socketavailable PR_SocketAvailable
#define macsock_send PR_Send
#define macsock_sendto PR_SendTo
#define macsock_recvfrom PR_RecvFrom
#define macsock_recv PR_Recv

#ifdef __cplusplus
}
#endif
//extern int errno;

/*
macsock_sendmsg
macsock_readv
macsock_writev
*/

/* New definitions that are not defined in macsock.h in macsock library */
struct	protoent {
    char	*p_name;		/* official protocol name */
    char	**p_aliases;	/* alias list */
    int		p_proto;		/* protocol # */
};

extern struct protoent *getprotobyname(const char * name);
extern struct protoent *getprotobynumber(int number);

extern int gethostname (char *name, int namelen);
extern struct hostent *gethostbyname(const char * name);
extern struct hostent *gethostbyaddr(const void *addr, int addrlen, int type);

#define INADDR_LOOPBACK	0x7F000001

#define SO_KEEPALIVE	TCP_KEEPALIVE
#define SO_RCVBUF		XTI_RCVBUF
#define SO_SNDBUF		XTI_SNDBUF
#define SO_LINGER       XTI_LINGER          /* linger on close if data present */

#define IPPROTO_IP		INET_IP

/* Get/Set sock opt until fixed in NSPR 2.0 */
struct  linger {
        int     l_onoff;                /* option on/off */
        int     l_linger;               /* linger time */
};

struct ip_mreq {
        struct in_addr  imr_multiaddr;  /* IP multicast address of group */
        struct in_addr  imr_interface;  /* local IP address of interface */
};

#endif /* macksocket_h___ */