001: /*
002: * Copyright (c) 2000 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.sandStorm.lib.Gnutella;
026:
027: import java.util.*;
028: import java.io.*;
029: import java.net.*;
030:
031: /**
032: * A Gnutella network pong packet.
033: */
034: public class GnutellaPongPacket extends GnutellaPacket {
035:
036: private static final boolean DEBUG = false;
037:
038: private int port;
039: private InetAddress address;
040: private int numfiles;
041: private int numkb;
042:
043: /**
044: * Create a pong packet with the given payload.
045: */
046: public GnutellaPongPacket(byte[] payload)
047: throws UnknownHostException {
048: super (GNUTELLA_FN_PONG, payload);
049: if (payload != null)
050: parsePayload();
051: }
052:
053: /**
054: * Create a pong packet with the given GUID, TTL, hops, and payload.
055: */
056: public GnutellaPongPacket(GnutellaGUID guid, int ttl, int hops,
057: byte[] payload) throws UnknownHostException {
058: super (guid, GNUTELLA_FN_PONG, ttl, hops, payload);
059: if (payload != null)
060: parsePayload();
061: }
062:
063: /**
064: * Create a pong packet with the given numfiles and numkb, with the
065: * default port and local host address.
066: */
067: public GnutellaPongPacket(GnutellaGUID guid, int numfiles, int numkb)
068: throws UnknownHostException {
069: this (guid, DEFAULT_GNUTELLA_PORT, InetAddress.getLocalHost(),
070: numfiles, numkb);
071: }
072:
073: /**
074: * Create a pong packet with the given port, address, numfiles and numkb.
075: */
076: public GnutellaPongPacket(GnutellaGUID guid, int port,
077: InetAddress address, int numfiles, int numkb)
078: throws UnknownHostException {
079: super (guid, GNUTELLA_FN_PONG, null);
080: this .port = port;
081: this .address = address;
082: this .numfiles = numfiles;
083: this .numkb = numkb;
084: }
085:
086: public String toString() {
087: return "GnutellaPongPacket " + guid.toString() + " ["
088: + address.getHostAddress() + ":" + port + " - "
089: + numfiles + " files/" + numkb + " KB]";
090: }
091:
092: protected void prepareForSend() {
093: payload = new byte[14];
094:
095: writeLEShort(((short) port & 0xffff), payload, 0);
096: byte addr[] = address.getAddress();
097: payload[2] = addr[0];
098: payload[3] = addr[1];
099: payload[4] = addr[2];
100: payload[5] = addr[3];
101:
102: writeLEInt(numfiles, payload, 6);
103: writeLEInt(numkb, payload, 10);
104: }
105:
106: private void parsePayload() throws UnknownHostException {
107: port = (int) readLEShort(payload, 0);
108: String addr = (int) (payload[2] & 0xff) + "."
109: + (int) (payload[3] & 0xff) + "."
110: + (int) (payload[4] & 0xff) + "."
111: + (int) (payload[5] & 0xff);
112: address = InetAddress.getByName(addr);
113:
114: numfiles = readLEInt(payload, 6);
115: numkb = readLEInt(payload, 10);
116: }
117:
118: /**
119: * Return the address represented by this packet.
120: */
121: public InetAddress getInetAddress() {
122: return address;
123: }
124:
125: /**
126: * Return the port represented by this packet.
127: */
128: public int getPort() {
129: return port;
130: }
131:
132: /**
133: * Return a string "host:port" represented by this packet.
134: */
135: public String getHost() {
136: return address.getHostAddress() + ":" + port;
137: }
138:
139: /**
140: * Return the number of files shared by the machine from which this
141: * packet originated.
142: */
143: public int getNumFiles() {
144: return numfiles;
145: }
146:
147: /**
148: * Return the number of kilobytes of files shared by the machine from
149: * which this packet originated.
150: */
151: public int getNumKB() {
152: return numkb;
153: }
154:
155: }
|