comparison src/c/urweb.c @ 2226:e10881cd92da

Merge.
author Ziv Scully <ziv@mit.edu>
date Fri, 27 Mar 2015 11:26:06 -0400
parents 5709482a2afd 882556b3029d
children 2f7ed04332a0
comparison
equal deleted inserted replaced
2225:6262dabc08d6 2226:e10881cd92da
165 165
166 void *uw_init_client_data(); 166 void *uw_init_client_data();
167 void uw_free_client_data(void *); 167 void uw_free_client_data(void *);
168 void uw_copy_client_data(void *dst, void *src); 168 void uw_copy_client_data(void *dst, void *src);
169 169
170 static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER;
171
172 static uw_Basis_int my_rand() {
173 pthread_mutex_lock(&rand_mutex);
174 int ret, r = RAND_bytes((unsigned char *)&ret, sizeof ret);
175 pthread_mutex_unlock(&rand_mutex);
176
177 if (r)
178 return abs(ret);
179 else
180 return -1;
181 }
182
170 static client *new_client() { 183 static client *new_client() {
171 client *c; 184 client *c;
172 185
173 pthread_mutex_lock(&clients_mutex); 186 pthread_mutex_lock(&clients_mutex);
174 187
190 clients[n_clients-1] = c; 203 clients[n_clients-1] = c;
191 } 204 }
192 205
193 pthread_mutex_lock(&c->lock); 206 pthread_mutex_lock(&c->lock);
194 c->mode = USED; 207 c->mode = USED;
195 c->pass = rand(); 208 c->pass = my_rand();
196 c->sock = -1; 209 c->sock = -1;
197 c->last_contact = time(NULL); 210 c->last_contact = time(NULL);
198 uw_buffer_reset(&c->msgs); 211 uw_buffer_reset(&c->msgs);
199 c->n_channels = 0; 212 c->n_channels = 0;
200 c->refcount = 0; 213 c->refcount = 0;
347 360
348 extern void uw_global_custom(); 361 extern void uw_global_custom();
349 extern void uw_init_crypto(); 362 extern void uw_init_crypto();
350 363
351 void uw_global_init() { 364 void uw_global_init() {
352 srand(time(NULL) ^ getpid());
353
354 clients = malloc(0); 365 clients = malloc(0);
355 366
356 uw_global_custom(); 367 uw_global_custom();
357 uw_init_crypto(); 368 uw_init_crypto();
358 } 369 }
4232 else 4243 else
4233 fprintf(stderr, "%s\n", s); 4244 fprintf(stderr, "%s\n", s);
4234 return uw_unit_v; 4245 return uw_unit_v;
4235 } 4246 }
4236 4247
4237 static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER;
4238
4239 uw_Basis_int uw_Basis_rand(uw_context ctx) { 4248 uw_Basis_int uw_Basis_rand(uw_context ctx) {
4240 uw_Basis_int ret; 4249 int r = my_rand();
4241 pthread_mutex_lock(&rand_mutex); 4250
4242 int r = RAND_bytes((unsigned char *)&ret, sizeof ret); 4251 if (r >= 0)
4243 pthread_mutex_unlock(&rand_mutex); 4252 return r;
4244
4245 if (r)
4246 return abs(ret);
4247 else 4253 else
4248 uw_error(ctx, FATAL, "Random number generation failed"); 4254 uw_error(ctx, FATAL, "Random number generation failed");
4249 } 4255 }
4250 4256
4251 void uw_noPostBody(uw_context ctx) { 4257 void uw_noPostBody(uw_context ctx) {