-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpmcastserver.py
125 lines (97 loc) · 3.38 KB
/
udpmcastserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# udpmcastserver.py
# UDP Multicast Server for use OA CoT testing
# Bobby Krupczak
# CC0 1.0
# https://creativecommons.org/publicdomain/zero/1.0/deed.en
import os, socket, sys, getopt, select
# socket list for selecting
inputs = [ ]
# ----------------------------------------------------
# receive a message as stream of chars until \n\n
# caller can strip the \n\n
# or end of data? XXX
def recvMessage(sock):
buf = b''
bufStr = ""
while True:
(newbuf,fromAddress) = sock.recvfrom(2048)
if not newbuf:
return (None,None)
buf += newbuf
bufStr += buf.decode()
if bufStr.find('\n\n') >= 0:
break;
if bufStr.find('\r\n\r\n') >= 0:
break;
# print "recvMessage received ",len(buf)," bytes"
return (bufStr,fromAddress)
# receive what can be had from a single
# call to recv for use with testing Cursor on Target (CoT) messages
#
def recvSingleMessage(sock):
buf = b''
bufStr = ""
(newbuf,fromAddress) = sock.recvfrom(2048)
if not newbuf:
return (None,None)
buf += newbuf
bufStr += buf.decode()
# print "recvMessage received ",len(buf)," bytes"
return (bufStr,fromAddress)
# ----------------------------------------------------
# process a message by receiving then echoing back
# to sender
def processMessage(sock):
try:
(data,fromAddress) = recvSingleMessage(sock)
if data is None:
return -1
data = data.rstrip('\n')
print("processMessage: received from ",fromAddress);
print("processMessage: received: '",data);
reply = data+"\n\n"
reply = reply.encode()
sock.sendto(reply,fromAddress)
return 1
except:
print("processMessage: some sort of error")
return -1
# ----------------------------------------------------
# main
if len(sys.argv) != 3:
print("Usage: udpmcastserver <listen-ip> <portno> ")
sys.exit(-1)
print("Listen ip is "+sys.argv[1])
print("Port is "+sys.argv[2])
serverSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
serverSock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
serverSock.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_TTL,4)
serverSock.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_LOOP,1)
serverAddress = ('239.2.3.1',int(sys.argv[2]))
serverSock.bind(serverAddress)
# set the mcast interface
serverSock.setsockopt(socket.SOL_IP,socket.IP_MULTICAST_IF,socket.inet_aton(sys.argv[1]))
# what ip address/interface do we want to add membership
serverSock.setsockopt(socket.SOL_IP,socket.IP_ADD_MEMBERSHIP, socket.inet_aton('239.2.3.1') + socket.inet_aton(sys.argv[1]))
inputs.append(serverSock)
while True:
print("Going to select over list of sockets . . .",len(inputs))
try:
readable, writable, exceptional = select.select(inputs,[],inputs)
for s in exceptional:
print("select exception with ",s)
# if s != serverSock:
# inputs.remove(s)
# s.close(s)
for s in readable:
try:
processMessage(s)
sys.stdout.flush()
sys.stderr.flush()
except Exception as e:
print("Error reading from server socket")
print(e)
except Exception as e:
print("Server select error . . . .")
print(e)