s3: nmbd: Add a talloc_stackframe().
[sfrench/samba-autobuild/.git] / source3 / nmbd / nmbd_packets.c
index 26486797de702c2bbe647059206dd6425e79e1a9..c0c4925d5b0ff6858d57995f7753e96714e353fe 100644 (file)
@@ -1871,6 +1871,32 @@ 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 = 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 = private_data;
+       attr->triggered = true;
+}
+
 /****************************************************************************
   Listens for NMB or DGRAM packets, and queues them.
   return True if the socket is dead
@@ -1891,10 +1917,12 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
        int dns_pollidx = -1;
 #endif
        struct processed_packet *processed_packet_list = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
 
        if ((fds == NULL) || rescan_listen_set) {
                if (create_listen_pollfds(&fds, &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;
@@ -1908,6 +1936,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
 
        fds = talloc_realloc(NULL, fds, struct pollfd, listen_number);
        if (fds == NULL) {
+               TALLOC_FREE(frame);
                return true;
        }
        num_sockets = listen_number;
@@ -1917,6 +1946,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
        if (dns_fd != -1) {
                fds = talloc_realloc(NULL, fds, struct pollfd, num_sockets+1);
                if (fds == NULL) {
+                       TALLOC_FREE(frame);
                        return true;
                }
                attrs = talloc_realloc(NULL,
@@ -1925,6 +1955,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
                                        num_sockets + 1);
                if (attrs == NULL) {
                        TALLOC_FREE(fds);
+                       TALLOC_FREE(frame);
                        return true;
                }
                dns_pollidx = num_sockets;
@@ -1946,6 +1977,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
 
        /* Process a signal and timer events now... */
        if (run_events_poll(nmbd_event_context(), 0, NULL, 0)) {
+               TALLOC_FREE(frame);
                return False;
        }
 
@@ -1965,10 +1997,12 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
        pollrtn = poll(fds, num_sockets, timeout);
 
        if (run_events_poll(nmbd_event_context(), pollrtn, fds, num_sockets)) {
+               TALLOC_FREE(frame);
                return False;
        }
 
        if (pollrtn == -1) {
+               TALLOC_FREE(frame);
                return False;
        }
 
@@ -2004,7 +2038,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
                        client_port = DGRAM_PORT;
                }
 
-               packet = read_packet(fds[i].fd, packet_type);
+               packet = read_packet(attrs[i].fd, packet_type);
                if (!packet) {
                        continue;
                }
@@ -2014,7 +2048,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
                 * only is set then check it came from one of our local nets.
                 */
                if (lp_bind_interfaces_only() &&
-                   (fds[i].fd == client_fd) &&
+                   (attrs[i].fd == client_fd) &&
                    (!is_local_net_v4(packet->ip))) {
                        DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n",
                                packet_name, inet_ntoa(packet->ip), packet->port));
@@ -2053,16 +2087,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;
 }