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