diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2012-07-25 19:55:52 +0100 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2012-07-25 19:55:52 +0100 |
commit | 40ae258e7e701475fd599aafab17d8c20d10723c (patch) | |
tree | f2778847770fe55dcc3cdd5d5a7e717c6fa815bb | |
parent | 39f399f42442af8b3286ae3ff679d74762b60c35 (diff) | |
download | Nim-40ae258e7e701475fd599aafab17d8c20d10723c.tar.gz |
Some improvements to the sockets module.
-rwxr-xr-x | lib/pure/sockets.nim | 24 | ||||
-rwxr-xr-x | lib/windows/winlean.nim | 12 | ||||
-rwxr-xr-x | lib/wrappers/openssl.nim | 3 |
3 files changed, 23 insertions, 16 deletions
diff --git a/lib/pure/sockets.nim b/lib/pure/sockets.nim index 67dbd6d9f..c2774e84f 100755 --- a/lib/pure/sockets.nim +++ b/lib/pure/sockets.nim @@ -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 diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim index 1cd2f3b08..4c0671df5 100755 --- a/lib/windows/winlean.nim +++ b/lib/windows/winlean.nim @@ -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 {. diff --git a/lib/wrappers/openssl.nim b/lib/wrappers/openssl.nim index dadd02818..5e541cc6b 100755 --- a/lib/wrappers/openssl.nim +++ b/lib/wrappers/openssl.nim @@ -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".} |