Odds and ends

This commit is contained in:
Joseph Henry 2015-10-12 21:50:41 -04:00
parent 4e1e857949
commit 82052459a2
2 changed files with 58 additions and 59 deletions

View file

@ -53,7 +53,6 @@
namespace ZeroTier { namespace ZeroTier {
NetconEthernetTap::NetconEthernetTap( NetconEthernetTap::NetconEthernetTap(
const char *homePath, const char *homePath,
const MAC &mac, const MAC &mac,
@ -285,6 +284,9 @@ void NetconEthernetTap::closeConnection(TcpConnection *conn)
delete conn; delete conn;
} }
/*
* Close a single RPC connection and associated PhySocket
*/
void NetconEthernetTap::closeClient(PhySocket *sock) void NetconEthernetTap::closeClient(PhySocket *sock)
{ {
for(int i=0; i<rpc_sockets.size(); i++) { for(int i=0; i<rpc_sockets.size(); i++) {
@ -295,6 +297,9 @@ void NetconEthernetTap::closeClient(PhySocket *sock)
_phy.close(sock); _phy.close(sock);
} }
/*
* Close all RPC and TCP connections
*/
void NetconEthernetTap::closeAll() void NetconEthernetTap::closeAll()
{ {
while(rpc_sockets.size()) while(rpc_sockets.size())
@ -357,9 +362,7 @@ void NetconEthernetTap::threadMain()
void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr) void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
{ {
//fprintf(stderr, "phyOnUnixClose() CLOSING: %d\n", _phy.getDescriptor(sock)); // FIXME: What do?
//closeClient(sock);
// FIXME:
} }
/* /*
@ -420,7 +423,6 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns
break; break;
case RPC_KILL_INTERCEPT: case RPC_KILL_INTERCEPT:
fprintf(stderr, "RPC_KILL_INTERCEPT\n"); fprintf(stderr, "RPC_KILL_INTERCEPT\n");
//scloseClient(sock);
break; break;
case RPC_CONNECT: case RPC_CONNECT:
fprintf(stderr, "RPC_CONNECT\n"); fprintf(stderr, "RPC_CONNECT\n");
@ -633,8 +635,7 @@ void NetconEthernetTap::nc_err(void *arg, err_t err)
l->tap->send_return_value(l->conn, -1, ENOMEM); l->tap->send_return_value(l->conn, -1, ENOMEM);
break; break;
case ERR_BUF: case ERR_BUF:
// FIXME: From user's perspective, this is the same as an ENOMEM error. I think. l->tap->send_return_value(l->conn, -1, ENOBUFS);
l->tap->send_return_value(l->conn, -1, ENOMEM);
break; break;
case ERR_TIMEOUT: case ERR_TIMEOUT:
l->tap->send_return_value(l->conn, -1, ETIMEDOUT); l->tap->send_return_value(l->conn, -1, ETIMEDOUT);
@ -752,9 +753,8 @@ err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
*/ */
err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err) err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{ {
//fprintf(stderr, "nc_connected\n");
Larg *l = (Larg*)arg; Larg *l = (Larg*)arg;
l->tap->send_return_value(l->conn, err); l->tap->send_return_value(l->conn, ERR_OK);
return ERR_OK; return ERR_OK;
} }
@ -762,10 +762,29 @@ err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err
----------------------------- RPC Handler functions ---------------------------- ----------------------------- RPC Handler functions ----------------------------
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
/**
* Handles a return value (client's perceived fd) and completes a mapping
* so that we know what connection an RPC call should be associated with.
*
* @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC
*
*/
void NetconEthernetTap::handle_retval(PhySocket *sock, void **uptr, unsigned char* buf)
{
TcpConnection *conn = (TcpConnection*)*uptr;
if(conn->pending) {
memcpy(&(conn->perceived_fd), &buf[1], sizeof(int));
//fprintf(stderr, "handle_retval(): Mapping [our=%d -> their=%d]\n",
//_phy.getDescriptor(conn->dataSock), conn->perceived_fd);
conn->pending = false;
}
}
/* /*
* Handles an RPC to bind an LWIP PCB to a given address and port * Handles an RPC to bind an LWIP PCB to a given address and port
* *
* @param Client that is making the RPC * @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC * @param structure containing the data and parameters for this client's RPC
* *
@ -773,18 +792,18 @@ err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err
[X] EADDRINUSE - The given address is already in use. [X] EADDRINUSE - The given address is already in use.
[X] EBADF - sockfd is not a valid descriptor. [X] EBADF - sockfd is not a valid descriptor.
[X] EINVAL - The socket is already bound to an address. [X] EINVAL - The socket is already bound to an address.
[ ] ENOTSOCK - sockfd is a descriptor for a file, not a socket. [i] ENOTSOCK - sockfd is a descriptor for a file, not a socket.
[ ] The following errors are specific to UNIX domain (AF_UNIX) sockets: [-] The following errors are specific to UNIX domain (AF_UNIX) sockets:
[ ] EACCES - Search permission is denied on a component of the path prefix. (See also path_resolution(7).) [-] EACCES - Search permission is denied on a component of the path prefix. (See also path_resolution(7).)
[ ] EADDRNOTAVAIL - A nonexistent interface was requested or the requested address was not local. [-] EADDRNOTAVAIL - A nonexistent interface was requested or the requested address was not local.
[ ] EFAULT - addr points outside the user's accessible address space. [-] EFAULT - addr points outside the user's accessible address space.
[ ] EINVAL - The addrlen is wrong, or the socket was not in the AF_UNIX family. [-] EINVAL - The addrlen is wrong, or the socket was not in the AF_UNIX family.
[ ] ELOOP - Too many symbolic links were encountered in resolving addr. [-] ELOOP - Too many symbolic links were encountered in resolving addr.
[ ] ENAMETOOLONG - s addr is too long. [-] ENAMETOOLONG - s addr is too long.
[ ] ENOENT - The file does not exist. [-] ENOENT - The file does not exist.
[X] ENOMEM - Insufficient kernel memory was available. [X] ENOMEM - Insufficient kernel memory was available.
[ ] ENOTDIR - A component of the path prefix is not a directory. [-] ENOTDIR - A component of the path prefix is not a directory.
[ ] EROFS - The socket inode would reside on a read-only file system. [-] EROFS - The socket inode would reside on a read-only file system.
*/ */
void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st *bind_rpc) void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st *bind_rpc)
@ -824,16 +843,16 @@ void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st
send_return_value(conn, -1, EINVAL); send_return_value(conn, -1, EINVAL);
} }
} }
else { //else {
fprintf(stderr, "handle_bind(): can't locate connection for PCB\n"); // fprintf(stderr, "handle_bind(): can't locate connection for PCB\n");
send_return_value(conn, -1, EBADF); // FIXME: This makes no sense // send_return_value(conn, -1, EBADF); // FIXME: This makes no sense
} //}
} }
/* /*
* Handles an RPC to put an LWIP PCB into LISTEN mode * Handles an RPC to put an LWIP PCB into LISTEN mode
* *
* @param Client that is making the RPC * @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC * @param structure containing the data and parameters for this client's RPC
* *
@ -872,25 +891,6 @@ void NetconEthernetTap::handle_listen(PhySocket *sock, void **uptr, struct liste
} }
} }
/**
* Handles a return value (client's perceived fd) and completes a mapping
* so that we know what connection an RPC call should be associated with.
*
* @param Client that is making the RPC
* @param structure containing the data and parameters for this client's RPC
*
*/
void NetconEthernetTap::handle_retval(PhySocket *sock, void **uptr, unsigned char* buf)
{
TcpConnection *conn = (TcpConnection*)*uptr;
if(conn->pending) {
memcpy(&(conn->perceived_fd), &buf[1], sizeof(int));
//fprintf(stderr, "handle_retval(): Mapping [our=%d -> their=%d]\n",
//_phy.getDescriptor(conn->dataSock), conn->perceived_fd);
conn->pending = false;
}
}
/* /*
* Handles an RPC to create a socket (LWIP PCB and associated socketpair) * Handles an RPC to create a socket (LWIP PCB and associated socketpair)
* *
@ -899,20 +899,20 @@ void NetconEthernetTap::handle_retval(PhySocket *sock, void **uptr, unsigned cha
* is then required to tell the service what new file descriptor it has allocated * is then required to tell the service what new file descriptor it has allocated
* for this connection. After the mapping is complete, the socket can be used. * for this connection. After the mapping is complete, the socket can be used.
* *
* @param Client that is making the RPC * @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC * @param structure containing the data and parameters for this client's RPC
* *
TODO: set errno appropriately TODO: set errno appropriately
[ ] EACCES - Permission to create a socket of the specified type and/or protocol is denied. [-] EACCES - Permission to create a socket of the specified type and/or protocol is denied.
[?] EAFNOSUPPORT - The implementation does not support the specified address family. [?] EAFNOSUPPORT - The implementation does not support the specified address family.
[?] EINVAL - Unknown protocol, or protocol family not available. [?] EINVAL - Unknown protocol, or protocol family not available.
[?] EINVAL - Invalid flags in type. [?] EINVAL - Invalid flags in type.
[ ] EMFILE - Process file table overflow. [i] EMFILE - Process file table overflow.
[ ] ENFILE - The system limit on the total number of open files has been reached. [i] ENFILE - The system limit on the total number of open files has been reached.
[X] ENOBUFS or ENOMEM - Insufficient memory is available. The socket cannot be created until sufficient resources are freed. [X] ENOBUFS or ENOMEM - Insufficient memory is available. The socket cannot be created until sufficient resources are freed.
[ ] EPROTONOSUPPORT - The protocol type or the specified protocol is not supported within this domain. [?] EPROTONOSUPPORT - The protocol type or the specified protocol is not supported within this domain.
*/ */
void NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc) void NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc)
@ -945,7 +945,7 @@ void NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, struct socke
/* /*
* Handles an RPC to connect to a given address and port * Handles an RPC to connect to a given address and port
* *
* @param Client that is making the RPC * @param PhySocket associated with this RPC connection
* @param structure containing the data and parameters for this client's RPC * @param structure containing the data and parameters for this client's RPC
--- Error handling in this method will only catch problems which are immeidately --- Error handling in this method will only catch problems which are immeidately
@ -961,11 +961,11 @@ void NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, struct socke
[ ] ECONNREFUSED - No-one listening on the remote address. [ ] ECONNREFUSED - No-one listening on the remote address.
[i] EFAULT - The socket structure address is outside the user's address space. [i] EFAULT - The socket structure address is outside the user's address space.
[ ] EINPROGRESS - The socket is nonblocking and the connection cannot be completed immediately. [ ] EINPROGRESS - The socket is nonblocking and the connection cannot be completed immediately.
[ ] EINTR - The system call was interrupted by a signal that was caught. [?] EINTR - The system call was interrupted by a signal that was caught.
[X] EISCONN - The socket is already connected. [X] EISCONN - The socket is already connected.
[?] ENETUNREACH - Network is unreachable. [?] ENETUNREACH - Network is unreachable.
[ ] ENOTSOCK - The file descriptor is not associated with a socket. [ ] ENOTSOCK - The file descriptor is not associated with a socket.
[ ] ETIMEDOUT - Timeout while attempting connection. [X] ETIMEDOUT - Timeout while attempting connection.
* *
*/ */

View file

@ -135,9 +135,8 @@ pthread_mutex_t loglock;
static int is_initialized = 0; static int is_initialized = 0;
static int fdret_sock; // used for fd-transfers static int fdret_sock; // used for fd-transfers
static int newfd; // used for "this_end" socket static int newfd; // used for "this_end" socket
static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab";
static int thispid; static int thispid;
static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab";
/* /*
* Check for forking * Check for forking
@ -166,8 +165,8 @@ void send_command(int rpc_fd, char *cmd)
} }
/* /*
* Reads a return value from the service and sets errno (if applicable) * Reads a return value from the service and sets errno (if applicable)
*/ */
int get_retval() int get_retval()
{ {
if(fdret_sock >= 0) { if(fdret_sock >= 0) {