Browse Source

retval+errno return test

Joseph Henry 10 years ago
parent
commit
fe8e7ded8c

+ 49 - 18
netcon/NetconEthernetTap.cpp

@@ -166,11 +166,9 @@ std::vector<InetAddress> NetconEthernetTap::ips() const
 void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
 {
 	struct pbuf *p,*q;
-	//fprintf(stderr, "_put(%s,%s,%.4x,[data],%u)\n",from.toString().c_str(),to.toString().c_str(),etherType,len);
 	if (!_enabled)
 		return;
 
-	//printf(">> %.4x %s\n",etherType,Utils::hex(data,len).c_str());
 	struct eth_hdr ethhdr;
 	from.copyTo(ethhdr.src.addr, 6);
 	to.copyTo(ethhdr.dest.addr, 6);
@@ -202,7 +200,6 @@ void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType
 		return;
 	}
 
-	//printf("p->len == %u, p->payload == %s\n",p->len,Utils::hex(p->payload,p->len).c_str());
 	{
 		Mutex::Lock _l2(lwipstack->_lock);
 		if(interface.input(p, &interface) != ERR_OK) {
@@ -448,19 +445,19 @@ void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,uns
 /*
  * Send a return value to the client for an RPC
  */
-int NetconEthernetTap::send_return_value(TcpConnection *conn, int retval)
+int NetconEthernetTap::send_return_value(TcpConnection *conn, int retval, int _errno = 0)
 {
-  char retmsg[4];
+  char retmsg[sizeof(retval) + sizeof(_errno)];
   memset(&retmsg, '\0', sizeof(retmsg));
   retmsg[0]=RPC_RETVAL;
   memcpy(&retmsg[1], &retval, sizeof(retval));
+	memcpy(&retmsg[1]+sizeof(retval), &_errno, sizeof(_errno));
+	fprintf(stderr, "errno = %d\n", _errno);
   int n = write(_phy.getDescriptor(conn->rpcSock), &retmsg, sizeof(retmsg));
-  if(n > 0) {
-		// signal that we've satisfied this requirement
+  if(n > 0)
     conn->pending = false;
-  }
   else {
-    fprintf(stderr, "unable to send return value to the intercept\n");
+    fprintf(stderr, "Unable to send return value to the intercept. Closing connection\n");
 		closeConnection(conn);
   }
   return n;
@@ -628,12 +625,13 @@ err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
 	//fprintf(stderr, "nc_poll(): now = %u\n", now);
 	//fprintf(stderr, "nc_poll\n");
 
+/*
 	Larg *l = (Larg*)arg;
 	TcpConnection *conn = l->conn;
 	NetconEthernetTap *tap = l->tap;
 	if(conn && conn->idx) // if valid connection and non-zero index (indicating data present)
 		tap->handle_write(conn);
-
+*/
 	return ERR_OK;
 }
 
@@ -658,7 +656,7 @@ err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
 		l->tap->_phy.setNotifyReadable(l->conn->dataSock, true);
 		//uint64_t now = OSUtils::now();
 		//fprintf(stderr, "nc_sent(): now = %u\n", now);
-		//l->tap->_phy.whack();
+		l->tap->_phy.whack();
 		//l->tap->handle_write(l->conn);
 	}
 	return ERR_OK;
@@ -692,9 +690,30 @@ err_t NetconEthernetTap::nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err
  * @param Client that is making the RPC
  * @param structure containing the data and parameters for this client's RPC
  *
+
+	TODO: set errno appropriately
+
+	[ ]	EACCES - The address is protected, and the user is not the superuser.
+	[X]	EADDRINUSE - The given address is already in use.
+	[ ]	EBADF - sockfd is not a valid descriptor.
+	[ ]	EINVAL - The socket is already bound to an address.
+	[ ]	ENOTSOCK - sockfd is a descriptor for a file, not a socket.
+	[ ]	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).)
+	[ ]	EADDRNOTAVAIL - A nonexistent interface was requested or the requested address was not local.
+	[ ]	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.
+	[ ]	ELOOP - Too many symbolic links were encountered in resolving addr.
+	[ ]	ENAMETOOLONG -s addr is too long.
+	[ ]	ENOENT - The file does not exist.
+	[ ]	ENOMEM - Insufficient kernel memory was available.
+	[ ]	ENOTDIR - A component of the path prefix is not a directory.
+	[ ]	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)
 {
+	int _errno;
   struct sockaddr_in *connaddr;
   connaddr = (struct sockaddr_in *) &bind_rpc->addr;
   int conn_port = lwipstack->ntohs(connaddr->sin_port);
@@ -706,11 +725,16 @@ void NetconEthernetTap::handle_bind(PhySocket *sock, void **uptr, struct bind_st
   if(conn) {
     if(conn->pcb->state == CLOSED){
       int err = lwipstack->tcp_bind(conn->pcb, &conn_addr, conn_port);
-      if(err != ERR_OK) {
+			send_return_value(conn, err, -99);
+      if(err == ERR_USE) {
+				_errno = EADDRINUSE;
+				send_return_value(conn, err, -99);
+			}
+			if(err != ERR_OK) {
 				int ip = connaddr->sin_addr.s_addr;
 				unsigned char d[4];
 				d[0] = ip & 0xFF;
-				d[1] = (ip >> 8) & 0xFF;
+				d[1] = (ip >>  8) & 0xFF;
 				d[2] = (ip >> 16) & 0xFF;
 				d[3] = (ip >> 24) & 0xFF;
 				fprintf(stderr, "handle_bind(): error binding to %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], conn_port);
@@ -784,6 +808,18 @@ void NetconEthernetTap::handle_retval(PhySocket *sock, void **uptr, unsigned cha
  * @param Client that is making the RPC
  * @param structure containing the data and parameters for this client's RPC
  *
+
+	TODO: set errno appropriately
+
+	[ ] 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.
+  [ ] EINVAL - Unknown protocol, or protocol family not available.
+  [ ] EINVAL - Invalid flags in type.
+  [ ] EMFILE - Process file table overflow.
+  [ ] ENFILE - The system limit on the total number of open files has been reached.
+  [ ] 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.
+
  */
 void NetconEthernetTap::handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc)
 {
@@ -869,7 +905,6 @@ void NetconEthernetTap::handle_write(TcpConnection *conn)
 		corresponding PhySocket until nc_sent() is called and confirms that there is
 		now space on the buffer */
 		if(sndbuf == 0) {
-			//fprintf(stderr, "sndbuf = 0, setting read-notify = false\n");
 			_phy.setNotifyReadable(conn->dataSock, false);
 			lwipstack->_tcp_output(conn->pcb);
 			return;
@@ -878,7 +913,6 @@ void NetconEthernetTap::handle_write(TcpConnection *conn)
 		int read_fd = _phy.getDescriptor(conn->dataSock);
 
 		if((r = read(read_fd, (&conn->buf)+conn->idx, sndbuf)) > 0) {
-			//fprintf(stderr, "read = %d\n", r);
 			conn->idx += r;
 			/* Writes data pulled from the client's socket buffer to LWIP. This merely sends the
 			 * data to LWIP to be enqueued and eventually sent to the network. */
@@ -905,9 +939,6 @@ void NetconEthernetTap::handle_write(TcpConnection *conn)
 				return;
 			}
 		}
-		//else {
-		//	fprintf(stderr, "handle_write(): could not read from PhySocket for this connection\n");
-		//}
 	}
 }
 

+ 1 - 3
netcon/NetconEthernetTap.hpp

@@ -111,7 +111,7 @@ private:
 	void handle_connect(PhySocket *sock, void **uptr, struct connect_st* connect_rpc);
 	void handle_write(TcpConnection *conn);
 
-	int send_return_value(TcpConnection *conn, int retval);
+	int send_return_value(TcpConnection *conn, int retval, int _errno);
 
 	void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
 	void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
@@ -183,7 +183,6 @@ static err_t tapif_init(struct netif *netif)
 
 static err_t low_level_output(struct netif *netif, struct pbuf *p)
 {
-	//fprintf(stderr, "low_level_output()\n");
   struct pbuf *q;
   char buf[ZT_MAX_MTU+32];
   char *bufptr;
@@ -217,7 +216,6 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
 
   tap->_handler(tap->_arg,tap->_nwid,src_mac,dest_mac,
     Utils::ntoh((uint16_t)ethhdr->type),0,buf + sizeof(struct eth_hdr),tot_len - sizeof(struct eth_hdr));
-	//printf("low_level_output(): length = %d -- ethertype = %d\n", tot_len - sizeof(struct eth_hdr), Utils::ntoh((uint16_t)ethhdr->type));
   return ERR_OK;
 }
 

+ 16 - 16
netcon/common.c

@@ -42,8 +42,8 @@
 void dwr(const char *fmt, ...);
 
 /* defined in intercept and service */
-extern FILE* logfile;
-extern char* logfilename;
+//extern FILE* logfile;
+//extern char* logfilename;
 //extern flog = -1;
 
 extern pthread_mutex_t loglock;
@@ -78,8 +78,8 @@ extern pthread_mutex_t loglock;
 	timestamp = time(NULL);
 	strftime(timestring, sizeof(timestring), "%H:%M:%S", localtime(&timestamp));
 
-  if(logfile)
-    fprintf(logfile, "%s ", timestring);
+  //if(logfile)
+  //  fprintf(logfile, "%s ", timestring);
   fprintf(stderr, "%s ", timestring);
 
 #ifdef NETCON_SERVICE
@@ -87,13 +87,13 @@ extern pthread_mutex_t loglock;
 	{
     size_t num_intercepts = ns->intercepts.size();
     size_t num_connections = ns->connections.size();
-    if(logfile)
-      fprintf(logfile, "[i/c/tid=%3lu|%3lu|%7d]", num_intercepts, num_connections, pid);
+    //if(logfile)
+    //  fprintf(logfile, "[i/c/tid=%3lu|%3lu|%7d]", num_intercepts, num_connections, pid);
     fprintf(stderr, "[i/c/tid=%3lu|%3lu|%7d]", num_intercepts, num_connections, pid);
 	}
 	else {
-    if(logfile)
-      fprintf(logfile, "[i/c/tid=%3d|%3d|%7d]", 0, 0, -1);
+    //if(logfile)
+    //  fprintf(logfile, "[i/c/tid=%3d|%3d|%7d]", 0, 0, -1);
     fprintf(stderr, "[i/c/tid=%3d|%3d|%7d]", 0, 0, -1);
   }
 
@@ -102,23 +102,23 @@ extern pthread_mutex_t loglock;
 #ifdef NETCON_INTERCEPT
   //pthread_mutex_lock(&loglock);
   int pid = getpid();
-  if(logfile)
-	  fprintf(logfile, "[tid=%7d]", pid);
+  //if(logfile)
+	//  fprintf(logfile, "[tid=%7d]", pid);
   fprintf(stderr, "[tid=%7d]", pid);
   //pthread_mutex_unlock(&loglock);
 #endif
 
-	if(logfile)
-    fputs(" ", logfile);
+	//if(logfile)
+  //  fputs(" ", logfile);
   fputs(" ", stderr);
 
   /* logfile */
 	va_start(ap, fmt);
 	saveerr = errno;
-	if(logfile){
-    vfprintf(logfile, fmt, ap);
-    fflush(logfile);
-  }
+	//if(logfile){
+  //  vfprintf(logfile, fmt, ap);
+  //  fflush(logfile);
+  //}
   va_end(ap);
 
   /* console */

+ 24 - 0
netcon/intercept.c

@@ -675,6 +675,30 @@ int bind(BIND_SIG)
   pthread_mutex_lock(&lock);
   write(fdret_sock, cmd, BUF_SZ);
   pthread_mutex_unlock(&lock);
+
+  /*
+    If we successfully wrote the RPC, try to read a return value
+     - Also get errno value
+  */
+  if(fdret_sock >= 0) {
+    int retval;
+    int _errno;
+    char mynewbuf[BUF_SZ];
+    memset(&mynewbuf, '\0', sizeof(mynewbuf));
+    int n_read = read(fdret_sock, &mynewbuf, sizeof(mynewbuf));
+    if(n_read > 0) {
+      memcpy(&retval, &mynewbuf[1], sizeof(retval));
+      memcpy(&_errno, &mynewbuf[1]+sizeof(retval), sizeof(_errno));
+      dwr("errno = %d\n", _errno);
+      errno = _errno;
+      pthread_mutex_unlock(&lock);
+      return retval;
+    }
+    else {
+      pthread_mutex_unlock(&lock);
+      dwr("unable to read connect: return value\n");
+    }
+  }
   return 0; /* FIXME: get real return value */
 #endif
 }

BIN
netcon/libintercept.so.1.0


BIN
netcon/liblwip.so


+ 1 - 1
netcon/make-intercept.mk

@@ -47,7 +47,7 @@ lib:
 install:
 	cp libintercept.so.1.0 /lib/libintercept.so.1.0
 	ln -sf /lib/libintercept.so.1.0 /lib/libintercept
-	/usr/bin/install -c netcon/intercept /usr/bin
+	/usr/bin/install -c intercept /usr/bin
 
 uninstall:
 	rm -r /lib/libintercept.so.1.0