1073f3bb34ecee0dea8374f42a685a6fa29280fd
[openwrt/openwrt.git] / package / uhttpd / src / uhttpd-utils.c
1 /*
2 * uhttpd - Tiny single-threaded httpd - Utility functions
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #define _XOPEN_SOURCE 500 /* crypt() */
20 #define _BSD_SOURCE /* strcasecmp(), strncasecmp() */
21
22 #include "uhttpd.h"
23 #include "uhttpd-utils.h"
24
25 #ifdef HAVE_TLS
26 #include "uhttpd-tls.h"
27 #endif
28
29
30 static char *uh_index_files[] = {
31 "index.html",
32 "index.htm",
33 "default.html",
34 "default.htm"
35 };
36
37
38 const char * sa_straddr(void *sa)
39 {
40 static char str[INET6_ADDRSTRLEN];
41 struct sockaddr_in *v4 = (struct sockaddr_in *)sa;
42 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)sa;
43
44 if( v4->sin_family == AF_INET )
45 return inet_ntop(AF_INET, &(v4->sin_addr), str, sizeof(str));
46 else
47 return inet_ntop(AF_INET6, &(v6->sin6_addr), str, sizeof(str));
48 }
49
50 const char * sa_strport(void *sa)
51 {
52 static char str[6];
53 snprintf(str, sizeof(str), "%i", sa_port(sa));
54 return str;
55 }
56
57 int sa_port(void *sa)
58 {
59 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
60 }
61
62 int sa_rfc1918(void *sa)
63 {
64 struct sockaddr_in *v4 = (struct sockaddr_in *)sa;
65 unsigned long a = htonl(v4->sin_addr.s_addr);
66
67 if( v4->sin_family == AF_INET )
68 {
69 return ((a >= 0x0A000000) && (a <= 0x0AFFFFFF)) ||
70 ((a >= 0xAC100000) && (a <= 0xAC1FFFFF)) ||
71 ((a >= 0xC0A80000) && (a <= 0xC0A8FFFF));
72 }
73
74 return 0;
75 }
76
77 /* Simple strstr() like function that takes len arguments for both haystack and needle. */
78 char *strfind(char *haystack, int hslen, const char *needle, int ndlen)
79 {
80 int match = 0;
81 int i, j;
82
83 for( i = 0; i < hslen; i++ )
84 {
85 if( haystack[i] == needle[0] )
86 {
87 match = ((ndlen == 1) || ((i + ndlen) <= hslen));
88
89 for( j = 1; (j < ndlen) && ((i + j) < hslen); j++ )
90 {
91 if( haystack[i+j] != needle[j] )
92 {
93 match = 0;
94 break;
95 }
96 }
97
98 if( match )
99 return &haystack[i];
100 }
101 }
102
103 return NULL;
104 }
105
106 /* interruptable select() */
107 int select_intr(int n, fd_set *r, fd_set *w, fd_set *e, struct timeval *t)
108 {
109 int rv;
110 sigset_t ssn, sso;
111
112 /* unblock SIGCHLD */
113 sigemptyset(&ssn);
114 sigaddset(&ssn, SIGCHLD);
115 sigaddset(&ssn, SIGPIPE);
116 sigprocmask(SIG_UNBLOCK, &ssn, &sso);
117
118 rv = select(n, r, w, e, t);
119
120 /* restore signal mask */
121 sigprocmask(SIG_SETMASK, &sso, NULL);
122
123 return rv;
124 }
125
126
127 int uh_tcp_send_lowlevel(struct client *cl, const char *buf, int len)
128 {
129 fd_set writer;
130 struct timeval timeout;
131
132 FD_ZERO(&writer);
133 FD_SET(cl->socket, &writer);
134
135 timeout.tv_sec = cl->server->conf->network_timeout;
136 timeout.tv_usec = 0;
137
138 if (select(cl->socket + 1, NULL, &writer, NULL, &timeout) > 0)
139 return send(cl->socket, buf, len, 0);
140
141 return -1;
142 }
143
144 int uh_tcp_send(struct client *cl, const char *buf, int len)
145 {
146 #ifdef HAVE_TLS
147 if (cl->tls)
148 return cl->server->conf->tls_send(cl, (void *)buf, len);
149 else
150 #endif
151 return uh_tcp_send_lowlevel(cl, buf, len);
152 }
153
154 int uh_tcp_peek(struct client *cl, char *buf, int len)
155 {
156 /* sanity check, prevent overflowing peek buffer */
157 if (len > sizeof(cl->peekbuf))
158 return -1;
159
160 int sz = uh_tcp_recv(cl, buf, len);
161
162 /* store received data in peek buffer */
163 if( sz > 0 )
164 {
165 cl->peeklen = sz;
166 memcpy(cl->peekbuf, buf, sz);
167 }
168
169 return sz;
170 }
171
172 int uh_tcp_recv_lowlevel(struct client *cl, char *buf, int len)
173 {
174 fd_set reader;
175 struct timeval timeout;
176
177 FD_ZERO(&reader);
178 FD_SET(cl->socket, &reader);
179
180 timeout.tv_sec = cl->server->conf->network_timeout;
181 timeout.tv_usec = 0;
182
183 if (select(cl->socket + 1, &reader, NULL, NULL, &timeout) > 0)
184 return recv(cl->socket, buf, len, 0);
185
186 return -1;
187 }
188
189 int uh_tcp_recv(struct client *cl, char *buf, int len)
190 {
191 int sz = 0;
192 int rsz = 0;
193
194 /* first serve data from peek buffer */
195 if (cl->peeklen > 0)
196 {
197 sz = min(cl->peeklen, len);
198 len -= sz; cl->peeklen -= sz;
199 memcpy(buf, cl->peekbuf, sz);
200 memmove(cl->peekbuf, &cl->peekbuf[sz], cl->peeklen);
201 }
202
203 /* caller wants more */
204 if (len > 0)
205 {
206 #ifdef HAVE_TLS
207 if (cl->tls)
208 rsz = cl->server->conf->tls_recv(cl, (void *)&buf[sz], len);
209 else
210 #endif
211 rsz = uh_tcp_recv_lowlevel(cl, (void *)&buf[sz], len);
212
213 if (rsz < 0)
214 return rsz;
215
216 sz += rsz;
217 }
218
219 return sz;
220 }
221
222
223 int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...)
224 {
225 va_list ap;
226
227 char buffer[UH_LIMIT_MSGHEAD];
228 int len;
229
230 len = snprintf(buffer, sizeof(buffer),
231 "HTTP/1.1 %03i %s\r\n"
232 "Connection: close\r\n"
233 "Content-Type: text/plain\r\n"
234 "Transfer-Encoding: chunked\r\n\r\n",
235 code, summary
236 );
237
238 ensure_ret(uh_tcp_send(cl, buffer, len));
239
240 va_start(ap, fmt);
241 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
242 va_end(ap);
243
244 ensure_ret(uh_http_sendc(cl, buffer, len));
245 ensure_ret(uh_http_sendc(cl, NULL, 0));
246
247 return 0;
248 }
249
250
251 int uh_http_sendc(struct client *cl, const char *data, int len)
252 {
253 char chunk[8];
254 int clen;
255
256 if( len == -1 )
257 len = strlen(data);
258
259 if( len > 0 )
260 {
261 clen = snprintf(chunk, sizeof(chunk), "%X\r\n", len);
262 ensure_ret(uh_tcp_send(cl, chunk, clen));
263 ensure_ret(uh_tcp_send(cl, data, len));
264 ensure_ret(uh_tcp_send(cl, "\r\n", 2));
265 }
266 else
267 {
268 ensure_ret(uh_tcp_send(cl, "0\r\n\r\n", 5));
269 }
270
271 return 0;
272 }
273
274 int uh_http_sendf(
275 struct client *cl, struct http_request *req, const char *fmt, ...
276 ) {
277 va_list ap;
278 char buffer[UH_LIMIT_MSGHEAD];
279 int len;
280
281 va_start(ap, fmt);
282 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
283 va_end(ap);
284
285 if( (req != NULL) && (req->version > 1.0) )
286 ensure_ret(uh_http_sendc(cl, buffer, len));
287 else if( len > 0 )
288 ensure_ret(uh_tcp_send(cl, buffer, len));
289
290 return 0;
291 }
292
293 int uh_http_send(
294 struct client *cl, struct http_request *req, const char *buf, int len
295 ) {
296 if( len < 0 )
297 len = strlen(buf);
298
299 if( (req != NULL) && (req->version > 1.0) )
300 ensure_ret(uh_http_sendc(cl, buf, len));
301 else if( len > 0 )
302 ensure_ret(uh_tcp_send(cl, buf, len));
303
304 return 0;
305 }
306
307
308 /* blen is the size of buf; slen is the length of src. The input-string need
309 ** not be, and the output string will not be, null-terminated. Returns the
310 ** length of the decoded string. */
311 int uh_urldecode(char *buf, int blen, const char *src, int slen)
312 {
313 int i;
314 int len = 0;
315
316 #define hex(x) \
317 (((x) <= '9') ? ((x) - '0') : \
318 (((x) <= 'F') ? ((x) - 'A' + 10) : \
319 ((x) - 'a' + 10)))
320
321 for( i = 0; (i < slen) && (len < blen); i++ )
322 {
323 if( src[i] == '%' )
324 {
325 if( ((i+2) < slen) && isxdigit(src[i+1]) && isxdigit(src[i+2]) )
326 {
327 buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
328 i += 2;
329 }
330 else
331 {
332 buf[len++] = '%';
333 }
334 }
335 else
336 {
337 buf[len++] = src[i];
338 }
339 }
340
341 return len;
342 }
343
344 /* blen is the size of buf; slen is the length of src. The input-string need
345 ** not be, and the output string will not be, null-terminated. Returns the
346 ** length of the encoded string. */
347 int uh_urlencode(char *buf, int blen, const char *src, int slen)
348 {
349 int i;
350 int len = 0;
351 const char hex[] = "0123456789abcdef";
352
353 for( i = 0; (i < slen) && (len < blen); i++ )
354 {
355 if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
356 (src[i] == '.') || (src[i] == '~') )
357 {
358 buf[len++] = src[i];
359 }
360 else if( (len+3) <= blen )
361 {
362 buf[len++] = '%';
363 buf[len++] = hex[(src[i] >> 4) & 15];
364 buf[len++] = hex[ src[i] & 15];
365 }
366 else
367 {
368 break;
369 }
370 }
371
372 return len;
373 }
374
375 int uh_b64decode(char *buf, int blen, const unsigned char *src, int slen)
376 {
377 int i = 0;
378 int len = 0;
379
380 unsigned int cin = 0;
381 unsigned int cout = 0;
382
383
384 for( i = 0; (i <= slen) && (src[i] != 0); i++ )
385 {
386 cin = src[i];
387
388 if( (cin >= '0') && (cin <= '9') )
389 cin = cin - '0' + 52;
390 else if( (cin >= 'A') && (cin <= 'Z') )
391 cin = cin - 'A';
392 else if( (cin >= 'a') && (cin <= 'z') )
393 cin = cin - 'a' + 26;
394 else if( cin == '+' )
395 cin = 62;
396 else if( cin == '/' )
397 cin = 63;
398 else if( cin == '=' )
399 cin = 0;
400 else
401 continue;
402
403 cout = (cout << 6) | cin;
404
405 if( (i % 4) == 3 )
406 {
407 if( (len + 3) < blen )
408 {
409 buf[len++] = (char)(cout >> 16);
410 buf[len++] = (char)(cout >> 8);
411 buf[len++] = (char)(cout);
412 }
413 else
414 {
415 break;
416 }
417 }
418 }
419
420 buf[len++] = 0;
421 return len;
422 }
423
424 static char * canonpath(const char *path, char *path_resolved)
425 {
426 char path_copy[PATH_MAX];
427 char *path_cpy = path_copy;
428 char *path_res = path_resolved;
429
430 struct stat s;
431
432
433 /* relative -> absolute */
434 if( *path != '/' )
435 {
436 getcwd(path_copy, PATH_MAX);
437 strncat(path_copy, "/", PATH_MAX - strlen(path_copy));
438 strncat(path_copy, path, PATH_MAX - strlen(path_copy));
439 }
440 else
441 {
442 strncpy(path_copy, path, PATH_MAX);
443 }
444
445 /* normalize */
446 while( (*path_cpy != '\0') && (path_cpy < (path_copy + PATH_MAX - 2)) )
447 {
448 if( *path_cpy == '/' )
449 {
450 /* skip repeating / */
451 if( path_cpy[1] == '/' )
452 {
453 path_cpy++;
454 continue;
455 }
456
457 /* /./ or /../ */
458 else if( path_cpy[1] == '.' )
459 {
460 /* skip /./ */
461 if( (path_cpy[2] == '/') || (path_cpy[2] == '\0') )
462 {
463 path_cpy += 2;
464 continue;
465 }
466
467 /* collapse /x/../ */
468 else if( (path_cpy[2] == '.') &&
469 ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))
470 ) {
471 while( (path_res > path_resolved) && (*--path_res != '/') )
472 ;
473
474 path_cpy += 3;
475 continue;
476 }
477 }
478 }
479
480 *path_res++ = *path_cpy++;
481 }
482
483 /* remove trailing slash if not root / */
484 if( (path_res > (path_resolved+1)) && (path_res[-1] == '/') )
485 path_res--;
486 else if( path_res == path_resolved )
487 *path_res++ = '/';
488
489 *path_res = '\0';
490
491 /* test access */
492 if( !stat(path_resolved, &s) && (s.st_mode & S_IROTH) )
493 return path_resolved;
494
495 return NULL;
496 }
497
498 struct path_info * uh_path_lookup(struct client *cl, const char *url)
499 {
500 static char path_phys[PATH_MAX];
501 static char path_info[PATH_MAX];
502 static struct path_info p;
503
504 char buffer[UH_LIMIT_MSGHEAD];
505 char *docroot = cl->server->conf->docroot;
506 char *pathptr = NULL;
507
508 int slash = 0;
509 int no_sym = cl->server->conf->no_symlinks;
510 int i = 0;
511 struct stat s;
512
513 /* back out early if url is undefined */
514 if ( url == NULL )
515 return NULL;
516
517 memset(path_phys, 0, sizeof(path_phys));
518 memset(path_info, 0, sizeof(path_info));
519 memset(buffer, 0, sizeof(buffer));
520 memset(&p, 0, sizeof(p));
521
522 /* copy docroot */
523 memcpy(buffer, docroot,
524 min(strlen(docroot), sizeof(buffer) - 1));
525
526 /* separate query string from url */
527 if( (pathptr = strchr(url, '?')) != NULL )
528 {
529 p.query = pathptr[1] ? pathptr + 1 : NULL;
530
531 /* urldecode component w/o query */
532 if( pathptr > url )
533 uh_urldecode(
534 &buffer[strlen(docroot)],
535 sizeof(buffer) - strlen(docroot) - 1,
536 url, pathptr - url
537 );
538 }
539
540 /* no query string, decode all of url */
541 else
542 {
543 uh_urldecode(
544 &buffer[strlen(docroot)],
545 sizeof(buffer) - strlen(docroot) - 1,
546 url, strlen(url)
547 );
548 }
549
550 /* create canon path */
551 for( i = strlen(buffer), slash = (buffer[max(0, i-1)] == '/'); i >= 0; i-- )
552 {
553 if( (buffer[i] == 0) || (buffer[i] == '/') )
554 {
555 memset(path_info, 0, sizeof(path_info));
556 memcpy(path_info, buffer, min(i + 1, sizeof(path_info) - 1));
557
558 if( no_sym ? realpath(path_info, path_phys)
559 : canonpath(path_info, path_phys)
560 ) {
561 memset(path_info, 0, sizeof(path_info));
562 memcpy(path_info, &buffer[i],
563 min(strlen(buffer) - i, sizeof(path_info) - 1));
564
565 break;
566 }
567 }
568 }
569
570 /* check whether found path is within docroot */
571 if( strncmp(path_phys, docroot, strlen(docroot)) ||
572 ((path_phys[strlen(docroot)] != 0) &&
573 (path_phys[strlen(docroot)] != '/'))
574 ) {
575 return NULL;
576 }
577
578 /* test current path */
579 if( ! stat(path_phys, &p.stat) )
580 {
581 /* is a regular file */
582 if( p.stat.st_mode & S_IFREG )
583 {
584 p.root = docroot;
585 p.phys = path_phys;
586 p.name = &path_phys[strlen(docroot)];
587 p.info = path_info[0] ? path_info : NULL;
588 }
589
590 /* is a directory */
591 else if( (p.stat.st_mode & S_IFDIR) && !strlen(path_info) )
592 {
593 /* ensure trailing slash */
594 if( path_phys[strlen(path_phys)-1] != '/' )
595 path_phys[strlen(path_phys)] = '/';
596
597 /* try to locate index file */
598 memset(buffer, 0, sizeof(buffer));
599 memcpy(buffer, path_phys, sizeof(buffer));
600 pathptr = &buffer[strlen(buffer)];
601
602 /* if requested url resolves to a directory and a trailing slash
603 is missing in the request url, redirect the client to the same
604 url with trailing slash appended */
605 if( !slash )
606 {
607 uh_http_sendf(cl, NULL,
608 "HTTP/1.1 302 Found\r\n"
609 "Location: %s%s%s\r\n"
610 "Connection: close\r\n\r\n",
611 &path_phys[strlen(docroot)],
612 p.query ? "?" : "",
613 p.query ? p.query : ""
614 );
615
616 p.redirected = 1;
617 }
618 else if( cl->server->conf->index_file )
619 {
620 strncat(buffer, cl->server->conf->index_file, sizeof(buffer));
621
622 if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
623 {
624 memcpy(path_phys, buffer, sizeof(path_phys));
625 memcpy(&p.stat, &s, sizeof(p.stat));
626 }
627 }
628 else
629 {
630 for( i = 0; i < array_size(uh_index_files); i++ )
631 {
632 strncat(buffer, uh_index_files[i], sizeof(buffer));
633
634 if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
635 {
636 memcpy(path_phys, buffer, sizeof(path_phys));
637 memcpy(&p.stat, &s, sizeof(p.stat));
638 break;
639 }
640
641 *pathptr = 0;
642 }
643 }
644
645 p.root = docroot;
646 p.phys = path_phys;
647 p.name = &path_phys[strlen(docroot)];
648 }
649 }
650
651 return p.phys ? &p : NULL;
652 }
653
654
655 static struct auth_realm *uh_realms = NULL;
656
657 struct auth_realm * uh_auth_add(char *path, char *user, char *pass)
658 {
659 struct auth_realm *new = NULL;
660 struct passwd *pwd;
661
662 #ifdef HAVE_SHADOW
663 struct spwd *spwd;
664 #endif
665
666 if((new = (struct auth_realm *)malloc(sizeof(struct auth_realm))) != NULL)
667 {
668 memset(new, 0, sizeof(struct auth_realm));
669
670 memcpy(new->path, path,
671 min(strlen(path), sizeof(new->path) - 1));
672
673 memcpy(new->user, user,
674 min(strlen(user), sizeof(new->user) - 1));
675
676 /* given password refers to a passwd entry */
677 if( (strlen(pass) > 3) && !strncmp(pass, "$p$", 3) )
678 {
679 #ifdef HAVE_SHADOW
680 /* try to resolve shadow entry */
681 if( ((spwd = getspnam(&pass[3])) != NULL) && spwd->sp_pwdp )
682 {
683 memcpy(new->pass, spwd->sp_pwdp,
684 min(strlen(spwd->sp_pwdp), sizeof(new->pass) - 1));
685 }
686
687 else
688 #endif
689
690 /* try to resolve passwd entry */
691 if( ((pwd = getpwnam(&pass[3])) != NULL) && pwd->pw_passwd &&
692 (pwd->pw_passwd[0] != '!') && (pwd->pw_passwd[0] != 0)
693 ) {
694 memcpy(new->pass, pwd->pw_passwd,
695 min(strlen(pwd->pw_passwd), sizeof(new->pass) - 1));
696 }
697 }
698
699 /* ordinary pwd */
700 else
701 {
702 memcpy(new->pass, pass,
703 min(strlen(pass), sizeof(new->pass) - 1));
704 }
705
706 if( new->pass[0] )
707 {
708 new->next = uh_realms;
709 uh_realms = new;
710
711 return new;
712 }
713
714 free(new);
715 }
716
717 return NULL;
718 }
719
720 int uh_auth_check(
721 struct client *cl, struct http_request *req, struct path_info *pi
722 ) {
723 int i, plen, rlen, protected;
724 char buffer[UH_LIMIT_MSGHEAD];
725 char *user = NULL;
726 char *pass = NULL;
727
728 struct auth_realm *realm = NULL;
729
730 plen = strlen(pi->name);
731 protected = 0;
732
733 /* check whether at least one realm covers the requested url */
734 for( realm = uh_realms; realm; realm = realm->next )
735 {
736 rlen = strlen(realm->path);
737
738 if( (plen >= rlen) && !strncasecmp(pi->name, realm->path, rlen) )
739 {
740 req->realm = realm;
741 protected = 1;
742 break;
743 }
744 }
745
746 /* requested resource is covered by a realm */
747 if( protected )
748 {
749 /* try to get client auth info */
750 foreach_header(i, req->headers)
751 {
752 if( !strcasecmp(req->headers[i], "Authorization") &&
753 (strlen(req->headers[i+1]) > 6) &&
754 !strncasecmp(req->headers[i+1], "Basic ", 6)
755 ) {
756 memset(buffer, 0, sizeof(buffer));
757 uh_b64decode(buffer, sizeof(buffer) - 1,
758 (unsigned char *) &req->headers[i+1][6],
759 strlen(req->headers[i+1]) - 6);
760
761 if( (pass = strchr(buffer, ':')) != NULL )
762 {
763 user = buffer;
764 *pass++ = 0;
765 }
766
767 break;
768 }
769 }
770
771 /* have client auth */
772 if( user && pass )
773 {
774 /* find matching realm */
775 for( realm = uh_realms; realm; realm = realm->next )
776 {
777 rlen = strlen(realm->path);
778
779 if( (plen >= rlen) &&
780 !strncasecmp(pi->name, realm->path, rlen) &&
781 !strcmp(user, realm->user)
782 ) {
783 req->realm = realm;
784 break;
785 }
786 }
787
788 /* found a realm matching the username */
789 if( realm )
790 {
791 /* check user pass */
792 if (!strcmp(pass, realm->pass) ||
793 !strcmp(crypt(pass, realm->pass), realm->pass))
794 return 1;
795 }
796 }
797
798 /* 401 */
799 uh_http_sendf(cl, NULL,
800 "HTTP/%.1f 401 Authorization Required\r\n"
801 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
802 "Content-Type: text/plain\r\n"
803 "Content-Length: 23\r\n\r\n"
804 "Authorization Required\n",
805 req->version, cl->server->conf->realm
806 );
807
808 return 0;
809 }
810
811 return 1;
812 }
813
814
815 static struct listener *uh_listeners = NULL;
816 static struct client *uh_clients = NULL;
817
818 struct listener * uh_listener_add(int sock, struct config *conf)
819 {
820 struct listener *new = NULL;
821 socklen_t sl;
822
823 if( (new = (struct listener *)malloc(sizeof(struct listener))) != NULL )
824 {
825 memset(new, 0, sizeof(struct listener));
826
827 new->socket = sock;
828 new->conf = conf;
829
830 /* get local endpoint addr */
831 sl = sizeof(struct sockaddr_in6);
832 memset(&(new->addr), 0, sl);
833 getsockname(sock, (struct sockaddr *) &(new->addr), &sl);
834
835 new->next = uh_listeners;
836 uh_listeners = new;
837
838 return new;
839 }
840
841 return NULL;
842 }
843
844 struct listener * uh_listener_lookup(int sock)
845 {
846 struct listener *cur = NULL;
847
848 for( cur = uh_listeners; cur; cur = cur->next )
849 if( cur->socket == sock )
850 return cur;
851
852 return NULL;
853 }
854
855
856 struct client * uh_client_add(int sock, struct listener *serv)
857 {
858 struct client *new = NULL;
859 socklen_t sl;
860
861 if( (new = (struct client *)malloc(sizeof(struct client))) != NULL )
862 {
863 memset(new, 0, sizeof(struct client));
864
865 new->socket = sock;
866 new->server = serv;
867
868 /* get remote endpoint addr */
869 sl = sizeof(struct sockaddr_in6);
870 memset(&(new->peeraddr), 0, sl);
871 getpeername(sock, (struct sockaddr *) &(new->peeraddr), &sl);
872
873 /* get local endpoint addr */
874 sl = sizeof(struct sockaddr_in6);
875 memset(&(new->servaddr), 0, sl);
876 getsockname(sock, (struct sockaddr *) &(new->servaddr), &sl);
877
878 new->next = uh_clients;
879 uh_clients = new;
880 }
881
882 return new;
883 }
884
885 struct client * uh_client_lookup(int sock)
886 {
887 struct client *cur = NULL;
888
889 for( cur = uh_clients; cur; cur = cur->next )
890 if( cur->socket == sock )
891 return cur;
892
893 return NULL;
894 }
895
896 void uh_client_remove(int sock)
897 {
898 struct client *cur = NULL;
899 struct client *prv = NULL;
900
901 for( cur = uh_clients; cur; prv = cur, cur = cur->next )
902 {
903 if( cur->socket == sock )
904 {
905 if( prv )
906 prv->next = cur->next;
907 else
908 uh_clients = cur->next;
909
910 free(cur);
911 break;
912 }
913 }
914 }
915
916
917 #ifdef HAVE_CGI
918 static struct interpreter *uh_interpreters = NULL;
919
920 struct interpreter * uh_interpreter_add(const char *extn, const char *path)
921 {
922 struct interpreter *new = NULL;
923
924 if( (new = (struct interpreter *)
925 malloc(sizeof(struct interpreter))) != NULL )
926 {
927 memset(new, 0, sizeof(struct interpreter));
928
929 memcpy(new->extn, extn, min(strlen(extn), sizeof(new->extn)-1));
930 memcpy(new->path, path, min(strlen(path), sizeof(new->path)-1));
931
932 new->next = uh_interpreters;
933 uh_interpreters = new;
934
935 return new;
936 }
937
938 return NULL;
939 }
940
941 struct interpreter * uh_interpreter_lookup(const char *path)
942 {
943 struct interpreter *cur = NULL;
944 const char *e;
945
946 for( cur = uh_interpreters; cur; cur = cur->next )
947 {
948 e = &path[max(strlen(path) - strlen(cur->extn), 0)];
949
950 if( !strcmp(e, cur->extn) )
951 return cur;
952 }
953
954 return NULL;
955 }
956 #endif