2

I'm trying to implement an SMTP server.
The problem is after my socket accepts a new connection, it receives no bytes (blank line in example output) and therefore my read loop is terminated.

I tried sending 220 and 250 commands after the initial accept, but still without success.


My output:

Socket created
Waiting for incoming connections...
Connection accepted
250 mail.domain.com says hello

Finished reading current message

My code:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAX_CONNECTIONS 10
#define BUF_SIZE        2000
#define STREQU(a,b)     (strcmp(a, b) == 0)

void init_local_sockaddr_in(struct sockaddr_in *sock, int portnum) {
    sock->sin_family = AF_INET;
    sock->sin_addr.s_addr = inet_addr("127.0.0.1");
    sock->sin_port = htons(portnum);
}

int main(int argc, char *argv[])
{
    int listen_sockfd, input_sockfd, c, read_size, n;
    struct sockaddr_in server, client;
    char client_message[2000] = {0};
    char bufferout[2000] = {0};

    listen_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (listen_sockfd == -1) {
        printf("Could not create sockets");
    }
    puts("Socket created");

    // Just for better debugging
    int optval = 1;
    setsockopt(listen_sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int));

    init_local_sockaddr_in(&server, 10025);
    n = bind(listen_sockfd, (struct sockaddr*) &server, sizeof(server));
    if (n < 0) {
        perror("bind failed. Error");
        return 1;
    }

    listen(listen_sockfd , MAX_CONNECTIONS);

    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    while(1) {
        input_sockfd = accept(listen_sockfd, (struct sockaddr*) &client, (socklen_t*) &c);
        if (input_sockfd < 0) {
            perror("accept failed");
            return 1;
        }
        puts("Connection accepted");

        sprintf(bufferout, "250 mail.domain.com says hello\r\n"); // I tried different replies here, including 220
        send(input_sockfd, bufferout, strlen(bufferout), 0);
        puts(bufferout);

        int keep_running = 1;

        while (keep_running) {
            n = read(listen_sockfd, client_message, sizeof(client_message));
            if (STREQU(client_message, ".") || n < 0) {
                keep_running = 0;
                break;
            }
            printf("Read %lu bytes: %s\n", strlen(client_message), client_message);
            memset(client_message, 0, sizeof(client_message));
        }
        close(input_sockfd);
        puts("Finished reading current message");
    }
    close(listen_sockfd);

    return 0;
}
GalAbra
  • 5,048
  • 4
  • 23
  • 42
  • @kiranBiradar Sorry for that, I tried simplifying my code and didn't notice this buffer. The whole process starts once an incoming mail arrives – GalAbra Jun 17 '19 at 06:11

1 Answers1

2

You are reading from the wrong socket. accept returns the socket that you should read from, i.e. input_sockfd. You are reading from the socket that you passed to accept, which is not connected to the client, so read correctly returns 0 bytes.

You also have an off-by-one in the check n < 0. read returns 0 when the connection is closed, so you really should check for n <= 0, else the loop will go on forever, always reading 0 bytes.

rtoijala
  • 1,200
  • 10
  • 20
  • Omg great catch! I don't believe that was eventually the issue. I'm sitting on variations of this for more than two days and got confused by the amount of time. Thank you very much! – GalAbra Jun 17 '19 at 06:21
  • @GalAbra Added a second bugfix to the answer. – rtoijala Jun 17 '19 at 06:26