@@ -1512,91 +1512,79 @@ impl Cage {
15121512
15131513 /// ### Description
15141514 ///
1515- /// `accept_syscall` accepts a connection on a socket
1515+ /// `listen_syscall` listen for connections on a socket
15161516 ///
15171517 /// ### Arguments
15181518 ///
15191519 /// it accepts two parameters:
1520- /// * `fd` - the file descriptor that refers to the listening socket
1521- /// * `addr` - the address of the peer socket (i.e. the client that is connecting)
1522- /// // ** Do we deal with the case in which addr may be NULL ?? ** //
1520+ /// * `sockfd` - a file descriptor that refers to a socket
1521+ /// of type SOCK_STREAM or SOCK_SEQPACKET.
1522+ /// // ** Do we deal with SOCK_SEQPACKET ?? ** //
1523+ /// * `backlog` - defines the maximum length to which the
1524+ /// queue of pending connections for sockfd may grow. If a
1525+ /// connection request arrives when the queue is full, the client may
1526+ /// receive an error with an indication of ECONNREFUSED or, if the
1527+ /// underlying protocol supports retransmission, the request may be
1528+ /// ignored so that a later reattempt at connection succeeds.
15231529 ///
15241530 /// ### Returns
15251531 ///
1526- /// for a successful call, the return value will be a file descriptor for the
1527- /// accepted socket (a nonnegative integer). On error, -errno is
1532+ /// for a successful call, zero is returned. On error, -errno is
15281533 /// returned and errno is set to indicate the error
15291534 ///
15301535 /// ### Errors
15311536 ///
1532- /// * EAGAIN or EWOULDBLOCK - The socket is marked nonblocking and no
1533- /// connections are present to be accepted.
1534- /// POSIX.1-2001 and POSIX.1-2008
1535- /// allow either error to be returned for this case, and do
1536- /// not require these constants to have the same value, so a
1537- /// portable application should check for both possibilities.
1537+ /// * EADDRINUSE - Another socket is already listening on the same port.
15381538 ///
1539- /// * EBADF - sockfd is not an open file descriptor.
1539+ /// * EADDRINUSE - (Internet domain sockets) The socket referred to by sockfd
1540+ /// had not previously been bound to an address and, upon
1541+ /// attempting to bind it to an ephemeral port, it was
1542+ /// determined that all port numbers in the ephemeral port
1543+ /// range are currently in use. See the discussion of
1544+ /// /proc/sys/net/ipv4/ip_local_port_range in ip(7).
15401545 ///
1541- /// * ECONNABORTED - A connection has been aborted.
1542- ///
1543- /// * EFAULT - The addr argument is not in a writable part of the user
1544- /// address space.
1545- ///
1546- /// * EINTR - The system call was interrupted by a signal that was
1547- /// caught before a valid connection arrived; see signal(7).
1548- ///
1549- /// * EINVAL - Socket is not listening for connections, or addrlen is
1550- /// invalid (e.g., is negative).
1551- ///
1552- /// * EMFILE - The per-process limit on the number of open file
1553- /// descriptors has been reached.
1554- ///
1555- /// * ENFILE - The system-wide limit on the total number of open files
1556- /// has been reached.
1557- ///
1558- /// * ENOMEM - Not enough free memory. This often means that the memory
1559- /// allocation is limited by the socket buffer limits, not by
1560- /// the system memory.
1546+ /// * EBADF - The argument sockfd is not a valid file descriptor.
15611547 ///
15621548 /// * ENOTSOCK - The file descriptor sockfd does not refer to a socket.
15631549 ///
1564- /// * EOPNOTSUPP - The referenced socket is not of type SOCK_STREAM.
1565- ///
1566- /// * EPERM - Firewall rules forbid connection.
1567- ///
1568- /// * EPROTO - Protocol error.
1569- ///
1570- /// In addition, network errors for the new socket and as defined for
1571- /// the protocol may be returned. Various Linux kernels can return
1572- /// other errors such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT,
1573- /// ETIMEDOUT. The value ERESTARTSYS may be seen during a trace.
1550+ /// * EOPNOTSUPP - The socket is not of a type that supports the listen()
1551+ /// operation.
15741552 ///
15751553 /// ### Panics
15761554 ///
15771555 /// * invalid or out-of-bounds file descriptor), calling unwrap() on it will cause a panic.
1578- /// * Unknown errno value from fcntl returned, will cause panic.
1556+ /// * unknown errno value from socket bind sys call from libc in the case
1557+ /// that the socket isn't assigned an address
1558+ /// * unknown errno value from socket listen sys call from libc
15791559 ///
15801560 /// for more detailed description of all the commands and return values, see
1581- /// [accept (2)](https://linux.die.net/man/2/accept )
1561+ /// [listen (2)](https://linux.die.net/man/2/listen )
15821562 //
1583- // ** we currently ignore backlog **
1584- // ** What does the above comment mean ?? ** //
1563+ // TODO: We are currently ignoring backlog
15851564 pub fn listen_syscall ( & self , fd : i32 , _backlog : i32 ) -> i32 {
1565+ //If fd is out of range of [0,MAXFD], process will panic
1566+ //Otherwise, we obtain a write gaurd to the Option<FileDescriptor> object
15861567 let checkedfd = self . get_filedescriptor ( fd) . unwrap ( ) ;
15871568 let mut unlocked_fd = checkedfd. write ( ) ;
15881569 if let Some ( filedesc_enum) = & mut * unlocked_fd {
15891570 match filedesc_enum {
1571+ //If the file descriptor refers to a socket
15901572 Socket ( ref mut sockfdobj) => {
15911573 //get or create the socket and bind it before listening
1574+ //Gain write access to the socket handle
15921575 let sock_tmp = sockfdobj. handle . clone ( ) ;
15931576 let mut sockhandle = sock_tmp. write ( ) ;
15941577
1578+ //If the given socket is already listening, return with
1579+ //success
15951580 match sockhandle. state {
15961581 ConnState :: LISTEN => {
1597- return 0 ; //Already done!
1582+ return 0 ;
15981583 }
15991584
1585+ //If the given socket is connected to another socket or
1586+ //if a non blocking socket is in progress of connecting
1587+ //to another socket
16001588 ConnState :: CONNECTED
16011589 | ConnState :: CONNRDONLY
16021590 | ConnState :: CONNWRONLY
@@ -1608,7 +1596,11 @@ impl Cage {
16081596 ) ;
16091597 }
16101598
1599+ //If the given socket is not connected, it is ready
1600+ //to begin listening
16111601 ConnState :: NOTCONNECTED => {
1602+ //If the given socket is not a TCP socket, then the
1603+ //socket can not listen for connections
16121604 if sockhandle. protocol != IPPROTO_TCP {
16131605 return syscall_error (
16141606 Errno :: EOPNOTSUPP ,
@@ -1617,12 +1609,15 @@ impl Cage {
16171609 ) ;
16181610 }
16191611
1620- // simple if it's a domain socket
1612+ //If the given socket is a Unix socket, lind handles
1613+ //the connection, return with success
16211614 if sockhandle. domain == AF_UNIX {
16221615 sockhandle. state = ConnState :: LISTEN ;
16231616 return 0 ;
16241617 }
16251618
1619+ //If the given socket is not assigned an address,
1620+ //attempt to bind the socket to an address.
16261621 if sockhandle. localaddr . is_none ( ) {
16271622 let shd = sockhandle. domain as i32 ;
16281623 let ibindret = self . _implicit_bind ( & mut * sockhandle, shd) ;
@@ -1634,18 +1629,27 @@ impl Cage {
16341629 }
16351630 }
16361631
1637- let ladr = sockhandle. localaddr . unwrap ( ) . clone ( ) ; //must have been populated by implicit bind
1632+ //The socket must have been assigned an address by implicit bind
1633+ let ladr = sockhandle. localaddr . unwrap ( ) . clone ( ) ;
1634+ //Grab a tuple of the address, port, and port type
1635+ //to be inserted into the set of listening ports
16381636 let porttuple = mux_port (
16391637 ladr. addr ( ) . clone ( ) ,
16401638 ladr. port ( ) ,
16411639 sockhandle. domain ,
16421640 TCPPORT ,
16431641 ) ;
16441642
1643+ //Set the socket connection state to listening
1644+ //to readily accept connections
16451645 NET_METADATA . listening_port_set . insert ( porttuple. clone ( ) ) ;
16461646 sockhandle. state = ConnState :: LISTEN ;
16471647
1648- let listenret = sockhandle. innersocket . as_ref ( ) . unwrap ( ) . listen ( 5 ) ; //default backlog in repy for whatever reason, we replicate it
1648+ //Call listen from libc on the socket
1649+ //Set the backlog to 5:
1650+ //default backlog in repy for whatever reason, we replicate it
1651+ //** Would we ever want to change the backlog?? **/
1652+ let listenret = sockhandle. innersocket . as_ref ( ) . unwrap ( ) . listen ( 5 ) ;
16491653 if listenret < 0 {
16501654 let lr = match Errno :: from_discriminant ( interface:: get_errno ( ) ) {
16511655 Ok ( i) => syscall_error (
@@ -1657,30 +1661,44 @@ impl Cage {
16571661 panic ! ( "Unknown errno value from socket listen returned!" )
16581662 }
16591663 } ;
1664+ //Remove the tuple of the address, port, and
1665+ //port type from the set of listening ports
1666+ //as we are returning from an error
1667+ //** Why dont we use 'porttuple' as the argument for the key ?? */
16601668 NET_METADATA . listening_port_set . remove ( & mux_port (
16611669 ladr. addr ( ) . clone ( ) ,
16621670 ladr. port ( ) ,
16631671 sockhandle. domain ,
16641672 TCPPORT ,
16651673 ) ) ;
1674+ //Set the socket state to NOTCONNECTED, as
1675+ //the socket is not listening
16661676 sockhandle. state = ConnState :: NOTCONNECTED ;
16671677 return lr;
16681678 } ;
16691679
1670- //set rawfd for select
1680+ //set rawfd for select sys call
1681+ //** Why is this being done in listen ??
1682+ // We are also doing it in connect right now **/
16711683 sockfdobj. rawfd = sockhandle. innersocket . as_ref ( ) . unwrap ( ) . raw_sys_fd ;
16721684
1685+ //If listening socket is not in the table of pending
1686+ //connections, we must insert it as the key with
1687+ //an empty vector as the value
1688+ //We can now track incoming connections
16731689 if !NET_METADATA . pending_conn_table . contains_key ( & porttuple) {
16741690 NET_METADATA
16751691 . pending_conn_table
16761692 . insert ( porttuple. clone ( ) , vec ! [ ] ) ;
16771693 }
16781694
1679- return 0 ;
1695+ return 0 ; //return on success
16801696 }
16811697 }
16821698 }
16831699
1700+ //Otherwise, the file descriptor refers to something other
1701+ //than a socket, return with error
16841702 _ => {
16851703 return syscall_error (
16861704 Errno :: ENOTSOCK ,
@@ -1689,6 +1707,7 @@ impl Cage {
16891707 ) ;
16901708 }
16911709 }
1710+ //Otherwise, file descriptor is invalid, return with error
16921711 } else {
16931712 return syscall_error ( Errno :: EBADF , "listen" , "invalid file descriptor" ) ;
16941713 }
0 commit comments