Lines Matching +full:0 +full:d

76     if(strcmp(protocol, "udp") == 0)  in add_port()
78 else if(strcmp(protocol, "tcp") == 0) in add_port()
82 for(i = 0; i < num_ports; i++){ in add_port()
129 port = htons(strtol(str, &end, 0)); in add_port_string()
175 if(strcmp(p, "+") == 0) { in parse_ports()
183 *q++ = 0; in parse_ports()
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
238 for (i = 0; i < n; ++i) in reinit_descrs()
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()
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
319 int num = 0; in init_sockets()
330 d = malloc(addresses.len * num_ports * sizeof(*d)); in init_sockets()
331 if (d == NULL) in init_sockets()
333 (unsigned long)num_ports * sizeof(*d)); in init_sockets()
335 for (i = 0; i < num_ports; i++){ in init_sockets()
336 for (j = 0; j < addresses.len; ++j) { 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()
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()
384 if(krb5_sockaddr2address(context, addr, &a) == 0) { in addr_to_string()
385 if(krb5_print_address(&a, str, len, &len) == 0) { in addr_to_string()
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()
410 l[0] = (reply->length >> 24) & 0xff; in send_reply()
411 l[1] = (reply->length >> 16) & 0xff; in send_reply()
412 l[2] = (reply->length >> 8) & 0xff; in send_reply()
413 l[3] = reply->length & 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()
456 kdc_log(context, config, 0, 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
475 kdc_log(context, config, 0, "Failed to allocate %lu bytes", (unsigned long)max_request_udp); in handle_udp()
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()
536 *q = '\0'; in de_http()
537 return 0; 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
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'.
581 * Return != 0 if fails
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()
595 kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).", in grow_descr()
596 (unsigned long)d->size + grow); in grow_descr()
597 clear_descr(d); in grow_descr()
600 tmp = realloc (d->buf, d->size + grow); in grow_descr()
602 kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.", in grow_descr()
603 (unsigned long)d->size + grow); in grow_descr()
604 clear_descr(d); in grow_descr()
607 d->size += grow; in grow_descr()
608 d->buf = tmp; in grow_descr()
610 return 0; 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()
628 kdc_log (context, config, 0, "krb5_storage_from_mem failed"); 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()
638 return 0; 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()
661 *p = 0; in handle_http_tcp()
666 kdc_log(context, config, 0, in handle_http_tcp()
667 "Missing HTTP operand (GET) request from %s", d->addr_string); in handle_http_tcp()
673 kdc_log(context, config, 0, in handle_http_tcp()
674 "Missing HTTP GET data in request from %s", d->addr_string); in handle_http_tcp()
680 kdc_log(context, config, 0, "Failed to allocate %lu bytes", in handle_http_tcp()
686 if(de_http(t) != 0) { in handle_http_tcp()
687 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string); in handle_http_tcp()
694 kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string); in handle_http_tcp()
699 if(len <= 0){ 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()
715 kdc_log(context, config, 0, "HTTP write failed: %s: %s", in handle_http_tcp()
716 d->addr_string, strerror(rk_SOCK_ERRNO)); in handle_http_tcp()
719 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { in handle_http_tcp()
720 kdc_log(context, config, 0, "HTTP write failed: %s: %s", in handle_http_tcp()
721 d->addr_string, strerror(rk_SOCK_ERRNO)); in handle_http_tcp()
734 if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) { in handle_http_tcp()
736 kdc_log(context, config, 0, "HTTP write failed: %s: %s", in handle_http_tcp()
737 d->addr_string, strerror(rk_SOCK_ERRNO)); in handle_http_tcp()
740 if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) { in handle_http_tcp()
742 kdc_log(context, config, 0, "HTTP write failed: %s: %s", in handle_http_tcp()
743 d->addr_string, strerror(rk_SOCK_ERRNO)); 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()
756 * Handle incoming data to the TCP socket in `d[index]'
762 struct descr *d, int idx, int min_free) in handle_tcp() argument
766 int ret = 0; in handle_tcp()
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()
779 } else if (n == 0) { 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()
797 "\r\n\r\n", 4) == 0) { 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()
803 if (ret < 0) 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()
813 kdc_log (context, config, 0, "TCP extension not supported"); in handle_tcp()
824 if (ret == 0) { in handle_tcp()
825 send_reply(context, config, TRUE, d + idx, &reply); in handle_tcp()
829 clear_descr(d + idx); in handle_tcp()
832 if (ret < 0) 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()
849 if(ndescr <= 0) in loop()
851 kdc_log(context, config, 0, "KDC started"); in loop()
852 while(exit_flag == 0){ in loop()
856 int max_fd = 0; in loop()
860 for(i = 0; i < ndescr; i++) { 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()
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()
878 FD_SET(d[i].s, &fds); in loop()
879 } else if(min_free < 0 || i < (size_t)min_free) 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()
899 tmout.tv_usec = 0; in loop()
900 switch(select(max_fd + 1, &fds, 0, 0, &tmout)){ in loop()
901 case 0: in loop()
908 for(i = 0; i < ndescr; i++) 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()
917 if (0); in loop()
920 kdc_log(context, config, 0, "CPU time limit exceeded"); in loop()
923 kdc_log(context, config, 0, "Terminated"); in loop()
925 kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag); in loop()
926 free (d); in loop()