Qbasicnews.com

Full Version: winsock2 stuff
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Being really bored today I decided to port some winsock2 stuff from some VB stuff I found a few months ago. So here it is .

oh ya you need libws2_32.a as for the icmp stuff i don't know the .a for it so i have it rem out because i couldn't test it.
you can get the .a stuff from mingw32

Option Explicit

'
' ---------------------------------------------------------------------------------
' File...........: mWinsock2.bas
' Author.........: Will Barden
' Created........: 02/05/03
' Modified.......: 09/05/03
' Version........: 1.0
' Website........: http://www.WinsockVB.com
' Contact........: admin@winsockvb.com
'
' Port of necessary Winsock2 declares, consts, types etc.. Will handle straight
' blocking I/O, WSAAsyncSelect and WSAEventSelect under both TCP/IP and UDP/IP.
' Will also handle sending ICMP echos (a ping) to check if a host is alive. Has
' some helper functions at the bottom, prefixed with "vb".
' ------------------------------------------------------------------------------
'
' ------------------------------------------------------------------------------
' Constants.
' ------------------------------------------------------------------------------
'
' Winsock version constants
Const WINSOCK_V1_1 As INTEGER = &H101
Const WINSOCK_V2_2 As INTEGER = &H202
' Length of fields within the WSADATA structure.
Const WSADESCRIPTION_LEN As INTEGER = 256
Const WSASYS_STATUS_LEN As INTEGER = 128
' For socket handle errors, and bas returns from APIs.
Const ERROR_SUCCESS As INTEGER = 0
Const SOCKET_ERROR As INTEGER = -1
Const INVALID_SOCKET As INTEGER = SOCKET_ERROR
' Internet addresses.
Const INADDR_ANY As INTEGER = &H0
Const INADDR_LOOPBACK As INTEGER = &H7F000001
Const INADDR_BROADCAST As INTEGER = &HFFFFFFFF
Const INADDR_NONE As INTEGER = &HFFFFFFFF
' Maximum backlog when calling listen().
Const SOMAXCONN As INTEGER = 5
' Messages send with WSAAsyncSelect().
Const FD_READ As INTEGER = &H1
Const FD_WRITE As INTEGER = &H2
Const FD_OOB As INTEGER = &H4
Const FD_ACCEPT As INTEGER = &H8
Const FD_CONNECT As INTEGER = &H10
Const FD_CLOSE As INTEGER = &H20
' Used with shutdown().
Const SD_RECEIVE As INTEGER = &H0
Const SD_SEND As INTEGER = &H1
Const SD_BOTH As INTEGER = &H2

' Winsock error constants.
Const WSABASEERR As INTEGER = 10000
Const WSAEINTR As INTEGER = WSABASEERR + 4
Const WSAEBADF As INTEGER = WSABASEERR + 9
Const WSAEACCES As INTEGER = WSABASEERR + 13
Const WSAEFAULT As INTEGER = WSABASEERR + 14
Const WSAEINVAL As INTEGER = WSABASEERR + 22
Const WSAEMFILE As INTEGER = WSABASEERR + 24
Const WSAEWOULDBLOCK As INTEGER = WSABASEERR + 35
Const WSAEINPROGRESS As INTEGER = WSABASEERR + 36
Const WSAEALREADY As INTEGER = WSABASEERR + 37
Const WSAENOTSOCK As INTEGER = WSABASEERR + 38
Const WSAEDESTADDRREQ As INTEGER = WSABASEERR + 39
Const WSAEMSGSIZE As INTEGER = WSABASEERR + 40
Const WSAEPROTOTYPE As INTEGER = WSABASEERR + 41
Const WSAENOPROTOOPT As INTEGER = WSABASEERR + 42
Const WSAEPROTONOSUPPORT As INTEGER = WSABASEERR + 43
Const WSAESOCKTNOSUPPORT As INTEGER = WSABASEERR + 44
Const WSAEOPNOTSUPP As INTEGER = WSABASEERR + 45
Const WSAEPFNOSUPPORT As INTEGER = WSABASEERR + 46
Const WSAEAFNOSUPPORT As INTEGER = WSABASEERR + 47
Const WSAEADDRINUSE As INTEGER = WSABASEERR + 48
Const WSAEADDRNOTAVAIL As INTEGER = WSABASEERR + 49
Const WSAENETDOWN As INTEGER = WSABASEERR + 50
Const WSAENETUNREACH As INTEGER = WSABASEERR + 51
Const WSAENETRESET As INTEGER = WSABASEERR + 52
Const WSAECONNABORTED As INTEGER = WSABASEERR + 53
Const WSAECONNRESET As INTEGER = WSABASEERR + 54
Const WSAENOBUFS As INTEGER = WSABASEERR + 55
Const WSAEISCONN As INTEGER = WSABASEERR + 56
Const WSAENOTCONN As INTEGER = WSABASEERR + 57
Const WSAESHUTDOWN As INTEGER = WSABASEERR + 58
Const WSAETOOMANYREFS As INTEGER = WSABASEERR + 59
Const WSAETIMEDOUT As INTEGER = WSABASEERR + 60
Const WSAECONNREFUSED As INTEGER = WSABASEERR + 61
Const WSAELOOP As INTEGER = WSABASEERR + 62
Const WSAENAMETOOLONG As INTEGER = WSABASEERR + 63
Const WSAEHOSTDOWN As INTEGER = WSABASEERR + 64
Const WSAEHOSTUNREACH As INTEGER = WSABASEERR + 65
Const WSAENOTEMPTY As INTEGER = WSABASEERR + 66
Const WSAEPROCLIM As INTEGER = WSABASEERR + 67
Const WSAEUSERS As INTEGER = WSABASEERR + 68
Const WSAEDQUOT As INTEGER = WSABASEERR + 69
Const WSAESTALE As INTEGER = WSABASEERR + 70
Const WSAEREMOTE As INTEGER = WSABASEERR + 71
Const WSASYSNOTREADY As INTEGER = WSABASEERR + 91
Const WSAVERNOTSUPPORTED As INTEGER = WSABASEERR + 92
Const WSANOTINITIALISED As INTEGER = WSABASEERR + 93
Const WSAHOST_NOT_FOUND As INTEGER = WSABASEERR + 1001

' Winsock 2 extensions.
Const WSA_IO_PENDING As INTEGER = 997
Const WSA_IO_INCOMPLETE As INTEGER = 996
Const WSA_INVALID_HANDLE As INTEGER = 6
Const WSA_INVALID_PARAMETER As INTEGER = 87
Const WSA_NOT_ENOUGH_MEMORY As INTEGER = 8
Const WSA_OPERATION_ABORTED As INTEGER = 995

Const WSA_WAIT_FAILED As INTEGER = -1
Const WSA_WAIT_EVENT_0 As INTEGER = 0
Const WSA_WAIT_IO_COMPLETION As INTEGER = &HC0
Const WSA_WAIT_TIMEOUT As INTEGER = &H102
Const WSA_INFINITE As INTEGER = -1

' Max size of event handle array when calling WSAWaitForMultipleEvents().
Const WSA_MAXIMUM_WAIT_EVENTS As INTEGER = 64
'
' Size of WSANETWORKEVENTS.iErrorCode[] array.
Const FD_MAX_EVENTS As INTEGER = 10
'
' Used to refer to particular elements of the WSANETWORKEVENTS.iErrorCodes[].
Const FD_READ_BIT As INTEGER = 0
Const FD_WRITE_BIT As INTEGER = 1
Const FD_OOB_BIT As INTEGER = 2
Const FD_ACCEPT_BIT As INTEGER = 3
Const FD_CONNECT_BIT As INTEGER = 4
Const FD_CLOSE_BIT As INTEGER = 5
Const FD_QOS_BIT As INTEGER = 6
Const FD_GROUP_QOS_BIT As INTEGER = 7
Const FD_ROUTING_INTERFACE_CHANGE_BIT As INTEGER = 8
Const FD_ADDRESS_LIST_CHANGE_BIT As INTEGER = 9
'
' ------------------------------------------------------------------------------
' Enumerations.
' ------------------------------------------------------------------------------
'
' Used with socket().
Enum Protocols
IPPROTO_IP = 0
IPPROTO_ICMP = 1
IPPROTO_GGP = 2
IPPROTO_TCP = 6
IPPROTO_PUP = 12
IPPROTO_UDP = 17
IPPROTO_IDP = 22
IPPROTO_ND = 77
IPPROTO_RAW = 255
IPPROTO_MAX = 256
end Enum
'
' Used with socket().
Enum SocketTypes
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_RDM = 4
SOCK_SEQPACKET = 5
end Enum
'
' Used with socket().
Enum AddressFamilies
AF_UNSPEC = 0
AF_UNIX = 1
AF_INET = 2
AF_IMPLINK = 3
AF_PUP = 4
AF_CHAOS = 5
AF_NS = 6
AF_IPX = 6
AF_ISO = 7
AF_OSI = 7
AF_ECMA = 8
AF_DATAKIT = 9
AF_CCITT = 10
AF_SNA = 11
AF_DECNET = 12
AF_DLI = 13
AF_LAT = 14
AF_HYLINK = 15
AF_APPLETALK = 16
AF_NETBIOS = 17
AF_MAX = 18
end Enum
'
' ------------------------------------------------------------------------------
' Types.
' ------------------------------------------------------------------------------
'
' To initialize Winsock.
Type WSADATA Field = 1
wVersion As SHORT
wHighVersion As SHORT
szDescription(WSADESCRIPTION_LEN + 1) As Byte
szSystemstatus(WSASYS_STATUS_LEN + 1) As Byte
iMaxSockets As SHORT
iMaxUpdDg As SHORT
lpVendorInfo As INTEGER
End Type
'
' Basic IPv4 addressing structures.
Type in_addr Field = 1
s_addr As INTEGER
End Type
'
Type sockaddr_in Field = 1
sin_family As SHORT
sin_port As SHORT
sin_addr As in_addr
sin_zero(0 To 7) As Byte
End Type
'
' Used with name resolution functions.
Type hostent Field = 1
h_name As INTEGER
h_aliases As INTEGER
h_addrtype As SHORT
h_length As SHORT
h_addr_list As INTEGER
End Type
'
'
' Used with WSAEnumNetworkEvents().
Type WSANETWORKEVENTS Field = 1
lNetworkEvents As INTEGER
iErrorCode(FD_MAX_EVENTS) As SHORT
End Type
'
' Used when sending ICMP echos (pings).
Type IP_OPTION_INFORMATION Field = 1
TTL As Byte
Tos As Byte
Flags As Byte
OptionsSize As INTEGER
OptionsData As String * 128
End Type
'
'
Type IP_ECHO_REPLY Field = 1
Address(0 To 3) As Byte
Status As INTEGER
RoundTripTime As INTEGER
DataSize As SHORT
Reserved As SHORT
data As INTEGER
Options As IP_OPTION_INFORMATION
End Type
'
' ------------------------------------------------------------------------------
' APIs.
' ------------------------------------------------------------------------------
'
' DLL handling functions.
Declare Function WSAStartup Lib "ws2_32" (ByVal wVersionRequested As SHORT, ByRef lpWSAData As WSADATA) As INTEGER
Declare Function WSACleanup Lib "ws2_32" () As INTEGER
Declare Function WSAGetLastError Lib "ws2_32" () As INTEGER
Declare Function WSASetLastError Lib "ws2_32" (ByVal err_fb As INTEGER) As INTEGER
'
' Resolution functions.
Declare Function getpeername Lib "ws2_32" (ByVal s As INTEGER, ByRef name As sockaddr_in, ByRef namelen As INTEGER) As INTEGER
Declare Function getsockname Lib "ws2_32" (ByVal s As INTEGER, ByRef name As sockaddr_in, ByRef namelen As INTEGER) As INTEGER
Declare Function gethostbyname Lib "ws2_32" (ByVal host_name As String) As INTEGER
Declare Function gethostbyaddr Lib "ws2_32" (haddr As INTEGER, ByVal hnlen As INTEGER, ByVal addrtype As INTEGER) As INTEGER
'
' Conversion functions.
Declare Function inet_addr Lib "ws2_32" (ByVal cp As String) As INTEGER
Declare Function inet_ntoa Lib "ws2_32" (ByVal laddr As INTEGER) As INTEGER
Declare Function htonl Lib "ws2_32" (ByVal hostlong As INTEGER) As INTEGER
Declare Function ntohl Lib "ws2_32" (ByVal netlong As INTEGER) As INTEGER
Declare Function htons Lib "ws2_32" (ByVal hostshort As INTEGER) As SHORT
Declare Function ntohs Lib "ws2_32" (ByVal netshort As SHORT) As SHORT
'
' Socket functions.
Declare Function socket Lib "ws2_32" (ByVal af As AddressFamilies, ByVal stype As SocketTypes, ByVal protocol As Protocols) As INTEGER
'
Declare Function bind Lib "ws2_32" (ByVal s As INTEGER, ByRef name As sockaddr_in, ByVal namelen As INTEGER) As INTEGER
Declare Function listen Lib "ws2_32" (ByVal s As INTEGER, ByVal backlog As INTEGER) As INTEGER
Declare Function accept Lib "ws2_32" (ByVal s As INTEGER, ByRef addr As sockaddr_in, ByRef addrlen As INTEGER) As INTEGER
Declare Function connect Lib "ws2_32" (ByVal s As INTEGER, ByRef name As sockaddr_in, ByVal namelen As INTEGER) As INTEGER
'
Declare Function send Lib "ws2_32" (ByVal s As INTEGER, ByRef buf As Byte, ByVal datalen As INTEGER, ByVal Flags As INTEGER) As INTEGER
Declare Function sendto Lib "ws2_32" (ByVal s As INTEGER, ByRef buf As Byte, ByVal datalen As INTEGER, ByVal Flags As INTEGER, ByRef toaddr As sockaddr_in, ByVal tolen As INTEGER) As INTEGER
Declare Function recv Lib "ws2_32" (ByVal s As INTEGER, ByRef buf As Byte, ByVal datalen As INTEGER, ByVal Flags As INTEGER) As INTEGER
Declare Function recvfrom Lib "ws2_32" (ByVal s As INTEGER, ByRef buf As Byte, ByVal datalen As INTEGER, ByVal Flags As INTEGER, ByRef fromaddr As sockaddr_in, ByRef fromlen As INTEGER) As INTEGER
'
Declare Function shutdown Lib "ws2_32" (ByVal s As INTEGER, ByVal how As INTEGER) As INTEGER
Declare Function closesocket Lib "ws2_32" (ByVal s As INTEGER) As INTEGER
'
' I/O model functions.
Declare Function WSAAsyncSelect Lib "ws2_32" (ByVal s As INTEGER, ByVal hwnd As INTEGER, ByVal wMsg As SHORT, ByVal lEvent As INTEGER) As INTEGER
'
Declare Function WSACreateEvent Lib "ws2_32" () As INTEGER
Declare Function WSAEventSelect Lib "ws2_32" (ByVal s As INTEGER, ByVal hEventObject As INTEGER, ByVal lNetworkEvents As INTEGER) As INTEGER
Declare Function WSAResetEvent Lib "ws2_32" (ByVal hEvent As INTEGER) As INTEGER
Declare Function WSASetEvent Lib "ws2_32" (ByVal hEvent As INTEGER) As INTEGER
Declare Function WSACloseEvent Lib "ws2_32" (ByVal hEvent As INTEGER) As INTEGER
Declare Function WSAWaitForMultipleEvents Lib "ws2_32" (ByVal cEvents As INTEGER, ByRef lphEvents As INTEGER, ByVal fWaitAll As SHORT, ByVal dwTimeout As INTEGER, ByVal fAlertable As SHORT) As INTEGER
Declare Function WSAEnumNetworkEvents Lib "ws2_32" (ByVal s As INTEGER, ByVal hEvent As INTEGER, ByRef lpNetworkEvents As WSANETWORKEVENTS) As INTEGER
'
' ICMP functions.
'Declare Function IcmpCreateFile Lib "icmp.dll" () As INTEGER
'Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As INTEGER) As SHORT
'Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As INTEGER, ByVal DestAddress As INTEGER, ByVal RequestData As String, ByVal RequestSize As SHORT, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As INTEGER, ByVal TimeOut As INTEGER) As SHORT
'
' Other general Win32 APIs.
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As INTEGER)


Dim hSocket As integer
Dim udtAddr As sockaddr_in
Dim udtData As WSADATA
Dim lngRet As integer

lngRet = WSAStartup(WINSOCK_V2_2, udtData)

hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
If (hSocket <> INVALID_SOCKET) Then

udtAddr.sin_family = AF_INET
udtAddr.sin_addr.s_addr = inet_addr("216.239.57.104")
udtAddr.sin_port = htons(80)

If (connect(hSocket, udtAddr, Len(udtAddr)) = ERROR_SUCCESS) Then
print "Connected"
else if
print "no connection"
End If

Call closesocket(hSocket)

Else
print "socket() failed with error: " + str$(WSAGetLastError())
End If
lngRet = WSACleanup()
Neat. Winsock's a big task...
ya i know it's a big task but we got to start some were.