Root/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
30static char *uh_index_files[] = {
31    "index.html",
32    "index.htm",
33    "default.html",
34    "default.htm"
35};
36
37
38const 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
50const 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
57int sa_port(void *sa)
58{
59    return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
60}
61
62int 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. */
78char *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() */
107int 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
127int 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
151int 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
165int 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
214int 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
242int 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
265int 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
284int 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
299int 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
332int 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
360int 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
409static 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
483struct 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
640static struct auth_realm *uh_realms = NULL;
641
642struct auth_realm * uh_auth_add(char *path, char *user, char *pass)
643{
644    struct auth_realm *new = NULL;
645    struct passwd *pwd;
646    struct spwd *spwd;
647
648    if((new = (struct auth_realm *)malloc(sizeof(struct auth_realm))) != NULL)
649    {
650        memset(new, 0, sizeof(struct auth_realm));
651
652        memcpy(new->path, path,
653            min(strlen(path), sizeof(new->path) - 1));
654
655        memcpy(new->user, user,
656            min(strlen(user), sizeof(new->user) - 1));
657
658        /* given password refers to a passwd entry */
659        if( (strlen(pass) > 3) && !strncmp(pass, "$p$", 3) )
660        {
661            /* try to resolve shadow entry */
662            if( ((spwd = getspnam(&pass[3])) != NULL) && spwd->sp_pwdp )
663            {
664                memcpy(new->pass, spwd->sp_pwdp,
665                    min(strlen(spwd->sp_pwdp), sizeof(new->pass) - 1));
666            }
667
668            /* try to resolve passwd entry */
669            else if( ((pwd = getpwnam(&pass[3])) != NULL) && pwd->pw_passwd &&
670                (pwd->pw_passwd[0] != '!') && (pwd->pw_passwd[0] != 0)
671            ) {
672                memcpy(new->pass, pwd->pw_passwd,
673                    min(strlen(pwd->pw_passwd), sizeof(new->pass) - 1));
674            }
675        }
676
677        /* ordinary pwd */
678        else
679        {
680            memcpy(new->pass, pass,
681                min(strlen(pass), sizeof(new->pass) - 1));
682        }
683
684        if( new->pass[0] )
685        {
686            new->next = uh_realms;
687            uh_realms = new;
688
689            return new;
690        }
691
692        free(new);
693    }
694
695    return NULL;
696}
697
698int uh_auth_check(
699    struct client *cl, struct http_request *req, struct path_info *pi
700) {
701    int i, plen, rlen, protected;
702    char buffer[UH_LIMIT_MSGHEAD];
703    char *user = NULL;
704    char *pass = NULL;
705
706    struct auth_realm *realm = NULL;
707
708    plen = strlen(pi->name);
709    protected = 0;
710
711    /* check whether at least one realm covers the requested url */
712    for( realm = uh_realms; realm; realm = realm->next )
713    {
714        rlen = strlen(realm->path);
715
716        if( (plen >= rlen) && !strncasecmp(pi->name, realm->path, rlen) )
717        {
718            req->realm = realm;
719            protected = 1;
720            break;
721        }
722    }
723
724    /* requested resource is covered by a realm */
725    if( protected )
726    {
727        /* try to get client auth info */
728        foreach_header(i, req->headers)
729        {
730            if( !strcasecmp(req->headers[i], "Authorization") &&
731                (strlen(req->headers[i+1]) > 6) &&
732                !strncasecmp(req->headers[i+1], "Basic ", 6)
733            ) {
734                memset(buffer, 0, sizeof(buffer));
735                uh_b64decode(buffer, sizeof(buffer) - 1,
736                    (unsigned char *) &req->headers[i+1][6],
737                    strlen(req->headers[i+1]) - 6);
738
739                if( (pass = strchr(buffer, ':')) != NULL )
740                {
741                    user = buffer;
742                    *pass++ = 0;
743                }
744
745                break;
746            }
747        }
748
749        /* have client auth */
750        if( user && pass )
751        {
752            /* find matching realm */
753            for( realm = uh_realms; realm; realm = realm->next )
754            {
755                rlen = strlen(realm->path);
756
757                if( (plen >= rlen) &&
758                    !strncasecmp(pi->name, realm->path, rlen) &&
759                    !strcmp(user, realm->user)
760                ) {
761                    req->realm = realm;
762                    break;
763                }
764            }
765
766            /* found a realm matching the username */
767            if( realm )
768            {
769                /* is a crypt passwd */
770                if( realm->pass[0] == '$' )
771                    pass = crypt(pass, realm->pass);
772
773                /* check user pass */
774                if( !strcmp(pass, realm->pass) )
775                    return 1;
776            }
777        }
778
779        /* 401 */
780        uh_http_sendf(cl, NULL,
781            "HTTP/%.1f 401 Authorization Required\r\n"
782            "WWW-Authenticate: Basic realm=\"%s\"\r\n"
783            "Content-Type: text/plain\r\n"
784            "Content-Length: 23\r\n\r\n"
785            "Authorization Required\n",
786                req->version, cl->server->conf->realm
787        );
788
789        return 0;
790    }
791
792    return 1;
793}
794
795
796static struct listener *uh_listeners = NULL;
797static struct client *uh_clients = NULL;
798
799struct listener * uh_listener_add(int sock, struct config *conf)
800{
801    struct listener *new = NULL;
802    socklen_t sl;
803
804    if( (new = (struct listener *)malloc(sizeof(struct listener))) != NULL )
805    {
806        memset(new, 0, sizeof(struct listener));
807
808        new->socket = sock;
809        new->conf = conf;
810
811        /* get local endpoint addr */
812        sl = sizeof(struct sockaddr_in6);
813        memset(&(new->addr), 0, sl);
814        getsockname(sock, (struct sockaddr *) &(new->addr), &sl);
815
816        new->next = uh_listeners;
817        uh_listeners = new;
818
819        return new;
820    }
821
822    return NULL;
823}
824
825struct listener * uh_listener_lookup(int sock)
826{
827    struct listener *cur = NULL;
828
829    for( cur = uh_listeners; cur; cur = cur->next )
830        if( cur->socket == sock )
831            return cur;
832
833    return NULL;
834}
835
836
837struct client * uh_client_add(int sock, struct listener *serv)
838{
839    struct client *new = NULL;
840    socklen_t sl;
841
842    if( (new = (struct client *)malloc(sizeof(struct client))) != NULL )
843    {
844        memset(new, 0, sizeof(struct client));
845
846        new->socket = sock;
847        new->server = serv;
848
849        /* get remote endpoint addr */
850        sl = sizeof(struct sockaddr_in6);
851        memset(&(new->peeraddr), 0, sl);
852        getpeername(sock, (struct sockaddr *) &(new->peeraddr), &sl);
853
854        /* get local endpoint addr */
855        sl = sizeof(struct sockaddr_in6);
856        memset(&(new->servaddr), 0, sl);
857        getsockname(sock, (struct sockaddr *) &(new->servaddr), &sl);
858
859        new->next = uh_clients;
860        uh_clients = new;
861    }
862
863    return new;
864}
865
866struct client * uh_client_lookup(int sock)
867{
868    struct client *cur = NULL;
869
870    for( cur = uh_clients; cur; cur = cur->next )
871        if( cur->socket == sock )
872            return cur;
873
874    return NULL;
875}
876
877void uh_client_remove(int sock)
878{
879    struct client *cur = NULL;
880    struct client *prv = NULL;
881
882    for( cur = uh_clients; cur; prv = cur, cur = cur->next )
883    {
884        if( cur->socket == sock )
885        {
886            if( prv )
887                prv->next = cur->next;
888            else
889                uh_clients = cur->next;
890
891            free(cur);
892            break;
893        }
894    }
895}
896
897
898#ifdef HAVE_CGI
899static struct interpreter *uh_interpreters = NULL;
900
901struct interpreter * uh_interpreter_add(const char *extn, const char *path)
902{
903    struct interpreter *new = NULL;
904
905    if( (new = (struct interpreter *)
906            malloc(sizeof(struct interpreter))) != NULL )
907    {
908        memset(new, 0, sizeof(struct interpreter));
909
910        memcpy(new->extn, extn, min(strlen(extn), sizeof(new->extn)-1));
911        memcpy(new->path, path, min(strlen(path), sizeof(new->path)-1));
912
913        new->next = uh_interpreters;
914        uh_interpreters = new;
915
916        return new;
917    }
918
919    return NULL;
920}
921
922struct interpreter * uh_interpreter_lookup(const char *path)
923{
924    struct interpreter *cur = NULL;
925    const char *e;
926
927    for( cur = uh_interpreters; cur; cur = cur->next )
928    {
929        e = &path[max(strlen(path) - strlen(cur->extn), 0)];
930
931        if( !strcmp(e, cur->extn) )
932            return cur;
933    }
934
935    return NULL;
936}
937#endif
938

Archive Download this file



interactive