ping source code

W

wana

I was reading through original source code of ping for some insight
and was confused by unusual code.

Entire listing available at:
http://www.ping127001.com/pingpage/ping.html

#include #include #include
#include #include #include #include
#include #include #include #include #include

What does this mean? That is all of the #include's there are.

main(argc, argv)
char *argv[];
{

That is how the main function starts. Is that legal?

There are structs that are network related that I don't know where
they come from because of the lack of any recongnizable #include file.
The code is well organized and commented. I understand that this
program is from 1982. Just for fun, I ran it through gcc and it
generated a lot of errors, many of which are due to the lack of
legitimate #includes. What does this all mean? Shouldn't the famous
Ping code compile with gcc?

Compile errors:

pg.c:2: error: parse error before "I"
pg.c:24:10: #include expects "FILENAME" or <FILENAME>
pg.c:25:10: #include expects "FILENAME" or <FILENAME>
pg.c:26:10: #include expects "FILENAME" or <FILENAME>
pg.c: In function `main':
pg.c:72: error: storage size of `from' isn't known
pg.c:82: error: `SO_DEBUG' undeclared (first use in this function)
pg.c:82: error: (Each undeclared identifier is reported only once
pg.c:82: error: for each function it appears in.)
pg.c:85: error: `SO_DONTROUTE' undeclared (first use in this function)
pg.c:104: error: invalid application of `sizeof' to an incomplete type
pg.c:105: error: dereferencing pointer to incomplete type
pg.c:105: error: `AF_INET' undeclared (first use in this function)
pg.c:106: error: dereferencing pointer to incomplete type
pg.c:107: error: dereferencing pointer to incomplete type
pg.c:111: warning: assignment makes pointer from integer without a
cast
pg.c:113: error: dereferencing pointer to incomplete type
pg.c:113: error: dereferencing pointer to incomplete type
pg.c:114: error: dereferencing pointer to incomplete type
pg.c:114: error: `caddr_t' undeclared (first use in this function)
pg.c:114: error: dereferencing pointer to incomplete type
pg.c:114: error: dereferencing pointer to incomplete type
pg.c:115: error: dereferencing pointer to incomplete type
pg.c:127: error: `stderr' undeclared (first use in this function)
pg.c:130: error: invalid application of `sizeof' to an incomplete type
pg.c:141: warning: assignment makes pointer from integer without a
cast
pg.c:141: error: `NULL' undeclared (first use in this function)
pg.c:146: error: `SOCK_RAW' undeclared (first use in this function)
pg.c:146: error: dereferencing pointer to incomplete type
pg.c:153: error: `SOL_SOCKET' undeclared (first use in this function)
pg.c:161: error: dereferencing pointer to incomplete type
pg.c:163: error: dereferencing pointer to incomplete type
pg.c:167: error: `stdout' undeclared (first use in this function)
pg.c:169: error: `SIGINT' undeclared (first use in this function)
pg.c:170: error: `SIGALRM' undeclared (first use in this function)
pg.c:180: error: `packet' undeclared (first use in this function)
pg.c:183: error: storage size of `timeout' isn't known
pg.c:195: error: `EINTR' undeclared (first use in this function)
pg.c: In function `catcher':
pg.c:232: error: `SIGALRM' undeclared (first use in this function)
pg.c: In function `pinger':
pg.c:248: error: syntax error before "outpack"
pg.c:249: error: `outpack' undeclared (first use in this function)
pg.c:252: error: syntax error before '*' token
pg.c:254: error: dereferencing pointer to incomplete type
pg.c:254: error: `ICMP_ECHO' undeclared (first use in this function)
pg.c:255: error: dereferencing pointer to incomplete type
pg.c:256: error: dereferencing pointer to incomplete type
pg.c:257: error: dereferencing pointer to incomplete type
pg.c:258: error: dereferencing pointer to incomplete type
pg.c:265: error: `iicmp_cksum' undeclared (first use in this function)
pg.c:268: error: invalid application of `sizeof' to an incomplete type
pg.c:268: error: parse error before ';' token
pg.c:274: error: `stdout' undeclared (first use in this function)
pg.c: At top level:
pg.c:276: error: parse error before "if"
pg.c:278: warning: parameter names (without types) in function
declaration
pg.c:278: warning: data definition has no type or storage class
pg.c:279: error: parse error before '}' token
pg.c: In function `pr_pack':
pg.c:332: error: `packet' undeclared (first use in this function)
pg.c:334: error: storage size of `tv' isn't known
pg.c:338: error: dereferencing pointer to incomplete type
pg.c:338: error: dereferencing pointer to incomplete type
pg.c:342: error: dereferencing pointer to incomplete type
pg.c:343: error: `ICMP_MINLEN' undeclared (first use in this function)
pg.c:346: error: dereferencing pointer to incomplete type
pg.c:351: error: dereferencing pointer to incomplete type
pg.c:351: error: `ICMP_ECHOREPLY' undeclared (first use in this
function)
pg.c:353: error: dereferencing pointer to incomplete type
pg.c:354: error: dereferencing pointer to incomplete type
pg.c:354: error: dereferencing pointer to incomplete type
pg.c:354: error: dereferencing pointer to incomplete type
pg.c:362: error: dereferencing pointer to incomplete type
pg.c:366: error: dereferencing pointer to incomplete type
pg.c:379: error: dereferencing pointer to incomplete type
pg.c:380: error: dereferencing pointer to incomplete type
pg.c:387: error: `stdout' undeclared (first use in this function)
pg.c: At top level:
pg.c:401: error: parse error before "u_short"
pg.c:401: warning: data definition has no type or storage class
pg.c:403: error: parse error before '{' token
pg.c:405: error: parse error before '*' token
pg.c:405: error: initializer element is not constant
pg.c:405: warning: data definition has no type or storage class
pg.c:406: error: parse error before "answer"
pg.c:406: warning: data definition has no type or storage class
pg.c:407: error: register name not specified for `sum'
pg.c:415: error: parse error before "while"
pg.c:424: error: parse error before '*' token
pg.c:431: error: redefinition of `sum'
pg.c:407: error: `sum' previously defined here
pg.c:431: error: initializer element is not constant
pg.c:431: warning: data definition has no type or storage class
pg.c:432: error: parse error before '+=' token
pg.c:433: error: initializer element is not constant
pg.c:433: warning: data definition has no type or storage class
pg.c:434: error: parse error before "return"
pg.c: In function `tvsub':
pg.c:447: error: dereferencing pointer to incomplete type
pg.c:447: error: dereferencing pointer to incomplete type
pg.c:448: error: dereferencing pointer to incomplete type
pg.c:449: error: dereferencing pointer to incomplete type
pg.c:451: error: dereferencing pointer to incomplete type
pg.c:451: error: dereferencing pointer to incomplete type
pg.c: In function `finish':
pg.c:466: error: `stdout' undeclared (first use in this function)
pg.c: At top level:
pg.c:42: error: storage size of `tz' isn't known
pg.c:44: error: storage size of `whereto' isn't known
 
J

Joona I Palaste

wana said:
I was reading through original source code of ping for some insight
and was confused by unusual code.
#include #include #include
#include #include #include #include
#include #include #include #include #include
What does this mean? That is all of the #include's there are.

What the heck? I can't fathom out at all what that means. Either it is
a non-standard dialect or the source code is broken.
main(argc, argv)
char *argv[];
{
That is how the main function starts. Is that legal?

It used to be, not any more. That's a pre-standard K&R style. It's
still accepted by some compilers.
There are structs that are network related that I don't know where
they come from because of the lack of any recongnizable #include file.
The code is well organized and commented. I understand that this
program is from 1982. Just for fun, I ran it through gcc and it
generated a lot of errors, many of which are due to the lack of
legitimate #includes. What does this all mean? Shouldn't the famous
Ping code compile with gcc?

Not necessarily. C has changed a lot from 1982. gcc might have
abandoned support for some obsolete features.
 
J

Joona I Palaste

What the heck? I can't fathom out at all what that means. Either it is
a non-standard dialect or the source code is broken.

*NOW* I understand. It actually reads
#include <foo.h>
#include <bar.h>
#include <quux.h>
#include <quuux.h>
(replace the silly names with proper ones)
but because the idiot hosting the web server is serving the page as
HTML instead of plain text, the web browser is filtering the header
names away, thinking they're non-standard HMTL tags.
 
M

Mike Wahler

wana said:
I was reading through original source code of ping for some insight
and was confused by unusual code.

Entire listing available at:
http://www.ping127001.com/pingpage/ping.html

#include #include #include
#include #include #include #include
#include #include #include #include #include

What does this mean? That is all of the #include's there are.

It means that the 'ping.html' file is not a properly
formatted HTML file. Try your browser's 'view source'
feature and copy/paste the code from there.
main(argc, argv)
char *argv[];
{

That is how the main function starts. Is that legal?

It's not standard C. It's the 'old style' function header.
Standard form is:

int main(int argc, char *argv[])
There are structs that are network related that I don't know where
they come from because of the lack of any recongnizable #include file.

See above about the #includes. But note that any networking
issues will will platform-specific, thus off-topic here.
The code is well organized and commented. I understand that this
program is from 1982.

Yes, the style is 'old'.
Just for fun, I ran it through gcc and it
generated a lot of errors, many of which are due to the lack of
legitimate #includes.


Then you need to fix the #includes, and disregard any other messages
which are very likly an artifact of the first error messages.
What does this all mean? Shouldn't the famous
Ping code compile with gcc?

Only if those nonstandard headers (and the libraries they
refer to) are provided and are compatible with gcc.

-Mike
 
J

Jens.Toerring

wana said:
I was reading through original source code of ping for some insight
and was confused by unusual code.
#include #include #include
#include #include #include #include
#include #include #include #include #include
What does this mean? That is all of the #include's there are.

That's simply broken code. Looks as if the required names of files
to be included are simply missing.
main(argc, argv)
char *argv[];
{
That is how the main function starts. Is that legal?

Yes, that's very old code (pre-C89) when C still used a different
method to declare function arguments. But modern compilers still
accept it, so it's not really a problem.
There are structs that are network related that I don't know where
they come from because of the lack of any recongnizable #include file.

That's highly likely. If you want to know where they come from ask
in e.g. comp.unix.programmer (or try to figure it out by yourself
by reading the man pages;-)
The code is well organized and commented. I understand that this
program is from 1982. Just for fun, I ran it through gcc and it
generated a lot of errors, many of which are due to the lack of
legitimate #includes. What does this all mean? Shouldn't the famous
Ping code compile with gcc?

No, definitely not without the missing files to be included.

Regards, Jens
 
D

Dave Vandervies

I was reading through original source code of ping for some insight
and was confused by unusual code.

Entire listing available at:
http://www.ping127001.com/pingpage/ping.html

#include #include #include
#include #include #include #include
#include #include #include #include #include

What does this mean? That is all of the #include's there are.

Without going to the URL you posted, this looks like a poor HTMLization
of source code, where the <> in `#include <foo.h>' get treated as tags
(and, since the browser doesn't recognize them, silently ignored).

main(argc, argv)
char *argv[];
{

That is how the main function starts. Is that legal?

Yes, but it's outdated.
In K&R C, this was how arguments were declared: names only in the argument
list, and types (defaulting to int if not specified) given between the
function name/argument list and the beginning of the function body.

Except for prototype-based type checking if you call a function after
it's been declared like this, this declaration is equivalent to:
--------
int main(int argc, char *argv[])
--------
which is the preferred form for newly written code. C90 still allows
the older type of declaration; I'm not sure about C99 - implicit int is
gone, but I'm not sure (and don't have a strong enough desire to know
to actually look it up) whether
--------
int main(argc,argv)
int argc;
char *argv[];
{
/*function body here*/
}
--------
is still allowed.

There are structs that are network related that I don't know where
they come from because of the lack of any recongnizable #include file.

If you dig into the HTML source, I'd expect you'll find the names of the
headers (see above). Most of them are probably unixisms rather than ones
defined by the C language, so if you have questions about them you'd be
likely to get better answers from comp.unix.programmer .
The code is well organized and commented. I understand that this
program is from 1982. Just for fun, I ran it through gcc and it
generated a lot of errors, many of which are due to the lack of
legitimate #includes. What does this all mean? Shouldn't the famous
Ping code compile with gcc?

Possibly.

For one thing, what your browser is showing you is probably a mangled
version, not the original code. Also, the language has changed somewhat
since 1982, so a lot of what's in the code is no longer considered
good style, and it's quite possible that some of it is no longer legal.
(People who have worked with both K&R C and ANSI/ISO C will be better
able to comment on the differences.)


dave
 
K

Kenneth Brody

wana said:
I was reading through original source code of ping for some insight
and was confused by unusual code.

Entire listing available at:
http://www.ping127001.com/pingpage/ping.html

#include #include #include
#include #include #include #include
#include #include #include #include #include

What does this mean? That is all of the #include's there are.
[...]

Someone called a text file ".html". Look at the source to the page:

==========
[...]
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
[...]
==========

The brackets used by C are being misinterpreted by the browser as HTML
markups, due to the ".html" extension.

Ignore the browser's representation, view the source, and copy-paste that
instead.
 
K

Kenneth Brody

Joona I Palaste wrote:
[...]
*NOW* I understand. It actually reads
#include <foo.h>
#include <bar.h>
#include <quux.h>
#include <quuux.h>
(replace the silly names with proper ones)
but because the idiot hosting the web server is serving the page as
HTML instead of plain text, the web browser is filtering the header
names away, thinking they're non-standard HMTL tags.

Don't blame "the idiot hosting the web server", blame "the idiot who
decided to use a '.html' extension on a plain text file". It's your
browser "filtering the header names away", not the server, and it's
doing it because it was told to do so.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I was reading through original source code of ping for some insight
and was confused by unusual code.

Entire listing available at:
http://www.ping127001.com/pingpage/ping.html [snip]
The code is well organized and commented. I understand that this
program is from 1982. Just for fun, I ran it through gcc and it
generated a lot of errors, many of which are due to the lack of
legitimate #includes. What does this all mean? Shouldn't the famous
Ping code compile with gcc?

Yes and no.

The late Mike Muus wrote ping for the original BSD sockets implementation, and
the source you found (a secondhand version, to be sure) isn't likely to be
compatable to the library implementations on your much more current system.
Assuming that you take the time to update the constants and syscalls to your
system's requirements, gcc should be able to compile the K&R C that Mike's
code is written in. It should even run.

If you want to read Mike's "The Story of the Ping program" and retrieve the
shar (shell archive - a unix shell script that self-unpacks the source code,
make files, and man pages), take a look at http://ftp.arl.mil/~mike/ping.html

I had the pleasure of having a short conversation with Mike a year or so
before he passed away.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBSieHagVFX4UWr64RAkEMAJ9diePJA6uWGGfYtexdjNYGWet4PACcCrUK
WbxEhW0m7s2xfdgNREwXomE=
=iZeK
-----END PGP SIGNATURE-----
 
J

Joona I Palaste

Kenneth Brody said:
Joona I Palaste wrote:
[...]
*NOW* I understand. It actually reads
#include <foo.h>
#include <bar.h>
#include <quux.h>
#include <quuux.h>
(replace the silly names with proper ones)
but because the idiot hosting the web server is serving the page as
HTML instead of plain text, the web browser is filtering the header
names away, thinking they're non-standard HMTL tags.
Don't blame "the idiot hosting the web server", blame "the idiot who
decided to use a '.html' extension on a plain text file". It's your
browser "filtering the header names away", not the server, and it's
doing it because it was told to do so.

Browsers are told how to display the content by a Content-Type header,
not by a file extension. At least Mozilla should be. I haven't checked
the Content-Type header on this page yet, though.
 
J

Joona I Palaste

Kenneth Brody said:
Joona I Palaste wrote:
[...]
*NOW* I understand. It actually reads
#include <foo.h>
#include <bar.h>
#include <quux.h>
#include <quuux.h>
(replace the silly names with proper ones)
but because the idiot hosting the web server is serving the page as
HTML instead of plain text, the web browser is filtering the header
names away, thinking they're non-standard HMTL tags.
Don't blame "the idiot hosting the web server", blame "the idiot who
decided to use a '.html' extension on a plain text file". It's your
browser "filtering the header names away", not the server, and it's
doing it because it was told to do so.

By "the idiot hosting the web server" I actually meant the idiot who
wrote that web page and uploaded it on the server, not the person or
people who operate the server itself. My apologies for being so
confusing, it was late at night and I was drunk.
 
D

Dave Vandervies

Kenneth Brody said:
Joona I Palaste wrote:
[...]
*NOW* I understand. It actually reads
#include <foo.h>
#include <bar.h>
#include <quux.h>
#include <quuux.h>
(replace the silly names with proper ones)
but because the idiot hosting the web server is serving the page as
HTML instead of plain text, the web browser is filtering the header
names away, thinking they're non-standard HMTL tags.
Don't blame "the idiot hosting the web server", blame "the idiot who
decided to use a '.html' extension on a plain text file". It's your
browser "filtering the header names away", not the server, and it's
doing it because it was told to do so.

Browsers are told how to display the content by a Content-Type header,
not by a file extension. At least Mozilla should be. I haven't checked
the Content-Type header on this page yet, though.

--------
dj3vande@perpugilliam:~ (0) $ lynx -dump -head http://www.ping127001.com/pingpage/ping.html | grep 'Content-Type'
Content-Type: text/html
dj3vande@perpugilliam:~ (0) $
--------
So the swerver is believing whoever put the file there when they said
it was HTML.


dave
 
K

Keith Thompson

Kenneth Brody said:
Joona I Palaste wrote:
[...]
*NOW* I understand. It actually reads
#include <foo.h>
#include <bar.h>
#include <quux.h>
#include <quuux.h>
(replace the silly names with proper ones)
but because the idiot hosting the web server is serving the page as
HTML instead of plain text, the web browser is filtering the header
names away, thinking they're non-standard HMTL tags.

Don't blame "the idiot hosting the web server", blame "the idiot who
decided to use a '.html' extension on a plain text file". It's your
browser "filtering the header names away", not the server, and it's
doing it because it was told to do so.

The first line of the html file is a "<pre>" directive. Apparently
the author mistakenly thought that a "<pre>" directive inhibits
special treatment of '<' characters (and didn't bother to check
how the pages is displayed).
 
K

Kenneth Brody

Joona said:
Kenneth Brody said:
Joona I Palaste wrote:
[...]
*NOW* I understand. It actually reads
#include <foo.h>
#include <bar.h>
#include <quux.h>
#include <quuux.h>
(replace the silly names with proper ones)
but because the idiot hosting the web server is serving the page as
HTML instead of plain text, the web browser is filtering the header
names away, thinking they're non-standard HMTL tags.
Don't blame "the idiot hosting the web server", blame "the idiot who
decided to use a '.html' extension on a plain text file". It's your
browser "filtering the header names away", not the server, and it's
doing it because it was told to do so.

Browsers are told how to display the content by a Content-Type header,
not by a file extension. At least Mozilla should be. I haven't checked
the Content-Type header on this page yet, though.

And the server typically determine the Content-Type by the extension.
(Unless it's script-generate, in which case the script generates the
Content-Type header.) Once again, it's the ".html" extension put there
by the person who put the file there. The server is doing what it was
told -- send it as HTML.
 
K

Kenneth Brody

Keith Thompson wrote:
[...]
The first line of the html file is a "<pre>" directive. Apparently
the author mistakenly thought that a "<pre>" directive inhibits
special treatment of '<' characters (and didn't bother to check
how the pages is displayed).

Not to mention the lack of <HTML>, <BODY>, </PRE>, </BODY>, and </HTML>.
 
K

Keith Thompson

Keith Thompson said:
The first line of the html file is a "<pre>" directive. Apparently
the author mistakenly thought that a "<pre>" directive inhibits
special treatment of '<' characters (and didn't bother to check
how the pages is displayed).

I really need to proofread better before I post. That should be "how
the *page* is displayed".
 
P

Peter Shaggy Haywood

Groovy hepcat Joona I Palaste was jivin' on 16 Sep 2004 19:53:00 GMT
in comp.lang.c.
Re: ping source code's a cool scene! Dig it!
wana said:
main(argc, argv)
char *argv[];
{
That is how the main function starts. Is that legal?

It used to be, not any more. That's a pre-standard K&R style. It's
still accepted by some compilers.

Actually, Joona, apart from the lack of a return type required by
C99, this is perfectly legal code. Old K&R style declarations are
still legal (though obsolescent).

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top