/*
* $Id: NetModule.java,v 1.24 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.net;
import anvil.core.Any;
import anvil.core.Array;
import anvil.script.Context;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.URL;
import sun.net.ftp.FtpClient;
import javax.servlet.http.Cookie;
///
/// @module anvil.net
/// Provides access to network services.
public class NetModule
{
public static final Any ECHO_PORT = Any.create(7);
/// @const FTP_PORT Port of FTP service (21).
public static final Any FTP_PORT = Any.create(21);
/// @const SSH_PORT Port of FTP service (22).
public static final Any SSH_PORT = Any.create(22);
/// @const TELNET_PORT Port of Telnet service (23).
public static final Any TELNET_PORT = Any.create(23);
/// @const SMTP_PORT Port of SMTP service (25).
public static final Any SMTP_PORT = Any.create(25);
public static final Any NAMESERVER_PORT = Any.create(42);
public static final Any WHOIS_PORT = Any.create(43);
public static final Any FINGER_PORT = Any.create(79);
public static final Any HTTP_PORT = Any.create(80);
public static final Any HTTPS_PORT = Any.create(443);
public static final Any NNTP_PORT = Any.create(119);
public static final Any POP2_PORT = Any.create(109);
public static final Any POP3_PORT = Any.create(110);
public static final Any IMAP2_PORT = Any.create(143);
public static final Any IMAP3_PORT = Any.create(220);
public static final Any IRC_PORT = Any.create(194);
public static final Any WHO_PORT = Any.create(513);
public static final Any PRINTER_PORT = Any.create(515);
///
/// @function getLocalHost
/// Returns the address of localhost, or null if it couldn't be resolved.
/// @synopsis InetAddress getLocalHost()
///
public static final Any getLocalHost(Context context)
{
try {
InetAddress addr = InetAddress.getLocalHost();
context.checkConnect(addr.getHostName(), -1);
return new AnyInetAddress(addr);
} catch (UnknownHostException e) {
throw context.exception(e);
}
}
///
/// @function getByName
/// Returns the address of given hostname resolved, or null if it couldn't be resolved.
/// @synopsis InetAddress getByName(string host)
///
public static final Object[] p_getByName = { "host" };
public static final Any getByName(Context context, String host)
{
context.checkConnect(host, -1);
try {
return new AnyInetAddress(InetAddress.getByName(host));
} catch (UnknownHostException e) {
throw context.exception(e);
}
}
///
/// @function getAllByName
/// Returns an array of all addresses of given hostname resolved, or null if resolving failed.
/// @synopsis InetAddress getAllByName(string host)
///
public static final Object[] p_getAllByName = { "host" };
public static final Any getAllByName(Context context, String host)
{
context.checkConnect(host, -1);
try {
InetAddress[] addrs = InetAddress.getAllByName(host);
int n = addrs.length;
Array list = new Array(n);
for(int i=0; i<n; i++) {
list.append(new AnyInetAddress(addrs[i]));
}
return list;
} catch (UnknownHostException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeNamespace __module__ =
new anvil.script.compiler.NativeNamespace(
"net",
NetModule.class,
new Class[] {
AnyCitizen.class,
AnyContext.class,
AnyCookie.class,
AnyFtpClient.class,
AnyInetAddress.class,
AnyRealm.class,
AnyRequest.class,
AnyResponse.class,
AnyServerSocket.class,
AnySession.class,
AnySessionContainer.class,
AnySocket.class,
AnyTribe.class,
AnyURL.class,
AnyURLConnection.class,
anvil.core.Throwables.IOError.UnknownHost.class,
anvil.core.Throwables.IOError.UnknownService.class,
anvil.core.Throwables.IOError.MalformedURL.class,
anvil.core.Throwables.IOError.ProtocolError.class,
anvil.core.Throwables.IOError.SocketError.BindError.class,
anvil.core.Throwables.IOError.SocketError.ConnectError.class,
anvil.core.Throwables.IOError.SocketError.NoRouteToHost.class,
anvil.core.Throwables.IOError.SocketError.class,
},
//DOC{{
""+
"\n" +
" @module anvil.net\n" +
" Provides access to network services.\n" +
" @const FTP_PORT Port of FTP service (21).\n" +
" @const SSH_PORT Port of FTP service (22).\n" +
" @const TELNET_PORT Port of Telnet service (23).\n" +
" @const SMTP_PORT Port of SMTP service (25).\n" +
"\n" +
" @function getLocalHost\n" +
" Returns the address of localhost, or null if it couldn't be resolved.\n" +
" @synopsis InetAddress getLocalHost()\n" +
"\n" +
"\n" +
" @function getByName\n" +
" Returns the address of given hostname resolved, or null if it couldn't be resolved. \n" +
" @synopsis InetAddress getByName(string host)\n" +
"\n" +
"\n" +
" @function getAllByName\n" +
" Returns an array of all addresses of given hostname resolved, or null if resolving failed. \n" +
" @synopsis InetAddress getAllByName(string host)\n" +
"\n"
//}}DOC
);
}