1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #if !defined(_GNU_SOURCE)
26 #define _GNU_SOURCE
27 #endif
28 #include "private-lib-core.h"
29
30 lws_usec_t
lws_now_usecs(void)31 lws_now_usecs(void)
32 {
33 #if defined(LWS_HAVE_CLOCK_GETTIME)
34 struct timespec ts;
35
36 if (clock_gettime(CLOCK_MONOTONIC, &ts))
37 return 0;
38
39 return (((lws_usec_t)ts.tv_sec) * LWS_US_PER_SEC) +
40 ((lws_usec_t)ts.tv_nsec / LWS_NS_PER_US);
41 #else
42 struct timeval now;
43
44 gettimeofday(&now, NULL);
45 return (((lws_usec_t)now.tv_sec) * LWS_US_PER_SEC) +
46 (lws_usec_t)now.tv_usec;
47 #endif
48 }
49
50 size_t
lws_get_random(struct lws_context * context,void * buf,size_t len)51 lws_get_random(struct lws_context *context, void *buf, size_t len)
52 {
53 #if defined(__COVERITY__)
54 memset(buf, 0, len);
55 return len;
56 #else
57 /* coverity[tainted_scalar] */
58 return (size_t)read(context->fd_random, (char *)buf, len);
59 #endif
60 }
61
lwsl_emit_syslog(int level,const char * line)62 void lwsl_emit_syslog(int level, const char *line)
63 {
64 int syslog_level = LOG_DEBUG;
65
66 switch (level) {
67 case LLL_ERR:
68 syslog_level = LOG_ERR;
69 break;
70 case LLL_WARN:
71 syslog_level = LOG_WARNING;
72 break;
73 case LLL_NOTICE:
74 syslog_level = LOG_NOTICE;
75 break;
76 case LLL_INFO:
77 syslog_level = LOG_INFO;
78 break;
79 }
80 syslog(syslog_level, "%s", line);
81 }
82
83
84 int
lws_plat_write_cert(struct lws_vhost * vhost,int is_key,int fd,void * buf,int len)85 lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
86 int len)
87 {
88 int n;
89
90 n = write(fd, buf, len);
91
92 fsync(fd);
93 if (lseek(fd, 0, SEEK_SET) < 0)
94 return 1;
95
96 return n != len;
97 }
98
99
100 int
lws_plat_recommended_rsa_bits(void)101 lws_plat_recommended_rsa_bits(void)
102 {
103 return 4096;
104 }
105