Stdout to UDP socket in inetd
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Stdout to UDP socket in inetd




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Stdout to UDP socket in inetd  
Obi-Wan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-10-06 10: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 N
ews==----
http://www.webservertalk.com The #1 Newsgroup Service in the World! 120,000+
 Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----





[ Post a follow-up to this message ]



    Re: Stdout to UDP socket in inetd  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-10-06 10: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.






[ Post a follow-up to this message ]



    Re: Stdout to UDP socket in inetd  
James Carlson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-10-06 10: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





[ Post a follow-up to this message ]



    Re: Stdout to UDP socket in inetd  
Keith Thompson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-11-06 01:47 AM

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.





[ Post a follow-up to this message ]



    Re: Stdout to UDP socket in inetd  
Obi-Wan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-13-06 10: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 N
ews==----
http://www.webservertalk.com The #1 Newsgroup Service in the World! 120,000+
 Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:01 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register