+ /* This p_connection is on caller's stack and not the heap.
+ * If we are interested, we will create a copy of the connection
+ * (on the heap) and add it to our list. */
+ p_connection = (tcp_connection_t *) p_void;
+
+ /* inspect the local port number of the connection to see if we're
+ * interested. */
+ if ((p_monitor->port_range_begin <= p_connection->local_port)
+ && (p_connection->local_port <= p_monitor->port_range_end)) {
+ /* the connection is in the range of the monitor. */
+
+ /* first check the hash to see if the connection is already there. */
+ if ((p_conn_hash = g_hash_table_lookup(p_monitor->hash,
+ (gconstpointer) p_connection))) {
+ /* it's already in the hash. reset the age of the connection. */
+ p_conn_hash->age = TCP_CONNECTION_STARTING_AGE;
+
+ return;
+ }
+
+ /* Connection is not yet in the hash.
+ * Add it if max_connections not exceeded. */
+ if (g_hash_table_size(p_monitor->hash)
+ >= p_monitor->max_port_monitor_connections) {
+ return;
+ }
+
+ /* create a new connection node */
+ if ((p_node = (tcp_connection_node_t *)
+ calloc(1, sizeof(tcp_connection_node_t))) == NULL) {
+ return;
+ }
+
+ /* copy the connection data */
+ if (copy_tcp_connection(&p_node->connection, p_connection) != 0) {
+ /* error copying the connection data. deallocate p_node to
+ * avoid leaks and return. */
+ free(p_node);
+ return;
+ }
+
+ p_node->connection.age = TCP_CONNECTION_STARTING_AGE;
+ p_node->p_next = NULL;
+
+ /* insert it into the monitor's hash table */
+#ifdef HASH_DEBUG
+ fprintf(stderr, "monitor hash insert of connection [%s]\n",
+ p_node->connection.key);
+#endif
+ g_hash_table_insert(p_monitor->hash,
+ (gpointer) &p_node->connection, (gpointer) &p_node->connection);
+
+ /* append the node to the monitor's connection list */
+ if (p_monitor->connection_list.p_tail == NULL) {
+ /* assume p_head is NULL too */
+ p_monitor->connection_list.p_head = p_node;
+ p_monitor->connection_list.p_tail = p_node;
+ p_node->p_prev = NULL;
+ } else {
+ p_monitor->connection_list.p_tail->p_next = p_node;
+ p_node->p_prev = p_monitor->connection_list.p_tail;
+ p_monitor->connection_list.p_tail = p_node;
+ }
+ }