Hi:

Inside /usr/src/usr.bin/ftp/ftp.c:initconn(), I can not understand the
following piece of code:

My problem is with SO_REUSEADDR. The first time,
sendport = -1, so data_addr.su_port = 0 and SO_REUSEADDR
is not call. Then the port is send to the server.

If something goes wrong, sendport =0 && goto noport.
This time, it is called to SO_REUSEADDR, 

why is it called now and not the first time ?

Also, if sendport = 0 the port is not send to the server, then
how is it openned the data connection ? By default, the data
connection is on port 20, but the code does not specified any
port at bind() call.


-- 
*********************************
Juan F. Rodriguez Hervella
Universidad Carlos III de Madrid
********************************
noport:
        data_addr = myctladdr;
        if (sendport)
                data_addr.su_port = 0;  /* let system pick one */
        if (data != -1)
                (void)close(data);
        data = socket(data_addr.su_family, SOCK_STREAM, 0);
        if (data < 0) {
                warn("socket");
                if (tmpno)
                        sendport = 1;
                return (1);
        }
        if (!sendport)
                if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
                                sizeof(on)) < 0) {
                        warn("setsockopt (reuse address)");
                        goto bad;
                }     

        if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
                warn("bind");
                goto bad;
        }
        if (options & SO_DEBUG &&
            setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
                        sizeof(on)) < 0)
                warn("setsockopt (ignored)");
        len = sizeof(data_addr);
        if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
                warn("getsockname");
                goto bad;
        }
        if (listen(data, 1) < 0)
                warn("listen");

        if (sendport) {        
                bla...bla...bla..        

        skip_port:
 
                if (result == ERROR && sendport == -1) {
                        sendport = 0;
                        tmpno = 1;
                        goto noport;
                }
                return (result != COMPLETE);
        }       

        if (tmpno)
                sendport = 1;
        return (0);
bad:
        (void)close(data), data = -1;
        if (tmpno)
                sendport = 1;
        return (1);
}    

Reply via email to