View Javadoc
1   // NAME
2   //      $RCSfile: StandardSocket.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 1.13 $
7   // CREATED
8   //      $Date: 2009/03/05 15:56:19 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 2000 - 2006 by Westhawk Ltd
16   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
17   *
18   * Permission to use, copy, modify, and distribute this software
19   * for any purpose and without fee is hereby granted, provided
20   * that the above copyright notices appear in all copies and that
21   * both the copyright notice and this permission notice appear in
22   * supporting documentation.
23   * This software is provided "as is" without express or implied
24   * warranty.
25   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
26   */
27  
28  package uk.co.westhawk.snmp.net;
29  
30  /*-
31   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32   * SNMP Java Client
33   * ჻჻჻჻჻჻
34   * Copyright 2023 MetricsHub, Westhawk
35   * ჻჻჻჻჻჻
36   * This program is free software: you can redistribute it and/or modify
37   * it under the terms of the GNU Lesser General Public License as
38   * published by the Free Software Foundation, either version 3 of the
39   * License, or (at your option) any later version.
40   *
41   * This program is distributed in the hope that it will be useful,
42   * but WITHOUT ANY WARRANTY; without even the implied warranty of
43   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44   * GNU General Lesser Public License for more details.
45   *
46   * You should have received a copy of the GNU General Lesser Public
47   * License along with this program.  If not, see
48   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50   */
51  
52  import java.io.*;
53  import java.net.*;
54  import uk.co.westhawk.snmp.stack.*;
55  
56  /**
57   * This is a wrapper class around the standard DatagramSocket. This
58   * sends packets over UDP, which is standard in SNMP.
59   *
60   * @see DatagramSocket
61   * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
62   * @version $Revision: 1.13 $ $Date: 2009/03/05 15:56:19 $
63   */
64  public class StandardSocket implements ContextSocketFace {
65      static final String version_id = "@(#)$Id: StandardSocket.java,v 1.13 2009/03/05 15:56:19 birgita Exp $ Copyright Westhawk Ltd";
66  
67      private DatagramSocket soc = null;
68      private InetAddress sendToHostAddr;
69      private int sendToHostPort;
70      private InetAddress receiveFromHostAddr;
71      private int receiveFromHostPort;
72      private InetAddress locBindAddr = null;
73  
74      public StandardSocket() {
75      }
76  
77      public void create(int port, String bindAddr) throws IOException {
78          sendToHostPort = port;
79          receiveFromHostPort = sendToHostPort; // initialise (once!)
80          try {
81              locBindAddr = null;
82              if (bindAddr != null) {
83                  locBindAddr = InetAddress.getByName(bindAddr);
84              }
85              soc = new DatagramSocket(sendToHostPort, locBindAddr);
86          } catch (SocketException exc) {
87              String str = "Socket problem: port=" + port + ", bindAddr="
88                      + bindAddr + " " + exc.getMessage();
89              throw (new IOException(str));
90          }
91      }
92  
93      public void create(String host, int port, String bindAddr) throws IOException {
94          sendToHostPort = port;
95          receiveFromHostPort = sendToHostPort; // initialise (once!)
96          try {
97              sendToHostAddr = InetAddress.getByName(host);
98              receiveFromHostAddr = sendToHostAddr; // initialise (once!)
99              locBindAddr = null;
100             if (bindAddr != null) {
101                 locBindAddr = InetAddress.getByName(bindAddr);
102             }
103             InetSocketAddress isa = new InetSocketAddress(locBindAddr, 0);
104             soc = new DatagramSocket(isa);
105         } catch (SocketException exc) {
106             String str = "Socket problem: host=" + host + ", port=" + port
107                     + ", bindAddr=" + bindAddr + " " + exc.getMessage();
108             throw (new IOException(str));
109         } catch (UnknownHostException exc) {
110             String str = "Cannot find host " + host + " " + exc.getMessage();
111             throw (new IOException(str));
112         }
113     }
114 
115     public String getReceivedFromHostAddress() {
116         String res = null;
117         if (receiveFromHostAddr != null) {
118             res = receiveFromHostAddr.getHostAddress();
119         }
120         return res;
121     }
122 
123     public String getSendToHostAddress() {
124         String res = null;
125         if (sendToHostAddr != null) {
126             res = sendToHostAddr.getHostAddress();
127         }
128         return res;
129     }
130 
131     public String getLocalSocketAddress() {
132         String res = null;
133         if (soc != null) {
134             SocketAddress sa = soc.getLocalSocketAddress();
135             if (sa != null) {
136                 res = sa.toString();
137             }
138         }
139         return res;
140     }
141 
142     public String getRemoteSocketAddress() {
143         String res = null;
144         if (soc != null) {
145             SocketAddress sa = soc.getRemoteSocketAddress();
146             if (sa != null) {
147                 res = sa.toString();
148             }
149         }
150         return res;
151     }
152 
153     public StreamPortItem receive(int maxRecvSize) throws IOException {
154         StreamPortItem item = null;
155         if (soc != null) {
156             byte[] data = new byte[maxRecvSize];
157             DatagramPacket p = new DatagramPacket(data, maxRecvSize);
158 
159             // timeout will throw an exception every 1000 secs whilst idle
160             // it is caught and ignored, but as a side effect it loops,
161             // checking 'me'
162             soc.setSoTimeout(1000);
163 
164             soc.receive(p);
165             receiveFromHostAddr = p.getAddress();
166             receiveFromHostPort = p.getPort();
167 
168             ByteArrayInputStream in = null;
169             in = new ByteArrayInputStream(p.getData(), 0, p.getLength());
170             item = new StreamPortItem(receiveFromHostAddr.getHostAddress(),
171                     receiveFromHostPort, in);
172             p = null;
173         }
174         return item;
175     }
176 
177     public void send(byte[] packet) throws IOException {
178         if (soc != null) {
179             DatagramPacket pack = new DatagramPacket(packet, packet.length,
180                     sendToHostAddr, sendToHostPort);
181             soc.send(pack);
182         }
183     }
184 
185     public void close() {
186         if (soc != null) {
187             soc.close();
188             soc = null;
189         }
190     }
191 
192 }