Changeset 21:bef197f6826f in freeDiameter for freeDiameter/server.c
- Timestamp:
- Oct 8, 2009, 8:05:16 PM (15 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
freeDiameter/server.c
r20 r21 62 62 struct client { 63 63 struct fd_list chain; /* link in the server's list of clients */ 64 65 64 struct cnxctx *conn; /* Parameters of the connection; sends its events to the ev fifo bellow */ 66 67 65 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 70 66 pthread_t cli_thr; /* connection state machine (simplified PSM) */ 71 67 }; … … 80 76 81 77 78 static 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 114 fatal_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 82 119 /* This thread is called when a new client had just connected */ 83 120 static void * handle_client_fast(void * arg) … … 90 127 { 91 128 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); 93 130 int rc = getnameinfo((sSA *)&cf->ss, sizeof(sSS), addr + offset, sizeof(addr) - offset, NULL, 0, 0); 94 131 if (rc) 95 132 memcpy(addr + offset, gai_strerror(rc), sizeof(addr) - offset); 96 133 134 fd_log_threadname ( addr ); 135 97 136 if (TRACE_BOOL(INFO)) { 98 137 fd_log_debug( "New connection %s, sock %d, from '%s'\n", cf->serv->serv_name, cf->sock, addr + offset); 99 138 } 100 101 fd_log_threadname ( addr );102 139 } 103 140 104 141 /* 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 ); 106 143 memset(c, 0, sizeof(struct client)); 107 144 fd_list_init(&c->chain, c); 108 c->cli_thr = pthread_self();109 145 110 146 /* 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 ); 112 148 113 149 /* In case we are a secure server, handshake now */ 114 150 if (cf->serv->secur) { 115 116 TODO("Continue");117 }151 CHECK_FCT_DO( fd_cnx_handshake(c->conn, GNUTLS_CLIENT), goto cleanup ); 152 } 153 118 154 119 155 /* 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 ); 121 157 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 167 cleanup: /* 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 174 fatal_error: /* This has effect to terminate the daemon */ 175 CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, NULL), ); 131 176 free(cf); 132 177 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), );136 178 return NULL; 137 179 } … … 142 184 struct server *sv = (struct server *)arg; 143 185 struct cli_fast cf; 186 pthread_attr_t attr; 144 187 145 188 CHECK_PARAMS_DO(sv, goto error); … … 150 193 cf.serv = sv; 151 194 195 CHECK_POSIX_DO( pthread_attr_init(&attr), goto error ); 196 CHECK_POSIX_DO( pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED), goto error ); 152 197 153 198 /* Accept incoming connections */ … … 171 216 172 217 /* 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 ); 174 219 175 220 } while (1); … … 231 276 ((sv->serv_status == 2) ? "Thread terminated" : 232 277 "Thread status unknown"))); 233 /* Dump the endpoints ?*/234 /* Dump the client list ? */278 /* Dump the client list */ 279 TODO("Dump client list"); 235 280 } 236 281 } … … 251 296 CHECK_FCT( fd_sctp_create_bind_server( &socket, fd_g_config->cnf_port ) ); 252 297 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 */ 256 302 257 303 #endif /* DISABLE_SCTP */ … … 261 307 if (!fd_g_config->cnf_flags.no_tcp) { 262 308 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 } 264 315 } 265 316 … … 270 321 void fd_servers_stop() 271 322 { 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.