Thread: Help using unix socket for daemon

  1. #1
    Registered User
    Join Date
    Jan 2004

    Help using unix socket for daemon

    I found this small daemon program that listens on a port. I want to write something that emulates a smtp daemon. I learned some things from the code in the small daemon, and wrote one for my purposes. The problem is, that it will not run. It will compile, and "./infection" will work, but it will not start as a process nor listen on a port as I need it to do. I edited it and put a loop in maybe thinking it would help it run (maybe not =P), well, not it just fork bombs me.

    Here is the code, maybe you can help me figure out what i'm doing wrong:

    /* Infection, Fake SMTP Daemon- By Tal0n 01-05-04 */
    /* Include the headers and etc that we need. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <signal.h>
    /* Define port we are using. */
    #define PORT 25
    /* Define the message we want displayed. */
    #define MSG "220 localhost ESMTP Sendmail 8.11.3/8.11.3; Mon, 5 Jan 2004 13:56:31 -0800"
    /* Begin Program */
    int main(int argc, char *argv[]);
    int main(int argc, char *argv[])
    /* Defining what the process's name will be. */
    strcpy(argv[0], "fake-smtpd");
    signal(SIGCHLD, SIG_IGN);
    /* Declare sockets and such. */
    int sockfd, newfd, size;
    struct sockaddr_in local;
    struct sockaddr_in remote;
    /* Zero the rest of the struct. */
    bzero(&local, sizeof(local));
    /* Get sockets ready to use. */
    local.sin_family = AF_INET;
    local.sin_port = (PORT);
    local.sin_addr.s_addr = INADDR_ANY;
    /* Zero the rest of the struct. */
    bzero(&(local.sin_zero), 8);
    /* Defining 'size' as the sizeof the socket. */
    size = sizeof(struct sockaddr_in);
    while(1) {
    /* Send the message if a connection is made. */
    if(!fork()) {
    send(newfd, MSG, sizeof(MSG), 0);
    /* Close Sockets. */
    close(newfd); } }
    /* End Program. */
    return 0;
    /* EOF */
    All help would be much appreciated.

    Thank you,


  2. #2
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    >> int main(int argc, char *argv[]);
    >> int main(int argc, char *argv[])

    I think, you don't need prototype for main().
    And your syntax for prototype is wrong.
    int func(int, char **);

    >> signal(SIGCHLD, SIG_IGN);

    I'm not sure this is the right way.
    You should catch SIGCHLD and call wait() from signal handler to avoid a zombie.
    [edit] AFAIK only System V and Unix 98 that signal(SIGCHLD,SIG_IGN) does not generate zombie.

    >> local.sin_port = (PORT);
    replaced with local.sin_port=htons(PORT);

    >> local.sin_addr.s_addr = INADDR_ANY;
    replaced with local.sin_addr.s_addr=htonl(INADDR_ANY)

    Your code doesn't generate a daemon, parent must fork() a child then parent terminated, leaving the child running.

    And it seems calls to socket(), bind() and listen() are missing.
    Consult your man pages.
    Last edited by Jaguar; 01-14-2004 at 02:55 AM.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Datagram Unix socket problem
    By karas in forum C Programming
    Replies: 9
    Last Post: 07-27-2006, 10:08 AM
  3. Packet Filter using Unix Socket
    By doraiashok in forum Networking/Device Communication
    Replies: 2
    Last Post: 12-12-2003, 08:14 AM
  4. unix socket
    By rotis23 in forum Linux Programming
    Replies: 4
    Last Post: 10-15-2003, 03:33 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM