Navigation


Changeset 21:bef197f6826f in freeDiameter for freeDiameter/server.c


Ignore:
Timestamp:
Oct 8, 2009, 8:05:16 PM (15 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Backup before week-end, cnxctx and server in progress

File:
1 edited

Legend:

Unmodified
Added
Removed
  • freeDiameter/server.c

    r20 r21  
    6262struct client {
    6363        struct fd_list   chain; /* link in the server's list of clients */
    64        
    6564        struct cnxctx   *conn;  /* Parameters of the connection; sends its events to the ev fifo bellow */
    66        
    6765        struct timespec  ts;    /* Delay for receiving CER: INCNX_TIMEOUT */
    68         struct fifo     *ev;    /* Events of the connection -- allowed: TIMEOUT, ERROR (cnx, tls), MSG_RCV (CER, other=>error) */
    69        
    7066        pthread_t        cli_thr; /* connection state machine (simplified PSM) */
    7167};
     
    8076
    8177
     78static void * client_simple_psm(void * arg)
     79{
     80        struct client * c = arg;
     81        struct server * s = NULL;
     82       
     83        TRACE_ENTRY("%p", c);
     84       
     85        CHECK_PARAMS_DO(c && c->conn && c->chain.head, goto fatal_error );
     86       
     87        s = c->chain.head->o;
     88       
     89        /* Name the current thread */
     90        {
     91                char addr[128];
     92                snprintf(addr, sizeof(addr), "Srv %d/Cli %s", s->socket, fd_cnx_getremoteid(c->conn));
     93                fd_log_threadname ( addr );
     94        }
     95       
     96        /* Set the timeout to receive the first message */
     97        CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &c->ts), goto fatal_error );
     98        c->ts.tv_sec += INCNX_TIMEOUT;
     99       
     100        TODO("receive message until c->ts");
     101       
     102        TODO("Timeout => close");
     103        TODO("Message != CER => close");
     104        TODO("Message == CER : ");
     105        TODO("Search matching peer");
     106        TODO("...");
     107       
     108        /* The end: we have freed the client structure already */
     109        TODO("Unlink the client structure");
     110        TODO(" pthread_detach(c->cli_thr); ");
     111        TODO(" free(c); ");
     112        return NULL;
     113       
     114fatal_error:    /* This has effect to terminate the daemon */
     115        CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, NULL), );
     116        return NULL;
     117}
     118
    82119/* This thread is called when a new client had just connected */
    83120static void * handle_client_fast(void * arg)
     
    90127        {
    91128                char addr[128];
    92                 int offset = snprintf(addr, sizeof(addr), "Srv %d/Cli %d : ", cf->serv->socket, cf->sock);
     129                int offset = snprintf(addr, sizeof(addr), "Srv %d/CliFast %d : ", cf->serv->socket, cf->sock);
    93130                int rc = getnameinfo((sSA *)&cf->ss, sizeof(sSS), addr + offset, sizeof(addr) - offset, NULL, 0, 0);
    94131                if (rc)
    95132                        memcpy(addr + offset, gai_strerror(rc), sizeof(addr) - offset);
    96133               
     134                fd_log_threadname ( addr );
     135       
    97136                if (TRACE_BOOL(INFO)) {
    98137                        fd_log_debug( "New connection %s, sock %d, from '%s'\n", cf->serv->serv_name, cf->sock, addr + offset);
    99138                }
    100        
    101                 fd_log_threadname ( addr );
    102139        }
    103140       
    104141        /* Create a client structure */
    105         CHECK_MALLOC_DO( c = malloc(sizeof(struct client)), goto early_error );
     142        CHECK_MALLOC_DO( c = malloc(sizeof(struct client)), goto fatal_error );
    106143        memset(c, 0, sizeof(struct client));
    107144        fd_list_init(&c->chain, c);
    108         c->cli_thr = pthread_self();
    109145       
    110146        /* Create the connection context */
    111         CHECK_MALLOC_DO( c->conn = fd_cnx_init(cf->sock, cf->serv->proto), goto early_error );
     147        CHECK_MALLOC_DO( c->conn = fd_cnx_init(cf->sock, cf->serv->proto), goto fatal_error );
    112148       
    113149        /* In case we are a secure server, handshake now */
    114150        if (cf->serv->secur) {
    115                
    116                 TODO("Continue");
    117         }
     151                CHECK_FCT_DO( fd_cnx_handshake(c->conn, GNUTLS_CLIENT), goto cleanup );
     152        }
     153       
    118154       
    119155        /* Save the client in the list */
    120         CHECK_POSIX_DO( pthread_mutex_lock( &cf->serv->clients_mtx ), goto early_error );
     156        CHECK_POSIX_DO( pthread_mutex_lock( &cf->serv->clients_mtx ), goto fatal_error );
    121157        fd_list_insert_before(&cf->serv->clients, &c->chain);
    122         CHECK_POSIX_DO( pthread_mutex_unlock( &cf->serv->clients_mtx ), goto error );
    123        
    124        
    125        
    126        
    127 early_error:
    128         TRACE_DEBUG(INFO, "Thread is detaching to die");
    129         pthread_detach(pthread_self());
    130         shutdown(cf->sock, SHUT_RDWR);
     158        CHECK_POSIX_DO( pthread_mutex_unlock( &cf->serv->clients_mtx ), goto fatal_error );
     159       
     160        /* Start the client thread */
     161        CHECK_POSIX_DO( pthread_create( &c->cli_thr, NULL, client_simple_psm, c ), goto fatal_error );
     162       
     163        /* We're done here */
     164        free(cf);
     165        return NULL;
     166       
     167cleanup:        /* Clean all objects and return (minor error on the connection)*/
     168        if (c && c->conn) {
     169                TODO( "Free the c->conn object & gnutls data" );
     170        }
     171       
     172        return NULL;   
     173       
     174fatal_error:    /* This has effect to terminate the daemon */
     175        CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, NULL), );
    131176        free(cf);
    132177        free(c);
    133 error: 
    134         TRACE_DEBUG(INFO, "Thread is terminating");
    135         CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, NULL), );
    136178        return NULL;
    137179}
     
    142184        struct server *sv = (struct server *)arg;
    143185        struct cli_fast cf;
     186        pthread_attr_t  attr;
    144187       
    145188        CHECK_PARAMS_DO(sv, goto error);
     
    150193        cf.serv = sv;
    151194       
     195        CHECK_POSIX_DO( pthread_attr_init(&attr), goto error );
     196        CHECK_POSIX_DO( pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED), goto error );
    152197       
    153198        /* Accept incoming connections */
     
    171216               
    172217                /* Create the thread to handle the new incoming connection */
    173                 CHECK_POSIX_DO( pthread_create( &thr /* we don't use it, but NULL is not standard */, NULL, handle_client_fast, ncf), goto error );
     218                CHECK_POSIX_DO( pthread_create( &thr, &attr, handle_client_fast, ncf), goto error );
    174219               
    175220        } while (1);
     
    231276                                ((sv->serv_status == 2) ? "Thread terminated" :
    232277                                                          "Thread status unknown")));
    233                 /* Dump the endpoints ? */
    234                 /* Dump the client list ? */
     278                /* Dump the client list */
     279                TODO("Dump client list");
    235280        }
    236281}
     
    251296                CHECK_FCT( fd_sctp_create_bind_server( &socket, fd_g_config->cnf_port ) );
    252297                CHECK_MALLOC( sv = new_serv(IPPROTO_SCTP, 0, socket) );
    253                
    254                
    255                
     298                TODO("Link");
     299                TODO("Start thread");
     300               
     301                /* Create the server on secure port */
    256302               
    257303#endif /* DISABLE_SCTP */
     
    261307        if (!fd_g_config->cnf_flags.no_tcp) {
    262308               
    263                
     309                if (FD_IS_LIST_EMPTY(&fd_g_config->cnf_endpoints)) {
     310                        /* if not no_IP : create server for 0.0.0.0 */
     311                        /* if not no_IP6 : create server for :: */
     312                } else {
     313                        /* Create all endpoints -- check flags */
     314                }
    264315        }
    265316       
     
    270321void fd_servers_stop()
    271322{
    272        
    273 }
     323        /* Loop on all servers */
     324                /* cancel thread */
     325                /* shutdown the socket */
     326                /* empty list of clients (stop them) */
     327}
Note: See TracChangeset for help on using the changeset viewer.