Unix Programming - Stdout to UDP socket in inetd

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Stdout to UDP socket in inetd





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Stdout to UDP socket in inetd
Obi-Wan

2006-03-10, 5:57 pm

I'm trying to write a daemon in C that runs under (x)inetd that
communicates over UDP. I'm trying to write a simple DNS server that
returns the same IP for every query it receives. Things work just
fine reading the request, but when I try to write(2) the response, it
returns -1 with errno 89: "Destination address required". This seems
to indicate the wrong setting on a socket. Here's my xinetd config
paragraph:

------------------------- Achilles tendon - cut here -------------------------
service domain
{
port = 53
protocol = udp
socket_type = dgram
wait = yes
only_from = 127.0.0.1
user = root
server = /usr/local/sbin/dns
log_on_failure += USERID
disable = no
}
------------------------- Achilles tendon - cut here -------------------------

The write that's failing is the first one I'm calling, and it takes
place less than a second after the request comes in. I verified this
with the following program:

------------------------- Achilles tendon - cut here -------------------------
main() {
int err;
char *buf = "hello";

openlog("dns", LOG_PID, LOG_DAEMON);
if ((err = write(1, buf, 1)) != 1)
syslog(LOG_ERR, "write returned %d: errno %d: %s",
err, errno, strerror(errno));
closelog();
return 0;
}
------------------------- Achilles tendon - cut here -------------------------

If I run that from the command line, it prints an "h". From xinetd,
it logs:
write returned -1: errno 89: Destination address required

What am I doing wrong?

I suppose I could rewrite this thing to act as a standalone daemon,
but that's more work & a lot of refresher reading on socket programming.

If it matters, this is CentOS (RedHat) linux with a 2.4 kernel.

--
Ben "Obi-Wan" Hollingsworth obiwan@jedi.com
The stuff of earth competes for the allegiance I owe only to the
Giver of all good things, so if I stand, let me stand on the
promise that You will pull me through. -- Rich Mullins

----== Posted via webservertalk.com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.webservertalk.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Maxim Yegorushkin

2006-03-10, 5:57 pm


Obi-Wan wrote:
> I'm trying to write a daemon in C that runs under (x)inetd that
> communicates over UDP. I'm trying to write a simple DNS server that
> returns the same IP for every query it receives. Things work just
> fine reading the request, but when I try to write(2) the response, it
> returns -1 with errno 89: "Destination address required". This seems
> to indicate the wrong setting on a socket.


To be able to call write() on a UDP socket you first need to set the
default destination address by calling connect() on the socket. I don't
know if xinetd can does that for you.

James Carlson

2006-03-10, 5:57 pm

Obi-Wan <bvh@tatooine.jedi.com> writes:
> I'm trying to write a daemon in C that runs under (x)inetd that
> communicates over UDP. I'm trying to write a simple DNS server that
> returns the same IP for every query it receives. Things work just
> fine reading the request, but when I try to write(2) the response, it
> returns -1 with errno 89: "Destination address required". This seems
> to indicate the wrong setting on a socket. Here's my xinetd config
> paragraph:


That's not going to work. As a UDP wait-type service, you're given
the actual socket that the inetd process has.

In other words, you should be doing something like this:

char buffer[1024];
struct sockaddr_in from;
int fromlen, retv;

for (;;) {
/* Kill myself if I've been idle for a while */
alarm(10);
fromlen = sizeof (from);
retv = recvfrom(0, buffer, sizeof (buffer), 0, &from,
&fromlen);
... do something ...
sendto(1, replymsg, replylen, 0, &from, fromlen);
}

I think there are some useful examples of this in the Stevens
networking books.

--
James Carlson, KISS Network <james.d.carlson@sun.com>
Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
Keith Thompson

2006-03-10, 8:47 pm

Obi-Wan <bvh@tatooine.jedi.com> writes:
> I'm trying to write a daemon in C that runs under (x)inetd that
> communicates over UDP.

[...]

Standard C does not provide any form of networking. Please drop
comp.lang.c from the newsgroups line if you post a followup. Thanks.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Obi-Wan

2006-03-13, 5:54 pm

In comp.unix.programmer James Carlson <james.d.carlson@sun.com> wrote:
> That's not going to work. As a UDP wait-type service, you're given
> the actual socket that the inetd process has.
>
> In other words, you should be doing something like this:
> retv = recvfrom(0, buffer, sizeof (buffer), 0, &from,
> &fromlen);
> sendto(1, replymsg, replylen, 0, &from, fromlen);


Excellent. That did the trick. Thanks for the sample code.

> I think there are some useful examples of this in the Stevens
> networking books.


I've got the Comer/Stevens TCP/IP trilogy. Now that I know what to
look for, I was able to find similar code in volume 3. I'd like
to pick up his Unix Network programming book someday. My current
job as a sysadmin just doesn't require me to do much C programming
any more. :-(

--
Ben "Obi-Wan" Hollingsworth obiwan@jedi.com
The stuff of earth competes for the allegiance I owe only to the
Giver of all good things, so if I stand, let me stand on the
promise that You will pull me through. -- Rich Mullins

----== Posted via webservertalk.com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.webservertalk.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com