-2

I developed an application which contains a small http server.

my application is launched in the boot. If I kill it (with kill -9 for example), the http server port will be taken directly by another daemon(acsd from broadcom).

I tried the same behavior with drop-bear, but the problem is not reproduced. If I kill drop-bear the acsd does not take its port.

here after the code of my server:

void http_server_init(void)
{
    struct sockaddr_in server;
    int cr_port;

    for(;;) {
        cr_port = conf.port;
        int i = (DEFAULT_PORT == cr_port)? 1 : 0;
        //Create socket
        cr_socket_desc = socket(AF_INET , SOCK_STREAM , 0);
        if (cr_socket_desc == -1)
        {
            LOG (ERROR,"Could not open server socket, Error no is : %d, Error description is : %s", errno, strerror(errno));
            sleep(1);
            continue;
        }

        /* enable SO_REUSEADDR */
        int reusaddr = 1;
        if (setsockopt(cr_socket_desc, SOL_SOCKET, SO_REUSEADDR, &reusaddr, sizeof(int)) < 0) {
            LOG (WARNING,"setsockopt(SO_REUSEADDR) failed");
        }

        //Prepare the sockaddr_in structure
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        for(;;i++) {
            server.sin_port = htons(cr_port);
            //Bind
            if( bind(cr_socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
            {
                //print the error message
                LOG (ERROR,"Could not bind server socket on the port %d, Error no is : %d, Error description is : %s", cr_port, errno, strerror(errno));
                cr_port = DEFAULT_PORT + i;
                LOG (INFO,"Trying to use another port: %d", cr_port);
                continue;
            }
            break;
        }
        break;
    }
    LOG (INFO,"server initiated with the port: %d", cr_port);
}

What I'm doing wrong in my http server?

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Anis_Stack
  • 3,306
  • 4
  • 30
  • 53
  • What is the port number you're running your client/server? have you tried changing it? – fredmaggiowski Jan 30 '15 at 14:06
  • the port is by default 51005 – Anis_Stack Jan 30 '15 at 14:08
  • you don't do anything wrong. what's drop-bear? – Karoly Horvath Jan 30 '15 at 14:08
  • dropbear is ssh server in linux – Anis_Stack Jan 30 '15 at 14:10
  • maybe it's just a matter of time... sending the kill sig actually kills the program without allowing it to release the port. Try waiting some minutes and see if the OS re-open it Otherwise avoid killing the executable try catching a sig and close the port from the program itself – fredmaggiowski Jan 30 '15 at 14:19
  • Most likely the problem -- if you want to call it that -- is that your server is using a port that `ascd` also wants to use. It is unlikely that killing or shutting down your process *causes* `ascd` to grab the port; more likely, `ascd` is repeatedly trying to bind to that port, and it successfully does so once your server stops blocking it. – John Bollinger Jan 30 '15 at 14:31
  • 1
    It would be worthwhile to test whether `ascd`'s choice of the port in question is in fact associated with your server at all: disable launching your server at boot, but leave `ascd` enabled; reboot, and check whether `ascd` grabs the target port. Also, try assigning a port to your server that you do not ordinarily see `ascd` use, and test whether `ascd` grabs it when your server stops. – John Bollinger Jan 30 '15 at 15:03

2 Answers2

-1

As I said in a comment it could be the fact that the OS is still seeing the port busy from your executable because you killed it and it didn't release the resource.

Googling a little bit I've found this question that is similar to what your problem is.

Now, it seems that the problem is not in your code but in how you manage the kill..

Try using kill instead of kill -9 or try catching the sig so that the executable is able to release its resources by itself

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
  • The stated problem is not that the port is not cleaned up, but rather that a different process grabs it straightaway. That pretty much indicates that the port *is* released when the OP's server stops. At least, the OS sees that there is no longer anyone listening. The `SO_REUSEADDR` option is enabled, so if there is no active listener then anyone can bind. – John Bollinger Jan 30 '15 at 14:34
  • The fact that made me think is the systematic behaviour that OP described. while it's true that SO_REUSEADDR let anyone bind the port once it has been released how is it possible that as soon as it releases the port it suddendly gets taken? – fredmaggiowski Jan 30 '15 at 14:52
  • If `ascd` consistently grabs the port as soon as the OP's server stops, then it must be one that `ascd` specifically wants to use and aggressively tries to grab. This is supported by the observation that `ascd` does not grab ports associated with other services when they get killed. – John Bollinger Jan 30 '15 at 14:58
  • then changing the port should actually solve OP problem? – fredmaggiowski Jan 30 '15 at 15:03
  • 1
    The bottom line is that your answer does not address the question. It is an inherent part of the problem that the OS does **not** see the port still busy. – John Bollinger Jan 30 '15 at 15:08
  • 1
    But yes, if I have analyzed the problem correctly then choosing a port that neither `ascd` nor any other running service wants to use should solve it. That is in fact a routine issue associated with implementing network servers. – John Bollinger Jan 30 '15 at 15:13
-2

since I don't have response for my question. I would like to share with you the response for that problem.

The acsd service take the port because the socket is not closed and the option SO_REUSEADDR is set

Anis_Stack
  • 3,306
  • 4
  • 30
  • 53