* Initialize the Call-ID generator -- generates random prefix */
| 59 | * Initialize the Call-ID generator -- generates random prefix |
| 60 | */ |
| 61 | int init_callid(void) |
| 62 | { |
| 63 | int rand_bits, i; |
| 64 | |
| 65 | /* calculate the initial call-id */ |
| 66 | /* how many bits and chars do we need to display the |
| 67 | * whole ULONG number */ |
| 68 | callid_prefix.len = sizeof(unsigned long) * 2; |
| 69 | callid_prefix.s = callid_buf; |
| 70 | |
| 71 | if (callid_prefix.len > CALLID_NR_LEN) { |
| 72 | LM_ERR("too small callid buffer\n"); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | for(rand_bits = 1, i = RAND_MAX; i; i >>= 1, rand_bits++); /* how long are the rand()s ? */ |
| 77 | i = callid_prefix.len * 4 / rand_bits; /* how many rands() fit in the ULONG ? */ |
| 78 | |
| 79 | /* now fill in the callid with as many random |
| 80 | * numbers as you can + 1 */ |
| 81 | callid_nr = rand(); /* this is the + 1 */ |
| 82 | |
| 83 | while(i--) { |
| 84 | callid_nr <<= rand_bits; |
| 85 | callid_nr |= rand(); |
| 86 | } |
| 87 | |
| 88 | i = snprintf(callid_prefix.s, callid_prefix.len + 1, "%0*lx", callid_prefix.len, callid_nr); |
| 89 | if ((i == -1) || (i > callid_prefix.len)) { |
| 90 | LM_CRIT("callid calculation failed\n"); |
| 91 | return -2; |
| 92 | } |
| 93 | |
| 94 | LM_DBG("Call-ID initialization: '%.*s'\n", callid_prefix.len, callid_prefix.s); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* |