What port is ping?

What port does ping use?

This seems like a simple question, but I cannot find a definitive answer.

Linux manual page for ping mentions an ECHO_REQUEST datagram:

"Ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.  ECHO_REQUEST datagrams (``pings'') have an IP and ICMP header, followed by a ``struct timeval'' and then an arbitrary number of ``pad'' bytes used to fill out the packet."

I searched the include directory and found this C preprocessor define:
/usr/include/netinet/icmp6.h
#define ICMP6_ECHO_REQUEST          128

I also found this in a document someone gave me:

*   128/tcp GSS X License Verification
*   128/udp GSS X License Verification

And, finally, in the RFC 1060 specification, I found this:

7       ECHO       Echo                                  [95,JBP]

What I'm trying to do is write a ping program in Java, if that's at all possible.  I know that the standard ping uses the ICMP (Internet Control Message Protocol) which is a low-level network administration protocol not supported in Java nor in Microsoft's Winsock.  ICMP on Win32 is provided by an unsupported and undocumented library icmp.dll.  I also know that it's sending an ICMP datagram.

However, all ping does is simply send a message to the host and report if it arrived.  That takes all of three lines of code in Java.  But, it's not working for most of the hosts I've tried where the standard ping program does.  I keep getting an exception of "Connection refused" every time.

These are the port numbers I've tried:

7   Echo service
13  Daytime
23  Telnet
128 ICMP6_ECHO_REQUEST

There may be some other reason why my Java ping program fails where the standard ping succeeds, but trying every possible port number with a range of hosts is impractical.  By knowing definitively what port ping uses, I can eliminate the other factors.

I'm asking this question in the Java area because I think the answer would benefit this community most, not because I think a Java expert would necessarily know the answer (I may be wrong, if so I apologize).  So, I've referenced this question in several other topic areas.
LVL 2
cpopinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
as you already know ICMP is low-level network protocol, which is not directly supported in Java.

in Java you can use TCP packets and UDP datagrams - both of them are relatively hi-level (and work over IP) (for TCP connection you need not only working network connection, but remote server app that listens on some port)

http://java.sun.com/people/linden/faq_d.html

(Sect. 15) How can I write the "ping" program?
 You can't do it directly. Quoting from the Java Networking FAQ,
Ping requires ICMP packets. These packets can only be created via a socket of the SOCK_RAW type. Currently, Java only allows SOCK_STREAM (TCP) and SOCK_DGRAM (UDP) sockets. It seems unlikely that this will be added very soon, since many Unix versions only allow SOCK_RAW sockets to be created by root, and winsock does not address ICMP packets (win32 includes an unsupported and undocumented ICMP.DLL).
You could write a program that handshakes with a remote system, to simulate ping, or you could use native code for the raw socket. Other sites:
You can find the Java Networking FAQ at http://www.io.com/~maus/JavaNetworkingFAQ.html
There is an additional Java Networking FAQ (which is being modified and reposted regularly) at http://www.davidreilly.com/java/java_network_programming/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JOKCommented:
Packet InterNet Groper (sorry, just found out the name, so had to use it) is handled before/below it ever gets to a port.
0
ArtimageCommented:
I actually wrote this code.

Here is the java part.

/*
  This was written by Artimage@ishiboo.com for Appnet.com
 
  This software (and its source code) are freely usable, modifiable,
  and redistributable under the terms of the GNU General Public License,
  available at http://www.gnu.org/copyleft/gpl.html, and in the ./COPYING
  file in the MARS distribution.
*/

package org.altara.mars;

import java.net.*;
import java.io.*;

/**
   Use ICMP to check if a machine is up.
*/


public class PINGProbe extends Probe {
    public native boolean nativePing(String fqdn);

// Other stuff that is unimportant here
}


Here is the c part

/*
   Native code for a ICMP probe to be used in java.
*/

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>

#include <jni.h>
#include <stdio.h>
#include <errno.h>
#include "PINGProbe.h"

#define MAX_BUFF 2046  

extern int h_errno;


   
/*  Probes the host, returns the result. The results should be interpreted
  as follows: true means the probe is OK (set status to Host.OK), false
  means the probe has timed out (set status to Host.TO), and if an
  exception is thrown, the probe failed outright (set status to Host.FAIL)
*/



JNIEXPORT jboolean JNICALL
Java_PINGProbe_nativePing(JNIEnv *env, jobject obj, jstring fqdn)
{
  const char * hostName;
  int s;
  struct hostent * host;
  struct sockaddr_in hostAddr;
  struct icmphdr ihdr;
  char * data;
  int dataLen;

  // BUGBUG Add code to check euid


 // Allocate memory for structures.
  if ( NULL == (host = (struct hostent *) malloc(sizeof(struct hostent))) ) {
    perror("Malloc failed:");
  }

  // Convert from java string to ascii
  hostName = (*env)->GetStringUTFChars(env, fqdn, 0);
  printf("%s", hostName);
 
  // Convert hostname to address.
  if( NULL == (host = gethostbyname(hostName))) {
    // Call failed.
    perror("error gethostbyname: ");

    // BUGBUG all of these failure returns should be throwing an exception
    //        instead.
    return 0;
  }

  // Release Java string
  (*env)->ReleaseStringUTFChars(env, fqdn, hostName);
 
  bzero((char *)&hostAddr, sizeof (hostAddr)); // zero out structure.
  // Copy address to address structure.
  bcopy(host->h_addr, (char *) &hostAddr.sin_addr, host->h_length);
  hostAddr.sin_family = host->h_addrtype;

  // create a raw socket
  if ( -1 == (s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) ) {
    perror("Socket creation failed:");
    return 0;
  }

  // Connect to host.
  if ( -1 == connect(s, (struct sockaddr *)&hostAddr, sizeof(hostAddr))){
    perror("Connect failed:");
    return 0;
  }

  // Get a buffer ready to store incoming data
  if ( NULL == (data = (char *)malloc(MAX_BUFF))) {
    perror("Malloc failed:");
    return 0;
  }

  dataLen = sizeof(data);

 
  // Create the icmp header.
  ihdr.type = ICMP_ECHO;
  ihdr.code = 0;
  ihdr.checksum = 0;
  ihdr.un.echo.sequence = 1;
  ihdr.un.echo.id = 42;

  // Loop until we succeed or 4 times..

  // Do a Send
  send(s, &ihdr, sizeof (ihdr), 0);


  // BUGBUG we should set the timeout.. Get it from mars.

  // Start listening for our ping to come back.    
  dataLen = recv(s, data, dataLen, 0);
 
 if (-1 == dataLen) {
    perror("recv error:");
    return 0;
  }
 
  // Close the connection
  close(s);

  return 1;
}


#ifdef 0
/*
  This is my test driver.
*/
int main (int argc, char ** argv)
{
  int r;

  if (argc != 2) {
    printf("Usage: %s hostname\n", argv[0]);
   exit(1);
  }

  printf("Here we go.\n");
  r = nativePing(argv[1]);
  printf("Done. %d \n", r);

}

#endif


You will need to figure out how to link these on your system though.

Look at this site.

http://java.sun.com/docs/books/tutorial/native1.1/index.html

0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

bedotCommented:
you can always use telnet on a different port
IE, trying from an unix box to contact a W98 with no service daemon (BUT port 139 is always surveyed on W)

root@med1091[/]
root@med1091[/]telnet 192.142.105.89 139
Trying...
Connected to 192.142.105.89.
Escape character is '^]'.

the connected response indicate this port on this address is active;
BUT, is necessary to know a surveyed port !
0
mbormannCommented:
i think chensu clarified it a bit already.

but u can follow this nice little thread
http://www.nfr.net/firewall-wizards/mail-archive/1999/Sep/0162.html
titled 'ICMP Well-Known Port'

for more...
0
cpopinAuthor Commented:
HeyHey,
Your first URL answered my question.  Your third URL looks like a good resource for Java FAQs.

Thanks,

-- CPopin
0
cpopinAuthor Commented:
mbormann,
chensu didn't comment on this thread.
0
mbormannCommented:
yeah ,I know he commented in C++ section but i was going thru that yesterday,and saw ur link from there to here.
0
cpopinAuthor Commented:
Yah, I noticed it too after reading more of my e-mail.  Not everyone is understands the reason for thread referencing.  You do that so that experts in other threads can all contribute to a single thread in the most proper topic area.  No one's supposed to comment on those zero point referencing threads.  They're supposed to comment in only the original thread (this one).  It makes for a more valuable PAQ.

Once the original thread becomes a PAQ, the referencing threads should be deleted.
0
cpopinAuthor Commented:
If a packet hits a pocket on a socket on a port,
and the bus is interrupted and the interrupt's not caught,
then the socket packet packet has an error to report.

-- Programmer's traditional nursery rhyme
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.