Changeset 5:c2d2729e3603 in freeDiameter
- Timestamp:
- Sep 3, 2009, 2:33:45 PM (15 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
freeDiameter/tests/testsess.c
r3 r5 47 47 }; 48 48 49 void mycleanup( char * sid, struct mystate * data )49 static void mycleanup( char * sid, struct mystate * data ) 50 50 { 51 51 /* sanity */ … … 61 61 } 62 62 63 static __inline__ struct mystate * new_state(char * sid, int *freed) 64 { 65 struct mystate *new; 66 new = malloc(sizeof(struct mystate)); 67 CHECK( 1, new ? 1 : 0 ); 68 memset(new, 0, sizeof(struct mystate)); 69 new->eyec = TEST_EYEC; 70 new->sid = strdup(sid); 71 CHECK( 1, new->sid ? 1 : 0 ); 72 new->freed = freed; 73 return new; 74 } 75 76 63 77 /* Main test routine */ 64 78 int main(int argc, char *argv[]) … … 78 92 CHECK( 0, fd_sess_handler_destroy( &hdl2 ) ); 79 93 CHECK( 0, fd_sess_handler_create ( &hdl2, mycleanup ) ); 80 #if 194 #if 0 81 95 fd_sess_dump_hdl(0, hdl1); 82 96 fd_sess_dump_hdl(0, hdl2); … … 89 103 CHECK( 0, fd_sess_new( &sess1, TEST_DIAM_ID, NULL, 0 ) ); 90 104 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, NULL, 0 ) ); 91 #if 1105 #if 0 92 106 fd_sess_dump(0, sess1); 93 107 fd_sess_dump(0, sess2); … … 106 120 CHECK( 0, fd_sess_new( &sess1, TEST_DIAM_ID, TEST_OPT, 0 ) ); 107 121 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, TEST_OPT, strlen(TEST_OPT) - 1 ) ); 108 #if 1122 #if 0 109 123 fd_sess_dump(0, sess1); 110 124 fd_sess_dump(0, sess2); … … 130 144 CHECK( sess3, sess1 ); 131 145 CHECK( 0, fd_sess_new( &sess2, NULL, TEST_SID, strlen(TEST_SID) - 1 ) ); 132 #if 1146 #if 0 133 147 fd_sess_dump(0, sess1); 134 148 fd_sess_dump(0, sess2); … … 140 154 141 155 CHECK( 0, fd_sess_destroy( &sess2 ) ); 142 } 143 144 145 146 /* 147 int fd_sess_new ( struct session ** session, char * diamId, char * opt, size_t optlen ); 148 int fd_sess_fromsid ( char * sid, size_t len, struct session ** session, int * new); 149 int fd_sess_getsid ( struct session * session, char ** sid ); 150 int fd_sess_settimeout( struct session * session, const struct timespec * timeout ); 151 int fd_sess_destroy ( struct session ** session ); 152 */ 153 156 CHECK( 0, fd_sess_destroy( &sess1 ) ); 157 } 158 159 /* Test fd_sess_fromsid */ 160 { 161 CHECK( 0, fd_sess_fromsid( TEST_SID, strlen(TEST_SID), &sess1, &new ) ); 162 CHECK( 1, new ? 1 : 0 ); 163 164 CHECK( 0, fd_sess_fromsid( TEST_SID, strlen(TEST_SID), &sess2, &new ) ); 165 CHECK( 0, new ); 166 CHECK( sess1, sess2 ); 167 168 CHECK( 0, fd_sess_fromsid( TEST_SID, strlen(TEST_SID), &sess3, NULL ) ); 169 CHECK( sess1, sess3 ); 170 171 CHECK( 0, fd_sess_destroy( &sess1 ) ); 172 } 173 174 /* Test timeout function */ 175 { 176 struct timespec timeout; 177 178 CHECK( 0, fd_sess_fromsid( TEST_SID, strlen(TEST_SID), &sess1, &new ) ); 179 CHECK( 1, new ? 1 : 0 ); 180 181 CHECK( 0, clock_gettime(CLOCK_REALTIME, &timeout) ); 182 CHECK( 0, fd_sess_settimeout( sess1, &timeout) ); 183 timeout.tv_sec = 0; 184 timeout.tv_nsec= 50000000; /* 50 ms */ 185 CHECK( 0, clock_nanosleep(CLOCK_REALTIME, 0, &timeout, NULL) ); 186 187 CHECK( 0, fd_sess_fromsid( TEST_SID, strlen(TEST_SID), &sess1, &new ) ); 188 CHECK( 1, new ? 1 : 0 ); 189 190 CHECK( 0, clock_gettime(CLOCK_REALTIME, &timeout) ); 191 timeout.tv_sec += 2678500; /* longer that SESS_DEFAULT_LIFETIME */ 192 CHECK( 0, fd_sess_settimeout( sess1, &timeout) ); 193 194 /* Create a second session */ 195 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, NULL, 0 ) ); 196 197 /* We don't really have away to verify the expiry list is in proper order automatically here... */ 198 199 CHECK( 0, fd_sess_destroy( &sess2 ) ); 200 CHECK( 0, fd_sess_destroy( &sess1 ) ); 201 } 202 203 204 /* Test states operations */ 205 { 206 struct mystate * ms[6], *tms; 207 int freed[6]; 208 int i; 209 struct timespec timeout; 210 211 /* Create three sessions */ 212 CHECK( 0, fd_sess_new( &sess1, TEST_DIAM_ID, NULL, 0 ) ); 213 CHECK( 0, fd_sess_new( &sess2, TEST_DIAM_ID, NULL, 0 ) ); 214 CHECK( 0, fd_sess_new( &sess3, TEST_DIAM_ID, NULL, 0 ) ); 215 216 /* Create 2 states */ 217 CHECK( 0, fd_sess_getsid(sess1, &str1) ); 218 freed[0] = 0; 219 ms[0] = new_state(str1, &freed[0]); 220 ms[1] = new_state(str1, NULL); 221 222 tms = ms[0]; /* save a copy */ 223 CHECK( 0, fd_sess_state_store ( hdl1, sess1, &ms[0] ) ); 224 CHECK( NULL, ms[0] ); 225 CHECK( EINVAL, fd_sess_state_store ( hdl1, sess1, NULL ) ); 226 CHECK( EALREADY, fd_sess_state_store ( hdl1, sess1, &ms[1] ) ); 227 CHECK( 1, ms[1] ? 1 : 0 ); 228 229 #if 0 230 fd_sess_dump(0, sess1); 231 #endif 232 233 CHECK( 0, fd_sess_state_retrieve( hdl1, sess1, &ms[0] ) ); 234 CHECK( tms, ms[0] ); 235 CHECK( 0, freed[0] ); 236 237 CHECK( 0, fd_sess_state_retrieve( hdl1, sess2, &tms ) ); 238 CHECK( NULL, tms ); 239 240 mycleanup(str1, ms[0]); 241 mycleanup(str1, ms[1]); 242 243 /* Now create 6 states */ 244 memset(&freed[0], 0, sizeof(freed)); 245 CHECK( 0, fd_sess_getsid(sess1, &str1) ); 246 ms[0] = new_state(str1, &freed[0]); 247 ms[1] = new_state(str1, &freed[1]); 248 CHECK( 0, fd_sess_getsid(sess2, &str1) ); 249 ms[2] = new_state(str1, &freed[2]); 250 ms[3] = new_state(str1, &freed[3]); 251 CHECK( 0, fd_sess_getsid(sess3, &str1) ); 252 ms[4] = new_state(str1, &freed[4]); 253 ms[5] = new_state(str1, &freed[5]); 254 str2 = strdup(str1); 255 CHECK( 1, str2 ? 1 : 0 ); 256 257 /* Store the six states */ 258 CHECK( 0, fd_sess_state_store ( hdl1, sess1, &ms[0] ) ); 259 CHECK( 0, fd_sess_state_store ( hdl2, sess1, &ms[1] ) ); 260 CHECK( 0, fd_sess_state_store ( hdl1, sess2, &ms[2] ) ); 261 CHECK( 0, fd_sess_state_store ( hdl2, sess2, &ms[3] ) ); 262 CHECK( 0, fd_sess_state_store ( hdl1, sess3, &ms[4] ) ); 263 CHECK( 0, fd_sess_state_store ( hdl2, sess3, &ms[5] ) ); 264 265 #if 0 266 fd_sess_dump(0, sess1); 267 fd_sess_dump(0, sess2); 268 fd_sess_dump(0, sess3); 269 #endif 270 271 /* Destroy session 3 */ 272 CHECK( 0, fd_sess_destroy( &sess3 ) ); 273 CHECK( 0, freed[0] ); 274 CHECK( 0, freed[1] ); 275 CHECK( 0, freed[2] ); 276 CHECK( 0, freed[3] ); 277 CHECK( 1, freed[4] ); 278 CHECK( 1, freed[5] ); 279 280 /* Destroy handler 2 */ 281 CHECK( 0, fd_sess_handler_destroy( &hdl2 ) ); 282 CHECK( 0, freed[0] ); 283 CHECK( 1, freed[1] ); 284 CHECK( 0, freed[2] ); 285 CHECK( 1, freed[3] ); 286 CHECK( 1, freed[4] ); 287 CHECK( 1, freed[5] ); 288 289 #if 1 290 fd_sess_dump(0, sess1); 291 fd_sess_dump(0, sess2); 292 #endif 293 294 /* Create again session 3, check that no data is associated to it */ 295 CHECK( 0, fd_sess_fromsid( str2, strlen(str2), &sess3, &new ) ); 296 CHECK( 1, new ? 1 : 0 ); 297 CHECK( 0, fd_sess_state_retrieve( hdl1, sess3, &tms ) ); 298 CHECK( NULL, tms ); 299 CHECK( 0, fd_sess_destroy( &sess3 ) ); 300 free(str2); 301 302 /* Timeout does call cleanups */ 303 CHECK( 0, clock_gettime(CLOCK_REALTIME, &timeout) ); 304 CHECK( 0, fd_sess_settimeout( sess2, &timeout) ); 305 #if 1 306 fd_sess_dump(0, sess1); 307 fd_sess_dump(0, sess2); 308 #endif 309 timeout.tv_sec = 0; 310 timeout.tv_nsec= 50000000; /* 50 ms */ 311 CHECK( 0, clock_nanosleep(CLOCK_REALTIME, 0, &timeout, NULL) ); 312 CHECK( 0, freed[0] ); 313 CHECK( 1, freed[1] ); 314 CHECK( 1, freed[2] ); 315 CHECK( 1, freed[3] ); 316 CHECK( 1, freed[4] ); 317 CHECK( 1, freed[5] ); 318 319 /* Check the last data can still be retrieved */ 320 CHECK( 0, fd_sess_state_retrieve( hdl1, sess1, &tms ) ); 321 CHECK( 0, fd_sess_getsid(sess1, &str1) ); 322 mycleanup(str1, tms); 323 } 324 325 154 326 /* That's all for the tests yet */ 155 327 PASSTEST(); -
include/freeDiameter/libfreeDiameter.h
r3 r5 365 365 #define TS_IS_INFERIOR( ts1, ts2 ) \ 366 366 ( ((ts1)->tv_sec < (ts2)->tv_sec ) \ 367 || (( ts1)->tv_nsec < (ts2)->tv_nsec))367 || (((ts1)->tv_sec == (ts2)->tv_sec ) && ((ts1)->tv_nsec < (ts2)->tv_nsec) )) 368 368 369 369 … … 1459 1459 * ENOMEM : Not enough memory to complete the operation 1460 1460 */ 1461 int fd_sess_state_store ( struct session_handler * handler, struct session * session, session_state ** state ); 1461 int fd_sess_state_store_int ( struct session_handler * handler, struct session * session, session_state ** state ); 1462 #define fd_sess_state_store( _handler, _session, _state ) \ 1463 fd_sess_state_store_int( (_handler), (_session), (void *)(_state) ) 1462 1464 1463 1465 /* … … 1479 1481 * EINVAL : A parameter is invalid. 1480 1482 */ 1481 int fd_sess_state_retrieve ( struct session_handler * handler, struct session * session, session_state ** state ); 1483 int fd_sess_state_retrieve_int ( struct session_handler * handler, struct session * session, session_state ** state ); 1484 #define fd_sess_state_retrieve( _handler, _session, _state ) \ 1485 fd_sess_state_retrieve_int( (_handler), (_session), (void *)(_state) ) 1482 1486 1483 1487 -
libfreeDiameter/sessions.c
r4 r5 296 296 if (st->hdl->id < del->id) 297 297 continue; 298 if (st->hdl->id > del->id) 299 break; 300 /* This state belongs to the handler we are deleting, move the item to the deleted_states list */ 301 fd_list_unlink(&st->chain); 302 CHECK_MALLOC( st->sid = strdup(sess->sid) ); 303 fd_list_insert_before(&deleted_states, &st->chain); 298 if (st->hdl->id == del->id) { 299 /* This state belongs to the handler we are deleting, move the item to the deleted_states list */ 300 fd_list_unlink(&st->chain); 301 CHECK_MALLOC( st->sid = strdup(sess->sid) ); 302 fd_list_insert_before(&deleted_states, &st->chain); 303 } 304 break; 304 305 } 305 306 CHECK_POSIX( pthread_mutex_unlock(&sess->stlock) ); … … 400 401 } 401 402 402 /* If the session did not exist, we can add it into the hash table*/403 /* If the session did not exist, we can link it in global tables */ 403 404 if (!found) { 404 fd_list_insert_before(li, &sess->chain_h); 405 fd_list_insert_before(li, &sess->chain_h); /* hash table */ 405 406 406 407 /* We must also insert in the expiry list */ … … 410 411 for (li = exp_sentinel.prev; li != &exp_sentinel; li = li->prev) { 411 412 struct session * s = (struct session *)(li->o); 412 413 413 if (TS_IS_INFERIOR( &s->timeout, &sess->timeout ) ) 414 414 break; 415 416 continue;417 415 } 418 416 fd_list_insert_after( li, &sess->expire ); 417 418 #if 0 419 if (TRACE_BOOL(ANNOYING)) { 420 TRACE_DEBUG(FULL, "-- Updated session expiry list --"); 421 for (li = exp_sentinel.next; li != &exp_sentinel; li = li->next) { 422 struct session * s = (struct session *)(li->o); 423 fd_sess_dump(FULL, s); 424 } 425 TRACE_DEBUG(FULL, "-- end of expiry list --"); 426 } 427 #endif 419 428 420 429 /* We added a new expiring element, we must signal */ … … 502 511 /* We added a new expiring element, we must signal */ 503 512 CHECK_POSIX( pthread_cond_signal(&exp_cond) ); 513 514 #if 0 515 if (TRACE_BOOL(ANNOYING)) { 516 TRACE_DEBUG(FULL, "-- Updated session expiry list --"); 517 for (li = exp_sentinel.next; li != &exp_sentinel; li = li->next) { 518 struct session * s = (struct session *)(li->o); 519 fd_sess_dump(FULL, s); 520 } 521 TRACE_DEBUG(FULL, "-- end of expiry list --"); 522 } 523 #endif 504 524 505 525 /* We're done */ … … 548 568 549 569 /* Save a state information with a session */ 550 int fd_sess_state_store ( struct session_handler * handler, struct session * session, session_state ** state )570 int fd_sess_state_store_int ( struct session_handler * handler, struct session * session, session_state ** state ) 551 571 { 552 572 struct state *new; … … 597 617 598 618 /* Get the data back */ 599 int fd_sess_state_retrieve ( struct session_handler * handler, struct session * session, session_state ** state )619 int fd_sess_state_retrieve_int ( struct session_handler * handler, struct session * session, session_state ** state ) 600 620 { 601 621 struct fd_list * li; … … 632 652 633 653 634 635 654 /* Dump functions */ 636 655 void fd_sess_dump(int level, struct session * session) 637 656 { 638 657 struct fd_list * li; 658 char buf[30]; 659 struct tm tm; 660 639 661 if (!TRACE_BOOL(level)) 640 662 return; 641 663 642 fd_log_debug(" Session @%p:\n", session);664 fd_log_debug("\t %*s -- Session @%p --\n", level, "", session); 643 665 if (!VALIDATE_SI(session)) { 644 fd_log_debug(" Invalid session object\n"); 645 return; 646 } 647 648 fd_log_debug(" sid '%s', hash %x\n", session->sid, session->hash); 649 fd_log_debug(" timeout %d.%09d\n", session->timeout.tv_sec, session->timeout.tv_nsec); 650 651 CHECK_POSIX_DO( pthread_mutex_lock(&session->stlock), /* ignore */ ); 652 for (li = session->states.next; li != &session->states; li = li->next) { 653 struct state * st = (struct state *)(li->o); 654 fd_log_debug(" handler %d registered data %p\n", st->hdl->id, st->state); 655 } 656 CHECK_POSIX_DO( pthread_mutex_unlock(&session->stlock), /* ignore */ ); 666 fd_log_debug("\t %*s Invalid session object\n", level, ""); 667 } else { 668 669 fd_log_debug("\t %*s sid '%s', hash %x\n", level, "", session->sid, session->hash); 670 671 strftime(buf, sizeof(buf), "%D,%T", localtime_r( &session->timeout.tv_sec , &tm )); 672 fd_log_debug("\t %*s timeout %s.%09ld\n", level, "", buf, session->timeout.tv_nsec); 673 674 CHECK_POSIX_DO( pthread_mutex_lock(&session->stlock), /* ignore */ ); 675 for (li = session->states.next; li != &session->states; li = li->next) { 676 struct state * st = (struct state *)(li->o); 677 fd_log_debug("\t %*s handler %d registered data %p\n", level, "", st->hdl->id, st->state); 678 } 679 CHECK_POSIX_DO( pthread_mutex_unlock(&session->stlock), /* ignore */ ); 680 } 681 fd_log_debug("\t %*s -- end of session @%p --\n", level, "", session); 657 682 } 658 683 … … 662 687 return; 663 688 664 fd_log_debug(" Handler @%p:\n", handler);689 fd_log_debug("\t %*s -- Handler @%p --\n", level, "", handler); 665 690 if (!VALIDATE_SH(handler)) { 666 fd_log_debug(" Invalid session handler object\n");667 return;668 }669 670 fd_log_debug(" id %d, cleanup %p\n", handler->id, handler->cleanup);691 fd_log_debug("\t %*s Invalid session handler object\n", level, ""); 692 } else { 693 fd_log_debug("\t %*s id %d, cleanup %p\n", level, "", handler->id, handler->cleanup); 694 } 695 fd_log_debug("\t %*s -- end of handler @%p --\n", level, "", handler); 671 696 }
Note: See TracChangeset
for help on using the changeset viewer.