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 Jun 24, 2020
1 parent 02a2e69 commit 79bdd17
Show file tree
Hide file tree
Showing 17 changed files with 624 additions and 145 deletions.
4 changes: 3 additions & 1 deletion src/errno.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ enum Errno
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) %}
EPROTO ETIME EOPNOTSUPP ENOTRECOVERABLE EOWNERDEAD
WSABASEERR WSAEINPROGRESS WSAEINTR) %}
{% if LibC.has_constant?(value) %}
{{value.id}} = LibC::{{value.id}}
{% end %}
Expand Down
3 changes: 2 additions & 1 deletion src/io/evented.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% skip_file if flag?(:win32) %}
# TODO
# {% skip_file if flag?(:win32) %}

require "crystal/thread_local_value"

Expand Down
6 changes: 6 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/arpa/inet.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "../winnt"

lib LibC
fun inet_ntop(family : INT, pAddr : PVOID, pStringBuf : PSTR, stringBufSize : SizeT) : PCSTR
fun inet_pton(family : INT, pszAddrString : PCSTR, pAddrBuf : PVOID) : INT
end
18 changes: 18 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/errno.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,23 @@ lib LibC
EILSEQ = 42
STRUNCATE = 80

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

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

EISCONN = 106
EALREADY = 114
EINPROGRESS = 115

alias ErrnoT = Int
end
2 changes: 2 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/fcntl.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "./sys/socket"

lib LibC
O_RDONLY = 0x0000
O_WRONLY = 0x0001
Expand Down
94 changes: 94 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/netdb.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# DIFF
# require "./netinet/in"
# require "./stdint"
require "./sys/socket"

#@[Link(ldflags: "WS2tcpip.obj")]
@[Link("Kernel32")]
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

# lin
AI_NUMERICSERV = 0x0400

# move to sys/socket
AF_UNSPEC = 0
AF_INET = 2
AF_IPX = 6
AF_APPLETALK = 16
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

## 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_SEQPACKET = 5

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

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

struct Addrinfo
ai_flags : Int
ai_family : Int
ai_socktype : Int
ai_protocol : Int
ai_addrlen : SizeT
ai_canonname : UInt8*
ai_addr : Sockaddr*
ai_next : Addrinfo*
end

alias ADDRINFOA = Addrinfo
alias PADDRINFOA = Addrinfo*

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*
fun formatMessageA = FormatMessageA(dwFlags : DWORD, lpSource : LPCVOID, dwMessageId : DWORD, dwLanguageId : DWORD, lpBuffer : LPSTR, nSize : DWORD, ...) : DWORD
end
50 changes: 50 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/netinet/in.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "../sys/socket"
require "../winnt.cr"

lib LibC
struct SUnB
s_b1 : UCHAR
s_b2 : UCHAR
s_b3 : UCHAR
s_b4 : UCHAR
end

struct SUnW
s_w1 : USHORT
s_w2 : USHORT
end

union InAddrU
s_un_b : SUnB
s_un_w : SUnW
s_addr : ULONG
end

struct InAddr
s_un : InAddrU
end

union In6AddrIn6U
byte : StaticArray(UCHAR, 16)
word : StaticArray(USHORT, 8)
end

struct In6Addr
u : In6AddrIn6U
end

struct SockaddrIn6
sin6_family : SHORT
sin6_port : USHORT
sin6_flowinfo : ULONG
sin6_addr : In6Addr
sin6_scope_id : ULONG
end

struct SockaddrIn
sin_family : SHORT
sin_port : USHORT
sin_addr : InAddr
sin_zero : StaticArray(CHAR, 8)
end
end
Empty file.
36 changes: 36 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/sys/socket.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "./types"

@[Link("WS2_32")]
lib LibC
alias SocklenT = UInt
alias SaFamilyT = UShort

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

struct Sockaddr
sa_family : SaFamilyT
sa_data : StaticArray(Char, 14)
end

alias SOCKADDR = Sockaddr

FIONBIO = -2147195266 # -2147195266 is the value after convertion to long, actual value 2147772030 with type unsigned

alias SOCKET = UInt
fun socket(af : Int, type : Int, protocol : Int) : SOCKET
fun bind(s : SOCKET, addr : Sockaddr*, namelen : Int) : Int
fun closesocket(s : SOCKET) : Int
fun send(s : SOCKET, buf : UInt8*, len : Int, flags : Int) : Int
fun setsockopt(s : SOCKET, level : Int, optname : Int, optval : UInt8*, len : Int) : Int
fun ioctlsocket(s : SOCKET, cmd : Int, argp : UInt32*) : Int
fun listen(s : SOCKET, backlog : Int) : Int
fun accept(s : SOCKET, addr : Sockaddr*, addrlen : Int*) : SOCKET
fun getpeername(s : SOCKET, name : Sockaddr*, namelen : Int*) : Int
fun ntohs(netshort : UShort) : UShort
fun recv(s : SOCKET, buf : UInt8*, len : Int, flags : Int) : Int
fun connect(s : SOCKET, name : Sockaddr*, namelen : Int) : Int
fun getsockname(s : SOCKET, name : Sockaddr*, namelen : Int*) : Int
fun htons(hostshort : UShort) : UShort
end
Empty file.
1 change: 1 addition & 0 deletions src/lib_c/x86_64-windows-msvc/c/winbase.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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

fun FormatMessageW(dwFlags : DWORD, lpSource : Void*, dwMessageId : DWORD, dwLanguageId : DWORD,
lpBuffer : LPWSTR, nSize : DWORD, arguments : Void*) : DWORD
Expand Down
11 changes: 11 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/winnt.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
lib LibC
alias BOOLEAN = BYTE
alias LONG = Int32
alias INT = Int32
alias VOID = Void
alias PVOID = Void*
alias LPCVOID = Void*
alias UCHAR = UChar
alias SHORT = Short
alias USHORT = UShort
alias ULONG = UInt32

alias CHAR = UChar
alias PCHAR = CHAR*
alias WCHAR = UInt16
alias LPSTR = CHAR*
alias PSTR = CHAR*
alias PCSTR = CHAR*
alias LPWSTR = WCHAR*
alias LPWCH = WCHAR*

Expand Down
Loading

0 comments on commit 79bdd17

Please sign in to comment.