-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWinSock.pas
221 lines (167 loc) · 8.19 KB
/
WinSock.pas
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
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
unit
WinSock;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
interface
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
uses
IPUtils;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
const
WINDOWS_SOCKETS_VERSION = $0202;
WINDOWS_SOCKETS_DLL = 'WS2_32.DLL';
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
PWSAData = ^TWSAData;
TWSAData = packed record
wVersion : Word;
wHighVersion : Word;
szDescription : Array [0..256] of Char;
szSystemStatus : Array [0..128] of Char;
iMaxSockets : Word;
iMaxUdpDg : Word;
lpVendorInfo : PChar;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
PIPv4SocketAddress = ^TIPv4SocketAddress;
TIPv4SocketAddress = packed record
sin_family : Word;
sin_port : Word;
sin_addr : TIPv4Address;
sin_zero : Array [0..7] of Char;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
PIPv6SocketAddress = ^TIPv6SocketAddress;
TIPv6SocketAddress = packed record
sin_family : Word;
sin_port : Word;
sin_flowinfo : LongInt;
sin_addr : TIPv6Address;
sin_scopeid : LongInt;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
PFDSet = ^TFDSet;
TFDSet = packed record
fd_count: Cardinal;
fd_array: Array [0..63] of Integer;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
PTimeVal = ^TTimeVal;
TTimeVal = packed record
tv_sec: LongInt;
tv_usec: LongInt;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
function WSAStartup(VersionRequired: Word; var WSAData: TWSAData): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'WSAStartup';
function Socket(AF, Struct, Protocol: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'socket';
function IPv4Bind(S: Integer; var Addr: TIPv4SocketAddress; AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'bind';
function IPv6Bind(S: Integer; var Addr: TIPv6SocketAddress; AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'bind';
function IPv4Connect(S: Integer; var Addr: TIPv4SocketAddress; AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'connect';
function IPv6Connect(S: Integer; var Addr: TIPv6SocketAddress; AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'connect';
function Select(NFDS: Integer; ReadFDS, WriteFDS, ExceptFDS: PFDSet; Timeout: PTimeVal): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'select';
function IPv4Listen(S: Integer; BackLog: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'listen';
function IPv6Listen(S: Integer; BackLog: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'listen';
function IPv4Accept(S: Integer; var Addr: TIPv4SocketAddress; var AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'accept';
function IPv6Accept(S: Integer; var Addr: TIPv6SocketAddress; var AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'accept';
function IPv4Recv(S: Integer; var Buf; Len, Flags: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'recv';
function IPv6Recv(S: Integer; var Buf; Len, Flags: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'recv';
function IPv4RecvFrom(S: Integer; var Buf; Len, Flags: Integer; var Addr: TIPv4SocketAddress; var AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'recvfrom';
function IPv6RecvFrom(S: Integer; var Buf; Len, Flags: Integer; var Addr: TIPv6SocketAddress; var AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'recvfrom';
function IPv4Send(S: Integer; var Buf; Len, Flags: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'send';
function IPv6Send(S: Integer; var Buf; Len, Flags: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'send';
function IPv4SendTo(S: Integer; var Buf; Len, Flags: Integer; var Addr: TIPv4SocketAddress; AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'sendto';
function IPv6SendTo(S: Integer; var Buf; Len, Flags: Integer; var Addr: TIPv6SocketAddress; AddrLen: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'sendto';
function IPv4Shutdown(S: Integer; How: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'shutdown';
function IPv6Shutdown(S: Integer; How: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'shutdown';
function CloseSocket(S: Integer): Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'closesocket';
function WSAGetLastError: Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'WSAGetLastError';
function WSACleanup: Integer; stdcall; external WINDOWS_SOCKETS_DLL name 'WSACleanup';
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
const
AF_INET = 2;
AF_INET6 = 23;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
const
SOCK_STREAM = 1;
SOCK_DGRAM = 2;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
const
IPPROTO_TCP = 6;
IPPROTO_UDP = 17;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
const
SD_SEND = 1;
SD_BOTH = 2;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
const
SOCKET_ERROR = -1;
INVALID_SOCKET = -1;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
function HTONS(Value: Word): Word;
function IsValidSocketHandle(SocketHandle: Integer): Boolean;
function IsValidSocketResult(SocketResult: Integer): Boolean;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
implementation
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
function HTONS(Value: Word): Word;
begin
Result := (Value shr $08) + ((Value and $ff) shl $08);
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
function IsValidSocketHandle(SocketHandle: Integer): Boolean;
begin
Result := SocketHandle <> INVALID_SOCKET;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
function IsValidSocketResult(SocketResult: Integer): Boolean;
begin
Result := SocketResult <> SOCKET_ERROR;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
end.