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 #include "private-lib-core.h"
26
lws_plat_apply_FD_CLOEXEC(int n)27 int lws_plat_apply_FD_CLOEXEC(int n)
28 {
29 return 0;
30 }
31
32
33 lws_fop_fd_t IRAM_ATTR
_lws_plat_file_open(const struct lws_plat_file_ops * fops,const char * filename,const char * vpath,lws_fop_flags_t * flags)34 _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
35 const char *vpath, lws_fop_flags_t *flags)
36 {
37 struct stat stat_buf;
38 lws_fop_fd_t fop_fd;
39 int ret = open(filename, *flags, 0664);
40
41 if (ret < 0)
42 return NULL;
43
44 if (fstat(ret, &stat_buf) < 0)
45 goto bail;
46
47 fop_fd = lws_malloc(sizeof(*fop_fd), "fops open");
48 if (!fop_fd)
49 goto bail;
50
51 fop_fd->fops = fops;
52 fop_fd->fd = ret;
53 fop_fd->flags = *flags;
54 fop_fd->filesystem_priv = NULL; /* we don't use it */
55 fop_fd->pos = 0;
56 fop_fd->len = stat_buf.st_size;
57
58 return fop_fd;
59
60 bail:
61 close(ret);
62
63 return NULL;
64 }
65
66 int IRAM_ATTR
_lws_plat_file_close(lws_fop_fd_t * fops_fd)67 _lws_plat_file_close(lws_fop_fd_t *fops_fd)
68 {
69 int fd = (*fops_fd)->fd;
70
71 lws_free(*fops_fd);
72 *fops_fd = NULL;
73
74 return close(fd);
75 }
76
77 lws_fileofs_t IRAM_ATTR
_lws_plat_file_seek_cur(lws_fop_fd_t fops_fd,lws_fileofs_t offset)78 _lws_plat_file_seek_cur(lws_fop_fd_t fops_fd, lws_fileofs_t offset)
79 {
80 return lseek(fops_fd->fd, offset, SEEK_CUR);
81 }
82
83 int IRAM_ATTR
_lws_plat_file_read(lws_fop_fd_t fops_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)84 _lws_plat_file_read(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
85 uint8_t *buf, lws_filepos_t len)
86 {
87 long n;
88
89 n = read(fops_fd->fd, buf, len);
90 if (n == -1) {
91 *amount = 0;
92 return -1;
93 }
94 fops_fd->pos += n;
95 *amount = n;
96
97 return 0;
98 }
99
100 int IRAM_ATTR
_lws_plat_file_write(lws_fop_fd_t fops_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)101 _lws_plat_file_write(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
102 uint8_t *buf, lws_filepos_t len)
103 {
104 long n;
105
106 n = write(fops_fd->fd, buf, len);
107 if (n == -1) {
108 *amount = 0;
109 return -1;
110 }
111 fops_fd->pos += n;
112 *amount = n;
113
114 return 0;
115 }
116
117 #if defined(LWS_AMAZON_RTOS)
118 int
lws_find_string_in_file(const char * filename,const char * string,int stringlen)119 lws_find_string_in_file(const char *filename, const char *string, int stringlen)
120 {
121 return 0;
122 }
123 #else
124 int
lws_find_string_in_file(const char * filename,const char * string,int stringlen)125 lws_find_string_in_file(const char *filename, const char *string, int stringlen)
126 {
127 nvs_handle nvh;
128 size_t s;
129 int n;
130 char buf[64], result[64];
131 const char *p = strchr(string, ':'), *q;
132
133 if (!p)
134 return 0;
135
136 q = string;
137 n = 0;
138 while ((size_t)n < sizeof(buf) - 1 && q != p)
139 buf[n++] = *q++;
140 buf[n] = '\0';
141
142 ESP_ERROR_CHECK(nvs_open(filename, NVS_READWRITE, &nvh));
143
144 s = sizeof(result) - 1;
145 n = nvs_get_str(nvh, buf, result, &s);
146 nvs_close(nvh);
147
148 if (n != ESP_OK)
149 return 0;
150
151 return !strcmp(p + 1, result);
152 }
153 #endif
154
155 #if !defined(LWS_AMAZON_RTOS)
156 int
lws_plat_write_file(const char * filename,void * buf,size_t len)157 lws_plat_write_file(const char *filename, void *buf, size_t len)
158 {
159 nvs_handle nvh;
160 int n;
161
162 if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
163 lwsl_notice("%s: failed to open nvs\n", __func__);
164 return -1;
165 }
166
167 n = nvs_set_blob(nvh, filename, buf, len);
168 if (n >= 0)
169 nvs_commit(nvh);
170
171 nvs_close(nvh);
172
173 lwsl_notice("%s: wrote %s (%d)\n", __func__, filename, n);
174
175 return n;
176 }
177
178 /* we write vhostname.cert.pem and vhostname.key.pem, 0 return means OK */
179
180 int
lws_plat_write_cert(struct lws_vhost * vhost,int is_key,int fd,void * buf,size_t len)181 lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
182 size_t len)
183 {
184 const char *name = vhost->tls.alloc_cert_path;
185
186 if (is_key)
187 name = vhost->tls.key_path;
188
189 return lws_plat_write_file(name, buf, len) < 0;
190 }
191
192 int
lws_plat_read_file(const char * filename,void * buf,size_t len)193 lws_plat_read_file(const char *filename, void *buf, size_t len)
194 {
195 nvs_handle nvh;
196 size_t s = 0;
197 int n = 0;
198
199 if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
200 lwsl_notice("%s: failed to open nvs\n", __func__);
201 return 1;
202 }
203
204 ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
205 if (nvs_get_blob(nvh, filename, NULL, &s) != ESP_OK)
206 goto bail;
207 if (s > len)
208 goto bail;
209
210 n = nvs_get_blob(nvh, filename, buf, &s);
211
212 nvs_close(nvh);
213
214 lwsl_notice("%s: read %s (%d)\n", __func__, filename, (int)s);
215
216 if (n)
217 return -1;
218
219 return (int)s;
220
221 bail:
222 nvs_close(nvh);
223
224 return -1;
225 }
226 #endif /* LWS_AMAZON_RTOS */
227