#!/usr/bin/env python # This is Pytecache. See the file README in this directory for information. # Pytecache is Copyright (C) 2000 Tom Goulet. # This program is protected under the terms of the GNU General Public # License version 2, see the file COPYING in this directory. # Pytecache requires a pickled hosts list named 'hosts' to function. import sys # for sys.exit() import string # for string manipulation import pickle # for loading the hosts list from socket import * # for scoket operations def makeguid(): # Create a random message identifier. guid = '' # Initialise as empty string. for i in range(16): # guids are 128 bits, which is 16 bytes. guid = guid + chr(whrandom.randint(0, 255))# return(guid) # Return the random message identifier. def sendping(): # Send a ping out guid = '' # random identifier flag = '\000' # ping ttl = '\004' # default ttl of 4 hops = '\000' # default hops of 0 payloadsize = '\000\000\000\000'# pings have precisely 0 payload guid = makeguid() # Generated random GUID. sendstring = guid+flag+ttl+hops+payloadsize# make a sendstring s.send(sendstring) # Send it print "Sent ping:", `sendstring`#diagnostic return() # Done, now. f = open("hosts", "r") # Open the hosts file hosts = pickle.load(f) # Read it into a variable s = socket(AF_INET, SOCK_STREAM) # Create socket... s.bind('', 6346) # Bind to localhost:6346 print "bound" # diagnostic s.listen(1) # Listen on port print "listening" # diagnostic while 1: # Loop forever print "Main loop."#diagnostic conn, addr = s.accept() # accept a connection print "conn, addr = s.accept()", `conn`, `addr`#diagnostic handshake = conn.recv(64) # receive handshake if not handshake: # It was in the example...I'm copying it print "'if not handshake:' returned true"#diagnostic conn.close() # I assume this is a good idea #break print "got handshake:", `handshake`#diagnostic if handshake == 'GNUTELLA CONNECT/0.4\n\n':#Specification's handshake conn.send('GNUTELLA OK\n\n')# Shake back. print "sent response: GNUTELLA OK\\n\\n"#diagnostic else: #Got something I didn't expect... print "handshake barfed"#diagnostic conn.close() # Close connection #break for i in range(4): # Don't deal with more than four packets print "packet loop"#diagnostic rheader = conn.recv(23) print "header received:",`rheader` if rheader[16] == '\000': print "ping with", guid = rheader[0:16] print "a guid of", `guid` for i in hosts: h = string.split(i[0], '.') host = chr(int(h[0])) + chr(int(h[1])) + chr(int(h[2])) + chr(int(h[3])) port = chr(i[1] % 256) + chr(i[1] / 256) sheader = guid+'\001\004\000\016\000\000\000' print "Sending header:", `sheader` conn.send(sheader) spayload = port+host+'\000\000\000\000\000\000\000\000' print "Sending payload:", `spayload` conn.send(spayload) print "pong flood sent." conn.close() break else: print "header", `rheader[16]`, "is not a ping" pll = ord(rheader[19]) + ord(rheader[20])*256 + ord(rheader[21])*65536 + ord(rheader[22])*16777216 #print "header says payload is this long:", pll rpayload = conn.recv(pll) #print "Received payload:",`payload` print "four packets or pongflood accomplished" conn.close() # EOF