Lines Matching +full:1 +full:d
10 * 1. Redistributions of source code must retain the above copyright
37 int enable_http = -1;
88 ports = realloc(ports, (num_ports + 1) * sizeof(*ports)); in add_port()
90 krb5_err (context, 1, errno, "realloc"); in add_port()
222 init_descr(struct descr *d) in init_descr() argument
224 memset(d, 0, sizeof(*d)); in init_descr()
225 d->sa = (struct sockaddr *)&d->__ss; in init_descr()
226 d->s = rk_INVALID_SOCKET; in init_descr()
230 * re-initialize all `n' ->sa in `d'.
234 reinit_descrs (struct descr *d, int n) in reinit_descrs() argument
239 d[i].sa = (struct sockaddr *)&d[i].__ss; in reinit_descrs()
243 * Create the socket (family, type, port) in `d'
249 struct descr *d, krb5_address *a, int family, int type, int port) in init_socket() argument
256 init_descr (d); in init_socket()
261 rk_closesocket(d->s); in init_socket()
262 d->s = rk_INVALID_SOCKET; in init_socket()
269 d->s = socket(family, type, 0); in init_socket()
270 if(rk_IS_BAD_SOCKET(d->s)){ in init_socket()
271 krb5_warn(context, errno, "socket(%d, %d, 0)", family, type); in init_socket()
272 d->s = rk_INVALID_SOCKET; in init_socket()
277 int one = 1; in init_socket()
278 setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one)); in init_socket()
281 d->type = type; in init_socket()
282 d->port = port; in init_socket()
284 if(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){ in init_socket()
289 krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port)); in init_socket()
290 rk_closesocket(d->s); in init_socket()
291 d->s = rk_INVALID_SOCKET; in init_socket()
294 if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){ in init_socket()
299 krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port)); in init_socket()
300 rk_closesocket(d->s); in init_socket()
301 d->s = rk_INVALID_SOCKET; in init_socket()
318 struct descr *d; in init_sockets() local
327 krb5_err (context, 1, ret, "krb5_get_all_server_addrs"); in init_sockets()
330 d = malloc(addresses.len * num_ports * sizeof(*d)); in init_sockets()
331 if (d == NULL) in init_sockets()
332 krb5_errx(context, 1, "malloc(%lu) failed", in init_sockets()
333 (unsigned long)num_ports * sizeof(*d)); in init_sockets()
337 init_socket(context, config, &d[num], &addresses.val[j], in init_sockets()
339 if(d[num].s != rk_INVALID_SOCKET){ in init_sockets()
356 d = realloc(d, num * sizeof(*d)); in init_sockets()
357 if (d == NULL && num != 0) in init_sockets()
358 krb5_errx(context, 1, "realloc(%lu) failed", in init_sockets()
359 (unsigned long)num * sizeof(*d)); in init_sockets()
360 reinit_descrs (d, num); in init_sockets()
361 *desc = d; in init_sockets()
370 descr_type(struct descr *d) in descr_type() argument
372 if (d->type == SOCK_DGRAM) in descr_type()
374 else if (d->type == SOCK_STREAM) in descr_type()
391 snprintf(str, len, "<family=%d>", addr->sa_family); in addr_to_string()
402 struct descr *d, in send_reply() argument
407 d->addr_string); in send_reply()
411 l[1] = (reply->length >> 16) & 0xff; in send_reply()
414 if(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) { in send_reply()
416 0, "sendto(%s): %s", d->addr_string, in send_reply()
421 if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) { in send_reply()
422 kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string, in send_reply()
429 * Handle the request in `buf, len' to socket `d'
436 struct descr *d) in do_request() argument
440 int datagram_reply = (d->type == SOCK_DGRAM); in do_request()
447 d->addr_string, d->sa, in do_request()
450 krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa); in do_request()
452 send_reply(context, config, prependlength, d, &reply); in do_request()
458 (unsigned long)len, d->addr_string); in do_request()
462 * Handle incoming data to the UDP socket in `d'
468 struct descr *d) in handle_udp() argument
479 d->sock_len = sizeof(d->__ss); in handle_udp()
480 n = recvfrom(d->s, buf, max_request_udp, 0, d->sa, &d->sock_len); in handle_udp()
484 addr_to_string (context, d->sa, d->sock_len, in handle_udp()
485 d->addr_string, sizeof(d->addr_string)); in handle_udp()
490 d->addr_string); in handle_udp()
500 send_reply(context, config, FALSE, d, &data); in handle_udp()
503 do_request(context, config, buf, n, FALSE, d); in handle_udp()
510 clear_descr(struct descr *d) in clear_descr() argument
512 if(d->buf) in clear_descr()
513 memset(d->buf, 0, d->size); in clear_descr()
514 d->len = 0; in clear_descr()
515 if(d->s != rk_INVALID_SOCKET) in clear_descr()
516 rk_closesocket(d->s); in clear_descr()
517 d->s = rk_INVALID_SOCKET; in clear_descr()
527 if(*p == '%' && isxdigit(p[1]) && isxdigit(p[2])) { in de_http()
529 if(sscanf((char *)p + 1, "%2x", &x) != 1) in de_http()
530 return -1; in de_http()
543 * accept a new TCP connection on `d[parent]' and store it in `d[child]'
549 struct descr *d, int parent, int child) in add_new_tcp() argument
553 if (child == -1) in add_new_tcp()
556 d[child].sock_len = sizeof(d[child].__ss); in add_new_tcp()
557 s = accept(d[parent].s, d[child].sa, &d[child].sock_len); in add_new_tcp()
571 d[child].s = s; in add_new_tcp()
572 d[child].timeout = time(NULL) + TCP_TIMEOUT; in add_new_tcp()
573 d[child].type = SOCK_STREAM; in add_new_tcp()
575 d[child].sa, d[child].sock_len, in add_new_tcp()
576 d[child].addr_string, sizeof(d[child].addr_string)); in add_new_tcp()
580 * Grow `d' to handle at least `n'.
587 struct descr *d, size_t n) in grow_descr() argument
589 if (d->size - d->len < n) { in grow_descr()
593 grow = max(1024, d->len + n); in grow_descr()
594 if (d->size + grow > max_request_tcp) { in grow_descr()
596 (unsigned long)d->size + grow); in grow_descr()
597 clear_descr(d); in grow_descr()
598 return -1; in grow_descr()
600 tmp = realloc (d->buf, d->size + grow); in grow_descr()
603 (unsigned long)d->size + grow); in grow_descr()
604 clear_descr(d); in grow_descr()
605 return -1; in grow_descr()
607 d->size += grow; in grow_descr()
608 d->buf = tmp; in grow_descr()
614 * Try to handle the TCP data at `d->buf, d->len'.
615 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
621 struct descr *d) in handle_vanilla_tcp() argument
626 sp = krb5_storage_from_mem(d->buf, d->len); in handle_vanilla_tcp()
629 return -1; in handle_vanilla_tcp()
633 if(d->len - 4 >= len) { in handle_vanilla_tcp()
634 memmove(d->buf, d->buf + 4, d->len - 4); in handle_vanilla_tcp()
635 d->len -= 4; in handle_vanilla_tcp()
636 return 1; in handle_vanilla_tcp()
642 * Try to handle the TCP/HTTP data at `d->buf, d->len'.
643 * Return -1 if failed, 0 if succesful, and 1 if data is complete.
649 struct descr *d) in handle_http_tcp() argument
656 s = (char *)d->buf; in handle_http_tcp()
667 "Missing HTTP operand (GET) request from %s", d->addr_string); in handle_http_tcp()
668 return -1; in handle_http_tcp()
674 "Missing HTTP GET data in request from %s", d->addr_string); in handle_http_tcp()
675 return -1; in handle_http_tcp()
682 return -1; in handle_http_tcp()
687 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string); in handle_http_tcp()
690 return -1; in handle_http_tcp()
694 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string); in handle_http_tcp()
696 return -1; in handle_http_tcp()
711 kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string); in handle_http_tcp()
714 if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) { in handle_http_tcp()
716 d->addr_string, strerror(rk_SOCK_ERRNO)); in handle_http_tcp()
717 return -1; in handle_http_tcp()
719 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { in handle_http_tcp()
721 d->addr_string, strerror(rk_SOCK_ERRNO)); in handle_http_tcp()
722 return -1; in handle_http_tcp()
724 return -1; in handle_http_tcp()
734 if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) { in handle_http_tcp()
737 d->addr_string, strerror(rk_SOCK_ERRNO)); in handle_http_tcp()
738 return -1; in handle_http_tcp()
740 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { in handle_http_tcp()
743 d->addr_string, strerror(rk_SOCK_ERRNO)); in handle_http_tcp()
744 return -1; in handle_http_tcp()
747 if ((size_t)len > d->len) in handle_http_tcp()
748 len = d->len; in handle_http_tcp()
749 memcpy(d->buf, data, len); in handle_http_tcp()
750 d->len = len; in handle_http_tcp()
752 return 1; in handle_http_tcp()
756 * Handle incoming data to the TCP socket in `d[index]'
762 struct descr *d, int idx, int min_free) in handle_tcp() argument
768 if (d[idx].timeout == 0) { in handle_tcp()
769 add_new_tcp (context, config, d, idx, min_free); in handle_tcp()
773 n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL); in handle_tcp()
775 krb5_warn(context, rk_SOCK_ERRNO, "recvfrom failed from %s to %s/%d", in handle_tcp()
776 d[idx].addr_string, descr_type(d + idx), in handle_tcp()
777 ntohs(d[idx].port)); in handle_tcp()
781 "bytes from %s to %s/%d", (unsigned long)d[idx].len, in handle_tcp()
782 d[idx].addr_string, descr_type(d + idx), in handle_tcp()
783 ntohs(d[idx].port)); in handle_tcp()
784 clear_descr (d + idx); in handle_tcp()
787 if (grow_descr (context, config, &d[idx], n)) in handle_tcp()
789 memcpy(d[idx].buf + d[idx].len, buf, n); in handle_tcp()
790 d[idx].len += n; in handle_tcp()
791 if(d[idx].len > 4 && d[idx].buf[0] == 0) { in handle_tcp()
792 ret = handle_vanilla_tcp (context, config, &d[idx]); in handle_tcp()
794 d[idx].len >= 4 && in handle_tcp()
795 strncmp((char *)d[idx].buf, "GET ", 4) == 0 && in handle_tcp()
796 strncmp((char *)d[idx].buf + d[idx].len - 4, in handle_tcp()
800 d[idx].buf[d[idx].len - 4] = '\0'; in handle_tcp()
802 ret = handle_http_tcp (context, config, &d[idx]); in handle_tcp()
804 clear_descr (d + idx); in handle_tcp()
805 } else if (d[idx].len > 4) { in handle_tcp()
807 0, "TCP data of strange type from %s to %s/%d", in handle_tcp()
808 d[idx].addr_string, descr_type(d + idx), in handle_tcp()
809 ntohs(d[idx].port)); in handle_tcp()
810 if (d[idx].buf[0] & 0x80) { in handle_tcp()
825 send_reply(context, config, TRUE, d + idx, &reply); in handle_tcp()
829 clear_descr(d + idx); in handle_tcp()
834 else if (ret == 1) { in handle_tcp()
836 d[idx].buf, d[idx].len, TRUE, &d[idx]); in handle_tcp()
837 clear_descr(d + idx); in handle_tcp()
845 struct descr *d; in loop() local
848 ndescr = init_sockets(context, config, &d); in loop()
850 krb5_errx(context, 1, "No sockets!"); in loop()
855 int min_free = -1; in loop()
861 if(!rk_IS_BAD_SOCKET(d[i].s)){ in loop()
862 if(d[i].type == SOCK_STREAM && in loop()
863 d[i].timeout && d[i].timeout < time(NULL)) { in loop()
864 kdc_log(context, config, 1, in loop()
866 d[i].addr_string, (unsigned long)d[i].len); in loop()
867 clear_descr(&d[i]); in loop()
871 if(max_fd < d[i].s) in loop()
872 max_fd = d[i].s; in loop()
875 krb5_errx(context, 1, "fd too large"); in loop()
878 FD_SET(d[i].s, &fds); in loop()
882 if(min_free == -1){ in loop()
884 tmp = realloc(d, (ndescr + 4) * sizeof(*d)); in loop()
888 d = tmp; in loop()
889 reinit_descrs (d, ndescr); in loop()
890 memset(d + ndescr, 0, 4 * sizeof(*d)); in loop()
892 init_descr (&d[i]); in loop()
900 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){ in loop()
903 case -1: in loop()
909 if(!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) { in loop()
910 if(d[i].type == SOCK_DGRAM) in loop()
911 handle_udp(context, config, &d[i]); in loop()
912 else if(d[i].type == SOCK_STREAM) in loop()
913 handle_tcp(context, config, d, i, min_free); in loop()
925 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag); in loop()
926 free (d); in loop()