>From 6593f9d59038b317a5f3820a7a13dc6ac0866acf Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 23 Sep 2016 10:22:30 -0700 Subject: [PATCH] [WIP] s3: nmbd - use strict tevent calls, no more source/lib/events.c Signed-off-by: Jeremy Allison --- source3/nmbd/nmbd_packets.c | 161 +++++++++++++++++++++++++++----------------- 1 file changed, 101 insertions(+), 60 deletions(-) diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c index c9a2dc7..0f6538e 100644 --- a/source3/nmbd/nmbd_packets.c +++ b/source3/nmbd/nmbd_packets.c @@ -1683,16 +1683,16 @@ on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_ struct socket_attributes { enum packet_type type; bool broadcast; + int fd; + bool triggered; }; -static bool create_listen_pollfds(struct pollfd **pfds, - struct socket_attributes **pattrs, +static bool create_listen_array(struct socket_attributes **pattrs, int *pnum_sockets) { struct subnet_record *subrec = NULL; int count = 0; int num = 0; - struct pollfd *fds; struct socket_attributes *attrs; /* The ClientNMB and ClientDGRAM sockets */ @@ -1716,29 +1716,21 @@ static bool create_listen_pollfds(struct pollfd **pfds, } } - fds = talloc_zero_array(NULL, struct pollfd, count); - if (fds == NULL) { - DEBUG(1, ("create_listen_pollfds: malloc fail for fds. " - "size %d\n", count)); - return true; - } - - attrs = talloc_array(NULL, struct socket_attributes, count); + attrs = talloc_zero_array(NULL, struct socket_attributes, count); if (attrs == NULL) { DEBUG(1, ("create_listen_pollfds: malloc fail for attrs. " "size %d\n", count)); - TALLOC_FREE(fds); return true; } num = 0; - fds[num].fd = ClientNMB; + attrs[num].fd = ClientNMB; attrs[num].type = NMB_PACKET; attrs[num].broadcast = false; num += 1; - fds[num].fd = ClientDGRAM; + attrs[num].fd = ClientDGRAM; attrs[num].type = DGRAM_PACKET; attrs[num].broadcast = false; num += 1; @@ -1746,37 +1738,34 @@ static bool create_listen_pollfds(struct pollfd **pfds, for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) { if (subrec->nmb_sock != -1) { - fds[num].fd = subrec->nmb_sock; + attrs[num].fd = subrec->nmb_sock; attrs[num].type = NMB_PACKET; attrs[num].broadcast = false; num += 1; } if (subrec->nmb_bcast != -1) { - fds[num].fd = subrec->nmb_bcast; + attrs[num].fd = subrec->nmb_bcast; attrs[num].type = NMB_PACKET; attrs[num].broadcast = true; num += 1; } if (subrec->dgram_sock != -1) { - fds[num].fd = subrec->dgram_sock; + attrs[num].fd = subrec->dgram_sock; attrs[num].type = DGRAM_PACKET; attrs[num].broadcast = false; num += 1; } if (subrec->dgram_bcast != -1) { - fds[num].fd = subrec->dgram_bcast; + attrs[num].fd = subrec->dgram_bcast; attrs[num].type = DGRAM_PACKET; attrs[num].broadcast = true; num += 1; } } - TALLOC_FREE(*pfds); - *pfds = fds; - TALLOC_FREE(*pattrs); *pattrs = attrs; @@ -1864,66 +1853,101 @@ static void free_processed_packet_list(struct processed_packet **pp_processed_pa } /**************************************************************************** + Timeout callback - just notice we timed out. +***************************************************************************/ + +static void nmbd_timeout_handler(struct tevent_context *ev, + struct tevent_timer *te, + struct timeval current_time, + void *private_data) +{ + bool *got_timeout = (bool *)private_data; + *got_timeout = true; +} + +/**************************************************************************** + fd callback - remember the fd that triggered. +***************************************************************************/ + +static void nmbd_fd_handler(struct tevent_context *ev, + struct tevent_fd *fde, + uint16_t flags, + void *private_data) +{ + struct socket_attributes *attr = + (struct socket_attributes *)private_data; + attr->triggered = true; +} + +/**************************************************************************** Listens for NMB or DGRAM packets, and queues them. return True if the socket is dead ***************************************************************************/ bool listen_for_packets(struct messaging_context *msg, bool run_election) { - static struct pollfd *fds = NULL; static struct socket_attributes *attrs = NULL; static int listen_number = 0; int num_sockets; int i; + int loop_rtn; - int pollrtn; - int timeout; #ifndef SYNC_DNS int dns_fd; int dns_pollidx = -1; #endif struct processed_packet *processed_packet_list = NULL; + struct timeval tv; + struct tevent_timer *te = NULL; + bool got_timeout = false; + TALLOC_CTX *frame = talloc_stackframe(); - if ((fds == NULL) || rescan_listen_set) { - if (create_listen_pollfds(&fds, &attrs, &listen_number)) { + if ((attrs == NULL) || rescan_listen_set) { + if (create_listen_array(&attrs, &listen_number)) { DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n")); + TALLOC_FREE(frame); return True; } rescan_listen_set = False; } - /* - * "fds" can be enlarged by event_add_to_poll_args - * below. Shrink it again to what was given to us by - * create_listen_pollfds. - */ - - fds = talloc_realloc(NULL, fds, struct pollfd, listen_number); - if (fds == NULL) { - return true; - } num_sockets = listen_number; #ifndef SYNC_DNS dns_fd = asyncdns_fd(); if (dns_fd != -1) { - fds = talloc_realloc(NULL, fds, struct pollfd, num_sockets+1); - if (fds == NULL) { + attrs = talloc_realloc(NULL, + attrs, + struct socket_attributes, + num_sockets + 1); + if (attrs == NULL) { + TALLOC_FREE(frame); return true; } dns_pollidx = num_sockets; - fds[num_sockets].fd = dns_fd; + attrs[dns_pollidx].fd = dns_fd; + /* + * dummy values, we only need + * fd and triggered. + */ + attrs[dns_pollidx].type = NMB_PACKET; + attrs[dns_pollidx].broadcast = false; num_sockets += 1; } #endif for (i=0; iip))) { DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n", packet_name, inet_ntoa(packet->ip), packet->port)); @@ -2030,16 +2070,17 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election) if (attrs[i].broadcast) { /* this is a broadcast socket */ - packet->send_fd = fds[i-1].fd; + packet->send_fd = attrs[i-1].fd; } else { /* this is already a unicast socket */ - packet->send_fd = fds[i].fd; + packet->send_fd = attrs[i].fd; } queue_packet(packet); } free_processed_packet_list(&processed_packet_list); + TALLOC_FREE(frame); return False; } -- 2.7.4