Example usage for org.apache.commons.net.tftp TFTPClient open

List of usage examples for org.apache.commons.net.tftp TFTPClient open

Introduction

In this page you can find the example usage for org.apache.commons.net.tftp TFTPClient open.

Prototype

public void open() throws SocketException 

Source Link

Document

Opens a DatagramSocket on the local host at the first available port.

Usage

From source file:org.sipfoundry.preflight.TFTP.java

public ResultCode validate(int timeout, NetworkResources networkResources, JournalService journalService,
        InetAddress bindAddress) {
    ResultCode results = NONE;/*from w w w.  j a v  a2s .  c o m*/
    InetAddress tftpServerAddress = null;
    String testFile = new String("00D01EFFFFFE");
    String[] verificationStrings = { "LIP-68XX configuration information", "[VOIP]", "outbound_proxy_server",
            "[PROVISION]", "decrypt_key" };

    if (networkResources.configServer == null) {
        journalService.println("No TFTP server provided, skipping test.\n");
        return CONFIG_SERVER_MISSING;
    }

    journalService.println("Starting TFTP server test.");

    TFTPClient tftp = new TFTPClient();

    tftp.setDefaultTimeout(timeout * 1000);

    if (IPAddressUtil.isLiteralIPAddress(networkResources.configServer)) {
        try {
            tftpServerAddress = InetAddress.getByName(networkResources.configServer);
        } catch (UnknownHostException e) {
            // Should never get here.
            e.printStackTrace();
        }
        journalService.println("Using TFTP server literal address: " + networkResources.configServer);
    } else {
        // Try to retrieve A RECORD for TFTP server, checking each DNS server.
        SimpleResolver resolver = null;
        try {
            resolver = new SimpleResolver();
            resolver.setTimeout(timeout);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        for (InetAddress dnsServer : networkResources.domainNameServers) {
            journalService.println(
                    "Looking up TFTP server address via DNS server: " + dnsServer.getCanonicalHostName());
            String targetMessage = new String(
                    "  TFTP server address \"" + networkResources.configServer + "\"");
            resolver.setAddress(dnsServer);
            Lookup aLookup = null;
            try {
                aLookup = new Lookup(networkResources.configServer, Type.A);
            } catch (TextParseException e) {
                journalService.println("  is malformed.\n");
                journalService.println(targetMessage);
                return TFTP_ADDRESS_MALFORMED;
            }
            aLookup.setResolver(resolver);
            Record[] aRecords = aLookup.run();
            switch (aLookup.getResult()) {
            case Lookup.SUCCESSFUL:
                if (aRecords != null) {
                    InetAddress targetAddress = ((ARecord) aRecords[0]).getAddress();
                    targetMessage += " resolves to: " + targetAddress.getHostAddress();
                    journalService.println(targetMessage);
                    if (tftpServerAddress == null) {
                        tftpServerAddress = targetAddress;
                    } else {
                        // Check that multiple lookups result in same address.
                        if (!tftpServerAddress.equals(targetAddress)) {
                            journalService.println("  TFTP server address does not match prior lookup.");
                            results = MULTIPLE_CONFIG_TARGETS;
                        }
                    }
                } else {
                    targetMessage += " could not be resolved.";
                    journalService.println(targetMessage);
                    results = TFTP_TARGET_UNRESOLVED;
                }
                break;
            case Lookup.UNRECOVERABLE:
                targetMessage += " [Unrecoverable error]";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            case Lookup.TRY_AGAIN:
                targetMessage += " [Lookup timeout]";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            case Lookup.HOST_NOT_FOUND:
                targetMessage += " could not be resolved.";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            case Lookup.TYPE_NOT_FOUND:
                targetMessage += " could not be resolved.";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            }
        }
    }

    if ((tftpServerAddress == null) || (results == MULTIPLE_CONFIG_TARGETS)) {
        journalService.println("Cannot recover from previous errors, aborting TFTP test.\n");
        return results;
    }

    journalService.println("Beginning TFTP get request of test file: " + testFile);
    // Open local socket
    try {
        tftp.open();
    } catch (SocketException e) {
        journalService.println("TFTP client failure. " + e.getMessage() + "\n");
        return TFTP_CLIENT_FAILURE;
    }

    // Try to receive remote file via TFTP
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        tftp.open(bindPort, bindAddress);
        tftp.receiveFile(testFile, org.apache.commons.net.tftp.TFTP.ASCII_MODE, output, tftpServerAddress);
        String testFileContents = output.toString();
        boolean verified = true;
        for (String verificationString : verificationStrings) {
            if (!testFileContents.contains(verificationString)) {
                verified = false;
            }
        }
        if (verified) {
            journalService.println("File received successfully.");
        } else {
            journalService.println("File received but contents do not verify.");
            results = TFTP_CONTENTS_FAILED;
        }
    } catch (UnknownHostException e) {
        journalService.println("TFTP client failure. " + e.getMessage());
        results = TFTP_CLIENT_FAILURE;
    } catch (IOException e) {
        journalService.println("TFTP get failed. " + e.getMessage());
        results = TFTP_GET_FAILED;
    } finally {
        // Close local socket and output file
        tftp.close();
    }

    journalService.println("");
    return results;
}

From source file:org.xerela.net.sim.tftp.TftpInterface.java

public void sendFile(IpAddress localIp, IpAddress remoteIp, String filename, InputStream file)
        throws UnknownHostException, IOException {
    TFTPClient client = null;
    try {// ww  w.  jav  a2s  .c o  m
        client = new TFTPClient();
        client.setDatagramSocketFactory(new SocketFactory(localIp.getRealAddress()));
        client.open();
        client.sendFile(filename, TFTPClient.BINARY_MODE, file, remoteIp.getIp());
    } finally {
        try {
            client.close();
        } catch (NullPointerException e) {
        }
    }
}

From source file:person.daizhongde.virtue.util.test.TFTPExample.java

private static void open(TFTPClient tftp) {
    try {/* www .  j  ava2  s .  c  o  m*/
        tftp.open();
    } catch (SocketException e) {
        System.err.println("Error: could not open local UDP socket.");
        System.err.println(e.getMessage());
        System.exit(1);
    }
}