[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Adding new network service - how?

On Mon, 26 Jun 2000, Art Sackett wrote:

> Greetings, All:
> Please forgive me if this is documented somewhere -- a pointer to the 
> documentation would be greatly appreciated!
> I've got a custom (just wrote it) standalone TCP/IP server daemon that listens 
> on a high port and works fine servicing connections from localhost. However, 
> when I try to connect (via telnet) from any other machine on the network, I 
> get "connection refused". I tried editing /etc/hosts.allow so it contains the 
> single line ALL:ALL and then /etc/init.d/netbase restart, to no avail. 
> (Reverted back to what it used to be right after -- it's an internet-connected 
> machine.)

NOTE: I have absolutely no idea what I'm talking about. This works for me. YMMV.

Are you binding to a specific IP address (eg,, or just
If you bind to a specific IP, only packets coming in on that interface
will actually appear. I've written a small daemon which handles multiple
requests by forking; I do something like this:

listen = serve(port);

while( 1 ) {

   request = getrequest(listen);
   pid = fork();
   if( pid == -1 ) exit_error("fork");
   if( pid )
   else {
      // Handle connection, reading and writing from/to request.
      // Exit when done.


The getrequest function uses memset to clear the contents of the
sockaddr_in structure, thus initializing the address to (and
setting the rest to sane values).

Ah, what the hell, I'll just list the whole function :P

int serve(unsigned short int port)
        int client;
        struct sockaddr_in client_addr;
        int opt, val;

        client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if(client == -1) {
                exiterr("socket create");

        opt = 1;
        val = setsockopt(client, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
        if( val == -1 ) {
                exiterr("setsockopt reuseaddr");
#ifdef SO_REUSEPORT             /* not def in Linux yet */
        val = setsockopt(client, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
        if( val == -1 )
                exiterr("setsockopt reuseport");

        memset(&client_addr, 0, sizeof(client_addr));

        client_addr.sin_family = AF_INET;
        client_addr.sin_port   = htons(port);

        if(bind(client, (struct sockaddr *)&client_addr, sizeof(client_addr))) {
                exiterr("socket create");

        listen(client, 0);

Immensely useful to me was the netcat source (almost as useful as the
netcat binary). Netcat does everything, so it's a good program to use for

Hopefully what I've included is useful, you can do whatever you'd like
with the code; it's not exactly a trade secret.

Questions, comments, flames, sister's phone numbers welcome.

-chet (chosey@budapress.com)

> I know it has to be an easy, probably obvious, operation to get this port 
> opened up, I just cannot find it. Any help anyone can provide would be 
> appreciated.
> -- 
> ----   Art Sackett   ----
> --  
> To UNSUBSCRIBE, email to debian-isp-request@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Reply to: