+/*--- iax2_devicestate: Part of the device state notification system ---*/
+static int iax2_devicestate(void *data)
+{
+ char *dest = (char *) data;
+ struct iax2_peer *p;
+ int found = 0;
+ char *ext, *host;
+ char tmp[256] = "";
+ int res = AST_DEVICE_INVALID;
+
+ strncpy(tmp, dest, sizeof(tmp) - 1);
+ host = strchr(tmp, '@');
+ if (host) {
+ *host = '\0';
+ host++;
+ ext = tmp;
+ } else {
+ host = tmp;
+ ext = NULL;
+ }
+
+ if (option_debug > 1)
+ ast_log(LOG_DEBUG, "Checking device state for device %s\n", dest);
+
+ p = find_peer(host, 1);
+ if (p) {
+ found++;
+ res = AST_DEVICE_UNAVAILABLE;
+ if (option_debug > 2)
+ ast_log(LOG_DEBUG, "Found peer. Now checking device state for peer %s\n", host);
+
+
+ if ((p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) &&
+ (!p->maxms || ((p->lastms > -1) && (p->lastms <= p->maxms)))) {
+ /* Peer is registred, or have default IP address
+ and a valid registration */
+ if (p->lastms == 0 || p->lastms <= p->lastms)
+ res = AST_DEVICE_NOT_INUSE;
+ else
+ res = AST_DEVICE_UNKNOWN; /* Not reachable */
+ }
+ } else {
+ if (option_debug > 2)
+ ast_log(LOG_DEBUG, "Devicestate: Can't find peer %s.\n", host);
+ }
+
+ if (p && ast_test_flag(p, IAX_TEMPONLY))
+ destroy_peer(p);
+ return res;
+}
+