Index: trunk/udpmcast/udpmcast.py |
— | — | @@ -89,12 +89,18 @@ |
90 | 90 | if debugging: |
91 | 91 | print msg; |
92 | 92 | |
93 | | -def multicast_diagrams(sock, addresses): |
| 93 | +def multicast_diagrams(sock, addrrules): |
94 | 94 | portnr = sock.getsockname()[1]; |
95 | 95 | |
96 | 96 | while 1: |
97 | | - diagram = sock.recv(2**14) |
| 97 | + diagram, srcaddr = sock.recvfrom(2**14) |
98 | 98 | if not diagram: break |
| 99 | + |
| 100 | + try: |
| 101 | + addresses = addrrules[srcaddr[0]] |
| 102 | + except KeyError: |
| 103 | + addresses = addrrules[0] |
| 104 | + |
99 | 105 | for addr in addresses: |
100 | 106 | try: |
101 | 107 | sock.sendto(diagram, 0, (addr, portnr)) |
— | — | @@ -113,7 +119,7 @@ |
114 | 120 | ip_mreq) |
115 | 121 | |
116 | 122 | def print_help(): |
117 | | - print 'Usage:\n\tudpmcast [ options ] { addresses }\n' |
| 123 | + print 'Usage:\n\tudpmcast [ options ] { addresses | forward rules }\n' |
118 | 124 | print 'Options:' |
119 | 125 | print '\t-d\tFork into the background (become a daemon)' |
120 | 126 | print '\t-p {portnr}\tUDP port number to listen on (default is 4827)' |
— | — | @@ -160,8 +166,20 @@ |
161 | 167 | debug('Joining multicast group ' + multicast_group) |
162 | 168 | join_multicast_group(sock, multicast_group) |
163 | 169 | |
| 170 | + # Parse the argument list |
| 171 | + addrrules = { 0: [] } |
| 172 | + for argument in arguments: |
| 173 | + if argument[0] == '{': |
| 174 | + # Forward rule |
| 175 | + addrrules.update(eval(argument)) |
| 176 | + else: |
| 177 | + # Default forward |
| 178 | + addrrules[0].append(argument) |
| 179 | + |
| 180 | + debug('Forward rules: ' + str(addrrules)) |
| 181 | + |
164 | 182 | # Multiplex everything that comes in |
165 | | - multicast_diagrams(sock, arguments) |
| 183 | + multicast_diagrams(sock, addrrules) |
166 | 184 | except socket.error, msg: |
167 | 185 | print msg[1]; |
168 | 186 | except KeyboardInterrupt: |