Manage returned message length

This commit is contained in:
Blallo 2018-07-27 14:35:13 +02:00
parent 3390eb8432
commit 508d6f6121

View file

@ -4,6 +4,7 @@ import hashlib
import logging import logging
import ssl import ssl
import socket import socket
import time
import click import click
@ -14,16 +15,44 @@ logging.basicConfig(level=logging.INFO,
logger = logging.getLogger('certo') logger = logging.getLogger('certo')
# The following two functions from
# https://stackoverflow.com/questions/17667903/python-socket-receive-large-amount-of-data
def recv_msg(sock, timeout):
# Read message length and unpack it into an integer
raw_msglen = recvall(sock, 1024, timeout)
if not raw_msglen:
return None
msglen = struct.unpack('>I', raw_msglen)[0]
# Read the message data
return recvall(sock, msglen, timeout)
def recvall(sock, n, timeout):
# Helper function to recv n bytes or return None if EOF is hit
data = b''
begin = time.time()
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
if time.time() - begin > timeout:
break
return data
def establish_conn(addr, port, starttls): def establish_conn(addr, port, starttls):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) sock.settimeout(5)
try: try:
if starttls: if starttls:
logger.debug("Using STARTTLS") logger.debug("Using STARTTLS")
logger.debug("Connecting to %s:%s" % (addr, port)) logger.debug("Connecting to %s:%s" % (addr, port))
sock.connect((addr, port)) sock.connect((addr, port))
sock.send(b"STARTTLS\n") sock.send(b"STARTTLS\r\n")
sock.recv(1000) data = recv_msg(sock, 5)
if data is None:
raise socket.error
wrapped_socket = ssl.wrap_socket(sock) wrapped_socket = ssl.wrap_socket(sock)
else: else:
wrapped_socket = ssl.wrap_socket(sock) wrapped_socket = ssl.wrap_socket(sock)