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
236
237
238
239
240
241
242
243
244
245
|
/* ------------------------------------------------------------ */
/*
HTTrack Website Copier, Offline Browser for Windows and Unix
Copyright (C) 1998-2014 Xavier Roche and other contributors
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 3 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, see <http://www.gnu.org/licenses/>.
Important notes:
- We hereby ask people using this source NOT to use it in purpose of grabbing
emails addresses, or collecting any other private information on persons.
This would disgrace our work, and spoil the many hours we spent on it.
Please visit our Website: http://www.httrack.com
*/
/* ------------------------------------------------------------ */
/* File: Net definitions */
/* Used in .c files that needs connect() functions and so */
/* Note: includes htsbasenet.h */
/* Author: Xavier Roche */
/* ------------------------------------------------------------ */
#ifndef HTS_DEFNETH
#define HTS_DEFNETH
/* basic net definitions */
#include "htsglobal.h"
#include "htsbasenet.h"
#include "htssafe.h"
#include <ctype.h>
#ifdef _WIN32
// pour read
#include <io.h>
// pour FindFirstFile
#include <winbase.h>
typedef USHORT in_port_t;
typedef ADDRESS_FAMILY sa_family_t;
#else
//typedef int T_SOC;
#define INVALID_SOCKET -1
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
/* Force for sun env. */
#ifndef BSD_COMP
#define BSD_COMP
#endif
#include <sys/ioctl.h>
/* gethostname & co */
#ifndef _WIN32
#include <unistd.h>
#endif
/* inet_addr */
#include <arpa/inet.h>
// pas la peine normalement..
#ifndef HTS_DO_NOT_REDEFINE_in_addr_t
typedef unsigned long in_addr_t;
#endif
#endif
/* Ipv4 structures */
#if HTS_INET6 != 0
typedef struct in6_addr INaddr;
#else
typedef struct in_addr INaddr;
#endif
/* This should handle all cases */
#ifndef HTS_DEF_FWSTRUCT_SOCaddr
#define HTS_DEF_FWSTRUCT_SOCaddr
typedef struct SOCaddr SOCaddr;
#endif
struct SOCaddr {
union {
struct sockaddr_in in;
#if HTS_INET6 != 0
struct sockaddr_in6 in6;
#endif
/* For network functions such as getnameinfo */
struct sockaddr sa;
} m_addr;
socklen_t size;
};
static HTS_INLINE HTS_UNUSED in_port_t* SOCaddr_sinport_(SOCaddr *const addr) {
#if HTS_INET6 != 0
if (addr->size == sizeof(struct sockaddr_in6)) {
return &addr->m_addr.in6.sin6_port;
} else {
#endif
assertf(addr->size == sizeof(struct sockaddr_in));
return &addr->m_addr.in.sin_port;
#if HTS_INET6 != 0
}
#endif
}
static HTS_INLINE HTS_UNUSED sa_family_t* SOCaddr_sinfamily_(SOCaddr *const addr) {
#if HTS_INET6 != 0
if (addr->size == sizeof(struct sockaddr_in6)) {
return (sa_family_t*) &addr->m_addr.in6.sin6_family;
} else {
#endif
assertf(addr->size == sizeof(struct sockaddr_in));
return (sa_family_t*) &addr->m_addr.in.sin_family;
#if HTS_INET6 != 0
}
#endif
}
/* Ipv4/6 structure members */
#define SOCaddr_sinfamily(server) (*SOCaddr_sinfamily_(&(server)))
#define SOCaddr_sinport(server) (*SOCaddr_sinport_(&(server)))
/* AF_xx */
#if HTS_INET6 != 0
#define AFinet AF_INET6
#else
#define AFinet AF_INET
#endif
/* Set port to sockaddr structure */
#define SOCaddr_initport(server, port) do { \
SOCaddr_sinport(server) = htons((in_port_t) (port)); \
} while(0)
static HTS_INLINE HTS_UNUSED socklen_t SOCaddr_initany_(SOCaddr*const addr) {
addr->size = sizeof(struct sockaddr_in);
memset(&addr->m_addr.in, 0, sizeof(addr->m_addr.in));
addr->m_addr.in.sin_family = AF_INET;
return addr->size;
}
#define SOCaddr_initany(server, server_len) do { \
server_len = (int) SOCaddr_initany_(&(server)); \
} while(0)
/*
Copy sockaddr_in/sockaddr_in6/raw IPv4/raw IPv6 to our opaque SOCaddr
*/
static HTS_UNUSED socklen_t SOCaddr_copyaddr_(SOCaddr*const server,
const void *data, const size_t data_size,
const char *file, const int line) {
assertf_(server != NULL, file, line);
assertf_(data != NULL, file, line);
if (data_size == sizeof(struct sockaddr_in)) {
memcpy(&server->m_addr.in, data, sizeof(struct sockaddr_in));
server->size = sizeof(struct sockaddr_in);
#if HTS_INET6 != 0
} else if (data_size == sizeof(struct sockaddr_in6)) {
memcpy(&server->m_addr.in6, data, sizeof(struct sockaddr_in6));
server->size = sizeof(struct sockaddr_in6);
#endif
} else if (data_size == 4) {
memset(&server->m_addr.in, 0, sizeof(server->m_addr.in));
server->m_addr.in.sin_family = AF_INET;
server->m_addr.in.sin_port = 0;
memcpy(&server->m_addr.in.sin_addr, data, 4);
server->size = sizeof(struct sockaddr_in);
#if HTS_INET6 != 0
} else if (data_size == 16) {
memset(&server->m_addr.in6, 0, sizeof(server->m_addr.in6));
server->m_addr.in6.sin6_family = AF_INET6;
server->m_addr.in6.sin6_port = 0;
memcpy(&server->m_addr.in6.sin6_addr, data, 16);
server->size = sizeof(struct sockaddr_in6);
#endif
} else {
server->size = 0; /* Error */
}
return server->size;
}
#define SOCaddr_copyaddr(server, server_len, hpaddr, hpsize) do { \
server_len = (int) SOCaddr_copyaddr_(&(server), hpaddr, hpsize, __FILE__, __LINE__); \
} while(0)
/* Get dotted address */
static HTS_UNUSED void SOCaddr_inetntoa_(char *namebuf, size_t namebuflen,
SOCaddr *const ss,
const char *file, const int line) {
assertf_(namebuf != NULL, file, line);
assertf_(ss != NULL, file, line);
if (getnameinfo(&ss->m_addr.sa, ss->size,
namebuf, namebuflen,
NULL, 0,
NI_NUMERICHOST) == 0) {
/* remove scope id(s) */
char *const pos = strchr(namebuf, '%');
if (pos != NULL) {
*pos = '\0';
}
} else {
namebuf[0] = '\0';
}
}
#define SOCaddr_inetntoa(namebuf, namebuflen, ss, sslen) \
SOCaddr_inetntoa_(namebuf, namebuflen, &(ss), __FILE__, __LINE__)
/* Get protocol ID */
#define SOCaddr_getproto(ss, sslen) ( (sslen) == sizeof(struct sockaddr_in) ? '1' : '2')
/* Socket length type */
typedef socklen_t SOClen;
/* Buffer structure to copy various hostent structures */
#ifndef HTS_DEF_FWSTRUCT_t_fullhostent
#define HTS_DEF_FWSTRUCT_t_fullhostent
typedef struct t_fullhostent t_fullhostent;
#endif
struct t_fullhostent {
t_hostent hp;
char *list[2];
char addr[HTS_MAXADDRLEN]; /* various struct sockaddr structures */
unsigned int addr_maxlen;
};
/* Initialize a t_fullhostent structure */
#define fullhostent_init(h) do { \
memset((h), 0, sizeof(t_fullhostent)); \
(h)->hp.h_addr_list = (char **) & ((h)->list); \
(h)->list[0] = (char *) & ((h)->addr); \
(h)->list[1] = NULL; \
(h)->addr_maxlen = HTS_MAXADDRLEN; \
} while(0)
#endif
|