• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
31  * Normally you don't want this, use lws_sul instead inside the event loop.
32  * But sometimes for drivers it makes sense, so there's an internal-only
33  * crossplatform api for it.
34  */
35 
36 void
lws_msleep(unsigned int ms)37 lws_msleep(unsigned int ms)
38 {
39         usleep((unsigned int)(ms * LWS_US_PER_MS));
40 }
41 
42 lws_usec_t
lws_now_usecs(void)43 lws_now_usecs(void)
44 {
45 #if defined(LWS_HAVE_CLOCK_GETTIME)
46 	struct timespec ts;
47 
48 	if (clock_gettime(CLOCK_MONOTONIC, &ts))
49 		return 0;
50 
51 	return (((lws_usec_t)ts.tv_sec) * LWS_US_PER_SEC) +
52 			((lws_usec_t)ts.tv_nsec / LWS_NS_PER_US);
53 #else
54 	struct timeval now;
55 
56 	gettimeofday(&now, NULL);
57 	return (((lws_usec_t)now.tv_sec) * LWS_US_PER_SEC) +
58 			(lws_usec_t)now.tv_usec;
59 #endif
60 }
61 
62 size_t
lws_get_random(struct lws_context * context,void * buf,size_t len)63 lws_get_random(struct lws_context *context, void *buf, size_t len)
64 {
65 #if defined(__COVERITY__)
66 	memset(buf, 0, len);
67 	return len;
68 #else
69 	/* coverity[tainted_scalar] */
70 	return (size_t)read(context->fd_random, (char *)buf, len);
71 #endif
72 }
73 
lwsl_emit_syslog(int level,const char * line)74 void lwsl_emit_syslog(int level, const char *line)
75 {
76 	int syslog_level = LOG_DEBUG;
77 
78 	switch (level) {
79 	case LLL_ERR:
80 		syslog_level = LOG_ERR;
81 		break;
82 	case LLL_WARN:
83 		syslog_level = LOG_WARNING;
84 		break;
85 	case LLL_NOTICE:
86 		syslog_level = LOG_NOTICE;
87 		break;
88 	case LLL_INFO:
89 		syslog_level = LOG_INFO;
90 		break;
91 	}
92 	syslog(syslog_level, "%s", line);
93 }
94 
95 
96 int
lws_plat_write_cert(struct lws_vhost * vhost,int is_key,int fd,void * buf,size_t len)97 lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
98 			size_t len)
99 {
100 	ssize_t n;
101 
102 	n = write(fd, buf, len);
103 
104 	if (n < 0 || fsync(fd))
105 		return 1;
106 	if (lseek(fd, 0, SEEK_SET) < 0)
107 		return 1;
108 
109 	return (size_t)n != len;
110 }
111 
112 
113 int
lws_plat_recommended_rsa_bits(void)114 lws_plat_recommended_rsa_bits(void)
115 {
116 	return 4096;
117 }
118 
119 /*
120  * Platform-specific ntpclient server configuration
121  */
122 
123 int
lws_plat_ntpclient_config(struct lws_context * context)124 lws_plat_ntpclient_config(struct lws_context *context)
125 {
126 #if defined(LWS_HAVE_GETENV)
127 	char *ntpsrv = getenv("LWS_NTP_SERVER");
128 
129 	if (ntpsrv && strlen(ntpsrv) < 64) {
130 		lws_system_blob_t *blob = lws_system_get_blob(context,
131                                             LWS_SYSBLOB_TYPE_NTP_SERVER, 0);
132 		if (!blob)
133 			return 0;
134 
135 		lws_system_blob_direct_set(blob, (const uint8_t *)ntpsrv,
136 					    strlen(ntpsrv));
137 		return 1;
138 	}
139 #endif
140 	return 0;
141 }
142 
143