comparison src/c/urweb.c @ 2104:7755f00a4fc3

Switch to using OpenSSL PRNG for the one remaining rand()
author Adam Chlipala <adam@chlipala.net>
date Mon, 12 Jan 2015 12:02:54 -0500
parents fd6d362666c0
children 882556b3029d
comparison
equal deleted inserted replaced
2103:661b531f55bd 2104:7755f00a4fc3
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 r = RAND_bytes((unsigned char *)&ret, sizeof ret);
175 pthread_mutex_unlock(&rand_mutex);
176
177 if (r)
178 return abs(r);
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;
4219 else 4232 else
4220 fprintf(stderr, "%s\n", s); 4233 fprintf(stderr, "%s\n", s);
4221 return uw_unit_v; 4234 return uw_unit_v;
4222 } 4235 }
4223 4236
4224 static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER;
4225
4226 uw_Basis_int uw_Basis_rand(uw_context ctx) { 4237 uw_Basis_int uw_Basis_rand(uw_context ctx) {
4227 uw_Basis_int ret; 4238 int r = my_rand();
4228 pthread_mutex_lock(&rand_mutex); 4239
4229 int r = RAND_bytes((unsigned char *)&ret, sizeof ret); 4240 if (r >= 0)
4230 pthread_mutex_unlock(&rand_mutex); 4241 return r;
4231
4232 if (r)
4233 return abs(ret);
4234 else 4242 else
4235 uw_error(ctx, FATAL, "Random number generation failed"); 4243 uw_error(ctx, FATAL, "Random number generation failed");
4236 } 4244 }
4237 4245
4238 void uw_noPostBody(uw_context ctx) { 4246 void uw_noPostBody(uw_context ctx) {