Some improvements to the sockets module.
This commit is contained in:
parent
39f399f424
commit
40ae258e7e
3 changed files with 23 additions and 16 deletions
|
|
@ -202,9 +202,9 @@ when defined(ssl):
|
||||||
proc SSLError(s = "") =
|
proc SSLError(s = "") =
|
||||||
if s != "":
|
if s != "":
|
||||||
raise newException(ESSL, s)
|
raise newException(ESSL, s)
|
||||||
let err = ErrGetError()
|
let err = ErrPeekLastError()
|
||||||
if err == 0:
|
if err == 0:
|
||||||
raise newException(ESSL, "An EOF was observed that violates the protocol.")
|
raise newException(ESSL, "No error reported.")
|
||||||
if err == -1:
|
if err == -1:
|
||||||
OSError()
|
OSError()
|
||||||
var errStr = ErrErrorString(err, nil)
|
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
|
# http://simplestcodings.blogspot.co.uk/2010/08/secure-server-client-using-openssl-in-c.html
|
||||||
proc loadCertificates(ctx: PSSL_CTX, certFile, keyFile: string) =
|
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 != "":
|
if certFile != "":
|
||||||
var ret = SSLCTXUseCertificateFile(ctx, certFile,
|
var ret = SSLCTXUseCertificateChainFile(ctx, certFile)
|
||||||
SSL_FILETYPE_PEM)
|
|
||||||
if ret != 1:
|
if ret != 1:
|
||||||
SSLError()
|
SSLError()
|
||||||
|
|
||||||
|
|
@ -350,7 +354,7 @@ proc bindAddr*(socket: TSocket, port = TPort(0), address = "") =
|
||||||
hints.ai_socktype = toInt(SOCK_STREAM)
|
hints.ai_socktype = toInt(SOCK_STREAM)
|
||||||
hints.ai_protocol = toInt(IPPROTO_TCP)
|
hints.ai_protocol = toInt(IPPROTO_TCP)
|
||||||
gaiNim(address, port, hints, aiList)
|
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()
|
OSError()
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
|
|
@ -586,7 +590,7 @@ proc getHostByAddr*(ip: string): THostEnt =
|
||||||
cint(sockets.AF_INET))
|
cint(sockets.AF_INET))
|
||||||
if s == nil: OSError()
|
if s == nil: OSError()
|
||||||
else:
|
else:
|
||||||
var s = posix.gethostbyaddr(addr(myaddr), sizeof(myaddr).cuint,
|
var s = posix.gethostbyaddr(addr(myaddr), sizeof(myaddr).TSockLen,
|
||||||
cint(posix.AF_INET))
|
cint(posix.AF_INET))
|
||||||
if s == nil:
|
if s == nil:
|
||||||
raise newException(EOS, $hStrError(h_errno))
|
raise newException(EOS, $hStrError(h_errno))
|
||||||
|
|
@ -629,7 +633,7 @@ proc getHostByName*(name: string): THostEnt =
|
||||||
proc getSockOptInt*(socket: TSocket, level, optname: int): int =
|
proc getSockOptInt*(socket: TSocket, level, optname: int): int =
|
||||||
## getsockopt for integer options.
|
## getsockopt for integer options.
|
||||||
var res: cint
|
var res: cint
|
||||||
var size = sizeof(res).cuint
|
var size = sizeof(res).TSockLen
|
||||||
if getsockopt(socket.fd, cint(level), cint(optname),
|
if getsockopt(socket.fd, cint(level), cint(optname),
|
||||||
addr(res), addr(size)) < 0'i32:
|
addr(res), addr(size)) < 0'i32:
|
||||||
OSError()
|
OSError()
|
||||||
|
|
@ -639,7 +643,7 @@ proc setSockOptInt*(socket: TSocket, level, optname, optval: int) =
|
||||||
## setsockopt for integer options.
|
## setsockopt for integer options.
|
||||||
var value = cint(optval)
|
var value = cint(optval)
|
||||||
if setsockopt(socket.fd, cint(level), cint(optname), addr(value),
|
if setsockopt(socket.fd, cint(level), cint(optname), addr(value),
|
||||||
sizeof(value).cuint) < 0'i32:
|
sizeof(value).TSockLen) < 0'i32:
|
||||||
OSError()
|
OSError()
|
||||||
|
|
||||||
proc connect*(socket: TSocket, name: string, port = TPort(0),
|
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 success = false
|
||||||
var it = aiList
|
var it = aiList
|
||||||
while it != nil:
|
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
|
success = true
|
||||||
break
|
break
|
||||||
it = it.ai_next
|
it = it.ai_next
|
||||||
|
|
@ -722,7 +726,7 @@ proc connectAsync*(socket: TSocket, name: string, port = TPort(0),
|
||||||
var success = false
|
var success = false
|
||||||
var it = aiList
|
var it = aiList
|
||||||
while it != nil:
|
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:
|
if ret == 0'i32:
|
||||||
success = true
|
success = true
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -430,20 +430,20 @@ proc socket*(af, typ, protocol: cint): TWinSocket {.
|
||||||
proc closesocket*(s: TWinSocket): cint {.
|
proc closesocket*(s: TWinSocket): cint {.
|
||||||
stdcall, importc: "closesocket", dynlib: ws2dll.}
|
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.}
|
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.}
|
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.}
|
stdcall, importc: "connect", dynlib: ws2dll.}
|
||||||
proc getsockname*(s: TWinSocket, name: ptr TSockAddr,
|
proc getsockname*(s: TWinSocket, name: ptr TSockAddr,
|
||||||
namelen: ptr cuint): cint {.
|
namelen: ptr TSockLen): cint {.
|
||||||
stdcall, importc: "getsockname", dynlib: ws2dll.}
|
stdcall, importc: "getsockname", dynlib: ws2dll.}
|
||||||
proc getsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
proc getsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
||||||
optlen: ptr cuint): cint {.
|
optlen: ptr TSockLen): cint {.
|
||||||
stdcall, importc: "getsockopt", dynlib: ws2dll.}
|
stdcall, importc: "getsockopt", dynlib: ws2dll.}
|
||||||
proc setsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
proc setsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
||||||
optlen: cuint): cint {.
|
optlen: TSockLen): cint {.
|
||||||
stdcall, importc: "setsockopt", dynlib: ws2dll.}
|
stdcall, importc: "setsockopt", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc listen*(s: TWinSocket, backlog: cint): cint {.
|
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_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{.
|
proc SSL_CTX_use_certificate_file*(ctx: PSSL_CTX, filename: cstring, typ: cInt): cInt{.
|
||||||
stdcall, dynlib: DLLSSLName, importc.}
|
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,
|
proc SSL_CTX_use_PrivateKey_file*(ctx: PSSL_CTX,
|
||||||
filename: cstring, typ: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
|
filename: cstring, typ: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
|
||||||
proc SSL_CTX_check_private_key*(ctx: PSSL_CTX): cInt{.cdecl, dynlib: DLLSSLName,
|
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,
|
proc ERR_error_string*(e: cInt, buf: cstring): cstring{.cdecl,
|
||||||
dynlib: DLLUtilName, importc.}
|
dynlib: DLLUtilName, importc.}
|
||||||
proc ERR_get_error*(): cInt{.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".}
|
proc OpenSSL_add_all_algorithms*(){.cdecl, dynlib: DLLSSLName, importc: "OPENSSL_add_all_algorithms_conf".}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue