-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·204 lines (172 loc) · 5.88 KB
/
Copy pathserver.py
File metadata and controls
executable file
·204 lines (172 loc) · 5.88 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import socket
import signal
import sys
import os
import ctrlpfx
import MySQLdb as mdb
from multiprocessing import Process
import smtplib
import time
from email.mime.text import MIMEText
config = {}
def updateDB_announce(pfx,trans_id):
con = mdb.connect('localhost', db_user_name, db_password, db_name)
with con:
cur = con.cursor()
cur.execute("UPDATE pfx SET TransactionId='"+str(trans_id)+"' where PFX = "+str(pfx))
def updateDB_withdraw(pfx):
con = mdb.connect('localhost', db_user_name, db_password, db_name)
with con:
cur = con.cursor()
cur.execute("UPDATE pfx SET TransactionId=NULL,Availability=1 where PFX = "+str(pfx))
#MUX might include AS numbers to poison
#it will be identified by "."
#multiple AS numbers will be separated by "/"
#if it includes AS number call the poison function in ctrlpfx
#format mux= WISC.73/74
#if 0 is given in ASN then withdraw is sent from that MUX
def Announce(pfx,mux):
print "Prefix Used for annoucement "+str(pfx)
#p = Process(target=ctrlpfx_new.announce, args=(int(pfx), mux))
#p.start()
str1=mux.split(".")
if len(str1)==2:
asn=str1[1].split("/")
mux=str1[0];
poison="";
for i in range(len(asn)):
poison=poison+asn[i]+" "
if len(asn) == 1:
if poison.strip() == "withdraw":
ctrlpfx.withdraw(pfx,mux) #pfx,MUX
elif poison.strip() == "unpoison":
ctrlpfx.unpoison(pfx,mux)
else:
poison=poison+"47065"
print "poisoning " +poison
print "Mux" + mux
ctrlpfx.poison(pfx,mux,poison)
else:
poison=poison+"47065"
print "poisoning " +poison
print "Mux " + mux +","
ctrlpfx.poison(pfx,mux,poison)
else:
ctrlpfx.announce(pfx,mux)
print "Announing "+mux
#getUniqueId();
def signal_handler(signal, frame):
print 'You pressed Ctrl+C!'
clientSocket.close()
sys.exit(0)
def getEmail(tid):
con = mdb.connect('localhost', db_user_name, db_password, db_name)
with con:
cur = con.cursor()
cur.execute("select username from transaction where TrasactionId='"+tid+"'")
username=cur.fetchone()
if username is not None:
cur.execute("select email from users where username='"+username[0]+"'")
if cur is not None:
email=cur.fetchone()
print email[0]
return email[0]
else:
return None
else:
return None
def sendEmail(email,pfx,info):
msg = MIMEText("Beacon: Prefix used 184.164."+str(pfx)+".0 for announcing "+info+"\nAnnoucement was made at "+time.strftime("%H:%M:%S")+"\nThis annoucement will expire in " +str(cycle_time)+" after which it might get overwritten")
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'Beacon Request has been announced'
msg['From'] = 'NSL@usc.edu'
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail('jeyarama@usc.edu', email, msg.as_string())
s.quit()
#Get Avaialble prefixes from Database and returns first avaialble prefix
def getfree_pfx():
con = mdb.connect('localhost', 'testuser', 'test623', 'DR');
with con:
cur = con.cursor()
cur.execute("SELECT pfx FROM pfx where Availability=1")
rows = cur.fetchall()
for row in rows:
#print "returning "+row
cur.execute("UPDATE pfx SET Availability=0 where pfx="+str(row[0]))
return row[0];
#Read the Config file
execfile("config.ini", config)
try:
MUXES=config["MUX"]
db_name=config["db_name"]
db_password=config["db_password"]
db_user_name=config["db_user_name"]
cycle_time=config["cycle_time"]
except:
print "Config values not given."
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#This is where announcer is listening for scheduler.
#Assumption : Announcer and Scheduler will be running the same machine.
#Announcer IP address is hardcoded for this reason
serversocket.bind(('localhost', 8999))
serversocket.listen(5)
print "Ready to Accept"
while True:
# process connections from clients
(clientSocket, address) = serversocket.accept()
while True:
req = clientSocket.recv(2048) # Maximum size of a configuration Message ! Assumption
if not req: break # client closed connection
msg = req.split(" ")
print "msg received from scheduler -->"+req
if msg[0] == 'announce' or msg[0] == 'research':
pfx=getfree_pfx()
mux=msg[1]
trans_id=msg[2]
if pfx is not None:
if trans_id=='Default':
print "Announcing Default Beacon Request"
else:
if msg[0] =='announce':
print "Announcing User Request"
else:
print "Announing Research Request"
muxes=mux.split(",")
for i in range(len(muxes)):
Announce(pfx,muxes[i])
print muxes[i]
updateDB_announce(pfx,trans_id)
if trans_id != "Default":
email=getEmail(trans_id)
if email is not None:
sendEmail(email,pfx,msg[1])
else:
print "No Email found in DB"
else:
print "No prefixes avaialble for advertisement"
print "Request is queued"
#Add to queue <Operation,MUX,PFX,Trans_ID>
if msg[0] == 'withdraw':
print 'withdrawing'
muxes=msg[2].split(",")
#for i in range(len(muxes)):
#if muxes[i] is not None and msg[1] is not None:
#p = Process(target=ctrlpfx_new.withdraw, args=(int(msg[1]),muxes[i]))
#p.start()
#ctrlpfx_new.withdraw(int(msg[1]),muxes[i]) #pfx,MUX
if msg[1] is not None:
try:
updateDB_withdraw(int(msg[1]))
except:
print "handled"
if msg[0] == 'poison':
print 'Poisoning'
if msg[1] is not None and msg[2] is not None:
ctrlpfx.poison(int(msg[1]),msg[2])
#updateDB(int(msg[1]))
clientSocket.close()