miniupnpc-libevent: handle error cases

This commit is contained in:
Thomas Bernard 2014-11-25 23:51:59 +01:00
parent b642e74124
commit 8325b715d3
3 changed files with 41 additions and 11 deletions

View File

@ -194,7 +194,9 @@ static void upnpc_receive_and_parse_ssdp(evutil_socket_t s, short events, upnpc_
/* we already have found a device */
} else if(!devices_to_search[p->device_index]) {
debug_printf("*** NO MORE DEVICES TO SEARCH ***\n");
/* no device found : set error */
event_del(p->ev_ssdp_recv);
/* no device found : report error */
p->ready_cb(UPNPC_ERR_NO_DEVICE_FOUND, p->cb_data);
} else {
/* send another SSDP M-SEARCH packet */
if(event_add(p->ev_ssdp_writable, NULL)) {
@ -223,8 +225,10 @@ static void upnpc_receive_and_parse_ssdp(evutil_socket_t s, short events, upnpc_
return;
}
upnpc_get_desc(p, p->root_desc_location);
event_del(p->ev_ssdp_recv); /* stop receiving SSDP responses */
} else {
/* or do nothing ? */
debug_printf("no location\n");
}
}
}
@ -337,9 +341,15 @@ static void upnpc_desc_received(struct evhttp_request * req, void * pvoid)
len = evbuffer_get_length(input_buffer);
data = evbuffer_pullup(input_buffer, len);
debug_printf("upnpc_desc_received %d (%d bytes)\n", evhttp_request_get_response_code(req), (int)len);
debug_printf("%.*s\n", (int)len, (char *)data);
if(data == NULL)
if(evhttp_request_get_response_code(req) != HTTP_OK) {
p->ready_cb(evhttp_request_get_response_code(req), p->cb_data);
return;
}
if(data == NULL) {
p->ready_cb(UPNPC_ERR_ROOT_DESC_ERROR, p->cb_data);
return;
}
debug_printf("%.*s\n", (int)len, (char *)data);
memset(&igd, 0, sizeof(struct IGDdatas));
memset(&parser, 0, sizeof(struct xmlparser));
@ -559,6 +569,7 @@ int upnpc_init(upnpc_t * p, struct event_base * base, const char * multicastif,
if(setsockopt(p->ssdp_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
#endif /* _WIN32 */
/* non fatal error ! */
debug_printf("setsockopt(%d, SOL_SOCKET, SO_REUSEADDR, ...) FAILED\n", p->ssdp_socket);
}
if(evutil_make_socket_nonblocking(p->ssdp_socket) < 0) {
debug_printf("evutil_make_socket_nonblocking FAILED\n");

View File

@ -1,4 +1,4 @@
/* $Id: miniupnpc-libevent.h,v 1.8 2014/11/18 09:10:16 nanard Exp $ */
/* $Id: miniupnpc-libevent.h,v 1.9 2014/11/25 22:49:19 nanard Exp $ */
/* miniupnpc-libevent
* Copyright (c) 2008-2014, Thomas BERNARD <miniupnp@free.fr>
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
@ -30,7 +30,9 @@ extern "C" {
#define UPNPC_ERR_INVALID_ARGS (-1)
#define UPNPC_ERR_SOCKET_FAILED (-2)
#define UPNPC_ERR_BIND_FAILED (-3)
#define UPNPC_ERR_UNKNOWN_STATE (-4)
#define UPNPC_ERR_NO_DEVICE_FOUND (-100)
#define UPNPC_ERR_ROOT_DESC_ERROR (-101)
typedef void(* upnpc_callback_fn)(int, void *);

View File

@ -1,4 +1,4 @@
/* $Id: upnpc-libevent.c,v 1.8 2014/11/18 09:10:16 nanard Exp $ */
/* $Id: upnpc-libevent.c,v 1.9 2014/11/25 22:49:19 nanard Exp $ */
/* miniupnpc-libevent
* Copyright (c) 2008-2014, Thomas BERNARD <miniupnp@free.fr>
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
@ -40,9 +40,26 @@ static void sighandler(int signal)
static void ready(int code, void * data)
{
upnpc_t * p = (upnpc_t *)data;
printf("READY ! %d %p\n", code, data);
/* 1st request */
upnpc_get_status_info(p);
if(code == 200) {
printf("READY ! %d\n", code);
/* 1st request */
upnpc_get_status_info(p);
} else {
printf("DISCOVER ERROR : %d\n", code);
switch(code) {
case UPNPC_ERR_NO_DEVICE_FOUND:
printf("UPNPC_ERR_NO_DEVICE_FOUND\n");
break;
case UPNPC_ERR_ROOT_DESC_ERROR:
printf("UPNPC_ERR_ROOT_DESC_ERROR\n");
break;
case 404:
printf("Root desc not found (404)\n");
break;
default:
printf("unknown error\n");
}
}
}
static enum {
@ -87,8 +104,8 @@ static void soap(int code, void * data)
case EDeletePortMapping:
printf("OK!\n");
state = EFinished;
default:
event_base_loopbreak(base);
/*default:
event_base_loopbreak(base);*/
}
} else {
printf("SOAP error :\n");