mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-20 07:51:32 +00:00
Some improvements to the sockets module.
This commit is contained in:
@@ -202,9 +202,9 @@ when defined(ssl):
|
||||
proc SSLError(s = "") =
|
||||
if s != "":
|
||||
raise newException(ESSL, s)
|
||||
let err = ErrGetError()
|
||||
let err = ErrPeekLastError()
|
||||
if err == 0:
|
||||
raise newException(ESSL, "An EOF was observed that violates the protocol.")
|
||||
raise newException(ESSL, "No error reported.")
|
||||
if err == -1:
|
||||
OSError()
|
||||
var errStr = ErrErrorString(err, nil)
|
||||
@@ -212,9 +212,13 @@ when defined(ssl):
|
||||
|
||||
# http://simplestcodings.blogspot.co.uk/2010/08/secure-server-client-using-openssl-in-c.html
|
||||
proc loadCertificates(ctx: PSSL_CTX, certFile, keyFile: string) =
|
||||
if certFile != "" and not existsFile(certFile):
|
||||
raise newException(system.EIO, "Certificate file could not be found: " & certFile)
|
||||
if keyFile != "" and not existsFile(keyFile):
|
||||
raise newException(system.EIO, "Key file could not be found: " & keyFile)
|
||||
|
||||
if certFile != "":
|
||||
var ret = SSLCTXUseCertificateFile(ctx, certFile,
|
||||
SSL_FILETYPE_PEM)
|
||||
var ret = SSLCTXUseCertificateChainFile(ctx, certFile)
|
||||
if ret != 1:
|
||||
SSLError()
|
||||
|
||||
@@ -350,7 +354,7 @@ proc bindAddr*(socket: TSocket, port = TPort(0), address = "") =
|
||||
hints.ai_socktype = toInt(SOCK_STREAM)
|
||||
hints.ai_protocol = toInt(IPPROTO_TCP)
|
||||
gaiNim(address, port, hints, aiList)
|
||||
if bindSocket(socket.fd, aiList.ai_addr, aiList.ai_addrLen.cuint) < 0'i32:
|
||||
if bindSocket(socket.fd, aiList.ai_addr, aiList.ai_addrLen.TSockLen) < 0'i32:
|
||||
OSError()
|
||||
|
||||
when false:
|
||||
@@ -586,7 +590,7 @@ proc getHostByAddr*(ip: string): THostEnt =
|
||||
cint(sockets.AF_INET))
|
||||
if s == nil: OSError()
|
||||
else:
|
||||
var s = posix.gethostbyaddr(addr(myaddr), sizeof(myaddr).cuint,
|
||||
var s = posix.gethostbyaddr(addr(myaddr), sizeof(myaddr).TSockLen,
|
||||
cint(posix.AF_INET))
|
||||
if s == nil:
|
||||
raise newException(EOS, $hStrError(h_errno))
|
||||
@@ -629,7 +633,7 @@ proc getHostByName*(name: string): THostEnt =
|
||||
proc getSockOptInt*(socket: TSocket, level, optname: int): int =
|
||||
## getsockopt for integer options.
|
||||
var res: cint
|
||||
var size = sizeof(res).cuint
|
||||
var size = sizeof(res).TSockLen
|
||||
if getsockopt(socket.fd, cint(level), cint(optname),
|
||||
addr(res), addr(size)) < 0'i32:
|
||||
OSError()
|
||||
@@ -639,7 +643,7 @@ proc setSockOptInt*(socket: TSocket, level, optname, optval: int) =
|
||||
## setsockopt for integer options.
|
||||
var value = cint(optval)
|
||||
if setsockopt(socket.fd, cint(level), cint(optname), addr(value),
|
||||
sizeof(value).cuint) < 0'i32:
|
||||
sizeof(value).TSockLen) < 0'i32:
|
||||
OSError()
|
||||
|
||||
proc connect*(socket: TSocket, name: string, port = TPort(0),
|
||||
@@ -661,7 +665,7 @@ proc connect*(socket: TSocket, name: string, port = TPort(0),
|
||||
var success = false
|
||||
var it = aiList
|
||||
while it != nil:
|
||||
if connect(socket.fd, it.ai_addr, it.ai_addrlen.cuint) == 0'i32:
|
||||
if connect(socket.fd, it.ai_addr, it.ai_addrlen.TSockLen) == 0'i32:
|
||||
success = true
|
||||
break
|
||||
it = it.ai_next
|
||||
@@ -722,7 +726,7 @@ proc connectAsync*(socket: TSocket, name: string, port = TPort(0),
|
||||
var success = false
|
||||
var it = aiList
|
||||
while it != nil:
|
||||
var ret = connect(socket.fd, it.ai_addr, it.ai_addrlen.cuint)
|
||||
var ret = connect(socket.fd, it.ai_addr, it.ai_addrlen.TSockLen)
|
||||
if ret == 0'i32:
|
||||
success = true
|
||||
break
|
||||
|
||||
@@ -430,20 +430,20 @@ proc socket*(af, typ, protocol: cint): TWinSocket {.
|
||||
proc closesocket*(s: TWinSocket): cint {.
|
||||
stdcall, importc: "closesocket", dynlib: ws2dll.}
|
||||
|
||||
proc accept*(s: TWinSocket, a: ptr TSockAddr, addrlen: ptr cuint): TWinSocket {.
|
||||
proc accept*(s: TWinSocket, a: ptr TSockAddr, addrlen: ptr TSockLen): TWinSocket {.
|
||||
stdcall, importc: "accept", dynlib: ws2dll.}
|
||||
proc bindSocket*(s: TWinSocket, name: ptr TSockAddr, namelen: cuint): cint {.
|
||||
proc bindSocket*(s: TWinSocket, name: ptr TSockAddr, namelen: TSockLen): cint {.
|
||||
stdcall, importc: "bind", dynlib: ws2dll.}
|
||||
proc connect*(s: TWinSocket, name: ptr TSockAddr, namelen: cuint): cint {.
|
||||
proc connect*(s: TWinSocket, name: ptr TSockAddr, namelen: TSockLen): cint {.
|
||||
stdcall, importc: "connect", dynlib: ws2dll.}
|
||||
proc getsockname*(s: TWinSocket, name: ptr TSockAddr,
|
||||
namelen: ptr cuint): cint {.
|
||||
namelen: ptr TSockLen): cint {.
|
||||
stdcall, importc: "getsockname", dynlib: ws2dll.}
|
||||
proc getsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
||||
optlen: ptr cuint): cint {.
|
||||
optlen: ptr TSockLen): cint {.
|
||||
stdcall, importc: "getsockopt", dynlib: ws2dll.}
|
||||
proc setsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
||||
optlen: cuint): cint {.
|
||||
optlen: TSockLen): cint {.
|
||||
stdcall, importc: "setsockopt", dynlib: ws2dll.}
|
||||
|
||||
proc listen*(s: TWinSocket, backlog: cint): cint {.
|
||||
|
||||
@@ -216,6 +216,8 @@ proc SSL_get_verify_result*(ssl: PSSL): int{.cdecl,
|
||||
proc SSL_CTX_set_cipher_list*(s: PSSLCTX, ciphers: cstring): cint{.cdecl, dynlib: DLLSSLName, importc.}
|
||||
proc SSL_CTX_use_certificate_file*(ctx: PSSL_CTX, filename: cstring, typ: cInt): cInt{.
|
||||
stdcall, dynlib: DLLSSLName, importc.}
|
||||
proc SSL_CTX_use_certificate_chain_file*(ctx: PSSL_CTX, filename: cstring): cInt{.
|
||||
stdcall, dynlib: DLLSSLName, importc.}
|
||||
proc SSL_CTX_use_PrivateKey_file*(ctx: PSSL_CTX,
|
||||
filename: cstring, typ: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
|
||||
proc SSL_CTX_check_private_key*(ctx: PSSL_CTX): cInt{.cdecl, dynlib: DLLSSLName,
|
||||
@@ -255,6 +257,7 @@ proc ERR_print_errors_fp*(fp: TFile){.cdecl, dynlib: DLLSSLName, importc.}
|
||||
proc ERR_error_string*(e: cInt, buf: cstring): cstring{.cdecl,
|
||||
dynlib: DLLUtilName, importc.}
|
||||
proc ERR_get_error*(): cInt{.cdecl, dynlib: DLLUtilName, importc.}
|
||||
proc ERR_peek_last_error*(): cInt{.cdecl, dynlib: DLLUtilName, importc.}
|
||||
|
||||
proc OpenSSL_add_all_algorithms*(){.cdecl, dynlib: DLLSSLName, importc: "OPENSSL_add_all_algorithms_conf".}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user