| 1 |
{==============================================================================| |
| 2 |
| Project : Ararat Synapse | 001.001.005 | |
| 3 |
|==============================================================================| |
| 4 |
| Content: Socket Independent Platform Layer - FreePascal definition include | |
| 5 |
|==============================================================================| |
| 6 |
| Copyright (c)2006-2013, Lukas Gebauer | |
| 7 |
| All rights reserved. | |
| 8 |
| | |
| 9 |
| Redistribution and use in source and binary forms, with or without | |
| 10 |
| modification, are permitted provided that the following conditions are met: | |
| 11 |
| | |
| 12 |
| Redistributions of source code must retain the above copyright notice, this | |
| 13 |
| list of conditions and the following disclaimer. | |
| 14 |
| | |
| 15 |
| Redistributions in binary form must reproduce the above copyright notice, | |
| 16 |
| this list of conditions and the following disclaimer in the documentation | |
| 17 |
| and/or other materials provided with the distribution. | |
| 18 |
| | |
| 19 |
| Neither the name of Lukas Gebauer nor the names of its contributors may | |
| 20 |
| be used to endorse or promote products derived from this software without | |
| 21 |
| specific prior written permission. | |
| 22 |
| | |
| 23 |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 24 |
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 25 |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 26 |
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR | |
| 27 |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 |
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 29 |
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 30 |
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 31 |
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 32 |
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
| 33 |
| DAMAGE. | |
| 34 |
|==============================================================================| |
| 35 |
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).| |
| 36 |
| Portions created by Lukas Gebauer are Copyright (c)2006-2013. | |
| 37 |
| All Rights Reserved. | |
| 38 |
|==============================================================================| |
| 39 |
| Contributor(s): | |
| 40 |
|==============================================================================| |
| 41 |
| History: see HISTORY.HTM from distribution package | |
| 42 |
| (Found at URL: http://www.ararat.cz/synapse/) | |
| 43 |
|==============================================================================} |
| 44 |
|
| 45 |
{:@exclude} |
| 46 |
|
| 47 |
{$IFDEF FPC} |
| 48 |
{For FreePascal 2.x.x} |
| 49 |
|
| 50 |
//{$DEFINE FORCEOLDAPI} |
| 51 |
{Note about define FORCEOLDAPI: |
| 52 |
If you activate this compiler directive, then is allways used old socket API |
| 53 |
for name resolution. If you leave this directive inactive, then the new API |
| 54 |
is used, when running system allows it. |
| 55 |
|
| 56 |
For IPv6 support you must have new API! |
| 57 |
} |
| 58 |
|
| 59 |
{$IFDEF FPC} |
| 60 |
{$MODE DELPHI} |
| 61 |
{$ENDIF} |
| 62 |
{$H+} |
| 63 |
|
| 64 |
{$ifdef FreeBSD} |
| 65 |
{$DEFINE SOCK_HAS_SINLEN} // BSD definition of scoketaddr |
| 66 |
{$endif} |
| 67 |
{$ifdef darwin} |
| 68 |
{$DEFINE SOCK_HAS_SINLEN} // BSD definition of scoketaddr |
| 69 |
{$endif} |
| 70 |
{$ifdef haiku} |
| 71 |
{$DEFINE SOCK_HAS_SINLEN} // BSD definition of scoketaddr |
| 72 |
{$endif} |
| 73 |
|
| 74 |
interface |
| 75 |
|
| 76 |
uses |
| 77 |
SyncObjs, SysUtils, Classes, |
| 78 |
synafpc, BaseUnix, Unix, termio, sockets, netdb; |
| 79 |
|
| 80 |
function InitSocketInterface(stack: string): Boolean; |
| 81 |
function DestroySocketInterface: Boolean; |
| 82 |
|
| 83 |
const |
| 84 |
DLLStackName = ''; |
| 85 |
WinsockLevel = $0202; |
| 86 |
|
| 87 |
cLocalHost = '127.0.0.1'; |
| 88 |
cAnyHost = '0.0.0.0'; |
| 89 |
c6AnyHost = '::0'; |
| 90 |
c6Localhost = '::1'; |
| 91 |
cLocalHostStr = 'localhost'; |
| 92 |
|
| 93 |
type |
| 94 |
TSocket = longint; |
| 95 |
TAddrFamily = integer; |
| 96 |
|
| 97 |
TMemory = pointer; |
| 98 |
|
| 99 |
|
| 100 |
type |
| 101 |
TFDSet = Baseunix.TFDSet; |
| 102 |
PFDSet = ^TFDSet; |
| 103 |
Ptimeval = Baseunix.ptimeval; |
| 104 |
Ttimeval = Baseunix.ttimeval; |
| 105 |
|
| 106 |
const |
| 107 |
FIONREAD = termio.FIONREAD; |
| 108 |
FIONBIO = termio.FIONBIO; |
| 109 |
{$IFNDEF HAIKU} |
| 110 |
FIOASYNC = termio.FIOASYNC; |
| 111 |
{$ENDIF} |
| 112 |
|
| 113 |
const |
| 114 |
IPPROTO_IP = 0; { Dummy } |
| 115 |
IPPROTO_ICMP = 1; { Internet Control Message Protocol } |
| 116 |
IPPROTO_IGMP = 2; { Internet Group Management Protocol} |
| 117 |
IPPROTO_TCP = 6; { TCP } |
| 118 |
IPPROTO_UDP = 17; { User Datagram Protocol } |
| 119 |
IPPROTO_IPV6 = 41; |
| 120 |
IPPROTO_ICMPV6 = 58; |
| 121 |
IPPROTO_RM = 113; |
| 122 |
|
| 123 |
IPPROTO_RAW = 255; |
| 124 |
IPPROTO_MAX = 256; |
| 125 |
|
| 126 |
type |
| 127 |
PInAddr = ^TInAddr; |
| 128 |
TInAddr = sockets.in_addr; |
| 129 |
|
| 130 |
PSockAddrIn = ^TSockAddrIn; |
| 131 |
TSockAddrIn = sockets.TInetSockAddr; |
| 132 |
|
| 133 |
|
| 134 |
TIP_mreq = record |
| 135 |
imr_multiaddr: TInAddr; // IP multicast address of group |
| 136 |
imr_interface: TInAddr; // local IP address of interface |
| 137 |
end; |
| 138 |
|
| 139 |
|
| 140 |
PInAddr6 = ^TInAddr6; |
| 141 |
TInAddr6 = sockets.Tin6_addr; |
| 142 |
|
| 143 |
PSockAddrIn6 = ^TSockAddrIn6; |
| 144 |
TSockAddrIn6 = sockets.TInetSockAddr6; |
| 145 |
|
| 146 |
|
| 147 |
TIPv6_mreq = record |
| 148 |
ipv6mr_multiaddr: TInAddr6; // IPv6 multicast address. |
| 149 |
ipv6mr_interface: integer; // Interface index. |
| 150 |
end; |
| 151 |
|
| 152 |
const |
| 153 |
INADDR_ANY = $00000000; |
| 154 |
INADDR_LOOPBACK = $7F000001; |
| 155 |
INADDR_BROADCAST = $FFFFFFFF; |
| 156 |
INADDR_NONE = $FFFFFFFF; |
| 157 |
ADDR_ANY = INADDR_ANY; |
| 158 |
INVALID_SOCKET = TSocket(NOT(0)); |
| 159 |
SOCKET_ERROR = -1; |
| 160 |
|
| 161 |
Const |
| 162 |
IP_TOS = sockets.IP_TOS; { int; IP type of service and precedence. } |
| 163 |
IP_TTL = sockets.IP_TTL; { int; IP time to live. } |
| 164 |
IP_HDRINCL = sockets.IP_HDRINCL; { int; Header is included with data. } |
| 165 |
IP_OPTIONS = sockets.IP_OPTIONS; { ip_opts; IP per-packet options. } |
| 166 |
// IP_ROUTER_ALERT = sockets.IP_ROUTER_ALERT; { bool } |
| 167 |
IP_RECVOPTS = sockets.IP_RECVOPTS; { bool } |
| 168 |
IP_RETOPTS = sockets.IP_RETOPTS; { bool } |
| 169 |
// IP_PKTINFO = sockets.IP_PKTINFO; { bool } |
| 170 |
// IP_PKTOPTIONS = sockets.IP_PKTOPTIONS; |
| 171 |
// IP_PMTUDISC = sockets.IP_PMTUDISC; { obsolete name? } |
| 172 |
// IP_MTU_DISCOVER = sockets.IP_MTU_DISCOVER; { int; see below } |
| 173 |
// IP_RECVERR = sockets.IP_RECVERR; { bool } |
| 174 |
// IP_RECVTTL = sockets.IP_RECVTTL; { bool } |
| 175 |
// IP_RECVTOS = sockets.IP_RECVTOS; { bool } |
| 176 |
IP_MULTICAST_IF = sockets.IP_MULTICAST_IF; { in_addr; set/get IP multicast i/f } |
| 177 |
IP_MULTICAST_TTL = sockets.IP_MULTICAST_TTL; { u_char; set/get IP multicast ttl } |
| 178 |
IP_MULTICAST_LOOP = sockets.IP_MULTICAST_LOOP; { i_char; set/get IP multicast loopback } |
| 179 |
IP_ADD_MEMBERSHIP = sockets.IP_ADD_MEMBERSHIP; { ip_mreq; add an IP group membership } |
| 180 |
IP_DROP_MEMBERSHIP = sockets.IP_DROP_MEMBERSHIP; { ip_mreq; drop an IP group membership } |
| 181 |
|
| 182 |
SOL_SOCKET = sockets.SOL_SOCKET; |
| 183 |
|
| 184 |
SO_DEBUG = sockets.SO_DEBUG; |
| 185 |
SO_REUSEADDR = sockets.SO_REUSEADDR; |
| 186 |
SO_TYPE = sockets.SO_TYPE; |
| 187 |
SO_ERROR = sockets.SO_ERROR; |
| 188 |
SO_DONTROUTE = sockets.SO_DONTROUTE; |
| 189 |
SO_BROADCAST = sockets.SO_BROADCAST; |
| 190 |
SO_SNDBUF = sockets.SO_SNDBUF; |
| 191 |
SO_RCVBUF = sockets.SO_RCVBUF; |
| 192 |
SO_KEEPALIVE = sockets.SO_KEEPALIVE; |
| 193 |
SO_OOBINLINE = sockets.SO_OOBINLINE; |
| 194 |
// SO_NO_CHECK = sockets.SO_NO_CHECK; |
| 195 |
// SO_PRIORITY = sockets.SO_PRIORITY; |
| 196 |
SO_LINGER = sockets.SO_LINGER; |
| 197 |
// SO_BSDCOMPAT = sockets.SO_BSDCOMPAT; |
| 198 |
// SO_REUSEPORT = sockets.SO_REUSEPORT; |
| 199 |
// SO_PASSCRED = sockets.SO_PASSCRED; |
| 200 |
// SO_PEERCRED = sockets.SO_PEERCRED; |
| 201 |
SO_RCVLOWAT = sockets.SO_RCVLOWAT; |
| 202 |
SO_SNDLOWAT = sockets.SO_SNDLOWAT; |
| 203 |
SO_RCVTIMEO = sockets.SO_RCVTIMEO; |
| 204 |
SO_SNDTIMEO = sockets.SO_SNDTIMEO; |
| 205 |
{ Security levels - as per NRL IPv6 - don't actually do anything } |
| 206 |
// SO_SECURITY_AUTHENTICATION = sockets.SO_SECURITY_AUTHENTICATION; |
| 207 |
// SO_SECURITY_ENCRYPTION_TRANSPORT = sockets.SO_SECURITY_ENCRYPTION_TRANSPORT; |
| 208 |
// SO_SECURITY_ENCRYPTION_NETWORK = sockets.SO_SECURITY_ENCRYPTION_NETWORK; |
| 209 |
// SO_BINDTODEVICE = sockets.SO_BINDTODEVICE; |
| 210 |
{ Socket filtering } |
| 211 |
// SO_ATTACH_FILTER = sockets.SO_ATTACH_FILTER; |
| 212 |
// SO_DETACH_FILTER = sockets.SO_DETACH_FILTER; |
| 213 |
|
| 214 |
{$IFDEF DARWIN} |
| 215 |
SO_NOSIGPIPE = $1022; |
| 216 |
{$ENDIF} |
| 217 |
|
| 218 |
SOMAXCONN = 1024; |
| 219 |
|
| 220 |
{$IFDEF HAIKU} |
| 221 |
IPV6_UNICAST_HOPS = 27; |
| 222 |
{$ELSE} |
| 223 |
IPV6_UNICAST_HOPS = sockets.IPV6_UNICAST_HOPS; |
| 224 |
{$ENDIF} |
| 225 |
IPV6_MULTICAST_IF = sockets.IPV6_MULTICAST_IF; |
| 226 |
IPV6_MULTICAST_HOPS = sockets.IPV6_MULTICAST_HOPS; |
| 227 |
IPV6_MULTICAST_LOOP = sockets.IPV6_MULTICAST_LOOP; |
| 228 |
{$IFDEF HAIKU} |
| 229 |
IPV6_JOIN_GROUP = 28; |
| 230 |
IPV6_LEAVE_GROUP = 29; |
| 231 |
{$ELSE} |
| 232 |
IPV6_JOIN_GROUP = sockets.IPV6_JOIN_GROUP; |
| 233 |
IPV6_LEAVE_GROUP = sockets.IPV6_LEAVE_GROUP; |
| 234 |
{$ENDIF} |
| 235 |
|
| 236 |
const |
| 237 |
SOCK_STREAM = 1; { stream socket } |
| 238 |
SOCK_DGRAM = 2; { datagram socket } |
| 239 |
SOCK_RAW = 3; { raw-protocol interface } |
| 240 |
SOCK_RDM = 4; { reliably-delivered message } |
| 241 |
SOCK_SEQPACKET = 5; { sequenced packet stream } |
| 242 |
|
| 243 |
{ TCP options. } |
| 244 |
TCP_NODELAY = $0001; |
| 245 |
|
| 246 |
{ Address families. } |
| 247 |
|
| 248 |
AF_UNSPEC = 0; { unspecified } |
| 249 |
AF_INET = sockets.AF_INET; { internetwork: UDP, TCP, etc. } |
| 250 |
AF_INET6 = sockets.AF_INET6; { Internetwork Version 6 } |
| 251 |
AF_MAX = 24; |
| 252 |
|
| 253 |
{ Protocol families, same as address families for now. } |
| 254 |
PF_UNSPEC = AF_UNSPEC; |
| 255 |
PF_INET = AF_INET; |
| 256 |
PF_INET6 = AF_INET6; |
| 257 |
PF_MAX = AF_MAX; |
| 258 |
|
| 259 |
type |
| 260 |
{ Structure used for manipulating linger option. } |
| 261 |
PLinger = ^TLinger; |
| 262 |
TLinger = packed record |
| 263 |
l_onoff: integer; |
| 264 |
l_linger: integer; |
| 265 |
end; |
| 266 |
|
| 267 |
const |
| 268 |
|
| 269 |
MSG_OOB = sockets.MSG_OOB; // Process out-of-band data. |
| 270 |
MSG_PEEK = sockets.MSG_PEEK; // Peek at incoming messages. |
| 271 |
{$if defined(DARWIN)} |
| 272 |
MSG_NOSIGNAL = $20000; // Do not generate SIGPIPE. |
| 273 |
// Works under MAC OS X, but is undocumented, |
| 274 |
// So FPC doesn't include it |
| 275 |
{$elseif defined(HAIKU)} |
| 276 |
MSG_NOSIGNAL = $0800; |
| 277 |
{$else} |
| 278 |
MSG_NOSIGNAL = sockets.MSG_NOSIGNAL; // Do not generate SIGPIPE. |
| 279 |
{$endif} |
| 280 |
|
| 281 |
{$IF DEFINED(HAIKU)} |
| 282 |
const |
| 283 |
ESysESTALE = (B_POSIX_ERROR_BASE + 40); |
| 284 |
ESysENOTSOCK = (B_POSIX_ERROR_BASE + 44); |
| 285 |
ESysEHOSTDOWN = (B_POSIX_ERROR_BASE + 45); |
| 286 |
ESysEDESTADDRREQ = (B_POSIX_ERROR_BASE + 48); |
| 287 |
ESysEDQUOT = (B_POSIX_ERROR_BASE + 49); |
| 288 |
// Fake error codes |
| 289 |
ESysEUSERS = (B_POSIX_ERROR_BASE + 128); |
| 290 |
ESysEREMOTE = (B_POSIX_ERROR_BASE + 129); |
| 291 |
ESysETOOMANYREFS = (B_POSIX_ERROR_BASE + 130); |
| 292 |
ESysESOCKTNOSUPPORT = (B_POSIX_ERROR_BASE + 131); |
| 293 |
{$ENDIF} |
| 294 |
|
| 295 |
const |
| 296 |
WSAEINTR = ESysEINTR; |
| 297 |
WSAEBADF = ESysEBADF; |
| 298 |
WSAEACCES = ESysEACCES; |
| 299 |
WSAEFAULT = ESysEFAULT; |
| 300 |
WSAEINVAL = ESysEINVAL; |
| 301 |
WSAEMFILE = ESysEMFILE; |
| 302 |
WSAEWOULDBLOCK = ESysEWOULDBLOCK; |
| 303 |
WSAEINPROGRESS = ESysEINPROGRESS; |
| 304 |
WSAEALREADY = ESysEALREADY; |
| 305 |
WSAENOTSOCK = ESysENOTSOCK; |
| 306 |
WSAEDESTADDRREQ = ESysEDESTADDRREQ; |
| 307 |
WSAEMSGSIZE = ESysEMSGSIZE; |
| 308 |
WSAEPROTOTYPE = ESysEPROTOTYPE; |
| 309 |
WSAENOPROTOOPT = ESysENOPROTOOPT; |
| 310 |
WSAEPROTONOSUPPORT = ESysEPROTONOSUPPORT; |
| 311 |
WSAESOCKTNOSUPPORT = ESysESOCKTNOSUPPORT; |
| 312 |
WSAEOPNOTSUPP = ESysEOPNOTSUPP; |
| 313 |
WSAEPFNOSUPPORT = ESysEPFNOSUPPORT; |
| 314 |
WSAEAFNOSUPPORT = ESysEAFNOSUPPORT; |
| 315 |
WSAEADDRINUSE = ESysEADDRINUSE; |
| 316 |
WSAEADDRNOTAVAIL = ESysEADDRNOTAVAIL; |
| 317 |
WSAENETDOWN = ESysENETDOWN; |
| 318 |
WSAENETUNREACH = ESysENETUNREACH; |
| 319 |
WSAENETRESET = ESysENETRESET; |
| 320 |
WSAECONNABORTED = ESysECONNABORTED; |
| 321 |
WSAECONNRESET = ESysECONNRESET; |
| 322 |
WSAENOBUFS = ESysENOBUFS; |
| 323 |
WSAEISCONN = ESysEISCONN; |
| 324 |
WSAENOTCONN = ESysENOTCONN; |
| 325 |
WSAESHUTDOWN = ESysESHUTDOWN; |
| 326 |
WSAETOOMANYREFS = ESysETOOMANYREFS; |
| 327 |
WSAETIMEDOUT = ESysETIMEDOUT; |
| 328 |
WSAECONNREFUSED = ESysECONNREFUSED; |
| 329 |
WSAELOOP = ESysELOOP; |
| 330 |
WSAENAMETOOLONG = ESysENAMETOOLONG; |
| 331 |
WSAEHOSTDOWN = ESysEHOSTDOWN; |
| 332 |
WSAEHOSTUNREACH = ESysEHOSTUNREACH; |
| 333 |
WSAENOTEMPTY = ESysENOTEMPTY; |
| 334 |
WSAEPROCLIM = -1; |
| 335 |
WSAEUSERS = ESysEUSERS; |
| 336 |
WSAEDQUOT = ESysEDQUOT; |
| 337 |
WSAESTALE = ESysESTALE; |
| 338 |
WSAEREMOTE = ESysEREMOTE; |
| 339 |
WSASYSNOTREADY = -2; |
| 340 |
WSAVERNOTSUPPORTED = -3; |
| 341 |
WSANOTINITIALISED = -4; |
| 342 |
WSAEDISCON = -5; |
| 343 |
WSAHOST_NOT_FOUND = 1; |
| 344 |
WSATRY_AGAIN = 2; |
| 345 |
WSANO_RECOVERY = 3; |
| 346 |
WSANO_DATA = -6; |
| 347 |
|
| 348 |
const |
| 349 |
WSADESCRIPTION_LEN = 256; |
| 350 |
WSASYS_STATUS_LEN = 128; |
| 351 |
type |
| 352 |
PWSAData = ^TWSAData; |
| 353 |
TWSAData = packed record |
| 354 |
wVersion: Word; |
| 355 |
wHighVersion: Word; |
| 356 |
szDescription: array[0..WSADESCRIPTION_LEN] of Char; |
| 357 |
szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char; |
| 358 |
iMaxSockets: Word; |
| 359 |
iMaxUdpDg: Word; |
| 360 |
lpVendorInfo: PChar; |
| 361 |
end; |
| 362 |
|
| 363 |
function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean; |
| 364 |
function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean; |
| 365 |
function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean; |
| 366 |
function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean; |
| 367 |
function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean; |
| 368 |
function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6):boolean; |
| 369 |
procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6); |
| 370 |
procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6); |
| 371 |
|
| 372 |
var |
| 373 |
in6addr_any, in6addr_loopback : TInAddr6; |
| 374 |
|
| 375 |
procedure FD_CLR(Socket: TSocket; var FDSet: TFDSet); |
| 376 |
function FD_ISSET(Socket: TSocket; var FDSet: TFDSet): Boolean; |
| 377 |
procedure FD_SET(Socket: TSocket; var FDSet: TFDSet); |
| 378 |
procedure FD_ZERO(var FDSet: TFDSet); |
| 379 |
|
| 380 |
{=============================================================================} |
| 381 |
|
| 382 |
var |
| 383 |
SynSockCS: SyncObjs.TCriticalSection; |
| 384 |
SockEnhancedApi: Boolean; |
| 385 |
SockWship6Api: Boolean; |
| 386 |
|
| 387 |
type |
| 388 |
TVarSin = packed record |
| 389 |
{$ifdef SOCK_HAS_SINLEN} |
| 390 |
sin_len : cuchar; |
| 391 |
{$endif} |
| 392 |
case integer of |
| 393 |
0: (AddressFamily: sa_family_t); |
| 394 |
1: ( |
| 395 |
case sin_family: sa_family_t of |
| 396 |
AF_INET: (sin_port: word; |
| 397 |
sin_addr: TInAddr; |
| 398 |
sin_zero: array[0..7] of Char); |
| 399 |
AF_INET6: (sin6_port: word; |
| 400 |
sin6_flowinfo: longword; |
| 401 |
sin6_addr: TInAddr6; |
| 402 |
sin6_scope_id: longword); |
| 403 |
); |
| 404 |
end; |
| 405 |
|
| 406 |
function SizeOfVarSin(sin: TVarSin): integer; |
| 407 |
|
| 408 |
function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer; |
| 409 |
function WSACleanup: Integer; |
| 410 |
function WSAGetLastError: Integer; |
| 411 |
function GetHostName: string; |
| 412 |
function Shutdown(s: TSocket; how: Integer): Integer; |
| 413 |
function SetSockOpt(s: TSocket; level, optname: Integer; optval: TMemory; |
| 414 |
optlen: Integer): Integer; |
| 415 |
function GetSockOpt(s: TSocket; level, optname: Integer; optval: TMemory; |
| 416 |
var optlen: Integer): Integer; |
| 417 |
function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer; |
| 418 |
function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer; |
| 419 |
function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer; |
| 420 |
function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer; |
| 421 |
function ntohs(netshort: word): word; |
| 422 |
function ntohl(netlong: longword): longword; |
| 423 |
function Listen(s: TSocket; backlog: Integer): Integer; |
| 424 |
function IoctlSocket(s: TSocket; cmd: DWORD; var arg: integer): Integer; |
| 425 |
function htons(hostshort: word): word; |
| 426 |
function htonl(hostlong: longword): longword; |
| 427 |
function GetSockName(s: TSocket; var name: TVarSin): Integer; |
| 428 |
function GetPeerName(s: TSocket; var name: TVarSin): Integer; |
| 429 |
function Connect(s: TSocket; const name: TVarSin): Integer; |
| 430 |
function CloseSocket(s: TSocket): Integer; |
| 431 |
function Bind(s: TSocket; const addr: TVarSin): Integer; |
| 432 |
function Accept(s: TSocket; var addr: TVarSin): TSocket; |
| 433 |
function Socket(af, Struc, Protocol: Integer): TSocket; |
| 434 |
function Select(nfds: Integer; readfds, writefds, exceptfds: PFDSet; |
| 435 |
timeout: PTimeVal): Longint; |
| 436 |
|
| 437 |
function IsNewApi(Family: integer): Boolean; |
| 438 |
function SetVarSin(var Sin: TVarSin; IP, Port: string; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer; |
| 439 |
function GetSinIP(Sin: TVarSin): string; |
| 440 |
function GetSinPort(Sin: TVarSin): Integer; |
| 441 |
procedure ResolveNameToIP(Name: string; Family, SockProtocol, SockType: integer; const IPList: TStrings); |
| 442 |
function ResolveIPToName(IP: string; Family, SockProtocol, SockType: integer): string; |
| 443 |
function ResolvePort(Port: string; Family, SockProtocol, SockType: integer): Word; |
| 444 |
|
| 445 |
|
| 446 |
{==============================================================================} |
| 447 |
implementation |
| 448 |
|
| 449 |
uses |
| 450 |
InitC; |
| 451 |
|
| 452 |
{$if defined(LINUX) or defined(OPENBSD)} |
| 453 |
{$define FIRST_ADDR_THEN_CANONNAME} |
| 454 |
{$elseif defined(FREEBSD) or defined(NETBSD) or defined(DRAGONFLY) or defined(SOLARIS) or defined(ANDROID) or defined(DARWIN) or defined(HAIKU)} |
| 455 |
{$define FIRST_CANONNAME_THEN_ADDR} |
| 456 |
{$else} |
| 457 |
{$error fatal 'Please consult the netdb.h file for your system to determine the order of ai_addr and ai_canonname'} |
| 458 |
{$endif} |
| 459 |
|
| 460 |
{$push}{$packrecords c} |
| 461 |
type |
| 462 |
PAddrInfo = ^addrinfo; |
| 463 |
addrinfo = record |
| 464 |
ai_flags: cint; {* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST *} |
| 465 |
ai_family: cint; {* PF_xxx *} |
| 466 |
ai_socktype: cint; {* SOCK_xxx *} |
| 467 |
ai_protocol: cint; {* 0 or IPPROTO_xxx for IPv4 and IPv6 *} |
| 468 |
ai_addrlen: TSockLen; {* length of ai_addr *} |
| 469 |
{$ifdef FIRST_CANONNAME_THEN_ADDR} |
| 470 |
ai_canonname: PAnsiChar; {* canonical name for hostname *} |
| 471 |
ai_addr: psockaddr; {* binary address *} |
| 472 |
{$endif} |
| 473 |
{$ifdef FIRST_ADDR_THEN_CANONNAME} |
| 474 |
ai_addr: psockaddr; {* binary address *} |
| 475 |
ai_canonname: PAnsiChar; {* canonical name for hostname *} |
| 476 |
{$endif} |
| 477 |
ai_next: PAddrInfo; {* next structure in linked list *} |
| 478 |
end; |
| 479 |
TAddrInfo = addrinfo; |
| 480 |
PPAddrInfo = ^PAddrInfo; |
| 481 |
{$pop} |
| 482 |
|
| 483 |
function getaddrinfo(name, service: PAnsiChar; hints: PAddrInfo; res: PPAddrInfo): cint; cdecl; external clib; |
| 484 |
procedure freeaddrinfo(ai: PAddrInfo); cdecl; external clib; |
| 485 |
|
| 486 |
function ResolveName(const HostName: String; Addresses: Pointer; MaxAddresses, Family: Integer): Integer; overload; |
| 487 |
var |
| 488 |
hints: TAddrInfo; |
| 489 |
res, ai: PAddrInfo; |
| 490 |
begin |
| 491 |
Result:= -1; |
| 492 |
if MaxAddresses = 0 then Exit; |
| 493 |
res:= nil; |
| 494 |
hints:= Default(TAddrInfo); |
| 495 |
hints.ai_family:= Family; |
| 496 |
hints.ai_socktype:= SOCK_STREAM; |
| 497 |
if (getaddrinfo(PAnsiChar(HostName), nil, @hints, @res) <> 0) or (res = nil) then |
| 498 |
Exit; |
| 499 |
ai:= res; |
| 500 |
Result:= 0; |
| 501 |
repeat |
| 502 |
if ai^.ai_family = Family then |
| 503 |
begin |
| 504 |
if Family = AF_INET then |
| 505 |
begin |
| 506 |
Move(PInetSockAddr(ai^.ai_addr)^.sin_addr, Addresses^, SizeOf(TInAddr)); |
| 507 |
Inc(PInAddr(Addresses)); |
| 508 |
end |
| 509 |
else begin |
| 510 |
Move(PInetSockAddr6(ai^.ai_addr)^.sin6_addr, Addresses^, SizeOf(TIn6Addr)); |
| 511 |
Inc(PIn6Addr(Addresses)); |
| 512 |
end; |
| 513 |
Inc(Result); |
| 514 |
end; |
| 515 |
ai:= ai^.ai_next; |
| 516 |
until (ai = nil) or (Result >= MaxAddresses); |
| 517 |
freeaddrinfo(res); |
| 518 |
end; |
| 519 |
|
| 520 |
function ResolveName(HostName: String; var Addresses: array of THostAddr): Integer; overload; |
| 521 |
begin |
| 522 |
Result:= ResolveName(HostName, @Addresses, Length(Addresses), AF_INET); |
| 523 |
end; |
| 524 |
|
| 525 |
function ResolveName6(HostName: String; var Addresses: array of THostAddr6): Integer; |
| 526 |
begin |
| 527 |
Result:= ResolveName(HostName, @Addresses, Length(Addresses), AF_INET6); |
| 528 |
end; |
| 529 |
|
| 530 |
function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean; |
| 531 |
begin |
| 532 |
Result := ((a^.u6_addr32[0] = 0) and (a^.u6_addr32[1] = 0) and |
| 533 |
(a^.u6_addr32[2] = 0) and (a^.u6_addr32[3] = 0)); |
| 534 |
end; |
| 535 |
|
| 536 |
function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean; |
| 537 |
begin |
| 538 |
Result := ((a^.u6_addr32[0] = 0) and (a^.u6_addr32[1] = 0) and |
| 539 |
(a^.u6_addr32[2] = 0) and |
| 540 |
(a^.u6_addr8[12] = 0) and (a^.u6_addr8[13] = 0) and |
| 541 |
(a^.u6_addr8[14] = 0) and (a^.u6_addr8[15] = 1)); |
| 542 |
end; |
| 543 |
|
| 544 |
function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean; |
| 545 |
begin |
| 546 |
Result := ((a^.u6_addr8[0] = $FE) and (a^.u6_addr8[1] = $80)); |
| 547 |
end; |
| 548 |
|
| 549 |
function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean; |
| 550 |
begin |
| 551 |
Result := ((a^.u6_addr8[0] = $FE) and (a^.u6_addr8[1] = $C0)); |
| 552 |
end; |
| 553 |
|
| 554 |
function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean; |
| 555 |
begin |
| 556 |
Result := (a^.u6_addr8[0] = $FF); |
| 557 |
end; |
| 558 |
|
| 559 |
function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6): boolean; |
| 560 |
begin |
| 561 |
Result := (CompareMem( a, b, sizeof(TInAddr6))); |
| 562 |
end; |
| 563 |
|
| 564 |
procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6); |
| 565 |
begin |
| 566 |
FillChar(a^, sizeof(TInAddr6), 0); |
| 567 |
end; |
| 568 |
|
| 569 |
procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6); |
| 570 |
begin |
| 571 |
FillChar(a^, sizeof(TInAddr6), 0); |
| 572 |
a^.u6_addr8[15] := 1; |
| 573 |
end; |
| 574 |
|
| 575 |
{=============================================================================} |
| 576 |
|
| 577 |
function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer; |
| 578 |
begin |
| 579 |
with WSData do |
| 580 |
begin |
| 581 |
wVersion := wVersionRequired; |
| 582 |
wHighVersion := $202; |
| 583 |
szDescription := 'Synsock - Synapse Platform Independent Socket Layer'; |
| 584 |
szSystemStatus := 'Running on Unix/Linux by FreePascal'; |
| 585 |
iMaxSockets := 32768; |
| 586 |
iMaxUdpDg := 8192; |
| 587 |
end; |
| 588 |
Result := 0; |
| 589 |
end; |
| 590 |
|
| 591 |
function WSACleanup: Integer; |
| 592 |
begin |
| 593 |
Result := 0; |
| 594 |
end; |
| 595 |
|
| 596 |
function WSAGetLastError: Integer; |
| 597 |
begin |
| 598 |
Result := fpGetErrno; |
| 599 |
end; |
| 600 |
|
| 601 |
function FD_ISSET(Socket: TSocket; var fdset: TFDSet): Boolean; |
| 602 |
begin |
| 603 |
Result := fpFD_ISSET(socket, fdset) <> 0; |
| 604 |
end; |
| 605 |
|
| 606 |
procedure FD_SET(Socket: TSocket; var fdset: TFDSet); |
| 607 |
begin |
| 608 |
fpFD_SET(Socket, fdset); |
| 609 |
end; |
| 610 |
|
| 611 |
procedure FD_CLR(Socket: TSocket; var fdset: TFDSet); |
| 612 |
begin |
| 613 |
fpFD_CLR(Socket, fdset); |
| 614 |
end; |
| 615 |
|
| 616 |
procedure FD_ZERO(var fdset: TFDSet); |
| 617 |
begin |
| 618 |
fpFD_ZERO(fdset); |
| 619 |
end; |
| 620 |
|
| 621 |
{=============================================================================} |
| 622 |
|
| 623 |
function SizeOfVarSin(sin: TVarSin): integer; |
| 624 |
begin |
| 625 |
case sin.sin_family of |
| 626 |
AF_INET: |
| 627 |
Result := SizeOf(TSockAddrIn); |
| 628 |
AF_INET6: |
| 629 |
Result := SizeOf(TSockAddrIn6); |
| 630 |
else |
| 631 |
Result := 0; |
| 632 |
end; |
| 633 |
end; |
| 634 |
|
| 635 |
{=============================================================================} |
| 636 |
|
| 637 |
function Bind(s: TSocket; const addr: TVarSin): Integer; |
| 638 |
begin |
| 639 |
if fpBind(s, @addr, SizeOfVarSin(addr)) = 0 then |
| 640 |
Result := 0 |
| 641 |
else |
| 642 |
Result := SOCKET_ERROR; |
| 643 |
end; |
| 644 |
|
| 645 |
function Connect(s: TSocket; const name: TVarSin): Integer; |
| 646 |
begin |
| 647 |
if fpConnect(s, @name, SizeOfVarSin(name)) = 0 then |
| 648 |
Result := 0 |
| 649 |
else |
| 650 |
Result := SOCKET_ERROR; |
| 651 |
end; |
| 652 |
|
| 653 |
function GetSockName(s: TSocket; var name: TVarSin): Integer; |
| 654 |
var |
| 655 |
len: integer; |
| 656 |
begin |
| 657 |
len := SizeOf(name); |
| 658 |
FillChar(name, len, 0); |
| 659 |
Result := fpGetSockName(s, @name, @Len); |
| 660 |
end; |
| 661 |
|
| 662 |
function GetPeerName(s: TSocket; var name: TVarSin): Integer; |
| 663 |
var |
| 664 |
len: integer; |
| 665 |
begin |
| 666 |
len := SizeOf(name); |
| 667 |
FillChar(name, len, 0); |
| 668 |
Result := fpGetPeerName(s, @name, @Len); |
| 669 |
end; |
| 670 |
|
| 671 |
function GetHostName: string; |
| 672 |
begin |
| 673 |
Result := unix.GetHostName; |
| 674 |
end; |
| 675 |
|
| 676 |
function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer; |
| 677 |
begin |
| 678 |
Result := fpSend(s, pointer(Buf), len, flags); |
| 679 |
end; |
| 680 |
|
| 681 |
function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer; |
| 682 |
begin |
| 683 |
Result := fpRecv(s, pointer(Buf), len, flags); |
| 684 |
end; |
| 685 |
|
| 686 |
function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer; |
| 687 |
begin |
| 688 |
Result := fpSendTo(s, pointer(Buf), len, flags, @addrto, SizeOfVarSin(addrto)); |
| 689 |
end; |
| 690 |
|
| 691 |
function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer; |
| 692 |
var |
| 693 |
x: integer; |
| 694 |
begin |
| 695 |
x := SizeOf(from); |
| 696 |
Result := fpRecvFrom(s, pointer(Buf), len, flags, @from, @x); |
| 697 |
end; |
| 698 |
|
| 699 |
function Accept(s: TSocket; var addr: TVarSin): TSocket; |
| 700 |
var |
| 701 |
x: integer; |
| 702 |
begin |
| 703 |
x := SizeOf(addr); |
| 704 |
Result := fpAccept(s, @addr, @x); |
| 705 |
end; |
| 706 |
|
| 707 |
function Shutdown(s: TSocket; how: Integer): Integer; |
| 708 |
begin |
| 709 |
Result := fpShutdown(s, how); |
| 710 |
end; |
| 711 |
|
| 712 |
function SetSockOpt(s: TSocket; level, optname: Integer; optval: Tmemory; |
| 713 |
optlen: Integer): Integer; |
| 714 |
begin |
| 715 |
Result := fpsetsockopt(s, level, optname, pointer(optval), optlen); |
| 716 |
end; |
| 717 |
|
| 718 |
function GetSockOpt(s: TSocket; level, optname: Integer; optval: Tmemory; |
| 719 |
var optlen: Integer): Integer; |
| 720 |
begin |
| 721 |
Result := fpgetsockopt(s, level, optname, pointer(optval), @optlen); |
| 722 |
end; |
| 723 |
|
| 724 |
function ntohs(netshort: word): word; |
| 725 |
begin |
| 726 |
Result := sockets.ntohs(NetShort); |
| 727 |
end; |
| 728 |
|
| 729 |
function ntohl(netlong: longword): longword; |
| 730 |
begin |
| 731 |
Result := sockets.ntohl(NetLong); |
| 732 |
end; |
| 733 |
|
| 734 |
function Listen(s: TSocket; backlog: Integer): Integer; |
| 735 |
begin |
| 736 |
if fpListen(s, backlog) = 0 then |
| 737 |
Result := 0 |
| 738 |
else |
| 739 |
Result := SOCKET_ERROR; |
| 740 |
end; |
| 741 |
|
| 742 |
function IoctlSocket(s: TSocket; cmd: DWORD; var arg: integer): Integer; |
| 743 |
begin |
| 744 |
Result := fpIoctl(s, cmd, @arg); |
| 745 |
end; |
| 746 |
|
| 747 |
function htons(hostshort: word): word; |
| 748 |
begin |
| 749 |
Result := sockets.htons(Hostshort); |
| 750 |
end; |
| 751 |
|
| 752 |
function htonl(hostlong: longword): longword; |
| 753 |
begin |
| 754 |
Result := sockets.htonl(HostLong); |
| 755 |
end; |
| 756 |
|
| 757 |
function CloseSocket(s: TSocket): Integer; |
| 758 |
begin |
| 759 |
Result := sockets.CloseSocket(s); |
| 760 |
end; |
| 761 |
|
| 762 |
function Socket(af, Struc, Protocol: Integer): TSocket; |
| 763 |
{$IFDEF DARWIN} |
| 764 |
var |
| 765 |
on_off: integer; |
| 766 |
{$ENDIF} |
| 767 |
begin |
| 768 |
Result := fpSocket(af, struc, protocol); |
| 769 |
// ##### Patch for Mac OS to avoid "Project XXX raised exception class 'External: SIGPIPE'" error. |
| 770 |
{$IFDEF DARWIN} |
| 771 |
if Result <> INVALID_SOCKET then |
| 772 |
begin |
| 773 |
on_off := 1; |
| 774 |
synsock.SetSockOpt(Result, integer(SOL_SOCKET), integer(SO_NOSIGPIPE), @on_off, SizeOf(integer)); |
| 775 |
end; |
| 776 |
{$ENDIF} |
| 777 |
end; |
| 778 |
|
| 779 |
function Select(nfds: Integer; readfds, writefds, exceptfds: PFDSet; |
| 780 |
timeout: PTimeVal): Longint; |
| 781 |
begin |
| 782 |
Result := fpSelect(nfds, readfds, writefds, exceptfds, timeout); |
| 783 |
end; |
| 784 |
|
| 785 |
{=============================================================================} |
| 786 |
function IsNewApi(Family: integer): Boolean; |
| 787 |
begin |
| 788 |
Result := SockEnhancedApi; |
| 789 |
if not Result then |
| 790 |
Result := (Family = AF_INET6) and SockWship6Api; |
| 791 |
end; |
| 792 |
|
| 793 |
function SetVarSin(var Sin: TVarSin; IP, Port: string; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer; |
| 794 |
var |
| 795 |
TwoPass: boolean; |
| 796 |
f1, f2: integer; |
| 797 |
|
| 798 |
function GetAddr(f:integer): integer; |
| 799 |
var |
| 800 |
a4: array [1..1] of in_addr; |
| 801 |
a6: array [1..1] of Tin6_addr; |
| 802 |
he: THostEntry; |
| 803 |
begin |
| 804 |
Result := WSAEPROTONOSUPPORT; |
| 805 |
case f of |
| 806 |
AF_INET: |
| 807 |
begin |
| 808 |
if IP = cAnyHost then |
| 809 |
begin |
| 810 |
Sin.sin_family := AF_INET; |
| 811 |
Result := 0; |
| 812 |
end |
| 813 |
else |
| 814 |
begin |
| 815 |
if lowercase(IP) = cLocalHostStr then |
| 816 |
a4[1].s_addr := htonl(INADDR_LOOPBACK) |
| 817 |
else |
| 818 |
begin |
| 819 |
a4[1].s_addr := 0; |
| 820 |
Result := WSAHOST_NOT_FOUND; |
| 821 |
a4[1] := StrTonetAddr(IP); |
| 822 |
if a4[1].s_addr = INADDR_ANY then |
| 823 |
if GetHostByName(ip, he) then |
| 824 |
a4[1]:=HostToNet(he.Addr) |
| 825 |
else |
| 826 |
Resolvename(ip, a4); |
| 827 |
end; |
| 828 |
if a4[1].s_addr <> INADDR_ANY then |
| 829 |
begin |
| 830 |
Sin.sin_family := AF_INET; |
| 831 |
sin.sin_addr := a4[1]; |
| 832 |
Result := 0; |
| 833 |
end; |
| 834 |
end; |
| 835 |
end; |
| 836 |
AF_INET6: |
| 837 |
begin |
| 838 |
if IP = c6AnyHost then |
| 839 |
begin |
| 840 |
Sin.sin_family := AF_INET6; |
| 841 |
Result := 0; |
| 842 |
end |
| 843 |
else |
| 844 |
begin |
| 845 |
if lowercase(IP) = cLocalHostStr then |
| 846 |
SET_LOOPBACK_ADDR6(@a6[1]) |
| 847 |
else |
| 848 |
begin |
| 849 |
Result := WSAHOST_NOT_FOUND; |
| 850 |
SET_IN6_IF_ADDR_ANY(@a6[1]); |
| 851 |
a6[1] := StrTonetAddr6(IP); |
| 852 |
if IN6_IS_ADDR_UNSPECIFIED(@a6[1]) then |
| 853 |
Resolvename6(ip, a6); |
| 854 |
end; |
| 855 |
if not IN6_IS_ADDR_UNSPECIFIED(@a6[1]) then |
| 856 |
begin |
| 857 |
Sin.sin_family := AF_INET6; |
| 858 |
sin.sin6_addr := a6[1]; |
| 859 |
Result := 0; |
| 860 |
end; |
| 861 |
end; |
| 862 |
end; |
| 863 |
end; |
| 864 |
end; |
| 865 |
begin |
| 866 |
Result := 0; |
| 867 |
FillChar(Sin, Sizeof(Sin), 0); |
| 868 |
Sin.sin_port := htons(Resolveport(port, family, SockProtocol, SockType)); |
| 869 |
TwoPass := False; |
| 870 |
if Family = AF_UNSPEC then |
| 871 |
begin |
| 872 |
if PreferIP4 then |
| 873 |
begin |
| 874 |
f1 := AF_INET; |
| 875 |
f2 := AF_INET6; |
| 876 |
TwoPass := True; |
| 877 |
end |
| 878 |
else |
| 879 |
begin |
| 880 |
f2 := AF_INET; |
| 881 |
f1 := AF_INET6; |
| 882 |
TwoPass := True; |
| 883 |
end; |
| 884 |
end |
| 885 |
else |
| 886 |
f1 := Family; |
| 887 |
Result := GetAddr(f1); |
| 888 |
if Result <> 0 then |
| 889 |
if TwoPass then |
| 890 |
Result := GetAddr(f2); |
| 891 |
end; |
| 892 |
|
| 893 |
function GetSinIP(Sin: TVarSin): string; |
| 894 |
begin |
| 895 |
Result := ''; |
| 896 |
case sin.AddressFamily of |
| 897 |
AF_INET: |
| 898 |
begin |
| 899 |
result := NetAddrToStr(sin.sin_addr); |
| 900 |
end; |
| 901 |
AF_INET6: |
| 902 |
begin |
| 903 |
result := NetAddrToStr6(sin.sin6_addr); |
| 904 |
end; |
| 905 |
end; |
| 906 |
end; |
| 907 |
|
| 908 |
function GetSinPort(Sin: TVarSin): Integer; |
| 909 |
begin |
| 910 |
if (Sin.sin_family = AF_INET6) then |
| 911 |
Result := synsock.ntohs(Sin.sin6_port) |
| 912 |
else |
| 913 |
Result := synsock.ntohs(Sin.sin_port); |
| 914 |
end; |
| 915 |
|
| 916 |
procedure ResolveNameToIP(Name: string; Family, SockProtocol, SockType: integer; const IPList: TStrings); |
| 917 |
var |
| 918 |
x, n: integer; |
| 919 |
a4: array [1..255] of in_addr; |
| 920 |
a6: array [1..255] of Tin6_addr; |
| 921 |
he: THostEntry; |
| 922 |
begin |
| 923 |
IPList.Clear; |
| 924 |
if (family = AF_INET) or (family = AF_UNSPEC) then |
| 925 |
begin |
| 926 |
if lowercase(name) = cLocalHostStr then |
| 927 |
IpList.Add(cLocalHost) |
| 928 |
else |
| 929 |
begin |
| 930 |
a4[1] := StrTonetAddr(name); |
| 931 |
if a4[1].s_addr = INADDR_ANY then |
| 932 |
if GetHostByName(name, he) then |
| 933 |
begin |
| 934 |
a4[1]:=HostToNet(he.Addr); |
| 935 |
x := 1; |
| 936 |
end |
| 937 |
else |
| 938 |
x := Resolvename(name, a4) |
| 939 |
else |
| 940 |
x := 1; |
| 941 |
for n := 1 to x do |
| 942 |
IpList.Add(netaddrToStr(a4[n])); |
| 943 |
end; |
| 944 |
end; |
| 945 |
|
| 946 |
if (family = AF_INET6) or (family = AF_UNSPEC) then |
| 947 |
begin |
| 948 |
if lowercase(name) = cLocalHostStr then |
| 949 |
IpList.Add(c6LocalHost) |
| 950 |
else |
| 951 |
begin |
| 952 |
a6[1] := StrTonetAddr6(name); |
| 953 |
if IN6_IS_ADDR_UNSPECIFIED(@a6[1]) then |
| 954 |
x := Resolvename6(name, a6) |
| 955 |
else |
| 956 |
x := 1; |
| 957 |
for n := 1 to x do |
| 958 |
IpList.Add(netaddrToStr6(a6[n])); |
| 959 |
end; |
| 960 |
end; |
| 961 |
|
| 962 |
if IPList.Count = 0 then |
| 963 |
IPList.Add(cLocalHost); |
| 964 |
end; |
| 965 |
|
| 966 |
function ResolvePort(Port: string; Family, SockProtocol, SockType: integer): Word; |
| 967 |
var |
| 968 |
ProtoEnt: TProtocolEntry; |
| 969 |
ServEnt: TServiceEntry; |
| 970 |
begin |
| 971 |
Result := StrToIntDef(Port, 0); |
| 972 |
if Result = 0 then |
| 973 |
begin |
| 974 |
ProtoEnt.Name := ''; |
| 975 |
GetProtocolByNumber(SockProtocol, ProtoEnt); |
| 976 |
ServEnt.port := 0; |
| 977 |
GetServiceByName(Port, ProtoEnt.Name, ServEnt); |
| 978 |
Result := ntohs(ServEnt.port); |
| 979 |
end; |
| 980 |
end; |
| 981 |
|
| 982 |
function ResolveIPToName(IP: string; Family, SockProtocol, SockType: integer): string; |
| 983 |
var |
| 984 |
n: integer; |
| 985 |
a4: array [1..1] of in_addr; |
| 986 |
a6: array [1..1] of Tin6_addr; |
| 987 |
a: array [1..1] of string; |
| 988 |
begin |
| 989 |
Result := IP; |
| 990 |
a4[1] := StrToNetAddr(IP); |
| 991 |
if a4[1].s_addr <> INADDR_ANY then |
| 992 |
begin |
| 993 |
//why ResolveAddress need address in HOST order? :-O |
| 994 |
n := ResolveAddress(nettohost(a4[1]), a); |
| 995 |
if n > 0 then |
| 996 |
Result := a[1]; |
| 997 |
end |
| 998 |
else |
| 999 |
begin |
| 1000 |
a6[1] := StrToNetAddr6(IP); |
| 1001 |
n := ResolveAddress6(a6[1], a); |
| 1002 |
if n > 0 then |
| 1003 |
Result := a[1]; |
| 1004 |
end; |
| 1005 |
end; |
| 1006 |
|
| 1007 |
{=============================================================================} |
| 1008 |
|
| 1009 |
function InitSocketInterface(stack: string): Boolean; |
| 1010 |
begin |
| 1011 |
SockEnhancedApi := False; |
| 1012 |
SockWship6Api := False; |
| 1013 |
// Libc.Signal(Libc.SIGPIPE, TSignalHandler(Libc.SIG_IGN)); |
| 1014 |
Result := True; |
| 1015 |
end; |
| 1016 |
|
| 1017 |
function DestroySocketInterface: Boolean; |
| 1018 |
begin |
| 1019 |
Result := True; |
| 1020 |
end; |
| 1021 |
|
| 1022 |
initialization |
| 1023 |
begin |
| 1024 |
SynSockCS := SyncObjs.TCriticalSection.Create; |
| 1025 |
SET_IN6_IF_ADDR_ANY (@in6addr_any); |
| 1026 |
SET_LOOPBACK_ADDR6 (@in6addr_loopback); |
| 1027 |
end; |
| 1028 |
|
| 1029 |
finalization |
| 1030 |
begin |
| 1031 |
SynSockCS.Free; |
| 1032 |
end; |
| 1033 |
|
| 1034 |
{$ENDIF} |
| 1035 |
|