Strange TCP connection (completely duplicate binding) - Unix

This is a discussion on Strange TCP connection (completely duplicate binding) - Unix ; Hello! I found strange TCP connection on my server. The output of netstat -nap: tcp 74160 138880 127.0.0.1:6113 127.0.0.1:6113 ESTABLISHED 4067/tnslsnr Stevens says: "We are never able to start multiple servers that bind the same IP address and same port: ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Strange TCP connection (completely duplicate binding)

  1. Strange TCP connection (completely duplicate binding)

    Hello!

    I found strange TCP connection on my server.
    The output of netstat -nap:

    tcp 74160 138880 127.0.0.1:6113
    127.0.0.1:6113 ESTABLISHED 4067/tnslsnr

    Stevens says:
    "We are never able to start multiple servers that bind the same IP
    address and same
    port: a completely duplicate binding."

    It looks like completely duplicate binding.
    Both sockets are bound to the same address and port.

    Is it normal (correct) connection?

    I tried to simulate this situation.
    I wrote a simple TCP client:
    socket
    setsockopt(SO_REUSEADDR)
    bind
    connect

    I passed the same address and port as already running server use.
    As expected, bind fails with error "Address already in use".

    How to manually create the connection like the one shown above?

    Thank you beforehand!

  2. Re: Strange TCP connection (completely duplicate binding)

    "Dmitry V. Krivenok" writes:
    > I found strange TCP connection on my server.
    > The output of netstat -nap:
    >
    > tcp 74160 138880 127.0.0.1:6113
    > 127.0.0.1:6113 ESTABLISHED 4067/tnslsnr
    >
    > Stevens says:
    > "We are never able to start multiple servers that bind the same IP
    > address and same
    > port: a completely duplicate binding."


    [...]

    > I wrote a simple TCP client:
    > socket
    > setsockopt(SO_REUSEADDR)
    > bind
    > connect
    >
    > I passed the same address and port as already running server use.
    > As expected, bind fails with error "Address already in use".
    >
    > How to manually create the connection like the one shown above?


    #include
    #include
    #include
    #include
    #include
    #include

    int main(void)
    {
    struct sockaddr_in sin;
    int fd, rc;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
    perror("socket");
    exit(1);
    }

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(3333);
    rc = bind(fd, (struct sockaddr *)&sin, sizeof(sin));
    if (rc == -1) {
    perror("bind");
    exit(1);
    }

    rc = connect(fd, (struct sockaddr *)&sin, sizeof(sin));
    if (rc == -1) {
    perror("connect");
    exit(1);
    }

    pause();
    return 0;
    }

  3. Re: Strange TCP connection (completely duplicate binding)

    On Oct 15, 1:09 pm, Rainer Weikusat wrote:
    > "Dmitry V. Krivenok" writes:
    >
    > > I found strange TCP connection on my server.
    > > The output of netstat -nap:

    >
    > > tcp 74160 138880 127.0.0.1:6113
    > > 127.0.0.1:6113 ESTABLISHED 4067/tnslsnr

    >
    > > Stevens says:
    > > "We are never able to start multiple servers that bind the same IP
    > > address and same
    > > port: a completely duplicate binding."

    >
    > [...]
    >
    > > I wrote a simple TCP client:
    > > socket
    > > setsockopt(SO_REUSEADDR)
    > > bind
    > > connect

    >
    > > I passed the same address and port as already running server use.
    > > As expected, bind fails with error "Address already in use".

    >
    > > How to manually create the connection like the one shown above?

    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > struct sockaddr_in sin;
    > int fd, rc;
    >
    > fd = socket(PF_INET, SOCK_STREAM, 0);
    > if (fd == -1) {
    > perror("socket");
    > exit(1);
    > }
    >
    > sin.sin_family = AF_INET;
    > sin.sin_addr.s_addr = INADDR_ANY;
    > sin.sin_port = htons(3333);
    > rc = bind(fd, (struct sockaddr *)&sin, sizeof(sin));
    > if (rc == -1) {
    > perror("bind");
    > exit(1);
    > }
    >
    > rc = connect(fd, (struct sockaddr *)&sin, sizeof(sin));
    > if (rc == -1) {
    > perror("connect");
    > exit(1);
    > }
    >
    > pause();
    > return 0;
    >
    > }


    Have you tried this code?

    Please look at results:

    krivenok@develop2 14:26:04 /tmp/cli $ cat cli.c
    #include
    #include
    #include
    #include
    #include
    #include

    int main(void)
    {
    struct sockaddr_in sin;
    int fd, rc;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
    perror("socket");
    exit(1);
    }

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(9997);
    rc = bind(fd, (struct sockaddr *)&sin, sizeof(sin));
    if (rc == -1) {
    perror("bind");
    exit(1);
    }

    rc = connect(fd, (struct sockaddr *)&sin, sizeof(sin));
    if (rc == -1) {
    perror("connect");
    exit(1);
    }

    pause();
    return 0;
    }
    krivenok@develop2 14:26:14 /tmp/cli $ gcc cli.c
    krivenok@develop2 14:26:21 /tmp/cli $ ./a.out
    bind: Address already in use
    krivenok@develop2 14:26:23 /tmp/cli $


    Your script works as expected, i.e. it's impossible to create
    connection like
    127.0.0.1:9997 -> 127.0.0.1:9997
    using it.

    However, such connection was created somehow as I wrote above.

    I just want to know is it possible to create such connection normally?
    Or maybe it's a bug in kernel's network stack.

    Thanks.

  4. Re: Strange TCP connection (completely duplicate binding)

    "Dmitry V. Krivenok" writes:

    [please see original]

    > krivenok@develop2 14:26:14 /tmp/cli $ gcc cli.c
    > krivenok@develop2 14:26:21 /tmp/cli $ ./a.out
    > bind: Address already in use
    > krivenok@develop2 14:26:23 /tmp/cli $
    >
    >
    > Your script works as expected, i.e. it's impossible to create
    > connection like
    > 127.0.0.1:9997 -> 127.0.0.1:9997
    > using it.


    Since the program (not 'script') already aborts when trying to bind to
    the apparently already used port, it is hardly surprising that it
    never calls connect.

    > However, such connection was created somehow as I wrote above.
    >
    > I just want to know is it possible to create such connection
    > normally?


    Stop wasting your time on that. Get compentent help in case of future
    'computer mysteries'.

  5. Re: Strange TCP connection (completely duplicate binding)

    Rainer Weikusat writes:

    [...]

    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > struct sockaddr_in sin;
    > int fd, rc;
    >
    > fd = socket(PF_INET, SOCK_STREAM, 0);
    > if (fd == -1) {
    > perror("socket");
    > exit(1);
    > }
    >
    > sin.sin_family = AF_INET;
    > sin.sin_addr.s_addr = INADDR_ANY;
    > sin.sin_port = htons(3333);
    > rc = bind(fd, (struct sockaddr *)&sin, sizeof(sin));
    > if (rc == -1) {
    > perror("bind");
    > exit(1);
    > }
    >
    > rc = connect(fd, (struct sockaddr *)&sin, sizeof(sin));
    > if (rc == -1) {
    > perror("connect");
    > exit(1);
    > }
    >
    > pause();
    > return 0;
    > }


    A more interesting observation on this: The program above works on
    Linux 2.6.26 (at least, it connects sucessfully, as can be verified by
    inserting something like puts("hi") before the pause), provided that
    listen is not called prior to connect. When listen was called, connect
    fails with EISCONN.

  6. Re: Strange TCP connection (completely duplicate binding)

    On Oct 15, 2:44 pm, Rainer Weikusat wrote:
    > "Dmitry V. Krivenok" writes:
    >
    > [please see original]
    >
    > > krivenok@develop2 14:26:14 /tmp/cli $ gcc cli.c
    > > krivenok@develop2 14:26:21 /tmp/cli $ ./a.out
    > > bind: Address already in use
    > > krivenok@develop2 14:26:23 /tmp/cli $

    >
    > > Your script works as expected, i.e. it's impossible to create
    > > connection like
    > > 127.0.0.1:9997 -> 127.0.0.1:9997
    > > using it.

    >
    > Since the program (not 'script') already aborts when trying to bind to
    > the apparently already used port, it is hardly surprising that it
    > never calls connect.


    Exactly!
    It never calls connect.
    It's absolutely correct behavior.
    Therefore, it's impossible to establish connection like
    127.0.0.1:6113 -> 127.0.0.1:6113

    However, I see this TCP connection on the real server:

    tcp 74160 138880 127.0.0.1:6113 127.0.0.1:6113
    ESTABLISHED 4067/tnslsnr

    >
    > > However, such connection was created somehow as I wrote above.

    >
    > > I just want to know is it possible to create such connection
    > > normally?

    >
    > Stop wasting your time on that. Get compentent help in case of future
    > 'computer mysteries'.



  7. Re: Strange TCP connection (completely duplicate binding)

    "Dmitry V. Krivenok" writes:
    > On Oct 15, 2:44 pm, Rainer Weikusat wrote:
    >> "Dmitry V. Krivenok" writes:
    >>
    >> [please see original]
    >>
    >> > krivenok@develop2 14:26:14 /tmp/cli $ gcc cli.c
    >> > krivenok@develop2 14:26:21 /tmp/cli $ ./a.out
    >> > bind: Address already in use
    >> > krivenok@develop2 14:26:23 /tmp/cli $

    >>
    >> > Your script works as expected, i.e. it's impossible to create
    >> > connection like
    >> > 127.0.0.1:9997 -> 127.0.0.1:9997
    >> > using it.

    >>
    >> Since the program (not 'script') already aborts when trying to bind to
    >> the apparently already used port, it is hardly surprising that it
    >> never calls connect.

    >
    > Exactly!
    > It never calls connect.


    .... because you have modified my original code to attempt to bind to a
    port which happens to be ALREADY USED ON YOUR SYSTEM. Therefore THE
    PROGRAM DOES NOT RUN TO COMPLETION ANYMORE and hence IT DOES NOT CALL
    CONNECT.

    Idiot.


  8. Re: Strange TCP connection (completely duplicate binding)

    "Dmitry V. Krivenok" writes:
    > On Oct 15, 2:44 pm, Rainer Weikusat wrote:
    >> "Dmitry V. Krivenok" writes:
    >>
    >> [please see original]
    >>
    >> > krivenok@develop2 14:26:14 /tmp/cli $ gcc cli.c
    >> > krivenok@develop2 14:26:21 /tmp/cli $ ./a.out
    >> > bind: Address already in use
    >> > krivenok@develop2 14:26:23 /tmp/cli $

    >>
    >> > Your script works as expected, i.e. it's impossible to create
    >> > connection like
    >> > 127.0.0.1:9997 -> 127.0.0.1:9997
    >> > using it.

    >>
    >> Since the program (not 'script') already aborts when trying to bind to
    >> the apparently already used port, it is hardly surprising that it
    >> never calls connect.

    >
    > Exactly!
    > It never calls connect.


    .... because you have modified my original code to attempt to bind to a
    port which happens to be ALREADY USED ON YOUR SYSTEM. Therefore THE
    PROGRAM DOES NOT RUN TO COMPLETION ANYMORE and hence IT DOES NOT CALL
    CONNECT.

    [insert expletive]


+ Reply to Thread