/* 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; }
/* 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; }