Newsgroups: alt.sources Subject: udpprobe - send a UDP datagram and look for response, version 1.2 Followup-To: alt.sources.d Organization: private Linux site, southern Germany Archive-Name: udpprobe Submitted-By: Olaf Titz This is probably one of the small utilities that have been written a thousand times already, but since I know of no prior art, I hacked it up and post it here. :-) This program allows you to send and receive UDP datagrams. E.g., udpprobe netgw daytime "" will query the daytime UDP port on "netgw" and return the time (hopefully). A more interesting use is signaling of any kind. Refer to README and manpage for details. Runs on Linux, Ultrix, HPUX (verified) and on every other Un*x with reasonably BSD-like networking routines. Public domain. No warranty of any kind. Version 1.2 - small bugfix. #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh 'udpprobe/README' <<'END_OF_FILE' Xudpprobe - send a UDP datagram and look for response, version 1.1 X$Id: README,v 1.2 1996/02/13 20:22:26 olaf Exp $ X XThis program sends a UDP datagram to a specified port and optionally Xwaits for response. It is intended mainly for testing and signaling Xpurposes. You can use it on the echo or daytime port (cf. X/etc/services and /etc/inetd.conf) to test if your network is alive Xand the UDP modules of specific hosts are working. You can also use it Xfor signaling: configure inetd to start a specific program whenever it Xreceives a packet on a special port. This way you can configure a Xsystem with a mail server and clients that are not always running, so Xthat the mailer is told from a client by sending a UDP packet (i.e. Xan "I am here" message) to deliver mail, and similar things. X XI have written this program on Linux 0.99.14 and tested it on Ultrix X4.3 and HPUX 8.07 as well. It should run on any system that has Xreasonably BSD-compatible networking and getopts(3) (not really needed Xbut I'm lazy :-). Perhaps you need to tune the header includes a Xbit. You don't need special privileges to compile or run this program. X XNote: If you're looking for a similar (but much more powerful) tool to Xaccess TCP ports, try socket(1) from the comp.sources.unix archive. X XWritten by Olaf Titz XThis software is released into the public domain. The author assumes Xno responsibility of any kind for its use. X XVersion history: X1.2 1996-02-13 Bugfix in port number handling. X1.1 1994-01-31 First release. X END_OF_FILE if test 1569 -ne `wc -c <'udpprobe/README'`; then echo shar: \"'udpprobe/README'\" unpacked with wrong size! fi # end of 'udpprobe/README' fi if test -f 'udpprobe/Makefile' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'udpprobe/Makefile'\" else echo shar: Extracting \"'udpprobe/Makefile'\" \(593 characters\) sed "s/^X//" >'udpprobe/Makefile' <<'END_OF_FILE' X# A rather generic Makefile X# $Id: Makefile,v 1.2 1996/02/13 20:18:27 olaf Exp $ X XCC = gcc XCFLAGS = -Wall -O3 -s XINSTALL = install XINSTDIR = /usr/local/bin XMANDIR = /usr/local/man/man1 X XPROGS = udpprobe X Xall: $(PROGS) X Xudpprobe: udpprobe.c X $(CC) $(CFLAGS) -o udpprobe udpprobe.c X Xinstall: all X $(INSTALL) -m 755 udpprobe $(INSTDIR) X $(INSTALL) -m 444 udpprobe.1 $(MANDIR) X Xclean: X rm -f $(PROGS) core core.* a.out *.o *.s *~ X Xdist: clean README Makefile udpprobe.c udpprobe.1 X cd ..; shar udpprobe udpprobe/README udpprobe/Makefile \ X udpprobe/udpprobe.c udpprobe/udpprobe.1 > udpprobe.shar END_OF_FILE if test 593 -ne `wc -c <'udpprobe/Makefile'`; then echo shar: \"'udpprobe/Makefile'\" unpacked with wrong size! fi # end of 'udpprobe/Makefile' fi if test -f 'udpprobe/udpprobe.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'udpprobe/udpprobe.c'\" else echo shar: Extracting \"'udpprobe/udpprobe.c'\" \(2479 characters\) sed "s/^X//" >'udpprobe/udpprobe.c' <<'END_OF_FILE' X/* X * udpprobe.c - send data over an UDP socket and look for response X */ X X#include X#include X#include X#include X#include X#include X#include X#include X#include X#include X Xstatic char RCSID[]= "$Id: udpprobe.c,v 1.2 1996/02/13 20:18:58 olaf Exp $"; X Xvoid usage(void) X{ X fputs("Usage: udpprobe [-w timeout] host port [text]\n",stderr); X exit(9); X} X Xvoid checkarg(int arg, int ind) X{ X if (arg<=ind) X usage(); X} X Xextern char *optarg; Xextern int optind; X Xvoid main(int argc, char *argv[]) X{ X int s, len, timeout=1; X struct sockaddr_in sa; X struct hostent *hp; X struct servent *sp; X struct timeval tm; X char buf[1024]; X char *rw; X fd_set lfd; X X /* Look for timeout argument */ X switch (getopt(argc,argv,"w:")) { X case 'w': timeout = atoi(optarg); X case EOF: break; X default: usage(); X } X checkarg(argc, optind); X X /* Figure out the address to send to */ X bzero(&sa, sizeof(sa)) ; X sa.sin_family=AF_INET ; X if ((sa.sin_addr.s_addr = inet_addr(argv[optind])) == -1) { X if ((hp = gethostbyname(argv[optind])) == NULL) { X herror("gethostbyname"); X exit(2); } X bcopy(hp->h_addr, &sa.sin_addr, hp->h_length) ; X sa.sin_family=hp->h_addrtype ; } X checkarg(argc, ++optind); X X /* ditto for the port */ X if ((sa.sin_port = htons(atoi(argv[optind])))==0) { X if ((sp = getservbyname(argv[optind], "udp"))==NULL) { X perror("getservbyname"); X exit(2); } X sa.sin_port = sp->s_port; } X ++optind; X X if ((s=socket(AF_INET, SOCK_DGRAM, 0))<0) { X perror("socket"); X exit(2); } X X if (connect(s, (struct sockaddr *) &sa, sizeof(sa))<0) { X perror("connect"); X exit(2); } X X bzero(buf, sizeof(buf)); X if (argc==optind) { X read(0, buf, sizeof(buf)-1); X rw = buf; } X else { X rw = argv[optind]; } X /* If message empty, send just one \0 char */ X len = strlen(rw); X if (len==0) X len = 1; X X if (send(s, rw, len, 0)<0) { X perror("send"); X exit(2); } X X if (timeout==0) X exit(0); X X /* Now collect response */ X bzero(buf, sizeof(buf)); X FD_ZERO(&lfd); X FD_SET(s, &lfd); X tm.tv_sec = timeout; X tm.tv_usec = 0; X if (select(s+1, &lfd, NULL, NULL, &tm)<0) { X perror("select"); X exit(2);} X if (FD_ISSET(s, &lfd)) { X if ((recv(s, buf, sizeof(buf)-1, 0)<0)) { X perror("recv"); X exit(2); } X else { X printf("%s", buf); X exit(0); } } X else X exit(1); X} END_OF_FILE if test 2479 -ne `wc -c <'udpprobe/udpprobe.c'`; then echo shar: \"'udpprobe/udpprobe.c'\" unpacked with wrong size! fi # end of 'udpprobe/udpprobe.c' fi if test -f 'udpprobe/udpprobe.1' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'udpprobe/udpprobe.1'\" else echo shar: Extracting \"'udpprobe/udpprobe.1'\" \(2747 characters\) sed "s/^X//" >'udpprobe/udpprobe.1' <<'END_OF_FILE' X.\" -*- nroff -*- X.ig X$Id: udpprobe.1,v 1.2 1996/02/13 20:29:11 olaf Exp $ X.. X.TH UDPPROBE 1 "Jan 31, 1994" X.SH NAME Xudpprobe \- send a UDP datagram and look for response X.SH SYNOPSIS X.B udpprobe X[ X.B \-w seconds X] X.B host port X[ X.B text X] X.SH DESCRIPTION X.BR udpprobe Xcreates an Internet domain UDP socket, sends one datagram over it and Xlooks for response. XThe X.BR host Xargument can be an Internet number in dot-notation (like X``129.13.10.91'') or a domain name. In this case it must be possible Xto resolve the name to a valid Internet address with X.IR gethostbyname (3). XThe X.BR port Xargument can be a port number or a service name which can be mapped to Xa port number by X.IR getservbyname (3). XThe optional X.BR text Xargument specifies text to send. If this argument is missing, the text Xis taken from the standard input. If it is a null string, a single Xnull charachter is sent. X.SH OPTIONS X.TP X.IR "\-w seconds" XSpecifies the timeout for response. After sending one datagram, X.BR udpprobe Xwaits at most this amount of time until the host returns a datagram in Xresponse. If a datagram is received, its content is printed on standard Xoutput. If the timeout is zero, X.BR udpprobe Xreturns immediately, discarding any response. Default is one second. X.SH EXAMPLES XThe command X.IP X\fCudpprobe localhost echo "Hello world"\fP X.LP Xsends the string "Hello world" to the local host's UDP echo port. This Xshould return the same string and print it on standard output, Xprovided the UDP echo service is available. X.br XThe command X.IP X\fCudpprobe -w0 mailgate 1001 ""\fP X.LP Xsends a dummy datagram to UDP port 1001 to host "mailgate" in the Xlocal domain and returns immediately. This could be used for signaling Xpurposes - mailgate's X.BR inetd Xcould be set up such that a datagram sent to this port causes a Xmail delivery program to be executed, etc. X.SH DIAGNOSTICS XIf any system call fails, the reason is printed to standard error and Xan exit code of 2 is returned. The exit code is 0 if a response Xdatagram is received, 1 if none is received after the timeout period. X.SH SEE ALSO X.BR socket (2), X.BR connect (2), X.BR gethostbyname (3), X.BR getservbyname (3), X.BR ping (8), X.BR inetd (8) X.SH BUGS XOnly one datagram is sent and received. XThe size of a datagram (for both directions) is limited to 1024 bytes. XWhether this arbitrary size actually fits into a datagram or gets Xtruncated, and what exactly happens when it is truncated, is Xsystem-dependent. X.br XDepending on the network and software environment, it may be Ximpossible to distinguish between a failure to deliver a datagram and Xa delivered datagram with no answer. X.SH VERSION X1.2 as of 13 Feb 1996. X.SH AUTHOR XOlaf Titz X.br XThis software is in the public domain. END_OF_FILE if test 2747 -ne `wc -c <'udpprobe/udpprobe.1'`; then echo shar: \"'udpprobe/udpprobe.1'\" unpacked with wrong size! fi # end of 'udpprobe/udpprobe.1' fi echo shar: End of shell archive. exit 0 -- ___ Olaf.Titz@inka.de or @{stud,informatik}.uni-karlsruhe.de ____ __ o __/<_ >> Just as long as the wheels keep on turning round _)>(_)______________ I will live for the groove 'til the sun goes down << ____