brcm47xx: update patches
[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(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 {
140 #ifdef HAVE_TLS
141 if( cl->tls )
142 return cl->server->conf->tls_send(cl, (void *)buf, len);
143 else
144 #endif
145 return send(cl->socket, buf, len, 0);
146 }
147
148 return -1;
149 }
150
151 int uh_tcp_peek(struct client *cl, char *buf, int len)
152 {
153 int sz = uh_tcp_recv(cl, buf, len);
154
155 /* store received data in peek buffer */
156 if( sz > 0 )
157 {
158 cl->peeklen = sz;
159 memcpy(cl->peekbuf, buf, sz);
160 }
161
162 return sz;
163 }
164
165 int uh_tcp_recv(struct client *cl, char *buf, int len)
166 {
167 int sz = 0;
168 int rsz = 0;
169
170 fd_set reader;
171 struct timeval timeout;
172
173 /* first serve data from peek buffer */
174 if( cl->peeklen > 0 )
175 {
176 sz = min(cl->peeklen, len);
177 len -= sz; cl->peeklen -= sz;
178
179 memcpy(buf, cl->peekbuf, sz);
180 memmove(cl->peekbuf, &cl->peekbuf[sz], cl->peeklen);
181 }
182
183 /* caller wants more */
184 if( len > 0 )
185 {
186 FD_ZERO(&reader);
187 FD_SET(cl->socket, &reader);
188
189 timeout.tv_sec = cl->server->conf->network_timeout;
190 timeout.tv_usec = 0;
191
192 if( select(cl->socket + 1, &reader, NULL, NULL, &timeout) > 0 )
193 {
194 #ifdef HAVE_TLS
195 if( cl->tls )
196 rsz = cl->server->conf->tls_recv(cl, (void *)&buf[sz], len);
197 else
198 #endif
199 rsz = recv(cl->socket, (void *)&buf[sz], len, 0);
200
201 if( (sz == 0) || (rsz > 0) )
202 sz += rsz;
203 }
204 else if( sz == 0 )
205 {
206 sz = -1;
207 }
208 }
209
210 return sz;
211 }
212
213
214 int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...)
215 {
216 va_list ap;
217
218 char buffer[UH_LIMIT_MSGHEAD];
219 int len;
220
221 len = snprintf(buffer, sizeof(buffer),
222 "HTTP/1.1 %03i %s\r\n"
223 "Connection: close\r\n"
224 "Content-Type: text/plain\r\n"
225 "Transfer-Encoding: chunked\r\n\r\n",
226 code, summary
227 );
228
229 ensure_ret(uh_tcp_send(cl, buffer, len));
230
231 va_start(ap, fmt);
232 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
233 va_end(ap);
234
235 ensure_ret(uh_http_sendc(cl, buffer, len));
236 ensure_ret(uh_http_sendc(cl, NULL, 0));
237
238 return 0;
239 }
240
241
242 int uh_http_sendc(struct client *cl, const char *data, int len)
243 {
244 char chunk[8];
245 int clen;
246
247 if( len == -1 )
248 len = strlen(data);
249
250 if( len > 0 )
251 {
252 clen = snprintf(chunk, sizeof(chunk), "%X\r\n", len);
253 ensure_ret(uh_tcp_send(cl, chunk, clen));
254 ensure_ret(uh_tcp_send(cl, data, len));
255 ensure_ret(uh_tcp_send(cl, "\r\n", 2));
256 }
257 else
258 {
259 ensure_ret(uh_tcp_send(cl, "0\r\n\r\n", 5));
260 }
261
262 return 0;
263 }
264
265 int uh_http_sendf(
266 struct client *cl, struct http_request *req, const char *fmt, ...
267 ) {
268 va_list ap;
269 char buffer[UH_LIMIT_MSGHEAD];
270 int len;
271
272 va_start(ap, fmt);
273 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
274 va_end(ap);
275
276 if( (req != NULL) && (req->version > 1.0) )
277 ensure_ret(uh_http_sendc(cl, buffer, len));
278 else if( len > 0 )
279 ensure_ret(uh_tcp_send(cl, buffer, len));
280
281 return 0;
282 }
283
284 int uh_http_send(
285 struct client *cl, struct http_request *req, const char *buf, int len
286 ) {
287 if( len < 0 )
288 len = strlen(buf);
289
290 if( (req != NULL) && (req->version > 1.0) )
291 ensure_ret(uh_http_sendc(cl, buf, len));
292 else if( len > 0 )
293 ensure_ret(uh_tcp_send(cl, buf, len));
294
295 return 0;
296 }
297
298
299 int uh_urldecode(char *buf, int blen, const char *src, int slen)
300 {
301 int i;
302 int len = 0;
303
304 #define hex(x) \
305 (((x) <= '9') ? ((x) - '0') : \
306 (((x) <= 'F') ? ((x) - 'A' + 10) : \
307 ((x) - 'a' + 10)))
308
309 for( i = 0; (i <= slen) && (i <= blen); i++ )
310 {
311 if( src[i] == '%' )
312 {
313 if( ((i+2) <= slen) && isxdigit(src[i+1]) && isxdigit(src[i+2]) )
314 {
315 buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
316 i += 2;
317 }
318 else
319 {
320 buf[len++] = '%';
321 }
322 }
323 else
324 {
325 buf[len++] = src[i];
326 }
327 }
328
329 return len;
330 }
331
332 int uh_urlencode(char *buf, int blen, const char *src, int slen)
333 {
334 int i;
335 int len = 0;
336 const char hex[] = "0123456789abcdef";
337
338 for( i = 0; (i <= slen) && (i <= blen); i++ )
339 {
340 if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
341 (src[i] == '.') || (src[i] == '~') )
342 {
343 buf[len++] = src[i];
344 }
345 else if( (len+3) <= blen )
346 {
347 buf[len++] = '%';
348 buf[len++] = hex[(src[i] >> 4) & 15];
349 buf[len++] = hex[(src[i] & 15) & 15];
350 }
351 else
352 {
353 break;
354 }
355 }
356
357 return len;
358 }
359
360 int uh_b64decode(char *buf, int blen, const unsigned char *src, int slen)
361 {
362 int i = 0;
363 int len = 0;
364
365 unsigned int cin = 0;
366 unsigned int cout = 0;
367
368
369 for( i = 0; (i <= slen) && (src[i] != 0); i++ )
370 {
371 cin = src[i];
372
373 if( (cin >= '0') && (cin <= '9') )
374 cin = cin - '0' + 52;
375 else if( (cin >= 'A') && (cin <= 'Z') )
376 cin = cin - 'A';
377 else if( (cin >= 'a') && (cin <= 'z') )
378 cin = cin - 'a' + 26;
379 else if( cin == '+' )
380 cin = 62;
381 else if( cin == '/' )
382 cin = 63;
383 else if( cin == '=' )
384 cin = 0;
385 else
386 continue;
387
388 cout = (cout << 6) | cin;
389
390 if( (i % 4) == 3 )
391 {
392 if( (len + 3) < blen )
393 {
394 buf[len++] = (char)(cout >> 16);
395 buf[len++] = (char)(cout >> 8);
396 buf[len++] = (char)(cout);
397 }
398 else
399 {
400 break;
401 }
402 }
403 }
404
405 buf[len++] = 0;
406 return len;
407 }
408
409 static char * canonpath(const char *path, char *path_resolved)
410 {
411 char path_copy[PATH_MAX];
412 char *path_cpy = path_copy;
413 char *path_res = path_resolved;
414
415 struct stat s;
416
417
418 /* relative -> absolute */
419 if( *path != '/' )
420 {
421 getcwd(path_copy, PATH_MAX);
422 strncat(path_copy, "/", PATH_MAX - strlen(path_copy));
423 strncat(path_copy, path, PATH_MAX - strlen(path_copy));
424 }
425 else
426 {
427 strncpy(path_copy, path, PATH_MAX);
428 }
429
430 /* normalize */
431 while( (*path_cpy != '\0') && (path_cpy < (path_copy + PATH_MAX - 2)) )
432 {
433 if( *path_cpy == '/' )
434 {
435 /* skip repeating / */
436 if( path_cpy[1] == '/' )
437 {
438 path_cpy++;
439 continue;
440 }
441
442 /* /./ or /../ */
443 else if( path_cpy[1] == '.' )
444 {
445 /* skip /./ */
446 if( (path_cpy[2] == '/') || (path_cpy[2] == '\0') )
447 {
448 path_cpy += 2;
449 continue;
450 }
451
452 /* collapse /x/../ */
453 else if( (path_cpy[2] == '.') &&
454 ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))
455 ) {
456 while( (path_res > path_resolved) && (*--path_res != '/') )
457 ;
458
459 path_cpy += 3;
460 continue;
461 }
462 }
463 }
464
465 *path_res++ = *path_cpy++;
466 }
467
468 /* remove trailing slash if not root / */
469 if( (path_res > (path_resolved+1)) && (path_res[-1] == '/') )
470 path_res--;
471 else if( path_res == path_resolved )
472 *path_res++ = '/';
473
474 *path_res = '\0';
475
476 /* test access */
477 if( !stat(path_resolved, &s) && (s.st_mode & S_IROTH) )
478 return path_resolved;
479
480 return NULL;
481 }
482
483 struct path_info * uh_path_lookup(struct client *cl, const char *url)
484 {
485 static char path_phys[PATH_MAX];
486 static char path_info[PATH_MAX];
487 static struct path_info p;
488
489 char buffer[UH_LIMIT_MSGHEAD];
490 char *docroot = cl->server->conf->docroot;
491 char *pathptr = NULL;
492
493 int slash = 0;
494 int no_sym = cl->server->conf->no_symlinks;
495 int i = 0;
496 struct stat s;
497
498 /* back out early if url is undefined */
499 if ( url == NULL )
500 return NULL;
501
502 memset(path_phys, 0, sizeof(path_phys));
503 memset(path_info, 0, sizeof(path_info));
504 memset(buffer, 0, sizeof(buffer));
505 memset(&p, 0, sizeof(p));
506
507 /* copy docroot */
508 memcpy(buffer, docroot,
509 min(strlen(docroot), sizeof(buffer) - 1));
510
511 /* separate query string from url */
512 if( (pathptr = strchr(url, '?')) != NULL )
513 {
514 p.query = pathptr[1] ? pathptr + 1 : NULL;
515
516 /* urldecode component w/o query */
517 if( pathptr > url )
518 uh_urldecode(
519 &buffer[strlen(docroot)],
520 sizeof(buffer) - strlen(docroot) - 1,
521 url, (int)(pathptr - url) - 1
522 );
523 }
524
525 /* no query string, decode all of url */
526 else
527 {
528 uh_urldecode(
529 &buffer[strlen(docroot)],
530 sizeof(buffer) - strlen(docroot) - 1,
531 url, strlen(url)
532 );
533 }
534
535 /* create canon path */
536 for( i = strlen(buffer), slash = (buffer[max(0, i-1)] == '/'); i >= 0; i-- )
537 {
538 if( (buffer[i] == 0) || (buffer[i] == '/') )
539 {
540 memset(path_info, 0, sizeof(path_info));
541 memcpy(path_info, buffer, min(i + 1, sizeof(path_info) - 1));
542
543 if( no_sym ? realpath(path_info, path_phys)
544 : canonpath(path_info, path_phys)
545 ) {
546 memset(path_info, 0, sizeof(path_info));
547 memcpy(path_info, &buffer[i],
548 min(strlen(buffer) - i, sizeof(path_info) - 1));
549
550 break;
551 }
552 }
553 }
554
555 /* check whether found path is within docroot */
556 if( strncmp(path_phys, docroot, strlen(docroot)) ||
557 ((path_phys[strlen(docroot)] != 0) &&
558 (path_phys[strlen(docroot)] != '/'))
559 ) {
560 return NULL;
561 }
562
563 /* test current path */
564 if( ! stat(path_phys, &p.stat) )
565 {
566 /* is a regular file */
567 if( p.stat.st_mode & S_IFREG )
568 {
569 p.root = docroot;
570 p.phys = path_phys;
571 p.name = &path_phys[strlen(docroot)];
572 p.info = path_info[0] ? path_info : NULL;
573 }
574
575 /* is a directory */
576 else if( (p.stat.st_mode & S_IFDIR) && !strlen(path_info) )
577 {
578 /* ensure trailing slash */
579 if( path_phys[strlen(path_phys)-1] != '/' )
580 path_phys[strlen(path_phys)] = '/';
581
582 /* try to locate index file */
583 memset(buffer, 0, sizeof(buffer));
584 memcpy(buffer, path_phys, sizeof(buffer));
585 pathptr = &buffer[strlen(buffer)];
586
587 /* if requested url resolves to a directory and a trailing slash
588 is missing in the request url, redirect the client to the same
589 url with trailing slash appended */
590 if( !slash )
591 {
592 uh_http_sendf(cl, NULL,
593 "HTTP/1.1 302 Found\r\n"
594 "Location: %s%s%s\r\n"
595 "Connection: close\r\n\r\n",
596 &path_phys[strlen(docroot)],
597 p.query ? "?" : "",
598 p.query ? p.query : ""
599 );
600
601 p.redirected = 1;
602 }
603 else if( cl->server->conf->index_file )
604 {
605 strncat(buffer, cl->server->conf->index_file, sizeof(buffer));
606
607 if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
608 {
609 memcpy(path_phys, buffer, sizeof(path_phys));
610 memcpy(&p.stat, &s, sizeof(p.stat));
611 }
612 }
613 else
614 {
615 for( i = 0; i < array_size(uh_index_files); i++ )
616 {
617 strncat(buffer, uh_index_files[i], sizeof(buffer));
618
619 if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
620 {
621 memcpy(path_phys, buffer, sizeof(path_phys));
622 memcpy(&p.stat, &s, sizeof(p.stat));
623 break;
624 }
625
626 *pathptr = 0;
627 }
628 }
629
630 p.root = docroot;
631 p.phys = path_phys;
632 p.name = &path_phys[strlen(docroot)];
633 }
634 }
635
636 return p.phys ? &p : NULL;
637 }
638
639
640 static struct auth_realm *uh_realms = NULL;
641
642 struct auth_realm * uh_auth_add(char *path, char *user, char *pass)
643 {
644 struct auth_realm *new = NULL;
645 struct passwd *pwd;
646
647 #ifdef HAVE_SHADOW
648 struct spwd *spwd;
649 #endif
650
651 if((new = (struct auth_realm *)malloc(sizeof(struct auth_realm))) != NULL)
652 {
653 memset(new, 0, sizeof(struct auth_realm));
654
655 memcpy(new->path, path,
656 min(strlen(path), sizeof(new->path) - 1));
657
658 memcpy(new->user, user,
659 min(strlen(user), sizeof(new->user) - 1));
660
661 /* given password refers to a passwd entry */
662 if( (strlen(pass) > 3) && !strncmp(pass, "$p$", 3) )
663 {
664 #ifdef HAVE_SHADOW
665 /* try to resolve shadow entry */
666 if( ((spwd = getspnam(&pass[3])) != NULL) && spwd->sp_pwdp )
667 {
668 memcpy(new->pass, spwd->sp_pwdp,
669 min(strlen(spwd->sp_pwdp), sizeof(new->pass) - 1));
670 }
671
672 else
673 #endif
674
675 /* try to resolve passwd entry */
676 if( ((pwd = getpwnam(&pass[3])) != NULL) && pwd->pw_passwd &&
677 (pwd->pw_passwd[0] != '!') && (pwd->pw_passwd[0] != 0)
678 ) {
679 memcpy(new->pass, pwd->pw_passwd,
680 min(strlen(pwd->pw_passwd), sizeof(new->pass) - 1));
681 }
682 }
683
684 /* ordinary pwd */
685 else
686 {
687 memcpy(new->pass, pass,
688 min(strlen(pass), sizeof(new->pass) - 1));
689 }
690
691 if( new->pass[0] )
692 {
693 new->next = uh_realms;
694 uh_realms = new;
695
696 return new;
697 }
698
699 free(new);
700 }
701
702 return NULL;
703 }
704
705 int uh_auth_check(
706 struct client *cl, struct http_request *req, struct path_info *pi
707 ) {
708 int i, plen, rlen, protected;
709 char buffer[UH_LIMIT_MSGHEAD];
710 char *user = NULL;
711 char *pass = NULL;
712
713 struct auth_realm *realm = NULL;
714
715 plen = strlen(pi->name);
716 protected = 0;
717
718 /* check whether at least one realm covers the requested url */
719 for( realm = uh_realms; realm; realm = realm->next )
720 {
721 rlen = strlen(realm->path);
722
723 if( (plen >= rlen) && !strncasecmp(pi->name, realm->path, rlen) )
724 {
725 req->realm = realm;
726 protected = 1;
727 break;
728 }
729 }
730
731 /* requested resource is covered by a realm */
732 if( protected )
733 {
734 /* try to get client auth info */
735 foreach_header(i, req->headers)
736 {
737 if( !strcasecmp(req->headers[i], "Authorization") &&
738 (strlen(req->headers[i+1]) > 6) &&
739 !strncasecmp(req->headers[i+1], "Basic ", 6)
740 ) {
741 memset(buffer, 0, sizeof(buffer));
742 uh_b64decode(buffer, sizeof(buffer) - 1,
743 (unsigned char *) &req->headers[i+1][6],
744 strlen(req->headers[i+1]) - 6);
745
746 if( (pass = strchr(buffer, ':')) != NULL )
747 {
748 user = buffer;
749 *pass++ = 0;
750 }
751
752 break;
753 }
754 }
755
756 /* have client auth */
757 if( user && pass )
758 {
759 /* find matching realm */
760 for( realm = uh_realms; realm; realm = realm->next )
761 {
762 rlen = strlen(realm->path);
763
764 if( (plen >= rlen) &&
765 !strncasecmp(pi->name, realm->path, rlen) &&
766 !strcmp(user, realm->user)
767 ) {
768 req->realm = realm;
769 break;
770 }
771 }
772
773 /* found a realm matching the username */
774 if( realm )
775 {
776 /* is a crypt passwd */
777 if( realm->pass[0] == '$' )
778 pass = crypt(pass, realm->pass);
779
780 /* check user pass */
781 if( !strcmp(pass, realm->pass) )
782 return 1;
783 }
784 }
785
786 /* 401 */
787 uh_http_sendf(cl, NULL,
788 "HTTP/%.1f 401 Authorization Required\r\n"
789 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
790 "Content-Type: text/plain\r\n"
791 "Content-Length: 23\r\n\r\n"
792 "Authorization Required\n",
793 req->version, cl->server->conf->realm
794 );
795
796 return 0;
797 }
798
799 return 1;
800 }
801
802
803 static struct listener *uh_listeners = NULL;
804 static struct client *uh_clients = NULL;
805
806 struct listener * uh_listener_add(int sock, struct config *conf)
807 {
808 struct listener *new = NULL;
809 socklen_t sl;
810
811 if( (new = (struct listener *)malloc(sizeof(struct listener))) != NULL )
812 {
813 memset(new, 0, sizeof(struct listener));
814
815 new->socket = sock;
816 new->conf = conf;
817
818 /* get local endpoint addr */
819 sl = sizeof(struct sockaddr_in6);
820 memset(&(new->addr), 0, sl);
821 getsockname(sock, (struct sockaddr *) &(new->addr), &sl);
822
823 new->next = uh_listeners;
824 uh_listeners = new;
825
826 return new;
827 }
828
829 return NULL;
830 }
831
832 struct listener * uh_listener_lookup(int sock)
833 {
834 struct listener *cur = NULL;
835
836 for( cur = uh_listeners; cur; cur = cur->next )
837 if( cur->socket == sock )
838 return cur;
839
840 return NULL;
841 }
842
843
844 struct client * uh_client_add(int sock, struct listener *serv)
845 {
846 struct client *new = NULL;
847 socklen_t sl;
848
849 if( (new = (struct client *)malloc(sizeof(struct client))) != NULL )
850 {
851 memset(new, 0, sizeof(struct client));
852
853 new->socket = sock;
854 new->server = serv;
855
856 /* get remote endpoint addr */
857 sl = sizeof(struct sockaddr_in6);
858 memset(&(new->peeraddr), 0, sl);
859 getpeername(sock, (struct sockaddr *) &(new->peeraddr), &sl);
860
861 /* get local endpoint addr */
862 sl = sizeof(struct sockaddr_in6);
863 memset(&(new->servaddr), 0, sl);
864 getsockname(sock, (struct sockaddr *) &(new->servaddr), &sl);
865
866 new->next = uh_clients;
867 uh_clients = new;
868 }
869
870 return new;
871 }
872
873 struct client * uh_client_lookup(int sock)
874 {
875 struct client *cur = NULL;
876
877 for( cur = uh_clients; cur; cur = cur->next )
878 if( cur->socket == sock )
879 return cur;
880
881 return NULL;
882 }
883
884 void uh_client_remove(int sock)
885 {
886 struct client *cur = NULL;
887 struct client *prv = NULL;
888
889 for( cur = uh_clients; cur; prv = cur, cur = cur->next )
890 {
891 if( cur->socket == sock )
892 {
893 if( prv )
894 prv->next = cur->next;
895 else
896 uh_clients = cur->next;
897
898 free(cur);
899 break;
900 }
901 }
902 }
903
904
905 #ifdef HAVE_CGI
906 static struct interpreter *uh_interpreters = NULL;
907
908 struct interpreter * uh_interpreter_add(const char *extn, const char *path)
909 {
910 struct interpreter *new = NULL;
911
912 if( (new = (struct interpreter *)
913 malloc(sizeof(struct interpreter))) != NULL )
914 {
915 memset(new, 0, sizeof(struct interpreter));
916
917 memcpy(new->extn, extn, min(strlen(extn), sizeof(new->extn)-1));
918 memcpy(new->path, path, min(strlen(path), sizeof(new->path)-1));
919
920 new->next = uh_interpreters;
921 uh_interpreters = new;
922
923 return new;
924 }
925
926 return NULL;
927 }
928
929 struct interpreter * uh_interpreter_lookup(const char *path)
930 {
931 struct interpreter *cur = NULL;
932 const char *e;
933
934 for( cur = uh_interpreters; cur; cur = cur->next )
935 {
936 e = &path[max(strlen(path) - strlen(cur->extn), 0)];
937
938 if( !strcmp(e, cur->extn) )
939 return cur;
940 }
941
942 return NULL;
943 }
944 #endif
This page took 0.092176 seconds and 5 git commands to generate.