-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWinSock.ctl
1570 lines (1485 loc) · 56.1 KB
/
WinSock.ctl
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
VERSION 5.00
Begin VB.UserControl WinSock
ClientHeight = 375
ClientLeft = 0
ClientTop = 0
ClientWidth = 390
InvisibleAtRuntime= -1 'True
ScaleHeight = 375
ScaleWidth = 390
Windowless = -1 'True
Begin VB.Shape Shape1
Height = 375
Left = 0
Shape = 3 'Circle
Top = 0
Width = 375
End
End
Attribute VB_Name = "WinSock"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'Download by http://www.codefans.net
Private Declare Function api_socket Lib "ws2_32.dll" Alias "socket" (ByVal af As Long, ByVal s_type As Long, ByVal Protocol As Long) As Long
Private Declare Function api_htons Lib "ws2_32.dll" Alias "htons" (ByVal hostshort As Integer) As Integer
Private Declare Function api_ntohs Lib "ws2_32.dll" Alias "ntohs" (ByVal netshort As Integer) As Integer
Private Declare Function api_connect Lib "ws2_32.dll" Alias "connect" (ByVal s As Long, ByRef name As sockaddr_in, ByVal namelen As Long) As Long
Private Declare Function api_gethostname Lib "ws2_32.dll" Alias "gethostname" (ByVal host_name As String, ByVal namelen As Long) As Long
Private Declare Function api_gethostbyname Lib "ws2_32.dll" Alias "gethostbyname" (ByVal host_name As String) As Long
Private Declare Function api_bind Lib "ws2_32.dll" Alias "bind" (ByVal s As Long, ByRef name As sockaddr_in, ByVal namelen As Long) As Long
Private Declare Function api_getsockname Lib "ws2_32.dll" Alias "getsockname" (ByVal s As Long, ByRef name As sockaddr_in, ByRef namelen As Long) As Long
Private Declare Function api_getpeername Lib "ws2_32.dll" Alias "getpeername" (ByVal s As Long, ByRef name As sockaddr_in, ByRef namelen As Long) As Long
Private Declare Function api_inet_addr Lib "ws2_32.dll" Alias "inet_addr" (ByVal cp As String) As Long
Private Declare Function api_send Lib "ws2_32.dll" Alias "send" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
Private Declare Function api_sendto Lib "ws2_32.dll" Alias "sendto" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long, ByRef toaddr As sockaddr_in, ByVal tolen As Long) As Long
Private Declare Function api_getsockopt Lib "ws2_32.dll" Alias "getsockopt" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, optlen As Long) As Long
Private Declare Function api_setsockopt Lib "ws2_32.dll" Alias "setsockopt" (ByVal s As Long, ByVal level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long
Private Declare Function api_recv Lib "ws2_32.dll" Alias "recv" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
Private Declare Function api_recvfrom Lib "ws2_32.dll" Alias "recvfrom" (ByVal s As Long, ByRef buf As Any, ByVal buflen As Long, ByVal flags As Long, ByRef from As sockaddr_in, ByRef fromlen As Long) As Long
Private Declare Function api_WSACancelAsyncRequest Lib "ws2_32.dll" Alias "WSACancelAsyncRequest" (ByVal hAsyncTaskHandle As Long) As Long
Private Declare Function api_listen Lib "ws2_32.dll" Alias "listen" (ByVal s As Long, ByVal backlog As Long) As Long
Private Declare Function api_accept Lib "ws2_32.dll" Alias "accept" (ByVal s As Long, ByRef addr As sockaddr_in, ByRef addrlen As Long) As Long
Private Declare Function api_inet_ntoa Lib "ws2_32.dll" Alias "inet_ntoa" (ByVal inn As Long) As Long
Private Declare Function api_ioctlsocket Lib "ws2_32.dll" Alias "ioctlsocket" (ByVal s As Long, ByVal cmd As Long, ByRef argp As Long) As Long
Private Declare Function api_closesocket Lib "ws2_32.dll" Alias "closesocket" (ByVal s As Long) As Long
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GlobalAlloc Lib "KERNEL32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "KERNEL32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "KERNEL32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "KERNEL32" (ByVal hMem As Long) As Long
Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVersionRequired As Long, lpWSADATA As WSAData) As Long
Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long
Private Declare Function WSAAsyncGetHostByName Lib "ws2_32.dll" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal strHostName As String, buf As Any, ByVal buflen As Long) As Long
Private Declare Function WSAAsyncSelect Lib "ws2_32.dll" (ByVal s As Long, ByVal hWnd As Long, ByVal wMsg As Long, ByVal lEvent As Long) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function LenA Lib "KERNEL32" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function api_lstrcpy Lib "KERNEL32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function LoadLibrary Lib "KERNEL32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function FreeLibrary Lib "KERNEL32" (ByVal hLibModule As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetModuleHandle Lib "KERNEL32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function GetProcAddress Lib "KERNEL32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Type tSubData
hWnd As Long
nAddrSub As Long
nAddrOrig As Long
nMsgCntA As Long
nMsgCntB As Long
aMsgTblA() As Long
aMsgTblB() As Long
End Type
Private aBuf(1 To 200) As Byte
Private sc_aSubData() As tSubData
Private bTrack As Boolean
Private bTrackUser32 As Boolean
Private bInCtrl As Boolean
Public Enum SockState
sckClosed = 0
sckOpen
sckListening
sckConnectionPending
sckResolvingHost
sckHostResolved
sckConnecting
sckConnected
sckClosing
sckError
End Enum
Public Enum ProtocolConstants
sckTCPProtocol = 0
sckUDPProtocol = 1
End Enum
Private m_blnInitiated As Boolean
Private m_lngSocksQuantity As Long
Private m_lngWindowHandle As Long
Private m_lngSocketHandle As Long
Private m_enmState As SockState
Private m_strTag As String
Private m_strRemoteHost As String
Private m_lngRemotePort As Long
Private m_strRemoteHostIP As String
Private m_lngLocalPort As Long
Private m_lngLocalPortBind As Long
Private m_strLocalIP As String
Private m_enmProtocol As ProtocolConstants
Private m_lngMemoryPointer As Long
Private m_lngMemoryHandle As Long
Private m_lngSendBufferLen As Long
Private m_lngRecvBufferLen As Long
Private m_strSendBuffer As String
Private m_strRecvBuffer As String
Private m_blnAcceptClass As Boolean
Private m_colAcceptList As Collection
Private m_colSocketsInst As Collection
Private Type WSAData
wVersion As Integer
wHighVersion As Integer
szDescription(256) As Byte
szSystemStatus(128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Private Type sockaddr_in
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero(1 To 8) As Byte
End Type
Public Event CloseSck()
Public Event Connect()
Public Event ConnectionRequest(ByVal requestID As Long)
Public Event DataArrival(ByVal bytesTotal As Long)
Public Event Error(ByVal Number As Integer, Description As String, ByVal sCode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Public Event SendComplete()
Public Event SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
Public Sub zSubclass_Proc(ByVal bBefore As Boolean, ByRef bHandled As Boolean, ByRef lReturn As Long, ByRef lng_hWnd As Long, ByRef uMsg As Long, ByRef wParam As Long, ByRef lParam As Long)
Select Case uMsg
Case 32768
PostResolution wParam, HiWord(lParam)
Case 32769
PostSocket (lParam And &HFFFF&), HiWord(lParam)
End Select
End Sub
Public Property Get RemotePort() As Long
RemotePort = m_lngRemotePort
End Property
Public Property Let RemotePort(ByVal lngPort As Long)
If Not (lngPort < 0 Or lngPort > 65535) Then
m_lngRemotePort = lngPort
End If
End Property
Public Property Get RemoteHost() As String
RemoteHost = m_strRemoteHost
End Property
Public Property Let RemoteHost(ByVal strHost As String)
m_strRemoteHost = strHost
End Property
Public Property Get RemoteHostIP() As String
RemoteHostIP = m_strRemoteHostIP
End Property
Public Property Get LocalPort() As Long
If m_lngLocalPortBind = 0 Then
LocalPort = m_lngLocalPort
Else
LocalPort = m_lngLocalPortBind
End If
End Property
Public Property Let LocalPort(ByVal lngPort As Long)
If Not (lngPort < 0 Or lngPort > 65535) Then
m_lngLocalPort = lngPort
End If
End Property
Public Property Get State() As SockState
State = m_enmState
End Property
Public Property Get LocalHostName() As String
LocalHostName = GetLocalHostName
End Property
Public Property Get LocalIP() As String
If m_enmState = sckConnected Then
LocalIP = m_strLocalIP
Else
LocalIP = GetLocalIP
End If
End Property
Public Property Get BytesReceived() As Long
If m_enmProtocol = sckTCPProtocol Then
BytesReceived = LenA(m_strRecvBuffer)
Else
BytesReceived = GetBufferLenUDP
End If
End Property
Public Property Get SocketHandle() As Long
SocketHandle = m_lngSocketHandle
End Property
Public Property Get Tag() As String
Tag = m_strTag
End Property
Public Property Let Tag(ByVal strTag As String)
m_strTag = strTag
End Property
Public Property Get Protocol() As ProtocolConstants
Protocol = m_enmProtocol
End Property
Public Property Let Protocol(ByVal enmProtocol As ProtocolConstants)
If m_enmState = sckClosed Then
m_enmProtocol = enmProtocol
End If
End Property
Private Sub DestroySocket()
If Not m_lngSocketHandle = -1 Then
Dim lngResult As Long
lngResult = api_closesocket(m_lngSocketHandle)
If lngResult = -1 Then
m_enmState = sckError
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
Else
m_lngSocketHandle = -1
End If
End If
End Sub
Public Sub CloseSck()
If m_lngSocketHandle = -1 Then Exit Sub
m_enmState = sckClosing
CleanResolutionSystem
DestroySocket
m_lngLocalPortBind = 0
m_strRemoteHostIP = ""
m_strRecvBuffer = ""
m_strSendBuffer = ""
m_lngSendBufferLen = 0
m_lngRecvBufferLen = 0
m_enmState = sckClosed
End Sub
Private Function SocketExists() As Boolean
SocketExists = True
Dim lngResult As Long
Dim lngErrorCode As Long
If m_lngSocketHandle = -1 Then
If m_enmProtocol = sckTCPProtocol Then
lngResult = api_socket(2, 1, 6)
Else
lngResult = api_socket(2, 2, 17)
End If
If lngResult = -1 Then
m_enmState = sckError
SocketExists = False
lngErrorCode = Err.LastDllError
EventMsg lngErrorCode, "WinSock.SocketExists"
Else
m_lngSocketHandle = lngResult
ProcessOptions
SocketExists = RegisterSocket(m_lngSocketHandle, True)
End If
End If
End Function
Public Sub Connect(Optional RemoteHost As Variant, Optional RemotePort As Variant)
If Not IsMissing(RemoteHost) Then
m_strRemoteHost = CStr(RemoteHost)
End If
If m_strRemoteHost = vbNullString Then
m_strRemoteHost = ""
End If
If Not IsMissing(RemotePort) Then
If IsNumeric(RemotePort) Then
If Not (CLng(RemotePort) > 65535 Or CLng(RemotePort) < 1) Then
m_lngRemotePort = CLng(RemotePort)
End If
End If
End If
If Not SocketExists Then Exit Sub
If Not BindInternal Then Exit Sub
If m_enmProtocol = sckUDPProtocol Then
m_enmState = sckOpen
Exit Sub
End If
Dim lngAddress As Long
lngAddress = ResolveIfHostname(m_strRemoteHost)
If lngAddress <> vbNull Then
ConnectToIP lngAddress, 0
End If
End Sub
Private Sub PostResolution(ByVal lngAsynHandle As Long, ByVal lngErrorCode As Long)
If m_enmState <> sckResolvingHost Then Exit Sub
If lngErrorCode = 0 Then
m_enmState = sckHostResolved
Dim udtHostent As HOSTENT
Dim lngPtrToIP As Long
Dim arrIpAddress(1 To 4) As Byte
Dim lngRemoteHostAddress As Long
Dim Count As Integer
Dim strIpAddress As String
CopyMemory udtHostent, ByVal m_lngMemoryPointer, LenB(udtHostent)
CopyMemory lngPtrToIP, ByVal udtHostent.hAddrList, 4
CopyMemory arrIpAddress(1), ByVal lngPtrToIP, 4
CopyMemory lngRemoteHostAddress, ByVal lngPtrToIP, 4
FreeMemory
For Count = 1 To 4
strIpAddress = strIpAddress & arrIpAddress(Count) & "."
Next
strIpAddress = Left$(strIpAddress, Len(strIpAddress) - 1)
ConnectToIP lngRemoteHostAddress, 0
Else
FreeMemory
ConnectToIP vbNull, lngErrorCode
End If
End Sub
Private Sub PostSocket(ByVal lngEventID As Long, ByVal lngErrorCode As Long)
If lngErrorCode <> 0 Then
m_enmState = sckError
EventMsg lngErrorCode, "WinSock.PostSocket"
Exit Sub
End If
Dim udtSockAddr As sockaddr_in
Dim lngResult As Long
Dim lngBytesReceived As Long
Select Case lngEventID
Case 16
If m_enmState <> sckConnecting Then
Exit Sub
End If
GetLocalInfo m_lngSocketHandle, m_lngLocalPortBind, m_strLocalIP
GetRemoteInfo m_lngSocketHandle, m_lngRemotePort, m_strRemoteHostIP, m_strRemoteHost
m_enmState = sckConnected
RaiseEvent Connect
Case 2
If m_enmState <> sckConnected Then
Exit Sub
End If
If Len(m_strSendBuffer) > 0 Then
SendBufferedData
End If
Case 1
If m_enmProtocol = sckTCPProtocol Then
If m_enmState <> sckConnected Then
Exit Sub
End If
lngBytesReceived = RecvDataToBuffer
If lngBytesReceived > 0 Then
RaiseEvent DataArrival(LenA(m_strRecvBuffer))
End If
Else
If m_enmState <> sckOpen Then
Exit Sub
End If
lngBytesReceived = GetBufferLenUDP
If lngBytesReceived > 0 Then
RaiseEvent DataArrival(lngBytesReceived)
End If
EmptyBuffer
End If
Case 8
If m_enmState <> sckListening Then
Exit Sub
End If
lngResult = api_accept(m_lngSocketHandle, udtSockAddr, LenB(udtSockAddr))
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
m_enmState = sckError
EventMsg lngErrorCode, "WinSock.PostSocket"
Else
RegisterAccept lngResult
Dim lngTempRP As Long
Dim strTempRHIP As String
Dim strTempRH As String
lngTempRP = m_lngRemotePort
strTempRHIP = m_strRemoteHostIP
strTempRH = m_strRemoteHost
m_strRemoteHost = ""
GetRemoteInfo lngResult, m_lngRemotePort, m_strRemoteHostIP, m_strRemoteHost
RaiseEvent ConnectionRequest(lngResult)
If m_enmState = sckListening Then
m_lngRemotePort = lngTempRP
m_strRemoteHostIP = strTempRHIP
m_strRemoteHost = strTempRH
End If
If IsAcceptRegistered(lngResult) Then
api_closesocket lngResult
End If
End If
Case &H20
If m_enmState <> sckConnected Then
Exit Sub
End If
m_enmState = sckClosing
RaiseEvent CloseSck
End Select
End Sub
Private Sub ConnectToIP(ByVal lngRemoteHostAddress As Long, ByVal lngErrorCode As Long)
If lngErrorCode <> 0 Then
m_enmState = sckError
EventMsg lngErrorCode, "WinSock.ConnectToIP"
Exit Sub
End If
m_enmState = sckConnecting
Dim udtSockAddr As sockaddr_in
Dim lngResult As Long
With udtSockAddr
.sin_addr = lngRemoteHostAddress
.sin_family = 2
.sin_port = api_htons(UnsignedToInteger(m_lngRemotePort))
End With
lngResult = api_connect(m_lngSocketHandle, udtSockAddr, LenB(udtSockAddr))
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
If lngErrorCode <> 10035 Then
If lngErrorCode <> 10049 Then
m_enmState = sckError
EventMsg lngErrorCode, "WinSock.ConnectToIP"
End If
End If
End If
End Sub
Public Sub Bind(Optional LocalPort As Variant, Optional LocalIP As Variant)
If BindInternal(LocalPort, LocalIP) Then
m_enmState = sckOpen
End If
End Sub
Private Function BindInternal(Optional ByVal varLocalPort As Variant, Optional ByVal varLocalIP As Variant) As Boolean
If m_enmState = sckOpen Then
BindInternal = True
Exit Function
End If
Dim lngLocalPortInternal As Long
Dim strLocalHostInternal As String
Dim strIP As String
Dim lngAddressInternal As Long
Dim lngResult As Long
Dim lngErrorCode As Long
BindInternal = False
If Not IsMissing(varLocalPort) Then
If IsNumeric(varLocalPort) Then
If varLocalPort < 0 Or varLocalPort > 65535 Then
BindInternal = False
Else
lngLocalPortInternal = CLng(varLocalPort)
End If
Else
BindInternal = False
End If
Else
lngLocalPortInternal = m_lngLocalPort
End If
If Not IsMissing(varLocalIP) Then
If varLocalIP <> vbNullString Then
strLocalHostInternal = CStr(varLocalIP)
Else
strLocalHostInternal = ""
End If
Else
strLocalHostInternal = ""
End If
lngAddressInternal = ResolveIfHostnameSync(strLocalHostInternal, strIP, lngResult)
If Not SocketExists Then Exit Function
Dim udtSockAddr As sockaddr_in
With udtSockAddr
.sin_addr = lngAddressInternal
.sin_family = 2
.sin_port = api_htons(UnsignedToInteger(lngLocalPortInternal))
End With
lngResult = api_bind(m_lngSocketHandle, udtSockAddr, LenB(udtSockAddr))
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
Else
If lngLocalPortInternal <> 0 Then
m_lngLocalPort = lngLocalPortInternal
Else
lngResult = GetLocalPort(m_lngSocketHandle)
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
Else
m_lngLocalPortBind = lngResult
End If
End If
BindInternal = True
End If
End Function
Private Function AllocateMemory() As Long
m_lngMemoryHandle = GlobalAlloc(0&, 1024)
If m_lngMemoryHandle <> 0 Then
m_lngMemoryPointer = GlobalLock(m_lngMemoryHandle)
If m_lngMemoryPointer <> 0 Then
GlobalUnlock (m_lngMemoryHandle)
AllocateMemory = m_lngMemoryPointer
Else
GlobalFree (m_lngMemoryHandle)
AllocateMemory = m_lngMemoryPointer
End If
Else
AllocateMemory = m_lngMemoryHandle
End If
End Function
Private Sub FreeMemory()
If m_lngMemoryHandle <> 0 Then
m_lngMemoryPointer = 0
GlobalFree m_lngMemoryHandle
m_lngMemoryHandle = 0
End If
End Sub
Private Function GetLocalHostName() As String
Dim strHostNameBuf As String * 256
Dim lngResult As Long
lngResult = api_gethostname(strHostNameBuf, 256)
If lngResult = -1 Then
GetLocalHostName = vbNullString
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
Else
GetLocalHostName = Left(strHostNameBuf, InStr(1, strHostNameBuf, vbNullChar) - 1)
End If
End Function
Private Function GetLocalIP() As String
Dim lngResult As Long
Dim lngPtrToIP As Long
Dim strLocalHost As String
Dim arrIpAddress(1 To 4) As Byte
Dim Count As Integer
Dim udtHostent As HOSTENT
Dim strIpAddress As String
strLocalHost = GetLocalHostName
lngResult = api_gethostbyname(strLocalHost)
If lngResult = 0 Then
GetLocalIP = vbNullString
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
Else
CopyMemory udtHostent, ByVal lngResult, LenB(udtHostent)
CopyMemory lngPtrToIP, ByVal udtHostent.hAddrList, 4
CopyMemory arrIpAddress(1), ByVal lngPtrToIP, 4
For Count = 1 To 4
strIpAddress = strIpAddress & arrIpAddress(Count) & "."
Next
strIpAddress = Left$(strIpAddress, Len(strIpAddress) - 1)
GetLocalIP = strIpAddress
End If
End Function
Private Function ResolveIfHostname(ByVal Host As String) As Long
Dim lngAddress As Long
lngAddress = api_inet_addr(Host)
If lngAddress = &HFFFF Then
ResolveIfHostname = vbNull
m_enmState = sckResolvingHost
If AllocateMemory Then
Dim lngAsynHandle As Long
lngAsynHandle = WSAAsyncGetHostByName(m_lngWindowHandle, 32768, Host, ByVal m_lngMemoryPointer, 1024)
If lngAsynHandle = 0 Then
FreeMemory
m_enmState = sckError
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
EventMsg lngErrorCode, "WinSock.ResolveIfHostname"
End If
Else
m_enmState = sckError
End If
Else
ResolveIfHostname = lngAddress
End If
End Function
Private Function ResolveIfHostnameSync(ByVal Host As String, ByRef strHostIP As String, ByRef lngErrorCode As Long) As Long
Dim lngPtrToHOSTENT As Long
Dim udtHostent As HOSTENT
Dim lngAddress As Long
Dim lngPtrToIP As Long
Dim arrIpAddress(1 To 4) As Byte
Dim Count As Integer
lngAddress = api_inet_addr(Host)
If lngAddress = &HFFFF Then
lngPtrToHOSTENT = api_gethostbyname(Host)
If lngPtrToHOSTENT = 0 Then
lngErrorCode = Err.LastDllError
strHostIP = vbNullString
ResolveIfHostnameSync = vbNull
Else
CopyMemory udtHostent, ByVal lngPtrToHOSTENT, LenB(udtHostent)
CopyMemory lngPtrToIP, ByVal udtHostent.hAddrList, 4
CopyMemory arrIpAddress(1), ByVal lngPtrToIP, 4
CopyMemory lngAddress, ByVal lngPtrToIP, 4
For Count = 1 To 4
strHostIP = strHostIP & arrIpAddress(Count) & "."
Next
strHostIP = Left$(strHostIP, Len(strHostIP) - 1)
lngErrorCode = 0
ResolveIfHostnameSync = lngAddress
End If
Else
lngErrorCode = 0
strHostIP = Host
ResolveIfHostnameSync = lngAddress
End If
End Function
Private Function GetLocalPort(ByVal lngSocket As Long) As Long
Dim udtSockAddr As sockaddr_in
Dim lngResult As Long
lngResult = api_getsockname(lngSocket, udtSockAddr, LenB(udtSockAddr))
If lngResult = -1 Then
GetLocalPort = -1
Else
GetLocalPort = IntegerToUnsigned(api_ntohs(udtSockAddr.sin_port))
End If
End Function
Public Sub SendData(Data As Variant)
Dim arrData() As Byte
If m_enmProtocol = sckTCPProtocol Then
If m_enmState <> sckConnected Then
Exit Sub
End If
Else
If Not SocketExists Then Exit Sub
If Not BindInternal Then Exit Sub
m_enmState = sckOpen
End If
Select Case varType(Data)
Case vbString
Dim strData As String
strData = CStr(Data)
If Len(strData) = 0 Then Exit Sub
ReDim arrData(LenA(strData) - 1)
arrData() = StrConv(strData, vbFromUnicode)
Case vbArray + vbByte
Dim strArray As String
strArray = StrConv(Data, vbUnicode)
If LenB(strArray) = 0 Then Exit Sub
arrData() = StrConv(strArray, vbFromUnicode)
Case vbBoolean
Dim blnData As Boolean
blnData = CBool(Data)
ReDim arrData(LenB(blnData) - 1)
CopyMemory arrData(0), blnData, LenB(blnData)
Case vbByte
Dim bytData As Byte
bytData = CByte(Data)
ReDim arrData(LenB(bytData) - 1)
CopyMemory arrData(0), bytData, LenB(bytData)
Case vbCurrency
Dim curData As Currency
curData = CCur(Data)
ReDim arrData(LenB(curData) - 1)
CopyMemory arrData(0), curData, LenB(curData)
Case vbDate
Dim datData As Date
datData = CDate(Data)
ReDim arrData(LenB(datData) - 1)
CopyMemory arrData(0), datData, LenB(datData)
Case vbDouble
Dim dblData As Double
dblData = CDbl(Data)
ReDim arrData(LenB(dblData) - 1)
CopyMemory arrData(0), dblData, LenB(dblData)
Case vbInteger
Dim intData As Integer
intData = CInt(Data)
ReDim arrData(LenB(intData) - 1)
CopyMemory arrData(0), intData, LenB(intData)
Case vbLong
Dim lngData As Long
lngData = CLng(Data)
ReDim arrData(LenB(lngData) - 1)
CopyMemory arrData(0), lngData, LenB(lngData)
Case vbSingle
Dim sngData As Single
sngData = CSng(Data)
ReDim arrData(LenB(sngData) - 1)
CopyMemory arrData(0), sngData, LenB(sngData)
Case Else
End Select
If Len(m_strSendBuffer) > 0 Then
m_strSendBuffer = m_strSendBuffer + StrConv(arrData(), vbUnicode)
Exit Sub
Else
m_strSendBuffer = m_strSendBuffer + StrConv(arrData(), vbUnicode)
End If
SendBufferedData
End Sub
Private Sub SendBufferedData()
If m_enmProtocol = sckTCPProtocol Then
SendBufferedDataTCP
Else
SendBufferedDataUDP
End If
End Sub
Private Sub SendBufferedDataUDP()
Dim lngAddress As Long
Dim udtSockAddr As sockaddr_in
Dim arrData() As Byte
Dim lngBufferLength As Long
Dim lngResult As Long
Dim lngErrorCode As Long
Dim strTemp As String
lngAddress = ResolveIfHostnameSync(m_strRemoteHost, strTemp, lngErrorCode)
If lngErrorCode <> 0 Then
m_strSendBuffer = ""
End If
With udtSockAddr
.sin_addr = lngAddress
.sin_family = 2
.sin_port = api_htons(UnsignedToInteger(m_lngRemotePort))
End With
lngBufferLength = LenA(m_strSendBuffer)
arrData() = StrConv(m_strSendBuffer, vbFromUnicode)
m_strSendBuffer = ""
lngResult = api_sendto(m_lngSocketHandle, arrData(0), lngBufferLength, 0&, udtSockAddr, LenB(udtSockAddr))
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
If lngErrorCode <> 10035 Then
m_enmState = sckError
EventMsg lngErrorCode, "WinSock.SendBufferedDataUDP"
End If
End If
End Sub
Private Sub SendBufferedDataTCP()
Dim arrData() As Byte
Dim lngBufferLength As Long
Dim lngResult As Long
Dim lngTotalSent As Long
Do Until lngResult = -1 Or Len(m_strSendBuffer) = 0
lngBufferLength = Len(m_strSendBuffer)
If lngBufferLength > m_lngSendBufferLen Then
lngBufferLength = m_lngSendBufferLen
arrData() = StrConv(Left$(m_strSendBuffer, m_lngSendBufferLen), vbFromUnicode)
Else
arrData() = StrConv(m_strSendBuffer, vbFromUnicode)
lngBufferLength = UBound(arrData) + 1
End If
lngResult = api_send(m_lngSocketHandle, arrData(0), lngBufferLength, 0&)
If lngResult = -1 Then
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
If lngErrorCode = 10035 Then
If lngTotalSent > 0 Then RaiseEvent SendProgress(lngTotalSent, Len(m_strSendBuffer))
Else
m_enmState = sckError
EventMsg lngErrorCode, "WinSock.SendBufferedData"
End If
Else
lngTotalSent = lngTotalSent + lngResult
If Len(m_strSendBuffer) > lngResult Then
m_strSendBuffer = Mid$(m_strSendBuffer, lngResult + 1)
Else
m_strSendBuffer = ""
Dim lngTemp As Long
lngTemp = lngTotalSent
lngTotalSent = 0
RaiseEvent SendProgress(lngTemp, 0)
RaiseEvent SendComplete
End If
End If
Loop
End Sub
Private Function RecvDataToBuffer() As Long
Dim arrBuffer() As Byte
Dim lngBytesReceived As Long
Dim strBuffTemporal As String
ReDim arrBuffer(m_lngRecvBufferLen - 1)
lngBytesReceived = api_recv(m_lngSocketHandle, arrBuffer(0), m_lngRecvBufferLen, 0&)
If lngBytesReceived = -1 Then
m_enmState = sckError
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
ElseIf lngBytesReceived > 0 Then
strBuffTemporal = StrConv(arrBuffer(), vbUnicode)
m_strRecvBuffer = m_strRecvBuffer & Left$(strBuffTemporal, lngBytesReceived)
RecvDataToBuffer = lngBytesReceived
End If
End Function
Private Sub ProcessOptions()
Dim lngResult As Long
Dim lngBuffer As Long
Dim lngErrorCode As Long
If m_enmProtocol = sckTCPProtocol Then
lngResult = api_getsockopt(m_lngSocketHandle, 65535, &H1002, lngBuffer, LenB(lngBuffer))
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
Else
m_lngRecvBufferLen = lngBuffer
End If
lngResult = api_getsockopt(m_lngSocketHandle, 65535, &H1001, lngBuffer, LenB(lngBuffer))
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
Else
m_lngSendBufferLen = lngBuffer
End If
Else
lngBuffer = 1
lngResult = api_setsockopt(m_lngSocketHandle, 65535, 32, lngBuffer, LenB(lngBuffer))
lngResult = api_getsockopt(m_lngSocketHandle, 65535, 8195, lngBuffer, LenB(lngBuffer))
If lngResult = -1 Then
lngErrorCode = Err.LastDllError
Else
m_lngRecvBufferLen = lngBuffer
m_lngSendBufferLen = lngBuffer
End If
End If
End Sub
Public Sub GetData(ByRef Data As Variant, Optional varType As Variant, Optional maxLen As Variant)
If m_enmProtocol = sckTCPProtocol Then
If m_enmState <> sckConnected And Not m_blnAcceptClass Then
Exit Sub
End If
Else
If m_enmState <> sckOpen Then
Exit Sub
End If
If GetBufferLenUDP = 0 Then Exit Sub
End If
If Not IsMissing(maxLen) Then
If Not IsNumeric(maxLen) Then
If m_enmProtocol = sckTCPProtocol Then
maxLen = LenA(m_strRecvBuffer)
Else
maxLen = GetBufferLenUDP
End If
End If
End If
Dim lngBytesRecibidos As Long
lngBytesRecibidos = RecvData(Data, False, varType, maxLen)
End Sub
Public Sub PeekData(ByRef Data As Variant, Optional varType As Variant, Optional maxLen As Variant)
If m_enmProtocol = sckTCPProtocol Then
If m_enmState <> sckConnected Then
Exit Sub
End If
Else
If m_enmState <> sckOpen Then
Exit Sub
End If
If GetBufferLenUDP = 0 Then Exit Sub
End If
If Not IsMissing(maxLen) Then
If IsNumeric(maxLen) Then
If CLng(maxLen) < 0 Then
End If
Else
If m_enmProtocol = sckTCPProtocol Then
maxLen = LenA(m_strRecvBuffer)
Else
maxLen = GetBufferLenUDP
End If
End If
End If
Dim lngBytesRecibidos As Long
lngBytesRecibidos = RecvData(Data, True, varType, maxLen)
End Sub
Private Function RecvData(ByRef Data As Variant, ByVal blnPeek As Boolean, Optional varClass As Variant, Optional maxLen As Variant) As Long
Dim blnMaxLenMiss As Boolean
Dim blnClassMiss As Boolean
Dim strRecvData As String
Dim lngBufferLen As Long
Dim arrBuffer() As Byte
Dim lngErrorCode As Long
If m_enmProtocol = sckTCPProtocol Then
lngBufferLen = LenB(m_strRecvBuffer)
Else
lngBufferLen = GetBufferLenUDP
End If
blnMaxLenMiss = IsMissing(maxLen)
blnClassMiss = IsMissing(varClass)
If varType(Data) = vbEmpty Then
If blnClassMiss Then varClass = vbArray + vbByte
Else
varClass = varType(Data)
End If
If varClass = vbString Or varClass = vbArray + vbByte Then
If blnMaxLenMiss Then
If lngBufferLen = 0 Then
RecvData = 0
arrBuffer = StrConv("", vbFromUnicode)
Data = arrBuffer
Exit Function
Else
RecvData = lngBufferLen
BuildArray lngBufferLen, blnPeek, lngErrorCode, arrBuffer
End If
Else
If maxLen = 0 Or lngBufferLen = 0 Then
RecvData = 0
arrBuffer = StrConv("", vbFromUnicode)
Data = arrBuffer
If m_enmProtocol = sckUDPProtocol Then
EmptyBuffer
End If
Exit Function
ElseIf maxLen > lngBufferLen Then
RecvData = lngBufferLen
BuildArray lngBufferLen, blnPeek, lngErrorCode, arrBuffer
Else
RecvData = CLng(maxLen)
BuildArray CLng(maxLen), blnPeek, lngErrorCode, arrBuffer
End If
End If
End If
Select Case varClass
Case vbString
Dim strData As String
strData = StrConv(arrBuffer(), vbUnicode)
Data = strData
Case vbArray + vbByte
Data = arrBuffer
Case vbBoolean
Dim blnData As Boolean
If LenB(blnData) > lngBufferLen Then Exit Function
BuildArray LenB(blnData), blnPeek, lngErrorCode, arrBuffer
RecvData = LenB(blnData)
CopyMemory blnData, arrBuffer(0), LenB(blnData)
Data = blnData
Case vbByte
Dim bytData As Byte
If LenB(bytData) > lngBufferLen Then Exit Function
BuildArray LenB(bytData), blnPeek, lngErrorCode, arrBuffer
RecvData = LenB(bytData)
CopyMemory bytData, arrBuffer(0), LenB(bytData)
Data = bytData
Case vbCurrency
Dim curData As Currency
If LenB(curData) > lngBufferLen Then Exit Function
BuildArray LenB(curData), blnPeek, lngErrorCode, arrBuffer
RecvData = LenB(curData)
CopyMemory curData, arrBuffer(0), LenB(curData)
Data = curData
Case vbDate
Dim datData As Date
If LenB(datData) > lngBufferLen Then Exit Function
BuildArray LenB(datData), blnPeek, lngErrorCode, arrBuffer
RecvData = LenB(datData)
CopyMemory datData, arrBuffer(0), LenB(datData)
Data = datData
Case vbDouble
Dim dblData As Double
If LenB(dblData) > lngBufferLen Then Exit Function
BuildArray LenB(dblData), blnPeek, lngErrorCode, arrBuffer
RecvData = LenB(dblData)
CopyMemory dblData, arrBuffer(0), LenB(dblData)
Data = dblData
Case vbInteger
Dim intData As Integer
If LenB(intData) > lngBufferLen Then Exit Function
BuildArray LenB(intData), blnPeek, lngErrorCode, arrBuffer
RecvData = LenB(intData)
CopyMemory intData, arrBuffer(0), LenB(intData)
Data = intData
Case vbLong
Dim lngData As Long
If LenB(lngData) > lngBufferLen Then Exit Function
BuildArray LenB(lngData), blnPeek, lngErrorCode, arrBuffer
RecvData = LenB(lngData)
CopyMemory lngData, arrBuffer(0), LenB(lngData)