コード例 #1
0
static gboolean initialize_device(struct upnp_device_descriptor *device_def,
				  struct upnp_device *result_device,
				  const char *ip_address,
				  unsigned short port)
{
	int rc;
	char *buf;

	rc = UpnpInit(ip_address, port);
	if (UPNP_E_SUCCESS != rc) {
		Log_error("upnp", "UpnpInit(ip=%s, port=%d) Error: %s (%d)",
			  ip_address, port, UpnpGetErrorMessage(rc), rc);
		return FALSE;
	}
	Log_info("upnp", "Registered IP=%s port=%d\n",
		 UpnpGetServerIpAddress(), UpnpGetServerPort());

	rc = UpnpEnableWebserver(TRUE);
	if (UPNP_E_SUCCESS != rc) {
		Log_error("upnp", "UpnpEnableWebServer() Error: %s (%d)",
			  UpnpGetErrorMessage(rc), rc);
		return FALSE;
	}

	if (!webserver_register_callbacks())
	  return FALSE;

	rc = UpnpAddVirtualDir("/upnp");
	if (UPNP_E_SUCCESS != rc) {
		Log_error("upnp", "UpnpAddVirtualDir() Error: %s (%d)",
			  UpnpGetErrorMessage(rc), rc);
		return FALSE;
	}

       	buf = upnp_create_device_desc(device_def);
	rc = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC,
				     buf, strlen(buf), 1,
				     &event_handler, result_device,
				     &(result_device->device_handle));
	free(buf);

	if (UPNP_E_SUCCESS != rc) {
		Log_error("upnp", "UpnpRegisterRootDevice2() Error: %s (%d)",
			  UpnpGetErrorMessage(rc), rc);
		return FALSE;
	}

	rc = UpnpSendAdvertisement(result_device->device_handle, 100);
	if (UPNP_E_SUCCESS != rc) {
		Log_error("unpp", "Error sending advertisements: %s (%d)",
			  UpnpGetErrorMessage(rc), rc);
		return FALSE;
	}

	return TRUE;
}
コード例 #2
0
ファイル: main.c プロジェクト: darcyg/u2pnpd
int main(int argc, char *argv[])
{
	UpnpDevice_Handle upnp_handle;
	UpnpDevice_Handle upnp_device;
	char *device_desc = NULL;
	int rv = EXIT_FAILURE;
	struct sigaction sa;

	shutdown_flag = 0;

	/* init options, preset some defaults and parse command line arguments */
	options = options_init();
	options_parse_cli(argc, argv, options);

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = signal_handler;
	sa.sa_flags = 0;
	sigfillset(&sa.sa_mask);

	if (sigaction(SIGINT, &sa, NULL) < 0) {
		perror("sigaction");
		return EXIT_FAILURE;
	}
	if (sigaction(SIGTERM, &sa, NULL) < 0) {
		perror("sigaction");
		return EXIT_FAILURE;
	}

#ifdef UPNP_ENABLE_IPV6
	rv = UpnpInit2(options->interface, 0);
#else
	if (options->interface)
		fprintf(stderr, "Warning: ignoring interface argument, not supported by libupnp.\n");
	rv = UpnpInit(NULL, 0);
#endif
	if (rv != UPNP_E_SUCCESS) {
		fprintf(stderr, "UpnpInit failed: %d\n", rv);
		return EXIT_FAILURE;
	}

	rv = UpnpEnableWebserver(1);
	if (rv != UPNP_E_SUCCESS) {
		fprintf(stderr, "Could not enabled UPnP's internal HTTP server.\n");
		goto upnp_finish;
	}

	device_desc = generate_device_desc(options);
	if (!device_desc) {
		fprintf(stderr, "Could not generated the UPnP device description.\n");
		goto upnp_finish;
	}

	rv = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC, device_desc, strlen(device_desc), 1,
	                             upnp_callback, &upnp_device, &upnp_device);
	if (rv != UPNP_E_SUCCESS) {
		fprintf(stderr, "Failed to register UPnP root device.\n");
		goto free_out;
	}

	rv = UpnpSendAdvertisement(upnp_device, UPNP_ALIVE_INTERVAL);
	if (rv != UPNP_E_SUCCESS) {
		fprintf(stderr, "Failed to announce UPnP device.\n");
		goto upnp_unregister;
	}

	while (!shutdown_flag)
		pause();

	rv = EXIT_SUCCESS;

upnp_unregister:
	UpnpUnRegisterRootDevice(upnp_device);
free_out:
	if (device_desc)
		free(device_desc);
upnp_finish:
	UpnpFinish();
err_out:
	options_free(options);
	return rv;
}
コード例 #3
0
ファイル: IUpnp.cpp プロジェクト: searKing/easydlna
ERROR_TYPE IUpnp::EnableWebserver(IN int enable)
{
    return (ERROR_TYPE)UpnpEnableWebserver(enable);
}
コード例 #4
0
struct device_private *upnp_device_init(struct device *device_def,
					const char *ip_address)
{
	int rc;
#ifdef HAVE_LIBUPNP
	short int port = 0;
#endif
	struct service *srv;
	struct icon *icon_entry;
	char *buf;
	struct device_private *priv = NULL;
	int i;

	assert(device_def != NULL);

	if (device_def->init_function)
	{
		rc = device_def->init_function();
		if (rc != 0) {
			goto out;
		}
	}

	priv = malloc(sizeof(*priv));
	priv->upnp_device = device_def;
#ifdef HAVE_LIBUPNP
	ithread_mutex_init(&(priv->device_mutex), NULL);
#endif

	//upnp_device = device_def;

	/* register icons in web server */
        for (i=0; (icon_entry = device_def->icons[i]); i++) {
		webserver_register_file(icon_entry->url, "image/png");
        }

	/* generate and register service schemas in web server */
        for (i=0; (srv = device_def->services[i]); i++) {
       		buf = upnp_get_scpd(srv);
		assert(buf != NULL);
                printf("registering '%s'\n", srv->scpd_url);
		webserver_register_buf(srv->scpd_url,buf,"text/xml");
	}

#ifdef HAVE_LIBUPNP
	rc = UpnpInit(ip_address, port);
	if (UPNP_E_SUCCESS != rc) {
		printf("UpnpInit() Error: %d\n", rc);
		goto upnp_err_out;
	}
	rc = UpnpEnableWebserver(TRUE);
	if (UPNP_E_SUCCESS != rc) {
		printf("UpnpEnableWebServer() Error: %d\n", rc);
		goto upnp_err_out;
	}
	rc = UpnpSetVirtualDirCallbacks(&virtual_dir_callbacks);
	if (UPNP_E_SUCCESS != rc) {
		printf("UpnpSetVirtualDirCallbacks() Error: %d\n", rc);
		goto upnp_err_out;
	}
	rc = UpnpAddVirtualDir("/upnp");
	if (UPNP_E_SUCCESS != rc) {
		printf("UpnpAddVirtualDir() Error: %d\n", rc);
		goto upnp_err_out;
	}


       	buf = upnp_get_device_desc(device_def);

	rc = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC,
				     buf, strlen(buf), 1,
				     &event_handler, priv,
				     &(priv->device_handle));
	if (UPNP_E_SUCCESS != rc) {
		printf("UpnpRegisterRootDevice2() Error: %d\n", rc);
		goto upnp_err_out;
	}

	rc = UpnpSendAdvertisement(priv->device_handle, 100);
	if (UPNP_E_SUCCESS != rc) {
		fprintf(stderr, "Error sending advertisements: %d\n", rc);
		goto upnp_err_out;
	}
#endif

	goto out;

#ifdef HAVE_LIBUPNP
upnp_err_out:
	UpnpFinish();
#endif
out:
	return priv;
}
コード例 #5
0
ファイル: upnp_device.c プロジェクト: dulton/dmr
int upnp_device_init(struct device *device_def, char *ip_address)
{
	int rc;
	int result = -1;
	short int port = 0;
	struct service *srv;
	struct icon *icon_entry;
	char *buf;
	int i;

	if (device_def->init_function)
	{
		rc = device_def->init_function();
		if (rc != 0) {
			goto out;
		}
	}

	upnp_device = device_def;

	/* register icons in web server */
        for (i=0; (icon_entry = upnp_device->icons[i]); i++) {
		webserver_register_file(icon_entry->url, "image/png");
        }

	/* generate and register service schemas in web server */
        for (i=0; (srv = upnp_device->services[i]); i++) {
       		buf = upnp_get_scpd(srv);
            debug_printf(MSG_INFO, "registering '%s'\n", srv->scpd_url);
		webserver_register_buf(srv->scpd_url,buf,"text/xml");
	}


	rc = UpnpInit(ip_address, port);
	if (UPNP_E_SUCCESS != rc) {
		debug_printf(MSG_ERROR, "UpnpInit() Error: %d\n", rc);
		goto upnp_err_out;
	}
	rc = UpnpEnableWebserver(TRUE);
	if (UPNP_E_SUCCESS != rc) {
		debug_printf(MSG_ERROR, "UpnpEnableWebServer() Error: %d\n", rc);
		goto upnp_err_out;
	}
	rc = UpnpSetVirtualDirCallbacks(&virtual_dir_callbacks);
	if (UPNP_E_SUCCESS != rc) {
		debug_printf(MSG_ERROR, "UpnpSetVirtualDirCallbacks() Error: %d\n", rc);
		goto upnp_err_out;
	}
	rc = UpnpAddVirtualDir("/upnp");
	if (UPNP_E_SUCCESS != rc) {
		debug_printf(MSG_ERROR, "UpnpAddVirtualDir() Error: %d\n", rc);
		goto upnp_err_out;
	}

       	buf = upnp_get_device_desc(device_def);

	rc = UpnpRegisterRootDevice2(UPNPREG_BUF_DESC,
				     buf, strlen(buf), 1,
				     &event_handler, &device_def,
				     &device_handle);
	if (UPNP_E_SUCCESS != rc) {
		debug_printf(MSG_ERROR, "UpnpRegisterRootDevice2() Error: %d\n", rc);
		goto upnp_err_out;
	}
	rc = UpnpSendAdvertisement(device_handle, 100);
	if (UPNP_E_SUCCESS != rc) {
		debug_printf(MSG_ERROR, "Error sending advertisements: %d\n", rc);
		goto upnp_err_out;
	}
	result = 0;
	goto out;

upnp_err_out:
	UpnpFinish();
out:
	return result;
}