Esempio n. 1
0
/* Do UPnP interface initialization */
int
upnp_ifattach(UPNP_CONTEXT *context, char *ifname, UPNP_DEVICE *device)
{
	UPNP_INTERFACE	*ifp;

	/* Allocate interface space */
	ifp = (UPNP_INTERFACE *)malloc(sizeof(*ifp));
	if (ifp == 0)
		return 0;

	memset(ifp, 0, sizeof(*ifp));

	/* Setup context */
	strncpy(ifp->ifname, ifname, sizeof(ifp->ifname));
	ifp->ifname[sizeof(ifp->ifname)-1] = '\0';
	ifp->http_sock = -1;

	if (get_if_ipaddr(ifp) != 0) {
		free(ifp);
		return 0;
	}

	/* Do prepend */
	ifp->next = context->iflist;
	context->iflist = ifp;

	/* Attache device */
	if (context->ssdp_sock == -1) {
		if (ssdp_init(context) != 0) {
			free(ifp);
			return -1;
		}
	}

	context->focus_ifp = ifp;

	/* Perform per interface protocol initialization */
	if (upnp_http_init(context) != 0) {
		upnp_syslog(LOG_ERR, "upnp_http_init::%s init error!", ifp->ifname);
		return -1;
	}

	if (ssdp_add_multi(context) == -1) {
		upnp_syslog(LOG_ERR, "ssdp_add_multi::%s error!", ifp->ifname);
		return -1;
	}

	/*
	 * Hook device table to each interface.
	 * The init function of each device
	 * intialize the event variables, and send SSDP ALIVE to each
	 * interface
	 */
	if (upnp_device_attach(context, device) == -1) {
		upnp_syslog(LOG_ERR, "upnp_device_attach::%s(%s) error!",
			device->root_device_xml, ifp->ifname);
		return -1;
	}
	return 0;
}
Esempio n. 2
0
/* Process the UPnP request message */
void
upnp_request_handler(UPNP_CONTEXT *context)
{
	int len;
	UPNP_REQUEST *request;
	UPNP_DEVICE *device = 0;
	UPNP_INTERFACE *ifp = context->focus_ifp;
	UPNP_DEVCHAIN *chain;

	/* Read message */
	len = read_request(context);
	if (len <= 0)
		return;

	request = (UPNP_REQUEST *)context->buf;
	if (get_device_from_reqcmd(request->cmd)) {
		int i;

		for (i = 0; (device = upnp_device_table[i]) != 0; i++) {
			if (strcmp(device->root_device_xml, request->url) == 0)
				break;
		}
		if (device == 0)
			return;
	}

	/* Switch the operation */
	switch (request->cmd)
	{
	case UPNP_REQ_DEV_ADD:
		upnp_syslog(LOG_INFO, "UPNP_CMD_DEV_ADD");

		upnp_device_attach(context, device);
		break;

	case UPNP_REQ_DEV_DEL:
		upnp_syslog(LOG_INFO, "UPNP_CMD_DEV_DEL");

		upnp_device_detach(context, device);
		break;

	case UPNP_REQ_GET_STATE_VAR:
		request->status = soap_get_state_var(
				context,
				request->url,
				request->var[0].statevar,
				&request->var[0].value);
		upnp_request_response(context, request);
		break;

	case UPNP_REQ_SET_EVENT_VAR:
		gena_event_alarm(context, request->url, request->num, request->var);
		break;

	case UPNP_REQ_GENA_NOTIFY:
		gena_event_alarm(context, request->url, 0, 0);
		break;

	default:
		upnp_syslog(LOG_INFO, "Device command.");
		for (chain = ifp->device_chain;
			chain;
			chain = chain->next) {
			if (chain->device == device) {
				ifp->focus_devchain = chain;
				device->request(context, request);
				break;
			}
		}
		break;
	}

	return;
}
Esempio n. 3
0
/* UPnP module initialization */
int
upnp_init(UPNP_CONTEXT *context)
{
	UPNP_INTERFACE	*ifp = 0;
	UPNP_DEVICE		*device;
	int	i;

	char ifname_list[256];
	
	
	char *name, *p, *next;

	sprintf(xml_InternetGatewayDevice_real,xml_InternetGatewayDevice,nvram_safe_get("DD_BOARD"),nvram_safe_get("router_name"),nvram_safe_get("DD_BOARD"));
	/* Clear flag */
	upnp_flag = 0;

	/* Clean up */
	memset(context, 0, sizeof(*context));

	upnp_osl_read_config(&context->config);
	oslib_ifname_list(ifname_list);

	/* Do context common initialization */
	context->adv_seconds = time(0);
	context->gena_last_check = time(0);
	context->ssdp_sock = -1;

	for (i = 0; (device = upnp_device_table[i]) != 0; i++) {
		(*device->common_init)(context);

		/*
		 * Give a unique uuid to this router,
		 * create same uuid when restart
		 */
		upnp_device_renew_uuid(context, device);
	}

	/* Create the mutlicast receive socket for all interfaces */
	if (ssdp_init(context) != 0)
		goto error_out;

	for (name = ifname_list, p = name;
	     name && name[0];
	     name = next, p = 0) {
		strtok_r(p, " ", &next);

		ifp = upnp_ifinit(context, name);
		if (ifp == 0)
			continue;

		context->focus_ifp = ifp;

		/* Perform per interface protocol initialization */
		if (upnp_http_init(context) != 0) {
			upnp_syslog(LOG_ERR, "upnp_http_init::%s init error!", ifp->ifname);
			goto error_out;
		}

		if (ssdp_add_multi(context) == -1) {
			upnp_syslog(LOG_ERR, "ssdp_init::%s error!", ifp->ifname);
			goto error_out;
		}

		if (upnp_request_init(context) != 0) {
			upnp_syslog(LOG_ERR, "upnp_request_init::init error!");
			goto error_out;
		}

		/*
		 * Hook device table to each interface.
		 * The init function of each device
		 * intialize the event variables, and send SSDP ALIVE to each
		 * interface
		 */
		for (i = 0; (device = upnp_device_table[i]) != 0; i++) {
			if (device->attach_mode == DEVICE_ATTACH_ALWAYS)
				upnp_device_attach(context, device);
		}
	}

	/* No interface found, return directly */
	if (context->iflist == 0) {
		upnp_syslog(LOG_ERR, "No UPnP interface specified, bye!");
		goto error_out;
	}

	upnp_syslog(LOG_INFO, "UPnP daemon is ready to run");
	return 0;

error_out:
	upnp_flag = UPNP_FLAG_SHUTDOWN;
	return -1;
}