2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-31 06:15:47 +00:00

python/ovs/socket_util: add tcp related helper functions which will be used by tcp

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Isaku Yamahata
2012-09-27 18:28:07 +09:00
committed by Ben Pfaff
parent 9d232a6d57
commit 67656b9ff2

View File

@@ -84,6 +84,41 @@ def check_connection_completion(sock):
return errno.EAGAIN
def inet_parse_active(target, default_port):
address = target.split(":")
host_name = address[0]
if not host_name:
raise Exception("%s: bad peer name format" % target)
if len(address) >= 2:
port = int(address[1])
elif default_port:
port = default_port
else:
raise Exception("%s: port number must be specified" % target)
return (host_name, port)
def inet_open_active(style, target, default_port, dscp):
address = inet_parse_active(target, default_port)
try:
sock = socket.socket(socket.AF_INET, style, 0)
except socket.error, e:
return get_exception_errno(e), None
try:
set_nonblocking(sock)
set_dscp(sock, dscp)
try:
sock.connect(address)
except socket.error, e:
if get_exception_errno(e) != errno.EINPROGRESS:
raise
return 0, sock
except socket.error, e:
sock.close()
return get_exception_errno(e), None
def get_socket_error(sock):
"""Returns the errno value associated with 'socket' (0 if no error) and
resets the socket's error status."""
@@ -148,3 +183,10 @@ def set_nonblocking(sock):
except socket.error, e:
vlog.err("could not set nonblocking mode on socket: %s"
% os.strerror(get_socket_error(e)))
def set_dscp(sock, dscp):
if dscp > 63:
raise Exception("Invalid dscp %d" % dscp)
val = dscp << 2
sock.setsockopt(socket.IPPROTO_IP, socket.IP_TOS, val)