*** channels.c	2007-01-28 18:16:28.000000000 -0500
--- ../openssh-4.6p1/channels.c	2007-07-25 16:43:33.000000000 -0400
***************
*** 78,83 ****
--- 78,87 ----
  
  /* -- channel core */
  
+ /* test channel code */
+ static CNode *head = NULL;
+ 
+ 
  /*
   * Pointer to an array containing all allocated channels.  The array is
   * dynamically extended as needed.
*************** channel_new(char *ctype, int type, int r
*** 324,346 ****
  	c->input_filter = NULL;
  	c->output_filter = NULL;
  	debug("channel %d: new [%s]", found, remote_name);
  	return c;
  }
  
  static int
  channel_find_maxfd(void)
  {
- 	u_int i;
  	int max = 0;
  	Channel *c;
  
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c != NULL) {
! 			max = MAX(max, c->rfd);
! 			max = MAX(max, c->wfd);
! 			max = MAX(max, c->efd);
! 		}
  	}
  	return max;
  }
--- 328,349 ----
  	c->input_filter = NULL;
  	c->output_filter = NULL;
  	debug("channel %d: new [%s]", found, remote_name);
+ 	channel_add_node(&head, channels[found]);
  	return c;
  }
  
  static int
  channel_find_maxfd(void)
  {
  	int max = 0;
  	Channel *c;
+ 	CNode *list;
  
! 	for (list = head; list != NULL; list = list->next) {
! 	  c = list->chPtr;
! 	  max = MAX(max, c->rfd);
! 	  max = MAX(max, c->wfd);
! 	  max = MAX(max, c->efd);
  	}
  	return max;
  }
*************** void
*** 378,388 ****
  channel_free(Channel *c)
  {
  	char *s;
! 	u_int i, n;
! 
! 	for (n = 0, i = 0; i < channels_alloc; i++)
! 		if (channels[i])
! 			n++;
  	debug("channel %d: free: %s, nchannels %u", c->self,
  	    c->remote_name ? c->remote_name : "???", n);
  
--- 381,392 ----
  channel_free(Channel *c)
  {
  	char *s;
! 	u_int n;
! 	CNode *list;
! 	
! 	n = 0;
! 	for (list = head; list != NULL; list = list->next) 
! 	  n++;
  	debug("channel %d: free: %s, nchannels %u", c->self,
  	    c->remote_name ? c->remote_name : "???", n);
  
*************** channel_free(Channel *c)
*** 403,419 ****
  		c->remote_name = NULL;
  	}
  	channels[c->self] = NULL;
  	xfree(c);
  }
  
  void
  channel_free_all(void)
  {
! 	u_int i;
! 
! 	for (i = 0; i < channels_alloc; i++)
! 		if (channels[i] != NULL)
! 			channel_free(channels[i]);
  }
  
  /*
--- 407,421 ----
  		c->remote_name = NULL;
  	}
  	channels[c->self] = NULL;
+ 	channel_del_node(&head, c);
  	xfree(c);
  }
  
  void
  channel_free_all(void)
  {
! 	while(head)
! 	  channel_free(head->chPtr);
  }
  
  /*
*************** channel_free_all(void)
*** 423,433 ****
  void
  channel_close_all(void)
  {
! 	u_int i;
  
! 	for (i = 0; i < channels_alloc; i++)
! 		if (channels[i] != NULL)
! 			channel_close_fds(channels[i]);
  }
  
  /*
--- 425,434 ----
  void
  channel_close_all(void)
  {
! 	CNode *list;
  
! 	for (list = head; list != NULL; list = list->next)
! 	  channel_close_fds(list->chPtr);
  }
  
  /*
*************** channel_close_all(void)
*** 436,457 ****
  void
  channel_stop_listening(void)
  {
- 	u_int i;
  	Channel *c;
  
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c != NULL) {
! 			switch (c->type) {
! 			case SSH_CHANNEL_AUTH_SOCKET:
! 			case SSH_CHANNEL_PORT_LISTENER:
! 			case SSH_CHANNEL_RPORT_LISTENER:
! 			case SSH_CHANNEL_X11_LISTENER:
! 				channel_close_fd(&c->sock);
! 				channel_free(c);
! 				break;
! 			}
! 		}
  	}
  }
  
--- 437,457 ----
  void
  channel_stop_listening(void)
  {
  	Channel *c;
+ 	CNode *list;
  
! 
! 	for (list = head; list != NULL; list = list->next) {
! 	  c = list->chPtr;
! 	  switch (c->type) {
! 	  case SSH_CHANNEL_AUTH_SOCKET:
! 	  case SSH_CHANNEL_PORT_LISTENER:
! 	  case SSH_CHANNEL_RPORT_LISTENER:
! 	  case SSH_CHANNEL_X11_LISTENER:
! 	    channel_close_fd(&c->sock);
! 	    channel_free(c);
! 	    break;
! 	  }
  	}
  }
  
*************** channel_stop_listening(void)
*** 462,473 ****
  int
  channel_not_very_much_buffered_data(void)
  {
- 	u_int i;
  	Channel *c;
  
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
  #if 0
  			if (!compat20 &&
  			    buffer_len(&c->input) > packet_get_maxsize()) {
--- 462,473 ----
  int
  channel_not_very_much_buffered_data(void)
  {
  	Channel *c;
+ 	CNode *list;
  
! 	for (list = head; list != NULL; list = list->next) {
! 	  c = list->chPtr;;
! 		if (c->type == SSH_CHANNEL_OPEN) {
  #if 0
  			if (!compat20 &&
  			    buffer_len(&c->input) > packet_get_maxsize()) {
*************** channel_not_very_much_buffered_data(void
*** 491,503 ****
  int
  channel_still_open(void)
  {
- 	u_int i;
  	Channel *c;
  
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c == NULL)
! 			continue;
  		switch (c->type) {
  		case SSH_CHANNEL_X11_LISTENER:
  		case SSH_CHANNEL_PORT_LISTENER:
--- 491,501 ----
  int
  channel_still_open(void)
  {
  	Channel *c;
+ 	CNode *list;
  
! 	for (list = head; list != NULL; list = list->next) {
! 		c = list->chPtr;
  		switch (c->type) {
  		case SSH_CHANNEL_X11_LISTENER:
  		case SSH_CHANNEL_PORT_LISTENER:
*************** channel_still_open(void)
*** 533,544 ****
  int
  channel_find_open(void)
  {
- 	u_int i;
  	Channel *c;
  
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c == NULL || c->remote_id < 0)
  			continue;
  		switch (c->type) {
  		case SSH_CHANNEL_CLOSED:
--- 531,542 ----
  int
  channel_find_open(void)
  {
  	Channel *c;
+ 	CNode *list;
  
! 	for (list = head; list != NULL; list = list->next) {
! 		c = list->chPtr;
! 		if (c->remote_id < 0)
  			continue;
  		switch (c->type) {
  		case SSH_CHANNEL_CLOSED:
*************** channel_find_open(void)
*** 554,565 ****
  		case SSH_CHANNEL_AUTH_SOCKET:
  		case SSH_CHANNEL_OPEN:
  		case SSH_CHANNEL_X11_OPEN:
! 			return i;
  		case SSH_CHANNEL_INPUT_DRAINING:
  		case SSH_CHANNEL_OUTPUT_DRAINING:
  			if (!compat13)
  				fatal("cannot happen: OUT_DRAIN");
! 			return i;
  		default:
  			fatal("channel_find_open: bad channel type %d", c->type);
  			/* NOTREACHED */
--- 552,563 ----
  		case SSH_CHANNEL_AUTH_SOCKET:
  		case SSH_CHANNEL_OPEN:
  		case SSH_CHANNEL_X11_OPEN:
! 			return c->self;
  		case SSH_CHANNEL_INPUT_DRAINING:
  		case SSH_CHANNEL_OUTPUT_DRAINING:
  			if (!compat13)
  				fatal("cannot happen: OUT_DRAIN");
! 			return c->self;
  		default:
  			fatal("channel_find_open: bad channel type %d", c->type);
  			/* NOTREACHED */
*************** channel_open_message(void)
*** 580,594 ****
  	Buffer buffer;
  	Channel *c;
  	char buf[1024], *cp;
! 	u_int i;
  
  	buffer_init(&buffer);
  	snprintf(buf, sizeof buf, "The following connections are open:\r\n");
  	buffer_append(&buffer, buf, strlen(buf));
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c == NULL)
! 			continue;
  		switch (c->type) {
  		case SSH_CHANNEL_X11_LISTENER:
  		case SSH_CHANNEL_PORT_LISTENER:
--- 578,592 ----
  	Buffer buffer;
  	Channel *c;
  	char buf[1024], *cp;
! 	CNode *list;
! 
  
  	buffer_init(&buffer);
  	snprintf(buf, sizeof buf, "The following connections are open:\r\n");
  	buffer_append(&buffer, buf, strlen(buf));
! 
! 	for (list = head; list != NULL; list = list->next) {
! 		c = list->chPtr;
  		switch (c->type) {
  		case SSH_CHANNEL_X11_LISTENER:
  		case SSH_CHANNEL_PORT_LISTENER:
*************** static void
*** 1809,1827 ****
  channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
  {
  	static int did_init = 0;
- 	u_int i;
  	Channel *c;
! 
  	if (!did_init) {
  		channel_handler_init();
  		did_init = 1;
  	}
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c == NULL)
! 			continue;
! 		if (ftab[c->type] != NULL)
! 			(*ftab[c->type])(c, readset, writeset);
  		channel_garbage_collect(c);
  	}
  }
--- 1807,1824 ----
  channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
  {
  	static int did_init = 0;
  	Channel *c;
! 	CNode *list;
! 	
  	if (!did_init) {
  		channel_handler_init();
  		did_init = 1;
  	}
! 
! 	for (list = head; list != NULL; list = list->next) {
! 	  c = list->chPtr;
! 	  if (ftab[c->type] != NULL)
! 	    (*ftab[c->type])(c, readset, writeset);
  		channel_garbage_collect(c);
  	}
  }
*************** void
*** 1874,1885 ****
  channel_output_poll(void)
  {
  	Channel *c;
! 	u_int i, len;
  
! 	for (i = 0; i < channels_alloc; i++) {
! 		c = channels[i];
! 		if (c == NULL)
! 			continue;
  
  		/*
  		 * We are only interested in channels that can have buffered
--- 1871,1881 ----
  channel_output_poll(void)
  {
  	Channel *c;
! 	u_int len;
! 	CNode *list;
  
! 	for (list = head; list != NULL; list = list->next) {
!     	        c = list->chPtr;
  
  		/*
  		 * We are only interested in channels that can have buffered
*************** channel_setup_fwd_listener(int type, con
*** 2472,2487 ****
  int
  channel_cancel_rport_listener(const char *host, u_short port)
  {
- 	u_int i;
  	int found = 0;
  
! 	for (i = 0; i < channels_alloc; i++) {
! 		Channel *c = channels[i];
  
! 		if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
  		    strncmp(c->path, host, sizeof(c->path)) == 0 &&
  		    c->listening_port == port) {
! 			debug2("%s: close channel %d", __func__, i);
  			channel_free(c);
  			found = 1;
  		}
--- 2468,2484 ----
  int
  channel_cancel_rport_listener(const char *host, u_short port)
  {
  	int found = 0;
+ 	CNode *list;
+ 	Channel *c;
  
! 	for (list = head; list != NULL; list = list->next) {
! 	        c = list->chPtr;
  
! 		if (c->type == SSH_CHANNEL_RPORT_LISTENER &&
  		    strncmp(c->path, host, sizeof(c->path)) == 0 &&
  		    c->listening_port == port) {
! 			debug2("%s: close channel %d", __func__, c->self);
  			channel_free(c);
  			found = 1;
  		}
*************** channel_connect_to(const char *host, u_s
*** 2824,2839 ****
  void
  channel_send_window_changes(void)
  {
- 	u_int i;
  	struct winsize ws;
! 
! 	for (i = 0; i < channels_alloc; i++) {
! 		if (channels[i] == NULL || !channels[i]->client_tty ||
! 		    channels[i]->type != SSH_CHANNEL_OPEN)
  			continue;
! 		if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
  			continue;
! 		channel_request_start(i, "window-change", 0);
  		packet_put_int((u_int)ws.ws_col);
  		packet_put_int((u_int)ws.ws_row);
  		packet_put_int((u_int)ws.ws_xpixel);
--- 2821,2837 ----
  void
  channel_send_window_changes(void)
  {
  	struct winsize ws;
! 	CNode *list;
! 	Channel *c;
! 	
! 	for (list = head; list != NULL; list = list->next) {
! 	  c = list->chPtr;
! 	  if (!c->client_tty || c->type != SSH_CHANNEL_OPEN)
  			continue;
! 		if (ioctl(c->rfd, TIOCGWINSZ, &ws) < 0)
  			continue;
! 		channel_request_start(c->self, "window-change", 0);
  		packet_put_int((u_int)ws.ws_col);
  		packet_put_int((u_int)ws.ws_row);
  		packet_put_int((u_int)ws.ws_xpixel);
*************** auth_request_forwarding(void)
*** 3234,3236 ****
--- 3232,3275 ----
  	packet_send();
  	packet_write_wait();
  }
+ 
+ 
+ /*channel test code*/
+ void channel_add_node (CNode** list, Channel* channel_ptr) {
+   CNode* newnode;
+   newnode = malloc(sizeof(CNode));
+ 
+   newnode->chPtr = channel_ptr;
+   newnode->chNum = channel_ptr->self;
+   newnode->next = *list;
+   *list = newnode;
+   debug("adding   channel %p(%d) to   node %p\n", channel_ptr, channel_ptr->self, newnode);
+   return;
+ }
+ 
+  void channel_del_node (CNode** list, Channel* channel_ptr) {
+   CNode* current;
+   CNode* prev;
+   prev = NULL;
+   
+   for (current = *list; current !=NULL; prev = current, current=current->next) {
+     if (current->chPtr == channel_ptr) {
+       if (prev == NULL) {
+ 	debug("setting *list = current->next to %p = %p", *list, current->next);
+ 	*list = current->next;
+       } else {
+ 	prev->next = current->next;
+       }
+       debug("removing channel %p(%d) from node %p\n",current->chPtr,current->chNum,current);
+       xfree(current);
+     }
+   }  
+   return;
+ }
+ 
+  void printList(CNode *list) {
+    while (list) {
+      //     printf("%d is %p hi!\n", list->chNum, list->chPtr);
+      list = list->next;
+    }
+  }
