-
Notifications
You must be signed in to change notification settings - Fork 3
/
socketfunc.pas
293 lines (284 loc) · 7.86 KB
/
socketfunc.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
unit socketfunc;
interface
uses winsock,Messages;
const
WM_SOCK= WM_USER+1;
Type
PIP_mreq = ^TIP_mreq;
TIP_mreq = record
imr_multiaddr : in_addr;
imr_interface : in_addr;
end;
function GetRemoteSocketAddress ( s : TSocket ) : ansiString;
function GetRemoteSocketPort ( s : TSocket ) : Integer;
function GetLocalSocketAddress ( s : TSocket ) : ansiString;
function GetLocalSocketPort ( s : TSocket ) : Integer;
implementation
//You can use the getsockname function for the local port and address and the getpeername for the remote port like so
function GetLocalSocketPort ( s : TSocket ) : Integer;
var
Addr : TSockAddrIn;
Size: integer;
begin
Size := sizeof(Addr);
getsockname(s, Addr, Size);
Result := ntohs(Addr.sin_port);
end;
function GetLocalSocketAddress ( s : TSocket ) : ansiString;
var
Addr : TSockAddrIn;
Size: integer;
begin
Size := sizeof(Addr);
getsockname(s, Addr, Size);
Result := inet_ntoa(Addr.sin_addr);
end;
function GetRemoteSocketPort ( s : TSocket ) : Integer;
var
Addr : TSockAddrIn;
Size: integer;
begin
Size := sizeof(Addr);
getpeername(s, Addr, Size);
Result := ntohs(Addr.sin_port);
end;
function GetRemoteSocketAddress ( s : TSocket ) : ansiString;
var
Addr : TSockAddrIn;
Size: integer;
begin
Size := sizeof(Addr);
getpeername(s, Addr, Size);
Result := inet_ntoa(Addr.sin_addr);
end;
{-------UDP Area-----}
{
MultiCast IP Class:
Class Name Address Bits From ... To Pourpose
Class A 0 0.0.0.0 - 127.255.255.255 Public IP address
Class B 10 128.0.0.0 - 191.255.255.255 Public IP address
Class C 110 192.0.0.0 - 223.255.255.255 Public IP address
Class D 1110 224.0.0.0 - 239.255.255.255 Multicast IP Addresses
Class E 11110 240.0.0.0 - 255.255.255.255 Reserved
}
function BindUDP(port:integer;handle:integer;MulticastAddr:String):integer;
var
udpSocket : integer;
udpBindAddr:TSockAddr;
imreq:Tip_mreq;
begin
result:=-1;
udpSocket:=Socket(PF_INET, SOCK_DGRAM, 0);
if (udpSocket = INVALID_SOCKET) then
exit;
udpBindAddr.sin_family := PF_INET;
udpBindAddr.sin_addr.S_addr := INADDR_ANY;
udpBindAddr.sin_port := htons(port);
if Bind(udpSocket, udpBindAddr, sizeof(udpBindAddr)) <> 0 then
begin
result:=-2;
exit;
end;
if MulticastAddr<>'' then
begin
imreq.imr_multiaddr.s_addr := inet_addr(pansichar(MulticastAddr));
imreq.imr_interface.s_addr := INADDR_ANY; // use DEFAULT interface
if setsockopt(udpSocket,IPPROTO_IP,IP_ADD_MEMBERSHIP,Pansichar(@imreq),sizeof(imreq)) = SOCKET_ERROR then
begin
result:=-1;
exit;
end;
end;
if handle>0 then
WSAAsyncSelect(udpSocket, Handle , WM_SOCK, FD_READ);
result:=udpSocket;
end;
{ async read sample
procedure ReadData(var Message: TMessage);
var
buffer: Array [1..4096] of char;
len: integer;
flen: integer;
Event: word;
value: string;
FSockAddrIn:TSockAddr;
begin
flen:=sizeof(FSockAddrIn);
FSockAddrIn.SIn_Port := htons(8943);
Event := WSAGetSelectEvent(Message.LParam);
if Event = FD_READ then
begin
len := recvfrom(udpSocket, buffer, sizeof(buffer), 0, FSockAddrIn, flen);
value := copy(buffer, 1, len);
CmdAddr:=INet_NToA(FSockAddrIn.SIn_Addr);
if remoteAddr='' then
begin
remoteAddr:=CmdAddr;
end
else
begin
if remoteAddr<>CmdAddr then
begin
ResponseData(myAddr+' Command by '+remoteAddr+' now');
exit;
end;
end;
parseCmd(value);
logs.Lines.add(value);
end;
end;}
function UDPBroadCast(Content: pchar;datalength,port:integer):integer;
var
len,i,optval: integer;
SockAddrIn:TSockAddr;
udpSocket: TSocket;
begin
udpSocket:=Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (udpSocket = INVALID_SOCKET) then
begin
result:=-1;
exit;
end;
optval:= 1;
if setsockopt(udpSocket,SOL_SOCKET,SO_BROADCAST,Pansichar(@optval),sizeof(optval)) = SOCKET_ERROR then
begin
result:=-1;
exit;
end;
SockAddrIn.SIn_Family := PF_INET;
SockAddrIn.SIn_Port := htons(port);
SockAddrIn.SIn_Addr.S_addr := INADDR_BROADCAST;
len := sendto(udpSocket, Content[0], datalength, 0, SockAddrIn, sizeof(SockAddrIn));
if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then
begin
result:=WSAGetLastError()*-1;
exit;
end;
if len = SOCKET_ERROR then
result:=-1;
while len < datalength do
begin
i:= sendto(udpSocket, Content[len], datalength-len, 0, SockAddrIn, sizeof(SockAddrIn));
if i = -1 then
begin
result:=WSAGetLastError()*-1;
exit;
end;
len:=len+i;
end;
result:=0;
end;
function UDPSend(Content: pchar;datalength:integer;host:String;port:integer):integer;
var
SockAddrIn: TSockAddr;
udpSocket: TSocket;
value: string;
len,i: integer;
begin
udpSocket:=Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (udpSocket = INVALID_SOCKET) then
begin
result:=-1;
exit;
end;
SockAddrIn.sin_family:=PF_INET;
SockAddrIn.sin_addr.S_addr:=inet_addr(Pansichar(host));
SockAddrIn.sin_port:=htons(port);
len := sendto(udpSocket, Content[0], datalength, 0, SockAddrIn, sizeof(SockAddrIn));
if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then
begin
result:=WSAGetLastError()*-1;
exit;
end;
if len = SOCKET_ERROR then
result:=-1;
while len < datalength do
begin
i:= sendto(udpSocket, Content[len], datalength-len, 0, SockAddrIn, sizeof(SockAddrIn));
if i = -1 then
begin
result:=WSAGetLastError()*-1;
exit;
end;
len:=len+i;
end;
result:=0;
end;
function UDPMultiCast(Content: pchar;datalength,port:integer;MulticastAddr:String):integer;
var
len,i,optval: integer;
SockAddrIn:TSockAddr;
udpSocket: TSocket;
iaddr:TInAddr;
ttl,one:char;
begin
udpSocket:=Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (udpSocket = INVALID_SOCKET) then
begin
result:=-1;
exit;
end;
optval:= 1;
if setsockopt(udpSocket,SOL_SOCKET,SO_BROADCAST,Pansichar(@optval),sizeof(optval)) = SOCKET_ERROR then
begin
result:=-1;
exit;
end;
SockAddrIn.SIn_Family := PF_INET;
if port>0 then
SockAddrIn.SIn_Port := htons(port)
else
SockAddrIn.SIn_Port := htons(0);// Use the first free port
SockAddrIn.SIn_Addr.S_addr := htonl(INADDR_ANY); // bind socket to any interface
//?nbind ??? UDP bind ?~?ব
if Bind(udpSocket, SockAddrIn, sizeof(SockAddrIn)) <> 0 then
begin
result:=-2;
exit;
end;
iaddr.s_addr := INADDR_ANY; // use DEFAULT interface
if setsockopt(udpSocket,IPPROTO_IP,IP_MULTICAST_IF,Pansichar(@iaddr),sizeof(iaddr)) = SOCKET_ERROR then
begin
result:=-1;
exit;
end;
// Set multicast packet TTL to 3; default TTL is 1
ttl:=#3;
if setsockopt(udpSocket,IPPROTO_IP,IP_MULTICAST_TTL,Pansichar(@ttl),sizeof(char)) = SOCKET_ERROR then
begin
result:=-1;
exit;
end;
// send multicast traffic to myself too
one:=#1;
if setsockopt(udpSocket,IPPROTO_IP,IP_MULTICAST_LOOP,Pansichar(@one),sizeof(char)) = SOCKET_ERROR then
begin
result:=-1;
exit;
end;
// set destination multicast address
SockAddrIn.sin_family := PF_INET;
SockAddrIn.sin_addr.s_addr := inet_addr(Pansichar(MulticastAddr));//MulticastAddr Ex: '226.0.0.1'
SockAddrIn.sin_port := htons(port);
len := sendto(udpSocket, Content[0], datalength, 0, SockAddrIn, sizeof(SockAddrIn));
if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then
begin
result:=WSAGetLastError()*-1;
exit;
end;
if len = SOCKET_ERROR then
result:=-1;
while len < datalength do
begin
i:= sendto(udpSocket, Content[len], datalength-len, 0, SockAddrIn, sizeof(SockAddrIn));
if i = -1 then
begin
result:=WSAGetLastError()*-1;
exit;
end;
len:=len+i;
end;
result:=0;
end;
{-------End UPD Area----}
end.