1 /*
2  * Copyright (c) 2000 by Matt Welsh and The Regents of the University of
3  * California. All rights reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation for any purpose, without fee, and without written agreement is
7  * hereby granted, provided that the above copyright notice and the following
8  * two paragraphs appear in all copies of this software.
9  *
10  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20  *
21  * Author: Matt Welsh <mdw@cs.berkeley.edu>
22  *
23  */
24 
25 package seda.sandStorm.lib.Gnutella;
26 
27 import java.util.*;
28 import java.io.*;
29 import java.net.*;
30 
31 /**
32  * A Gnutella network pong packet.
33  */
34 public class GnutellaPongPacket extends GnutellaPacket {
35 
36   private static final boolean DEBUG = false;
37 
38   private int port;
39   private InetAddress address;
40   private int numfiles;
41   private int numkb;
42 
43   /**
44    * Create a pong packet with the given payload.
45    */
GnutellaPongPacket(byte[] payload)46   public GnutellaPongPacket(byte[] payload) throws UnknownHostException {
47     super(GNUTELLA_FN_PONG, payload);
48     if (payload != null) parsePayload();
49   }
50 
51   /**
52    * Create a pong packet with the given GUID, TTL, hops, and payload.
53    */
GnutellaPongPacket(GnutellaGUID guid, int ttl, int hops, byte[] payload)54   public GnutellaPongPacket(GnutellaGUID guid, int ttl, int hops, byte[] payload) throws UnknownHostException {
55     super(guid, GNUTELLA_FN_PONG, ttl, hops, payload);
56     if (payload != null) parsePayload();
57   }
58 
59   /**
60    * Create a pong packet with the given numfiles and numkb, with the
61    * default port and local host address.
62    */
GnutellaPongPacket(GnutellaGUID guid, int numfiles, int numkb)63   public GnutellaPongPacket(GnutellaGUID guid, int numfiles, int numkb) throws UnknownHostException {
64     this(guid, DEFAULT_GNUTELLA_PORT, InetAddress.getLocalHost(), numfiles, numkb);
65   }
66 
67   /**
68    * Create a pong packet with the given port, address, numfiles and numkb.
69    */
GnutellaPongPacket(GnutellaGUID guid, int port, InetAddress address, int numfiles, int numkb)70   public GnutellaPongPacket(GnutellaGUID guid, int port, InetAddress address, int numfiles, int numkb) throws UnknownHostException {
71     super(guid, GNUTELLA_FN_PONG, null);
72     this.port = port;
73     this.address = address;
74     this.numfiles = numfiles;
75     this.numkb = numkb;
76   }
77 
toString()78   public String toString() {
79     return "GnutellaPongPacket "+guid.toString()+" ["+address.getHostAddress()+":"+port+" - "+numfiles+" files/"+numkb+" KB]";
80   }
81 
prepareForSend()82   protected void prepareForSend() {
83     payload = new byte[14];
84 
85     writeLEShort(((short)port & 0xffff), payload, 0);
86     byte addr[] = address.getAddress();
87     payload[2] = addr[0];
88     payload[3] = addr[1];
89     payload[4] = addr[2];
90     payload[5] = addr[3];
91 
92     writeLEInt(numfiles, payload, 6);
93     writeLEInt(numkb, payload, 10);
94   }
95 
parsePayload()96   private void parsePayload() throws UnknownHostException {
97     port = (int)readLEShort(payload, 0);
98     String addr = (int)(payload[2] & 0xff) +"."+ (int)(payload[3] & 0xff) +"."+ (int)(payload[4] & 0xff) +"."+ (int)(payload[5] & 0xff);
99     address = InetAddress.getByName(addr);
100 
101     numfiles = readLEInt(payload, 6);
102     numkb = readLEInt(payload, 10);
103   }
104 
105   /**
106    * Return the address represented by this packet.
107    */
getInetAddress()108   public InetAddress getInetAddress() {
109     return address;
110   }
111 
112   /**
113    * Return the port represented by this packet.
114    */
getPort()115   public int getPort() {
116     return port;
117   }
118 
119   /**
120    * Return a string "host:port" represented by this packet.
121    */
getHost()122   public String getHost() {
123     return address.getHostAddress()+":"+port;
124   }
125 
126   /**
127    * Return the number of files shared by the machine from which this
128    * packet originated.
129    */
getNumFiles()130   public int getNumFiles() {
131     return numfiles;
132   }
133 
134   /**
135    * Return the number of kilobytes of files shared by the machine from
136    * which this packet originated.
137    */
getNumKB()138   public int getNumKB() {
139     return numkb;
140   }
141 
142 }
143 
144