Skip to content

Commit

Permalink
Implement Socket for Windows (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
andraantariksa committed Jul 1, 2020
1 parent 50b1e3c commit 0a78b3c
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 123 deletions.
13 changes: 1 addition & 12 deletions src/errno.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@ end
enum Errno
NONE = 0

{% for value in %w(E2BIG EPERM ENOENT ESRCH EINTR EIO ENXIO ENOEXEC EBADF ECHILD EDEADLK ENOMEM
EACCES EFAULT ENOTBLK EBUSY EEXIST EXDEV ENODEV ENOTDIR EISDIR EINVAL ENFILE
EMFILE ENOTTY ETXTBSY EFBIG ENOSPC ESPIPE EROFS EMLINK EPIPE EDOM ERANGE EAGAIN
EWOULDBLOCK EINPROGRESS EALREADY ENOTSOCK EDESTADDRREQ EMSGSIZE EPROTOTYPE ENOPROTOOPT
EPROTONOSUPPORT ESOCKTNOSUPPORT EPFNOSUPPORT EAFNOSUPPORT EADDRINUSE EADDRNOTAVAIL
ENETDOWN ENETUNREACH ENETRESET ECONNABORTED ECONNRESET ENOBUFS EISCONN ENOTCONN
ESHUTDOWN ETOOMANYREFS ETIMEDOUT ECONNREFUSED ELOOP ENAMETOOLONG EHOSTDOWN
EHOSTUNREACH ENOTEMPTY EUSERS EDQUOT ESTALE EREMOTE ENOLCK ENOSYS EOVERFLOW
ECANCELED EIDRM ENOMSG EILSEQ EBADMSG EMULTIHOP ENODATA ENOLINK ENOSR ENOSTR
EPROTO ETIME EOPNOTSUPP ENOTRECOVERABLE EOWNERDEAD
WSABASEERR WSAEINPROGRESS WSAEINTR) %}
{% for value in %w(E2BIG EPERM ENOENT ESRCH EINTR EIO ENXIO ENOEXEC EBADF ECHILD EDEADLK ENOMEM EACCES EFAULT ENOTBLK EBUSY EEXIST EXDEV ENODEV ENOTDIR EISDIR EINVAL ENFILE EMFILE ENOTTY ETXTBSY EFBIG ENOSPC ESPIPE EROFS EMLINK EPIPE EDOM ERANGE EAGAIN EWOULDBLOCK EINPROGRESS EALREADY ENOTSOCK EDESTADDRREQ EMSGSIZE EPROTOTYPE ENOPROTOOPT EPROTONOSUPPORT ESOCKTNOSUPPORT EPFNOSUPPORT EAFNOSUPPORT EADDRINUSE EADDRNOTAVAIL ENETDOWN ENETUNREACH ENETRESET ECONNABORTED ECONNRESET ENOBUFS EISCONN ENOTCONN ESHUTDOWN ETOOMANYREFS ETIMEDOUT ECONNREFUSED ELOOP ENAMETOOLONG EHOSTDOWN EHOSTUNREACH ENOTEMPTY EUSERS EDQUOT ESTALE EREMOTE ENOLCK ENOSYS EOVERFLOW ECANCELED EIDRM ENOMSG EILSEQ EBADMSG EMULTIHOP ENODATA ENOLINK ENOSR ENOSTR EPROTO ETIME EOPNOTSUPP ENOTRECOVERABLE EOWNERDEAD WSABASEERR WSAEINPROGRESS WSAEINTR WSAENOPROTOOPT) %}
{% if LibC.has_constant?(value) %}
{{value.id}} = LibC::{{value.id}}
{% end %}
Expand Down
21 changes: 11 additions & 10 deletions src/lib_c/x86_64-windows-msvc/c/errno.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ lib LibC

# source https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2
WSAECONNABORTED = 10053
ECONNABORTED = 10053
WSAECONNRESET = 10054
ECONNRESET = 10054
ECONNABORTED = 10053
WSAECONNRESET = 10054
ECONNRESET = 10054
WSAECONNREFUSED = 10061
ECONNREFUSED = 10061
WSAEADDRINUSE = 10048
EADDRINUSE = 10048
ECONNREFUSED = 10061
WSAEADDRINUSE = 10048
EADDRINUSE = 10048

WSABASEERR = 10000
WSABASEERR = 10000
WSAEINPROGRESS = WSABASEERR + 36
WSAEINTR = WSABASEERR + 4
WSAEINTR = WSABASEERR + 4
ENOPROTOOPT = 10042

EISCONN = 106
EALREADY = 114
EISCONN = 106
EALREADY = 114
EINPROGRESS = 115

alias ErrnoT = Int
Expand Down
98 changes: 49 additions & 49 deletions src/lib_c/x86_64-windows-msvc/c/netdb.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,69 @@
require "./sys/socket"

lib LibC
AI_PASSIVE = 0x0001
AI_CANONNAME = 0x0002
AI_NUMERICHOST = 0x0004
AI_ALL = 0x0100
AI_ADDRCONFIG = 0x0400
AI_V4MAPPED = 0x0800
AI_NON_AUTHORITATIVE = 0x04000
AI_SECURE = 0x08000
AI_RETURN_PREFERRED_NAMES = 0x010000
AI_FQDN = 0x00020000
AI_FILESERVER = 0x00040000
AI_PASSIVE = 0x0001
AI_CANONNAME = 0x0002
AI_NUMERICHOST = 0x0004
AI_ALL = 0x0100
AI_ADDRCONFIG = 0x0400
AI_V4MAPPED = 0x0800
AI_NON_AUTHORITATIVE = 0x04000
AI_SECURE = 0x08000
AI_RETURN_PREFERRED_NAMES = 0x010000
AI_FQDN = 0x00020000
AI_FILESERVER = 0x00040000

# lin
AI_NUMERICSERV = 0x0400

# move to sys/socket
AF_UNSPEC = 0
AF_INET = 2
AF_IPX = 6
AF_UNSPEC = 0
AF_INET = 2
AF_IPX = 6
AF_APPLETALK = 16
AF_NETBIOS = 17
AF_INET6 = 23
AF_IRDA = 26
AF_BTH = 32
AF_NETBIOS = 17
AF_INET6 = 23
AF_IRDA = 26
AF_BTH = 32

# lin
PF_INET = 2
PF_INET6 = 10
PF_UNIX = LibC::PF_LOCAL
PF_UNSPEC = 0
PF_LOCAL = 1
AF_UNIX = LibC::PF_UNIX
PF_INET = 2
PF_INET6 = 10
PF_UNIX = LibC::PF_LOCAL
PF_UNSPEC = 0
PF_LOCAL = 1
AF_UNIX = LibC::PF_UNIX

## lin
EAI_AGAIN = -3
EAI_BADFLAGS = -1
EAI_FAIL = -4
EAI_FAMILY = -6
EAI_MEMORY = -10
EAI_NONAME = -2
EAI_SERVICE = -8
EAI_SOCKTYPE = -7
EAI_SYSTEM = -11
EAI_OVERFLOW = -12
# # lin
EAI_AGAIN = -3
EAI_BADFLAGS = -1
EAI_FAIL = -4
EAI_FAMILY = -6
EAI_MEMORY = -10
EAI_NONAME = -2
EAI_SERVICE = -8
EAI_SOCKTYPE = -7
EAI_SYSTEM = -11
EAI_OVERFLOW = -12

# move to sys/socket
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_RDM = 4
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_RDM = 4
SOCK_SEQPACKET = 5

# move to netinet/in
IPPROTO_TCP = 6
IPPROTO_UDP = 17
IPPROTO_RM = 113
IPPROTO_IGMP = 2
IPPROTO_TCP = 6
IPPROTO_UDP = 17
IPPROTO_RM = 113
IPPROTO_IGMP = 2
# ipcp
BTHPROTO_RFCOMM = 3
IPPROTO_ICMPV6 = 58
BTHPROTO_RFCOMM = 3
IPPROTO_ICMPV6 = 58

## lin
IPPROTO_IP = 0
# # lin
IPPROTO_IP = 0
IPPROTO_RAW = 255
IPPROTO_ICMP = 1

Expand All @@ -87,7 +87,7 @@ lib LibC
fun freeaddrinfo(pAddrInfo : PADDRINFOA) : VOID
fun getaddrinfo(pNodeName : PCSTR, pServiceName : PCSTR, pHints : ADDRINFOA*, ppResult : PADDRINFOA*) : INT
fun getnameinfo(pSockaddr : SOCKADDR*, sockaddrLength : SocklenT, pNodeBuffer : PCHAR, nodeBufferSize : DWORD, pServiceBuffer : PCHAR, serviceBufferSize : DWORD, flags : INT) : INT

# fun gai_strerror = gai_strerrorA(ecode : Int) : UInt8*
# See src/socket/addrinfo.cr for `gai_strerrorA` function definition
end
45 changes: 44 additions & 1 deletion src/lib_c/x86_64-windows-msvc/c/sys/socket.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "./types"
require "./un"

@[Link("WS2_32")]
lib LibC
Expand All @@ -8,7 +9,7 @@ lib LibC

SO_REUSEADDR = 0x0004
SO_BROADCAST = 0x0020
SOL_SOCKET = 0xFFFF
SOL_SOCKET = 0xFFFF

# -2147195266 is the value after convertion to long, actual value 2147772030 with type unsigned
FIONBIO = -2147195266
Expand All @@ -20,6 +21,26 @@ lib LibC

alias SOCKADDR = Sockaddr

struct WSAData
vVersion : WORD
wHighVersion : WORD
szDescription : StaticArray(UInt8, 257)
szSystemStatus : StaticArray(UInt8, 129)
iMaxSockets : UInt16
iMaxUdpDg : UInt16
lpVendorInfo : UInt8*
end

struct SockaddrStorage
ss_family : Short
__ss_pad1 : StaticArray(Char, 6)
__ss_align : Int64
__ss_pad2 : StaticArray(Char, 112)
end

alias LPWSADATA = WSAData*

fun wsastartup = WSAStartup(wVersionRequired : WORD, lpWSAData : LPWSADATA) : Int
fun socket(af : Int, type : Int, protocol : Int) : SOCKET
fun bind(s : SOCKET, addr : Sockaddr*, namelen : Int) : Int
fun closesocket(s : SOCKET) : Int
Expand All @@ -34,4 +55,26 @@ lib LibC
fun connect(s : SOCKET, name : Sockaddr*, namelen : Int) : Int
fun getsockname(s : SOCKET, name : Sockaddr*, namelen : Int*) : Int
fun htons(hostshort : UShort) : UShort
fun getsockopt(s : SOCKET, level : Int, optname : Int, optval : UInt8*, optlen : Int*) : Int
fun sendto(s : SOCKET, buf : UInt8*, len : Int, flags : Int, to : Sockaddr*, tolen : Int) : Int
fun recvfrom(s : SOCKET, buf : Char*, len : Int, flags : Int, from : Sockaddr*, fromlen : Int*) : Int

SO_RCVBUF = 0x1002
TCP_NODELAY = 0x0001
TCP_KEEPIDLE = 3
TCP_KEEPCNT = 16
TCP_KEEPINTVL = 17
IP_MULTICAST_LOOP = 11
IPV6_MULTICAST_LOOP = 11
IPPROTO_IPV6 = 41
IP_MULTICAST_TTL = 10
IP_MULTICAST_IF = 9
IPV6_MULTICAST_IF = 9
IPV6_MULTICAST_HOPS = 10
IP_ADD_MEMBERSHIP = 12
end

# TODO
wsadata = uninitialized LibC::WSAData
wsaVersion = 514
LibC.wsastartup(wsaVersion, pointerof(wsadata))
6 changes: 6 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/sys/un.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
lib LibC
# struct IpMreq
# imr_multiaddr : IN_ADDR
# imr_interface : IN_ADDR
# end
end
2 changes: 1 addition & 1 deletion src/lib_c/x86_64-windows-msvc/c/winbase.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lib LibC
FORMAT_MESSAGE_FROM_HMODULE = 0x00000800_u32
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000_u32
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000_u32
FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF_u32
FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF_u32

fun FormatMessageW(dwFlags : DWORD, lpSource : Void*, dwMessageId : DWORD, dwLanguageId : DWORD,
lpBuffer : LPWSTR, nSize : DWORD, arguments : Void*) : DWORD
Expand Down
Loading

0 comments on commit 0a78b3c

Please sign in to comment.