From 780f20b75ca1b1f1444dacce6733ad43d47ca98a Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 25 Feb 2016 13:54:23 +0100 Subject: [PATCH] add upnpc_event_unsubscribe() --- miniupnpc-libevent/miniupnpc-libevent.c | 41 +++++++++++++++++++++++++ miniupnpc-libevent/miniupnpc-libevent.h | 1 + miniupnpc-libevent/upnpc-libevent.c | 4 +++ 3 files changed, 46 insertions(+) diff --git a/miniupnpc-libevent/miniupnpc-libevent.c b/miniupnpc-libevent/miniupnpc-libevent.c index 47a22b8..5744d39 100644 --- a/miniupnpc-libevent/miniupnpc-libevent.c +++ b/miniupnpc-libevent/miniupnpc-libevent.c @@ -450,6 +450,17 @@ static void upnpc_subscribe_response(struct evhttp_request * req, void * pvoid) if(d->parent->subscribe_cb) d->parent->subscribe_cb(code, d->parent, d, d->parent->cb_data); } + +static void upnpc_unsubscribe_response(struct evhttp_request * req, void * pvoid) +{ + upnpc_device_t * d = (upnpc_device_t *)pvoid; + + debug_printf("%s\n", __func__); + if(d->parent->http_server) { + evhttp_free(d->parent->http_server); + d->parent->http_server = NULL; + } +} #endif /* ENABLE_UPNP_EVENTS */ static void upnpc_soap_response(struct evhttp_request * req, void * pvoid) @@ -987,6 +998,36 @@ int upnpc_event_subscribe(upnpc_device_t * p) p->state |= UPNPC_DEVICE_SOAP_REQ; return 0; } + +int upnpc_event_unsubscribe(upnpc_device_t * p) +{ + char hostname[MAXHOSTNAMELEN+1]; + char hostname_port[MAXHOSTNAMELEN+1+6]; + unsigned short port; + char * path; + unsigned int scope_id; + struct evhttp_request * req; + struct evkeyvalq * headers; + + if(!parseURL(p->event_conn_url, hostname, &port, &path, &scope_id)) { + return -1; + } + if(port != 80) + snprintf(hostname_port, sizeof(hostname_port), "%s:%hu", hostname, port); + else + strncpy(hostname_port, hostname, sizeof(hostname_port)); + if(p->soap_conn == NULL) { + p->soap_conn = evhttp_connection_base_new(p->parent->base, NULL, hostname, port); + } + evhttp_connection_set_extended_method_cmp(p->soap_conn, ext_methods_func); + req = evhttp_request_new(upnpc_unsubscribe_response, p); + headers = evhttp_request_get_output_headers(req); + evhttp_add_header(headers, "Host", hostname_port); + evhttp_add_header(headers, "SID", p->event_conn_sid); + evhttp_make_request(p->soap_conn, req, EVHTTP_REQ_SUBSCRIBE, path); + p->state |= UPNPC_DEVICE_SOAP_REQ; + return 0; +} #endif /* ENABLE_UPNP_EVENTS */ int upnpc_get_external_ip_address(upnpc_device_t * p) diff --git a/miniupnpc-libevent/miniupnpc-libevent.h b/miniupnpc-libevent/miniupnpc-libevent.h index 9e62603..e46363e 100644 --- a/miniupnpc-libevent/miniupnpc-libevent.h +++ b/miniupnpc-libevent/miniupnpc-libevent.h @@ -108,6 +108,7 @@ int upnpc_finalize(upnpc_t * p); #ifdef ENABLE_UPNP_EVENTS int upnpc_event_subscribe(upnpc_device_t * p); +int upnpc_event_unsubscribe(upnpc_device_t * p); #endif /* ENABLE_UPNP_EVENTS */ int upnpc_get_external_ip_address(upnpc_device_t * p); diff --git a/miniupnpc-libevent/upnpc-libevent.c b/miniupnpc-libevent/upnpc-libevent.c index 8705382..3e089ad 100644 --- a/miniupnpc-libevent/upnpc-libevent.c +++ b/miniupnpc-libevent/upnpc-libevent.c @@ -135,6 +135,10 @@ static void event_callback(upnpc_t * p, upnpc_device_t * d, void * data, { (void)p; (void)d; (void)data; printf("PROPERTY VALUE CHANGE (service=%s): %s=%s\n", service_id, property_name, property_value); + if(state == EFinished) { + printf("****** EFinished ******\n"); + upnpc_event_unsubscribe(d); + } } /* subscribe callback */