def rcvlet(mirror, rxgobj): sock = mirror['info']['sock'] # first, check if params were received correctly if session.recvmessage(sock) != b'PARAMS OK': raise Exception("Params were not delivered correctly or wrong format.") data = "0" first = True while data != '' and len(mirror['blocksrequested']) > 0 or first: first = False data = session.recvmessage(sock) rxgobj.notify_success(mirror['info'], data)
def _remote_query_helper_sock(socket, command): # issue the relevant command session.sendmessage(socket, command) # receive and return the answer rawanswer = session.recvmessage(socket) return rawanswer
def _remote_query_helper(serverlocation, command, defaultserverport): # private function that contains the guts of server communication. It # issues a single query and then closes the connection. This is used # both to talk to the vendor and also to talk to mirrors if type(serverlocation) != str and type(serverlocation) != unicode: raise TypeError("Server location must be a string, not "+str(type(serverlocation))) # now let's split it and ensure there are 0 or 1 colons splitlocationlist = serverlocation.split(':') if len(splitlocationlist) >2: raise TypeError("Server location may not contain more than one colon") # now either set the port or use the default if len(splitlocationlist) == 2: serverport = int(splitlocationlist[1]) else: serverport = defaultserverport # check that this port is in the right range if serverport <= 0 or serverport > 65535: raise TypeError("Server location's port is not in the allowed range") serverhostname = splitlocationlist[0] # now we actually download the information... # first open the socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # if serverport == 60443, use SSL if (serverport == 60443): ssl_serversocket = ssl.wrap_socket(serversocket, server_side=False, ca_certs="CA_Certs/ssl.crt", ssl_version=ssl.PROTOCOL_TLSv1, cert_reqs=ssl.CERT_REQUIRED) ssl_serversocket.connect((serverhostname, serverport)) #session.sendmessage(ssl_serversocket, command) ssl_serversocket.sendall(command) #rawanswer = session.recvmessage(ssl_serversocket) rawanswer = ssl_serversocket.recv(4096) ssl_serversocket.close() return rawanswer # else connect normally serversocket.connect((serverhostname, serverport)) # then issue the relevant command session.sendmessage(serversocket, command) # and return the answer rawanswer = session.recvmessage(serversocket) serversocket.close() return rawanswer
def cleanup(self): """cleanup. here: maybe request debug timing info and always close sockets""" for mirror in self.activemirrors: if self.timing: # request total computation time and measure delay ping_start = _timer() session.sendmessage(mirror['info']['sock'], "T") mirror['info']['comptime'] = float(session.recvmessage(mirror['info']['sock'])[1:]) mirror['info']['ping'] = _timer() - ping_start session.sendmessage(mirror['info']['sock'], "Q") mirror['info']['sock'].close()
def handle(self): # read the request from the socket... requeststring = session.recvmessage(self.request) # for logging purposes, get the remote info remoteip, remoteport = self.request.getpeername() # if it's a request for a XORBLOCK if requeststring.startswith('XORBLOCK'): bitstring = requeststring[len('XORBLOCK'):] expectedbitstringlength = uppirlib.compute_bitstring_length(_global_myxordatastore.numberofblocks) if len(bitstring) != expectedbitstringlength: # Invalid request length... _log("UPPIR "+remoteip+" "+str(remoteport)+" Invalid request with length: "+str(len(bitstring))) session.sendmessage(self.request, 'Invalid request length') return # Now let's process this... xoranswer = _global_myxordatastore.produce_xor_from_bitstring(bitstring) # and send the reply. session.sendmessage(self.request, xoranswer) _log("UPPIR "+remoteip+" "+str(remoteport)+" GOOD") # done! return elif requeststring == 'HELLO': # send a reply. session.sendmessage(self.request, "HI!") _log("UPPIR "+remoteip+" "+str(remoteport)+" HI!") # done! return else: # we don't know what this is! Log and tell the requestor _log("UPPIR "+remoteip+" "+str(remoteport)+" Invalid request type starts:'"+requeststring[:5]+"'") session.sendmessage(self.request, 'Invalid request type') return
def handle(self): # read the request from the socket... requeststring = session.recvmessage(self.request) # for logging purposes, get the remote info remoteip, remoteport = self.request.getpeername() # if it's a request for a XORBLOCK if requeststring == 'GET MANIFEST': session.sendmessage(self.request, _global_rawmanifestdata) _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" manifest request") # done! return elif requeststring == 'GET MIRRORLIST': # let's try to clean up the list. If we are busy with another attempt # to do this, the latter will be a NOOP _check_for_expired_mirrorinfo() # reply with the mirror list session.sendmessage(self.request, _global_rawmirrorlist) _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" mirrorlist request") # done! return elif requeststring.startswith('RUN TEST'): if random.random() < RANDOM_THRESHOLD: return testrawdata = requeststring[len('RUN TEST'):] try: testinfodict = json.loads(testrawdata) except (TypeError, ValueError), e: session.sendmessage(self.request, "Error cannot deserialize testinfo!") _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" cannot deserialize testinfo!"+str(e)) return if type(testinfodict) != dict or 'ip' not in testinfodict or 'port' not in testinfodict or 'data' not in testinfodict or 'chunklist' not in testinfodict: session.sendmessage(self.request, "Error, testinfodict has an invalid format.") _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" testinfodict has an invalid format") return _testmirror(self, testinfodict)
def _remote_query_helper(serverlocation, command, defaultserverport): # private function that contains the guts of server communication. It # issues a single query and then closes the connection. This is used # both to talk to the vendor and also to talk to mirrors if type(serverlocation) != str and type(serverlocation) != str: raise TypeError("Server location must be a string, not " + str(type(serverlocation))) # now let's split it and ensure there are 0 or 1 colons splitlocationlist = serverlocation.split(':') if len(splitlocationlist) > 2: raise TypeError("Server location may not contain more than one colon") # now either set the port or use the default if len(splitlocationlist) == 2: serverport = int(splitlocationlist[1]) else: serverport = defaultserverport # check that this port is in the right range if serverport <= 0 or serverport > 65535: raise TypeError("Server location's port is not in the allowed range") serverhostname = splitlocationlist[0] # now we actually download the information... # first open the socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.connect((serverhostname, serverport)) # then issue the relevant command session.sendmessage(serversocket, command) # and return the answer rawanswer = session.recvmessage(serversocket) serversocket.close() return rawanswer
def handle(self): global _batchrequests global _xorstrings global _finish global _batch_comp_time global _global_myxordatastore global _global_manifestdict global _request_restart _finish = False comp_time = 0 _batch_comp_time = 0 _batchrequests = 0 _xorstrings = b'' parallel = False requeststring = b'0' while requeststring != b'Q': # read the request from the socket... requeststring = session.recvmessage(self.request) # for logging purposes, get the remote info # remoteip, remoteport = self.request.getpeername() start_time = _timer() # if it's a request for a XORBLOCK if requeststring.startswith(b'X'): bitstring = requeststring[len(b'X'):] expectedbitstringlength = lib.bits_to_bytes(_global_myxordatastore.numberofblocks) if len(bitstring) != expectedbitstringlength: # Invalid request length... #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" Invalid request with length: "+str(len(bitstring))) session.sendmessage(self.request, 'Invalid request length') _finish = True _batchevent.set() return if not batch: # Now let's process this... xoranswer = _global_myxordatastore.produce_xor_from_bitstring(bitstring) comp_time = comp_time + _timer() - start_time # and immediately send the reply. session.sendmessage(self.request, xoranswer) #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") else: with _batchlock: _xorstrings += bitstring _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() # done! elif requeststring.startswith(b'C'): payload = requeststring[len(b'C'):] chunks = msgpack.unpackb(payload, raw=False) bitstring = lib.build_bitstring_from_chunks(chunks, k, chunklen, lastchunklen) if not batch: # Now let's process this... xoranswer = _global_myxordatastore.produce_xor_from_bitstring(bitstring) comp_time = comp_time + _timer() - start_time # and send the reply. session.sendmessage(self.request, xoranswer) #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") else: with _batchlock: _xorstrings += bitstring _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() #done! elif requeststring.startswith(b'R'): payload = requeststring[len(b'R'):] chunks = msgpack.unpackb(payload, raw=False) #iterate through r-1 random chunks for c in chunknumbers[1:]: if c == k - 1: length = lastchunklen else: length = chunklen chunks[c] = lib.nextrandombitsAES(cipher, length) bitstring = lib.build_bitstring_from_chunks(chunks, k, chunklen, lastchunklen) #the expanded query if not batch: # Now let's process this... xoranswer = _global_myxordatastore.produce_xor_from_bitstring(bitstring) comp_time = comp_time + _timer() - start_time # and send the reply. session.sendmessage(self.request, xoranswer) #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") else: with _batchlock: _xorstrings += bitstring _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() #done! elif requeststring == b'MANIFEST UPDATE': print("MANIFEST UPDATE") _request_restart = True elif requeststring.startswith(b'M'): parallel = True payload = requeststring[len(b'M'):] chunks = msgpack.unpackb(payload, raw=False) #iterate through r-1 random chunks for c in chunknumbers[1:]: if c == k - 1: length = lastchunklen else: length = chunklen chunks[c] = lib.nextrandombitsAES(cipher, length) bitstrings = lib.build_bitstring_from_chunks_parallel(chunks, k, chunklen, lastchunklen) #the expanded query if not batch: result = {} for c in chunknumbers: result[c] = _global_myxordatastore.produce_xor_from_bitstring(bitstrings[c]) comp_time = comp_time + _timer() - start_time # and send the reply. session.sendmessage(self.request, msgpack.packb(result, use_bin_type=True)) else: with _batchlock: for c in chunknumbers: _xorstrings += bitstrings[c] _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") #done! elif requeststring.startswith(b'P'): payload = requeststring[len(b'P'):] params = msgpack.unpackb(payload, raw=False) chunknumbers = params['cn'] k = params['k'] r = params['r'] chunklen = params['cl'] lastchunklen = params['lcl'] batch = params['b'] parallel = params['p'] if 's' in params: cipher = lib.initAES(params['s']) if batch: # create batch xor thread t = threading.Thread(target=BatchAnswer, args=[parallel, chunknumbers, self.request], name="RAID-PIR Batch XOR") t.daemon = True t.start() # and send the reply. session.sendmessage(self.request, b"PARAMS OK") #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" PARAMS received " + str(params)) #done! #Timing Request elif requeststring == b'T': session.sendmessage(self.request, b"T" + str(comp_time + _batch_comp_time)) comp_time = 0 _batch_comp_time = 0 #Debug Hello elif requeststring == b'HELLO': session.sendmessage(self.request, b"HI!") #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" HI!") # done! #the client asked to close the connection elif requeststring == b'Q': comp_time = 0 _finish = True _batchevent.set() return #this happens if the client closed the socket unexpectedly elif requeststring == b'': comp_time = 0 _finish = True _batchevent.set() return else: # we don't know what this is! Log and tell the requestor #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" Invalid request type starts:'"+requeststring[:5]+"'") session.sendmessage(self.request, 'Invalid request type') _finish = True _batchevent.set() return
def handle(self): # read the request from the socket... requeststring = session.recvmessage(self.request) # for logging purposes, get the remote info remoteip, remoteport = self.request.getpeername() # if it's a request for a XORBLOCK if requeststring == 'GET MANIFEST': session.sendmessage(self.request, _global_rawmanifestdata) _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" manifest request") # done! return elif requeststring == 'GET MIRRORLIST': # let's try to clean up the list. If we are busy with another attempt # to do this, the latter will be a NOOP _check_for_expired_mirrorinfo() # reply with the mirror list session.sendmessage(self.request, _global_rawmirrorlist) _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" mirrorlist request") # done! return elif requeststring.startswith('MIRRORADVERTISE'): # This is a mirror telling us it's ready to serve clients. mirrorrawdata = requeststring[len('MIRRORADVERTISE'):] # handle the case where the mirror provides data that is larger than # we want to serve if len(mirrorrawdata) > _commandlineoptions.maxmirrorinfo: session.sendmessage(self.request, "Error, mirrorinfo too large!") _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" mirrorinfo too large: "+str(len(mirrorrawdata))) return # Let's sanity check the data... # can we deserialize it? try: mirrorinfodict = json.loads(mirrorrawdata) except (TypeError, ValueError), e: session.sendmessage(self.request, "Error cannot deserialize mirrorinfo!") _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" cannot deserialize mirrorinfo!"+str(e)) return # is it a dictionary and does it have the required keys? if type(mirrorinfodict) != dict or 'ip' not in mirrorinfodict or 'port' not in mirrorinfodict: session.sendmessage(self.request, "Error, mirrorinfo has an invalid format.") _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" mirrorinfo has an invalid format") return # is it a dictionary and does it have the required keys? if mirrorinfodict['ip'] != remoteip: session.sendmessage(self.request, "Error, must provide mirrorinfo from the mirror's IP") _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" mirrorinfo provided from the wrong IP") return # add the information to the mirrorlist _add_mirrorinfo_to_list(mirrorinfodict) # and notify the user session.sendmessage(self.request, 'OK') _log("UPPIRVendor "+remoteip+" "+str(remoteport)+" mirrorinfo update "+str(len(mirrorrawdata))) # done! return
def handle(self): global _batchrequests global _xorstrings global _finish global _batch_comp_time global _global_myxordatastore global _global_manifestdict global _request_restart _finish = False comp_time = 0 _batch_comp_time = 0 _batchrequests = 0 _xorstrings = b'' parallel = False requeststring = b'0' while requeststring != b'Q': # read the request from the socket... requeststring = session.recvmessage(self.request) # for logging purposes, get the remote info # remoteip, remoteport = self.request.getpeername() start_time = _timer() # if it's a request for a XORBLOCK if requeststring.startswith(b'X'): bitstring = requeststring[len(b'X'):] expectedbitstringlength = lib.bits_to_bytes( _global_myxordatastore.numberofblocks) if len(bitstring) != expectedbitstringlength: # Invalid request length... #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" Invalid request with length: "+str(len(bitstring))) session.sendmessage(self.request, 'Invalid request length') _finish = True _batchevent.set() return if not batch: # Now let's process this... xoranswer = _global_myxordatastore.produce_xor_from_bitstring( bitstring) comp_time = comp_time + _timer() - start_time # and immediately send the reply. session.sendmessage(self.request, xoranswer) #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") else: with _batchlock: _xorstrings += bitstring _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() # done! elif requeststring.startswith(b'C'): payload = requeststring[len(b'C'):] chunks = msgpack.unpackb(payload, raw=False) bitstring = lib.build_bitstring_from_chunks( chunks, k, chunklen, lastchunklen) if not batch: # Now let's process this... xoranswer = _global_myxordatastore.produce_xor_from_bitstring( bitstring) comp_time = comp_time + _timer() - start_time # and send the reply. session.sendmessage(self.request, xoranswer) #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") else: with _batchlock: _xorstrings += bitstring _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() #done! elif requeststring.startswith(b'R'): payload = requeststring[len(b'R'):] chunks = msgpack.unpackb(payload, raw=False) #iterate through r-1 random chunks for c in chunknumbers[1:]: if c == k - 1: length = lastchunklen else: length = chunklen chunks[c] = lib.nextrandombitsAES(cipher, length) bitstring = lib.build_bitstring_from_chunks( chunks, k, chunklen, lastchunklen) #the expanded query if not batch: # Now let's process this... xoranswer = _global_myxordatastore.produce_xor_from_bitstring( bitstring) comp_time = comp_time + _timer() - start_time # and send the reply. session.sendmessage(self.request, xoranswer) #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") else: with _batchlock: _xorstrings += bitstring _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() #done! elif requeststring == b'MANIFEST UPDATE': print("MANIFEST UPDATE") _request_restart = True elif requeststring.startswith(b'M'): parallel = True payload = requeststring[len(b'M'):] chunks = msgpack.unpackb(payload, raw=False) #iterate through r-1 random chunks for c in chunknumbers[1:]: if c == k - 1: length = lastchunklen else: length = chunklen chunks[c] = lib.nextrandombitsAES(cipher, length) bitstrings = lib.build_bitstring_from_chunks_parallel( chunks, k, chunklen, lastchunklen) #the expanded query if not batch: result = {} for c in chunknumbers: result[ c] = _global_myxordatastore.produce_xor_from_bitstring( bitstrings[c]) comp_time = comp_time + _timer() - start_time # and send the reply. session.sendmessage( self.request, msgpack.packb(result, use_bin_type=True)) else: with _batchlock: for c in chunknumbers: _xorstrings += bitstrings[c] _batchrequests = _batchrequests + 1 # notify batch thread _batchevent.set() #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" GOOD") #done! elif requeststring.startswith(b'P'): payload = requeststring[len(b'P'):] params = msgpack.unpackb(payload, raw=False) chunknumbers = params['cn'] k = params['k'] r = params['r'] chunklen = params['cl'] lastchunklen = params['lcl'] batch = params['b'] parallel = params['p'] if 's' in params: cipher = lib.initAES(params['s']) if batch: # create batch xor thread t = threading.Thread( target=BatchAnswer, args=[parallel, chunknumbers, self.request], name="RAID-PIR Batch XOR") t.daemon = True t.start() # and send the reply. session.sendmessage(self.request, b"PARAMS OK") #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" PARAMS received " + str(params)) #done! #Timing Request elif requeststring == b'T': session.sendmessage(self.request, b"T" + str(comp_time + _batch_comp_time)) comp_time = 0 _batch_comp_time = 0 #Debug Hello elif requeststring == b'HELLO': session.sendmessage(self.request, b"HI!") #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" HI!") # done! #the client asked to close the connection elif requeststring == b'Q': comp_time = 0 _finish = True _batchevent.set() return #this happens if the client closed the socket unexpectedly elif requeststring == b'': comp_time = 0 _finish = True _batchevent.set() return else: # we don't know what this is! Log and tell the requestor #_log("RAID-PIR "+remoteip+" "+str(remoteport)+" Invalid request type starts:'"+requeststring[:5]+"'") session.sendmessage(self.request, 'Invalid request type') _finish = True _batchevent.set() return
def get_response(requeststring): s = socket.socket() s.connect((mirrortocheck,62294)) session.sendmessage(s,requeststring) return session.recvmessage(s)
def handle(self): # read the request from the socket... requeststring = session.recvmessage(self.request) # for logging purposes, get the remote info remoteip, remoteport = self.request.getpeername() # if it's a request for a XORBLOCK if requeststring == b'GET MANIFEST': print("GET MANIFEST") rawmanifestdata = open(_commandlineoptions.manifestfilename, 'rb').read() _global_rawmanifestdata = rawmanifestdata session.sendmessage(self.request, _global_rawmanifestdata) _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " manifest request") # done! return elif requeststring == b'MANIFEST UPDATE': print('MANIFEST UPDATE') rawmanifestdata = open(_commandlineoptions.manifestfilename, 'rb').read() _global_rawmanifestdata = rawmanifestdata # get a copy of the mirrorlist mirrorlist = list() _global_mirrorinfolock.acquire() try: for mirror in _global_mirrorinfodict: mirrorlist.append( _global_mirrorinfodict[mirror]['mirrorinfo']) finally: _global_mirrorinfolock.release() for mirror in mirrorlist: sock = None try: # Connect to server and send data sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((mirror['ip'], mirror['port'])) session.sendmessage(sock, 'MANIFEST UPDATE') except: print("Could not connect to mirror", mirror) pass finally: sock.close() elif requeststring == b'GET MIRRORLIST': # let's try to clean up the list. If we are busy with another attempt # to do this, the latter will be a NOOP _check_for_expired_mirrorinfo() # reply with the mirror list session.sendmessage(self.request, _global_rawmirrorlist) _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " mirrorlist request") # done! return elif requeststring.startswith(b'MIRRORADVERTISE'): # This is a mirror telling us it's ready to serve clients. _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " mirror advertise") mirrorrawdata = requeststring[len(b'MIRRORADVERTISE'):] # handle the case where the mirror provides data that is larger than # we want to serve if len(mirrorrawdata) > _commandlineoptions.maxmirrorinfo: session.sendmessage(self.request, "Error, mirrorinfo too large!") _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " mirrorinfo too large: " + str(len(mirrorrawdata))) return # Let's sanity check the data... # can we unpack it? try: mirrorinfodict = msgpack.unpackb(mirrorrawdata, raw=False) except (TypeError, ValueError) as e: session.sendmessage(self.request, "Error cannot unpack mirrorinfo!") _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " cannot unpack mirrorinfo!" + str(e)) return # is it a dictionary and does it have the required keys? if type( mirrorinfodict ) != dict or 'ip' not in mirrorinfodict or 'port' not in mirrorinfodict: session.sendmessage( self.request, "Error, mirrorinfo has an invalid format.") _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " mirrorinfo has an invalid format") return #is the mirror to add coming from the same ip? if _commandlineoptions.checkmirrorip: if mirrorinfodict['ip'] != remoteip: session.sendmessage( self.request, "Error, must provide mirrorinfo from the mirror's IP") _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " mirrorinfo provided from the wrong IP") return # add the information to the mirrorlist _add_mirrorinfo_to_list(mirrorinfodict) # and notify the user session.sendmessage(self.request, 'OK') _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " mirrorinfo update " + str(len(mirrorrawdata))) # done! return # add HELLO elif requeststring == b'HELLO': # send a reply. session.sendmessage(self.request, "VENDORHI!") _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " VENDORHI!") # done! return else: # we don't know what this is! Log and tell the requestor _log("RAID-PIR Vendor " + remoteip + " " + str(remoteport) + " Invalid request type starts:'" + requeststring[:5] + "'") session.sendmessage(self.request, 'Invalid request type') return
def handle(self): # read the request from the socket... requeststring = session.recvmessage(self.request) # for logging purposes, get the remote info remoteip, remoteport = self.request.getpeername() # if it's a request for a XORBLOCK if requeststring == 'GET MANIFEST': session.sendmessage(self.request, _global_rawmanifestdata) _log("UPPIRVendor " + remoteip + " " + str(remoteport) + " manifest request") # done! return elif requeststring == 'GET MIRRORLIST': # let's try to clean up the list. If we are busy with another attempt # to do this, the latter will be a NOOP _check_for_expired_mirrorinfo() # reply with the mirror list session.sendmessage(self.request, _global_rawmirrorlist) _log("UPPIRVendor " + remoteip + " " + str(remoteport) + " mirrorlist request") # done! return elif requeststring.startswith('MIRRORADVERTISE'): # This is a mirror telling us it's ready to serve clients. mirrorrawdata = requeststring[len('MIRRORADVERTISE'):] # handle the case where the mirror provides data that is larger than # we want to serve if len(mirrorrawdata) > _commandlineoptions.maxmirrorinfo: session.sendmessage(self.request, "Error, mirrorinfo too large!") _log("UPPIRVendor " + remoteip + " " + str(remoteport) + " mirrorinfo too large: " + str(len(mirrorrawdata))) return # Let's sanity check the data... # can we deserialize it? try: mirrorinfodict = json.loads(mirrorrawdata) except (TypeError, ValueError), e: session.sendmessage(self.request, "Error cannot deserialize mirrorinfo!") _log("UPPIRVendor " + remoteip + " " + str(remoteport) + " cannot deserialize mirrorinfo!" + str(e)) return # is it a dictionary and does it have the required keys? if type( mirrorinfodict ) != dict or 'ip' not in mirrorinfodict or 'port' not in mirrorinfodict: session.sendmessage( self.request, "Error, mirrorinfo has an invalid format.") _log("UPPIRVendor " + remoteip + " " + str(remoteport) + " mirrorinfo has an invalid format") return # is it a dictionary and does it have the required keys? if mirrorinfodict['ip'] != remoteip: session.sendmessage( self.request, "Error, must provide mirrorinfo from the mirror's IP") _log("UPPIRVendor " + remoteip + " " + str(remoteport) + " mirrorinfo provided from the wrong IP") return # add the information to the mirrorlist _add_mirrorinfo_to_list(mirrorinfodict) # and notify the user session.sendmessage(self.request, 'OK') _log("UPPIRVendor " + remoteip + " " + str(remoteport) + " mirrorinfo update " + str(len(mirrorrawdata))) # done! return
def get_response(requeststring): s = socket.socket() s.connect((mirrortocheck, 62294)) session.sendmessage(s, requeststring) return session.recvmessage(s)