<s> import platform <EOL> import socket <EOL> import struct <EOL> from ryu . lib import sockaddr <EOL> def _set_tcp_md5sig_linux ( s , addr , key ) : <EOL> TCP_MD5SIG = <NUM_LIT> <EOL> af = s . family <EOL> if af == socket . AF_INET : <EOL> sa = sockaddr . sa_in4 ( addr ) <EOL> elif af == socket . AF_INET6 : <EOL> sa = sockaddr . sa_in6 ( addr ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( af , ) ) <EOL> ss = sockaddr . sa_to_ss ( sa ) <EOL> tcp_md5sig = ss + struct . pack ( "<STR_LIT>" , len ( key ) , key ) <EOL> s . setsockopt ( socket . IPPROTO_TCP , TCP_MD5SIG , tcp_md5sig ) <EOL> def _set_tcp_md5sig_bsd ( s , _addr , _key ) : <EOL> TCP_MD5SIG = <NUM_LIT> <EOL> tcp_md5sig = struct . pack ( "<STR_LIT:I>" , <NUM_LIT:1> ) <EOL> s . setsockopt ( socket . IPPROTO_TCP , TCP_MD5SIG , tcp_md5sig ) <EOL> def set_tcp_md5sig ( s , addr , key ) : <EOL> """<STR_LIT>""" <EOL> impls = { <EOL> '<STR_LIT>' : _set_tcp_md5sig_bsd , <EOL> '<STR_LIT>' : _set_tcp_md5sig_linux , <EOL> '<STR_LIT>' : _set_tcp_md5sig_bsd , <EOL> } <EOL> system = platform . system ( ) <EOL> try : <EOL> impl = impls [ system ] <EOL> except KeyError : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> impl ( s , addr , key ) </s>
