Beispiel #1
0
  void connection::begin_timeout(unsigned int in_seconds)
  {
    cancel_timeout();

    tt_.expires_from_now(boost::posix_time::seconds(in_seconds));
    tt_.async_wait( boost::bind( &connection::do_timeout, shared_from_this(), _1 ));
  }
Beispiel #2
0
static void request_begin(void)
{
    if (active_requests++ == 0) {
        /* First request, cancel timer. */
        cancel_timeout();
    }
}
Beispiel #3
0
void handle_deinit(void) {
  hide_mainwin();
  if (g_device_id_list != NULL) {
    free(g_device_id_list);
    g_device_id_list = NULL;
  }
  cancel_timeout();
  cancel_status_check();
  if (inactivity_timer != NULL) app_timer_cancel(inactivity_timer);
}
Beispiel #4
0
// Callback for when user switches between devices
void device_switched() {
  reset_inactivity_timer();
  cancel_status_check();
  cancel_timeout();
  if (status_fetch_delay_timer != NULL) {
    app_timer_cancel(status_fetch_delay_timer);
    status_fetch_delay_timer = NULL;
  }
  // Fetch device details after a brief delay to avoid busy comms error
  if (details_fetch_delay_timer == NULL)
    details_fetch_delay_timer = app_timer_register(500, device_details_fetch_delayed, NULL);
  else
    app_timer_reschedule(details_fetch_delay_timer, 500);
}
Beispiel #5
0
void
lease_pinged(struct iaddr from, u_int8_t *packet, int length)
{
	struct lease *lp;

	/*
	 * Don't try to look up a pinged lease if we aren't trying to
	 * ping one - otherwise somebody could easily make us churn by
	 * just forging repeated ICMP EchoReply packets for us to look
	 * up.
	 */
	if (!outstanding_pings)
		return;

	lp = find_lease_by_ip_addr(from);

	if (!lp) {
		note("unexpected ICMP Echo Reply from %s", piaddr(from));
		return;
	}

	if (!lp->state && !lp->releasing) {
		warning("ICMP Echo Reply for %s arrived late or is spurious.",
		    piaddr(from));
		return;
	}

	/* At this point it looks like we pinged a lease and got a
	 * response, which shouldn't have happened.
	 * if it did it's either one of two two cases:
	 * 1 - we pinged this lease before offering it and
	 *     something answered, so we abandon it.
	 * 2 - we pinged this lease before releasing it
	 *     and something answered, so we don't release it.
	 */
	if (lp->releasing) {
		warning("IP address %s answers a ping after sending a release",
		    piaddr(lp->ip_addr));
		warning("Possible release spoof - Not releasing address %s",
		    piaddr(lp->ip_addr));
		lp->releasing = 0;
	} else {
		free_lease_state(lp->state, "lease_pinged");
		lp->state = NULL;
		abandon_lease(lp, "pinged before offer");
	}
	cancel_timeout(lease_ping_timeout, lp);
	--outstanding_pings;
}
Beispiel #6
0
// Callback to show any error received from the phone JS or MyQ servers
void comms_error(char *error_message) {
  cancel_status_check();
  cancel_timeout();
  show_msg(error_message, false, 0);
  if (g_device_count == 0) {
    if (inactivity_timer != NULL) {
      app_timer_cancel(inactivity_timer);
      inactivity_timer = NULL;
    }
    // If there are no devices, hide main win so the app closes when the error message is closed
    hide_mainwin();
  } else {
    reset_inactivity_timer();
  }
}
Beispiel #7
0
static void gcin_display_closed (GdkDisplay *display,
                         gboolean    is_error,
                         GtkIMContextGCIN *context_xim)
{
#if DBG
  puts("gcin_display_closed");
#endif
#if NEW_GTK_IM
  cancel_timeout(context_xim);
#endif
  if (!context_xim->gcin_ch)
    return;

  gcin_im_client_close(context_xim->gcin_ch);
  context_xim->gcin_ch = NULL;
}
Beispiel #8
0
static void CancelTimeout(int *t) {

unsigned long ps = 0;
int v;

#ifdef EMULATE_LYNXOS_ON_LINUX
   ps = 0;
#endif

   disable(ps);
   {
      if ((v = *t)) {
	 *t = 0;
	 cancel_timeout(v);
      }
   }
   restore(ps);
}
Beispiel #9
0
// Callbck for when the device status has been fetched
void device_status_fetched(int device_id, DeviceStatus status, char *status_changed) {
  APP_LOG(APP_LOG_LEVEL_DEBUG, "Status fetched - ID: %d, Status: %d, Selected ID: %d", 
          device_id, status, g_device_id_list[g_device_selected]);
  reset_inactivity_timer();
  
  if (g_device_id_list[g_device_selected] == device_id) {
    if (s_device_status_target != DSNone) {
      // If expecting the device status to change (e.g. Garage door opening/closing)
      cancel_status_check();
      
      if (((status == DSVGDOOpen) ? DSOnOpen : status) != s_device_status_target) {
        APP_LOG(APP_LOG_LEVEL_DEBUG, "Status still not reached target, checking again in 2 seconds...");
        // Still not reached target status, so check again after 2 seconds
        status_change_check_timer = app_timer_register(2000, status_change_check, NULL);
      } else {
        APP_LOG(APP_LOG_LEVEL_DEBUG, "Status has reached target");
        // Status changed, so stop checking
        cancel_timeout();
        s_device_status_target = DSNone;
        s_device_status = status;
        strncpy(s_status_changed, status_changed, sizeof(s_status_changed));
        s_status_changed[sizeof(s_status_changed)-1] = '\0';
        show_device_status(status, status_changed);
        light_enable_interaction();
        vibes_short_pulse();
      }
      
    } else {
      // Not expecting status to change, so just update the display
      cancel_status_check();
      
      if (status != s_device_status) {
        s_device_status = status;
        light_enable_interaction();
      }
      strncpy(s_status_changed, status_changed, sizeof(s_status_changed));
      s_status_changed[sizeof(s_status_changed)-1] = '\0';
      show_device_status(status, status_changed);
    }
  }
}
Beispiel #10
0
  void connection::on_body_read( const boost::system::error_code& error, std::size_t bytes_transferred)
  {
    if (error) {
      return stop();
    }

    cancel_timeout();

    bufsz_ += bytes_transferred;

    inbound_.append_body(bodybuf_.data(), bodybuf_.data() + bytes_transferred);
    // handler_.handle_chunk(string_t(bodybuf_.data()));

    // if we read enough bytes as specified in the Content-Length header, process body
    if (bufsz_ >= inbound_.content_length)
    {
      return on_request_read();
    }

    return read_body();
  }
Beispiel #11
0
  void connection::stop() {
    cancel_timeout();

    boost::system::error_code ignored_ec;

    // initiate graceful connection closure & stop all asynchronous ops
    try {
      socket_.cancel();
      socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
      socket_.close(ignored_ec);
    } catch (std::exception& e) {
      error() << "bad socket; couldn't close cleanly";
    }

    longevity_timer_.stop();

    debug() << "closed (elapsed: "
      << longevity_timer_.elapsed << "ms)";

    if (close_handler_) {
      close_handler_(shared_from_this());
    }
  }
Beispiel #12
0
static void cancel_lpe(int s) {
    if (sockets[s].lpevent>=0) {
	cancel_timeout(sockets[s].lpevent);
	sockets[s].lpevent=-1;
    }
}
Beispiel #13
0
  void
  connection::on_header_read(const boost::system::error_code& e, std::size_t bytes_transferred)
  {
    if (e) {
      return stop();
    }

    cancel_timeout();

    boost::tribool result;
    std::string trailer;

    // debug() << "received " << bytes_transferred << " bytes";
    result = parser_.parse_header(headbuf_.data(), headbuf_.data() + bytes_transferred, trailer);

    // parser failed processing the header for some reason
    if (!result)
    {
      debug() << "invalid request header, rejecting";
      return reject(reply::bad_request);
    }

    // header is incomplete, need more feed
    else if (result == boost::indeterminate)
    {
      return read_header();
    }

    // header was successfully parsed, now let's validate it
    if (!inbound_.validate())
    {
      reply::status_type t;

      // track the error
      switch(inbound_.status)
      {
        case request::status_t::invalid_method:
          error() << "request has a non-supported method '" << inbound_ << "', aborting";
          t = reply::not_implemented;
        break;
        case request::status_t::invalid_format:
          error() << "request has a non-supported format '" << inbound_.format_str << "', aborting";
          t = reply::not_implemented;
        break;
        case request::status_t::missing_length:
          info()
          << "request does not specify a Content-Length, can not attempt indeterminate body reading, aborting";
          t = reply::bad_request;
        break;
        case request::status_t::invalid_length:
        error() << "request has 0 Content-Length, aborting";
          t = reply::bad_request;
        break;
        default:
          error() << "unknown request status: " << (int)inbound_.status << ", investigate!";
          t = reply::internal_server_error;
      }

      return reject(t);
    }

    // set up the UUID logging
    // __set_uuid(inbound_.uuid);

    handler_.init();

    // process any trailing bytes that were not part of the HTTP header
    size_t trailing_bytes = trailer.size();
    if (trailing_bytes > 0)
    {
      std::istringstream buf(trailer);
      buf.read(bodybuf_.data(), trailing_bytes);

      debug()
        << "there are " << trailing_bytes << "bytes of trailing data"
        << " and req clength is " << inbound_.content_length << "b";

      inbound_.append_body(bodybuf_.data(), bodybuf_.data() + trailer.size());

      // did the trailer contain the whole request body?
      if (trailer.size() == inbound_.content_length)
        return on_request_read();

      bufsz_ += trailing_bytes;
    }

    reading_timer_.start();

    return read_body();
  }
Beispiel #14
0
/*
 * Loop waiting for packets, timeouts or routing messages.
 */
void
dispatch(void)
{
	int count, to_msec;
	struct pollfd fds[3];
	time_t cur_time, howlong;
	void (*func)(void);

	while (quit == 0) {
		/*
		 * Call expired timeout, and then if there's still
		 * a timeout registered, time out the select call then.
		 */
another:
		if (!ifi) {
			warning("No interfaces available");
			quit = INTERNALSIG;
			continue;
		}

		if (ifi->rdomain != get_rdomain(ifi->name)) {
			warning("Interface %s:"
			    " rdomain changed out from under us",
			    ifi->name);
			quit = INTERNALSIG;
			continue;
		}

		if (timeout.func) {
			time(&cur_time);
			if (timeout.when <= cur_time) {
				func = timeout.func;
				cancel_timeout();
				(*(func))();
				goto another;
			}
			/*
			 * Figure timeout in milliseconds, and check for
			 * potential overflow, so we can cram into an
			 * int for poll, while not polling with a
			 * negative timeout and blocking indefinitely.
			 */
			howlong = timeout.when - cur_time;
			if (howlong > INT_MAX / 1000)
				howlong = INT_MAX / 1000;
			to_msec = howlong * 1000;
		} else
			to_msec = -1;

		/* Set up the descriptors to be polled. */
		if (!ifi || ifi->rfdesc == -1) {
			warning("No live interface to poll on");
			quit = INTERNALSIG;
			continue;
		}

		fds[0].fd = ifi->rfdesc;
		fds[1].fd = routefd; /* Could be -1, which will be ignored. */
		fds[2].fd = unpriv_ibuf->fd;
		fds[0].events = fds[1].events = fds[2].events = POLLIN;

		if (unpriv_ibuf->w.queued)
			fds[2].events |= POLLOUT;

		/* Wait for a packet or a timeout or unpriv_ibuf->fd. XXX */
		count = poll(fds, 3, to_msec);

		/* Not likely to be transitory. */
		if (count == -1) {
			if (errno == EAGAIN || errno == EINTR) {
				continue;
			} else {
				warning("poll: %s", strerror(errno));
				quit = INTERNALSIG;
				continue;
			}
		}

		if ((fds[0].revents & (POLLIN | POLLHUP))) {
			if (ifi && ifi->linkstat && ifi->rfdesc != -1)
				got_one();
		}
		if ((fds[1].revents & (POLLIN | POLLHUP))) {
			if (ifi)
				routehandler();
		}
		if (fds[2].revents & POLLOUT) {
			if (msgbuf_write(&unpriv_ibuf->w) <= 0 &&
			    errno != EAGAIN) {
				warning("pipe write error to [priv]");
				quit = INTERNALSIG;
				continue;
			}
		}
		if ((fds[2].revents & (POLLIN | POLLHUP))) {
			/* Pipe to [priv] closed. Assume it emitted error. */
			quit = INTERNALSIG;
			continue;
		}
	}

	if (quit == SIGHUP) {
		/* Tell [priv] process that HUP has occurred. */
		sendhup(client->active);
		warning("%s; restarting", strsignal(quit));
		exit (0);
	} else if (quit != INTERNALSIG) {
		warning("%s; exiting", strsignal(quit));
		exit(1);
	}
}