Wednesday, January 20, 2010

UDP, scripts, and NetCat

The goal was to make a shell script that would communicate to the UDP port on my Labview GSE program. I'm sure that I've done this in the past, probably using rsh and ssh but trying to search for info on that was even harder to find what I was looking for.

The end result is, give up on using a shell script and just compile something quick in C. In fact, complete examples were found on the web. On the way though, I found out a few neat things, in particular about NetCat. Eventually, I figured out how to download NetCat for windows, played with a slightly modified version of the Labview UDP server example, and got NetCat to talk to it after learning a valuable lesson about UDP ports versus TCP ports. (Gotta use the -u in netcat to get it to run in UDP mode. What's interesting is that the same socket number in TCP and UDP seem to be able to exist simultaneously! When I was stuck, I was getting a labview client and server to talk to each other over a particular UDP port while getting two copies of NetCat to talk to each other on the same port number but not to the labview, because I wasn't using -u.)

First off, the basic information about ports in Labview which made it obvious that for my application I wanted to use UDP rather than TCP:

nice overview that points the way towards using UDP ports:

http://zone.ni.com/devzone/cda/tut/p/id/4950

Forum thread reinforcing the above and pointing to examples:

http://forums.ni.com/ni/board/message?board.id=170&thread.id=465668

Another publication of the same overview in HTML:

http://zone.ni.com/reference/en-XX/help/371361E-01/lvconcepts/using_labview_with_tcp_ip_and_udp/

Some links in my struggle to dig up examples of sending UDP datagrams using a shell script:

Interesting section of AWK tutorial that mentions the /inet/ device???

http://74.125.93.132/search?q=cache:BJbtpUrLc9EJ:oreilly.com/catalog/awkprog3/chapter/ch10.html+write+to+udp+port+csh&cd=2&hl=en&ct=clnk&gl=us

A really awesome tutorial on setting up simple socket servers and clients, with an emphasis on TCP and enabling the socket service in inetd. After a few examples of cocking some things together with shell scripts and testing them with telnet to get the basic ideas down, it dives into C examples for examples that are more capable.

http://www.troubleshooters.com/codecorn/sockets/

Check out Q9 in this faq which just has you write to /dev/tcp. I wonder if there's a /dev/udp?

http://kornshell.com/doc/faq.html

finally, an example of using the sockets functions. Seems to be talking about using csh?

http://bytes.com/topic/php/answers/6883-sending-binary-data-via-udp

Links about Netcat:

The thread that pointed me to netcat:

http://74.125.93.132/search?q=cache:5vVKux-MP74J:www.unix.com/shell-programming-scripting/74802-script-send-something-via-udp-intercept-other-end.html+shell+script+udp+port&cd=3&hl=en&ct=clnk&gl=us

An example page for netcat. Also has an example that reminded me that I used to use rsh and ssh to do stuff like this:

http://www.g-loaded.eu/2006/11/06/netcat-a-couple-of-useful-examples/

A somewhat questionable tutorial, in that the emphasis seems to be on hacking other peoples' systems using netcat:

http://www.ol-service.com/sikurezza/doc/netcat_eng2.pdf

Another pointer to netcat, which also has a really nebulous hint about using rsh:

http://www.unix.com/unix-advanced-expert-users/38449-send-message-another-system.html

Getting netcat for windows. There wasn't a handy gnuwin page, but as usual there are plenty of people who have done all the work for me:

Here's the home page for netcat:

http://netcat.sourceforge.net/download.php

A security forum provides a link to the one popular windows port for netcat. Note that the comments are freaking hilarious, yet disturbingly helpful from my own pathetically lost starting point:

http://www.securityfocus.com/cgi-bin/index.cgi?c=toolcomments&op=display_comments&ToolID=139&expand_all=true&mode=threaded

The wikipedia page was unexpectedly helpful, and also provided the link to Jon Craton's blog.

http://en.wikipedia.org/wiki/Netcat

Jon Craton's blog. Oddly his examples are all about hacking as well. However, he led me to the Rodney Beede version:

http://joncraton.org/blog/netcat-for-windows

The Rodney Beede download of netcat, which has the -e option removed, and consequently is not blocked by Windows:

http://www.rodneybeede.com/Compile_Netcat_on_Windows_using_MinGW.html

Compared to what I was able to find about shell script options, tutorials for C coding were plentiful and complete:

I could always read the man page:

http://linux.die.net/man/7/socket

A really beautiful tutorial on programming sockets in C:

http://gnosis.cx/publish/programming/sockets.html

The example UDP C client code handed to me on a plate:

http://www.linuxhowtos.org/C_C++/socket.htm