Federico Ressi | 90a7ee1 | 2018-06-06 12:09:55 +0200 | [diff] [blame] | 1 | # Copyright 2018 Red Hat, Inc. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | |
| 17 | COMMAND = 'socat' |
| 18 | |
| 19 | |
| 20 | class SocatAddress(object): |
| 21 | |
| 22 | def __init__(self, address, args=None, options=None): |
| 23 | self.address = address |
| 24 | self.args = args |
| 25 | self.options = options |
| 26 | |
| 27 | @classmethod |
| 28 | def udp_datagram(cls, host, port, options=None, ip_version=None): |
| 29 | address = 'UDP{}-DATAGRAM'.format(ip_version or '') |
| 30 | return cls(address, (host, int(port)), options) |
| 31 | |
| 32 | @classmethod |
| 33 | def udp_recvfrom(cls, port, options=None, ip_version=None): |
| 34 | address = 'UDP{}-RECVFROM'.format(ip_version or '') |
| 35 | return cls(address, (int(port),), options) |
| 36 | |
| 37 | @classmethod |
| 38 | def stdio(cls): |
| 39 | return cls('STDIO') |
| 40 | |
| 41 | def __str__(self): |
| 42 | address = self.address |
| 43 | if self.args: |
| 44 | address += ':' + ':'.join(str(a) for a in self.args) |
| 45 | if self.options: |
| 46 | address += ',' + ','.join(str(o) for o in self.options) |
| 47 | return address |
| 48 | |
| 49 | def format(self, *args, **kwargs): |
| 50 | return str(self).format(*args, **kwargs) |
| 51 | |
| 52 | |
| 53 | STDIO = SocatAddress.stdio() |
| 54 | |
| 55 | |
| 56 | class SocatOption(object): |
| 57 | |
| 58 | def __init__(self, name, *args): |
| 59 | self.name = name |
| 60 | self.args = args |
| 61 | |
| 62 | @classmethod |
| 63 | def bind(cls, host): |
| 64 | return cls('bind', host) |
| 65 | |
| 66 | @classmethod |
| 67 | def fork(cls): |
| 68 | return cls('fork') |
| 69 | |
| 70 | @classmethod |
| 71 | def ip_multicast_ttl(cls, ttl): |
| 72 | return cls('ip-multicast-ttl', int(ttl)) |
| 73 | |
| 74 | @classmethod |
| 75 | def ip_multicast_if(cls, interface_address): |
| 76 | return cls('ip-multicast-if', interface_address) |
| 77 | |
| 78 | @classmethod |
| 79 | def ip_add_membership(cls, multicast_address, interface_address): |
| 80 | return cls('ip-add-membership', multicast_address, interface_address) |
| 81 | |
| 82 | def __str__(self): |
| 83 | result = self.name |
| 84 | args = self.args |
| 85 | if args: |
| 86 | result += '=' + ':'.join(str(a) for a in args) |
| 87 | return result |
| 88 | |
| 89 | |
| 90 | class SocatCommand(object): |
| 91 | |
| 92 | def __init__(self, source=STDIO, destination=STDIO, command=COMMAND): |
| 93 | self.source = source |
| 94 | self.destination = destination |
| 95 | self.command = command |
| 96 | |
| 97 | def __str__(self): |
| 98 | words = [self.command, self.source, self.destination] |
| 99 | return ' '.join(str(obj) for obj in words) |
| 100 | |
| 101 | |
| 102 | def socat_command(source=STDIO, destination=STDIO, command=COMMAND): |
| 103 | command = SocatCommand(source=source, destination=destination, |
| 104 | command=command) |
| 105 | return str(command) |