2016-04-09 18:07:30 +02:00
|
|
|
/*
|
|
|
|
This file is part of actube.
|
|
|
|
|
|
|
|
actube is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-03-17 17:29:09 +01:00
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
#include "conn.h"
|
2016-04-09 18:13:32 +02:00
|
|
|
#include "connlist.h"
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
#include "sock.h"
|
|
|
|
|
|
|
|
|
2018-04-02 01:39:08 +02:00
|
|
|
#include "dbg.h"
|
|
|
|
|
|
|
|
static int cmp_by_addr_p ( const void * d1, const void *d2 )
|
|
|
|
{
|
|
|
|
struct conn * c1 = * ( void ** ) d1 ;
|
|
|
|
struct conn * c2 = * ( void ** ) d2 ;
|
|
|
|
return sock_cmpaddr ( ( struct sockaddr* ) &c1->addr, ( struct sockaddr* ) &c2->addr, 1 );
|
|
|
|
}
|
2016-04-10 15:54:25 +02:00
|
|
|
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
static int cmp_by_addr ( const void * d1, const void *d2 )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
struct conn * c1 = * ( void ** ) d1 ;
|
|
|
|
struct conn * c2 = * ( void ** ) d2 ;
|
2018-04-02 01:39:08 +02:00
|
|
|
return sock_cmpaddr ( ( struct sockaddr* ) &c1->addr, ( struct sockaddr* ) &c2->addr, 0 );
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 01:39:08 +02:00
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
static int cmp_by_session_id ( const void *d1, const void *d2 )
|
2016-04-10 15:54:25 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
struct conn * c1 = *( void ** ) d1;
|
|
|
|
struct conn * c2 = *( void ** ) d2;
|
|
|
|
return memcmp ( c1->session_id, c2->session_id, 16 );
|
2016-04-10 15:54:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-02 01:39:08 +02:00
|
|
|
/**
|
|
|
|
* @brief Create a connection list
|
|
|
|
* @param len initial length
|
|
|
|
* @param cmpports compare ports
|
|
|
|
* @return the create connection list or NULL if an error has occured.
|
|
|
|
*/
|
|
|
|
struct connlist * connlist_create ( int len, int cmpports )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
struct connlist * cl = malloc ( sizeof ( struct connlist ) );
|
|
|
|
|
|
|
|
if ( !cl )
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
2018-03-11 00:56:41 +01:00
|
|
|
|
2018-04-02 01:39:08 +02:00
|
|
|
if (cmpports){
|
|
|
|
cl->by_addr = mavl_create_ptr ( cmp_by_addr_p, NULL );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
cl->by_addr = mavl_create_ptr ( cmp_by_addr, NULL );
|
|
|
|
}
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
if ( !cl->by_addr ) {
|
|
|
|
free ( cl );
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
cl->by_session_id = mavl_create_ptr ( cmp_by_session_id, NULL );
|
|
|
|
|
|
|
|
|
|
|
|
if ( pthread_mutex_init ( &cl->connlist_mutex, NULL ) ) {
|
|
|
|
mavl_destroy ( cl->by_addr );
|
|
|
|
free ( cl );
|
2014-07-11 22:12:11 +02:00
|
|
|
return 0;
|
|
|
|
};
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
cl->len = len;
|
|
|
|
|
2014-07-11 22:12:11 +02:00
|
|
|
return cl;
|
|
|
|
}
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
void connlist_lock ( struct connlist * cl )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
pthread_mutex_lock ( &cl->connlist_mutex );
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
void connlist_unlock ( struct connlist * cl )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
pthread_mutex_unlock ( &cl->connlist_mutex );
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
void connlist_destroy ( struct connlist * cl )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
if ( !cl )
|
2015-04-29 11:39:13 +02:00
|
|
|
return;
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
if ( cl->by_addr )
|
|
|
|
mavl_destroy ( cl->by_addr );
|
|
|
|
|
|
|
|
pthread_mutex_destroy ( &cl->connlist_mutex );
|
|
|
|
free ( cl );
|
2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
struct conn * connlist_get ( struct connlist * cl, const struct sockaddr * addr )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2016-04-09 18:15:18 +02:00
|
|
|
struct conn search;
|
2018-03-11 00:56:41 +01:00
|
|
|
sock_copyaddr ( &search.addr, addr );
|
|
|
|
return mavl_get_ptr ( cl->by_addr, &search );
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
struct conn * connlist_add ( struct connlist * cl, struct conn * conn )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
if ( cl->len != 0 )
|
|
|
|
if ( cl->by_addr->count >= cl->len )
|
2016-04-10 15:54:25 +02:00
|
|
|
return NULL;
|
2018-03-11 00:56:41 +01:00
|
|
|
|
|
|
|
conn->connlist = cl;
|
|
|
|
return mavl_add_ptr ( cl->by_addr, conn );
|
2016-04-10 15:54:25 +02:00
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
struct conn * connlist_get_by_session_id ( struct connlist *cl, struct conn * conn )
|
2016-04-10 15:54:25 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
return mavl_get_ptr ( cl->by_session_id, conn );
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
struct conn * connlist_add_by_session_id ( struct connlist * cl, struct conn * conn )
|
2016-04-10 15:54:25 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
return mavl_add_ptr ( cl->by_session_id, conn );
|
2016-04-10 15:54:25 +02:00
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
void connlist_remove ( struct connlist *cl, struct conn * conn )
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
void * md;
|
|
|
|
md = conn;
|
|
|
|
mavl_del ( cl->by_session_id, &md );
|
|
|
|
md = conn;
|
|
|
|
mavl_del ( cl->by_addr, &md );
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|