Example #1
0
char* mupnp_net_selectaddr(struct sockaddr* remoteaddr)
{
  mUpnpNetworkInterfaceList* netIfList;
  mUpnpNetworkInterface* netIf;
  mUpnpNetworkInterface* selectNetIf;
  char* selectNetIfAddr;
  u_long laddr, lmask, raddr;
  struct addrinfo hints;
  struct addrinfo* netIfAddrInfo;
  struct addrinfo* netMaskAddrInfo;

  netIfList = mupnp_net_interfacelist_new();
  if (!netIfList)
    return mupnp_strdup("127.0.0.1");

  if (mupnp_net_gethostinterfaces(netIfList) <= 0) {
    mupnp_net_interfacelist_delete(netIfList);
    return mupnp_strdup("127.0.0.1");
  }

  raddr = ntohl(((struct sockaddr_in*)remoteaddr)->sin_addr.s_addr);

  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;

  selectNetIf = NULL;
  if (1 <= mupnp_net_gethostinterfaces(netIfList)) {
    for (netIf = mupnp_net_interfacelist_gets(netIfList); netIf; netIf = mupnp_net_interface_next(netIf)) {
      if (getaddrinfo(mupnp_net_interface_getaddress(netIf), NULL, &hints, &netIfAddrInfo) != 0)
        continue;
      if (getaddrinfo(mupnp_net_interface_getnetmask(netIf), NULL, &hints, &netMaskAddrInfo) != 0) {
        freeaddrinfo(netIfAddrInfo);
        continue;
      }
      laddr = ntohl(((struct sockaddr_in*)netIfAddrInfo->ai_addr)->sin_addr.s_addr);
      lmask = ntohl(((struct sockaddr_in*)netMaskAddrInfo->ai_addr)->sin_addr.s_addr);
      if ((laddr & lmask) == (raddr & lmask))
        selectNetIf = netIf;
      freeaddrinfo(netIfAddrInfo);
      freeaddrinfo(netMaskAddrInfo);
      if (selectNetIf)
        break;
    }
  }

  if (!selectNetIf)
    selectNetIf = mupnp_net_interfacelist_gets(netIfList);

  selectNetIfAddr = mupnp_strdup(mupnp_net_interface_getaddress(selectNetIf));

  mupnp_net_interfacelist_delete(netIfList);

  return selectNetIfAddr;
}
Example #2
0
void mupnp_upnpav_dms_filesys_content_setpubicdirectory(mUpnpMediaContent* con, char* dir)
{
  mUpnpMediaFileSystemContentData* data;

  data = mupnp_upnpav_dms_filesys_content_getdata(con);
  if (data->pubdir)
    free(data->pubdir);
  data->pubdir = mupnp_strdup(dir);
}
Example #3
0
File: log.c Project: dwlinux/mupnpc
/**
 * Add new logging target
 * @param target String identifying the logging target (Currently stdout|stderr|FILENAME)
 * @param mask Bitmask defining what messages are to be printed into this target
 */
int mupnp_log_add_target(char *target, int mask)
{
	struct fd_list *temp = NULL; 
	FILE *r_target = NULL;
	
	initialized = 1;

	/* Checking if target is standard stream or should we create
	 * new stream for file output.
	 */
	if (!strcmp(target, "stdout"))
		r_target=stdout;
	else if (!strcmp(target, "stderr"))
		r_target=stderr;
	else
	{
		/* Try to use existing fd */
		for (temp = descriptor_list; temp; temp=temp->next)
		{
			if (!strcmp(target, temp->name)) 
				r_target = temp->fd;
		}

		/* User is adding new file for output, note that file is cleared if
		 * it is not already open. */
		if ( NULL == r_target) 
			r_target=fopen(target, "w");
	}

	if (NULL == (temp = (struct fd_list*)malloc(sizeof(struct fd_list))))
	{
		return -1;
	}
	
	/* Adding new target into single linked list */
	temp->next=descriptor_list;
	temp->apply_mask = mask;
	temp->name = mupnp_strdup(target);
	temp->fd = r_target;
	descriptor_list = temp;

	return 1;
}
Example #4
0
mUpnpXmlNode *mupnp_soap_request_getbodynode(mUpnpSoapRequest *soapReq)
{
	mUpnpXmlNode *envNode;
	mUpnpXmlNode *bodyNode = NULL;
  mUpnpXmlAttribute *attr;
  char *name;
  mUpnpStringTokenizer *tok;
  char *nsPrefix;
  size_t bodyLen;
  char *body;

	mupnp_log_debug_l4("Entering...\n");

	envNode = mupnp_soap_request_getenvelopenode(soapReq);
	if (envNode == NULL)
		return NULL;
	if (mupnp_xml_node_haschildnodes(envNode) == false)
		return NULL;

        /* We cannot assume the namespace prefix for Body is 's'. 
           According to spec, it could be anything... */
        for (attr = mupnp_xml_node_getattributes(envNode); attr != NULL; 
             attr = mupnp_xml_attribute_next(attr)) {
                /* First, find the namespace declaration attribute. */
                /* Note: We must take a copy of the attr name. 
                   Tokenizer doesn't do it (by default) */
                name = mupnp_strdup( mupnp_xml_attribute_getname(attr) );
                tok = mupnp_string_tokenizer_new(name, ":");

                nsPrefix = mupnp_string_tokenizer_nexttoken(tok);
                if ( -1 != mupnp_strstr(nsPrefix, "xmlns")) {
                        /* This attribute is a namespace declaration. Check is 
                           it the one defined for SOAP. */
                        if (mupnp_strcmp(mupnp_xml_attribute_getvalue(attr), MUPNP_SOAP_XMLNS_URL) == 0) {
                                /* This namespace declaration is correct. 
                                   Use it to find the body node... */
                                if (mupnp_string_tokenizer_hasmoretoken(tok)) {
                                        /* There is a prefix */
                                        nsPrefix = mupnp_string_tokenizer_nexttoken(tok);
                                        bodyLen = mupnp_strlen(nsPrefix) + 
                                                mupnp_strlen(MUPNP_SOAP_DELIM) + 
                                                mupnp_strlen(MUPNP_SOAP_BODY) + 1; /* +1 for trailing '\0'*/
                                        body = (char*)malloc(bodyLen);

					if ( NULL == body )
					{
						mupnp_log_debug_s("Memory allocation failure!\n");
						return NULL;
					}
#if defined(HAVE_SNPRINTF)
                                        snprintf(body, bodyLen, "%s%s%s", nsPrefix, 
                                                 MUPNP_SOAP_DELIM, MUPNP_SOAP_BODY);
#else
                                        sprintf(body, "%s%s%s", nsPrefix, MUPNP_SOAP_DELIM, MUPNP_SOAP_BODY);
#endif
                                        bodyNode = mupnp_xml_node_getchildnode(envNode, body);
                                        free(body);
                                }
                                else {
                                        /* No prefix */
                                        bodyNode = mupnp_xml_node_getchildnode(envNode, MUPNP_SOAP_BODY);
                                }
                                /* Free memory before leaving the loop */
                                mupnp_string_tokenizer_delete(tok);
                                free(name);
                                break;
                        }
                }
                mupnp_string_tokenizer_delete(tok);
                free(name);
        }

	mupnp_log_debug_l4("Leaving...\n");
	
	return bodyNode;
}
Example #5
0
File: log.c Project: dwlinux/mupnpc
/**
 * Set log item separator
 * @param s String to use as a log item separator
 */
void mupnp_log_set_separator(char *s)
{
	if (separator != NULL) free(separator);

	separator = mupnp_strdup(s);
}
Example #6
0
char* mupnp_net_selectaddr(struct sockaddr* remoteaddr)
{
  struct ifaddrs *ifaddrs, *ifaddr;
  uint32_t laddr, lmask, raddr;
  char *address_candidate = NULL, *auto_ip_address_candidate = NULL;

  raddr = ntohl(((struct sockaddr_in*)remoteaddr)->sin_addr.s_addr);

  if (0 != getifaddrs(&ifaddrs)) {
    return NULL;
  }

  for (ifaddr = ifaddrs; NULL != ifaddr; ifaddr = ifaddr->ifa_next) {
    if (ifaddr->ifa_addr == NULL)
      continue;
    if (!(ifaddr->ifa_flags & IFF_UP))
      continue;
    if (ifaddr->ifa_flags & IFF_LOOPBACK)
      continue;
    if (ifaddr->ifa_flags & IFF_POINTOPOINT)
      continue;

    laddr = ntohl(((struct sockaddr_in*)ifaddr->ifa_addr)->sin_addr.s_addr);
    if (NULL != (struct sockaddr_in*)ifaddr->ifa_netmask)
      lmask = ntohl(((struct sockaddr_in*)ifaddr->ifa_netmask)->sin_addr.s_addr);
    else {
      mupnp_log_debug_s("No netmask for address %u!\n", laddr);
      continue;
    }

    /* Checking if we have an exact subnet match */
    if ((laddr & lmask) == (raddr & lmask)) {
      if (NULL != address_candidate)
        free(address_candidate);
      address_candidate = mupnp_strdup(
          inet_ntoa((struct in_addr)((struct sockaddr_in*)ifaddr->ifa_addr)->sin_addr));
      mupnp_log_debug_s("Address match! Selecting local address (%u)\n", laddr);
      break;
    }

    /* Checking if we have and auto ip address */
    if ((laddr & lmask) == MUPNP_NET_SOCKET_AUTO_IP_NET) {
      mupnp_log_debug_s("Found auto ip address. Selecting it for second address candidate (%u)\n", laddr);
      if (NULL != auto_ip_address_candidate)
        free(auto_ip_address_candidate);
      auto_ip_address_candidate = mupnp_strdup(
          inet_ntoa((struct in_addr)((struct sockaddr_in*)ifaddr->ifa_addr)->sin_addr));
    }
    /* Good. We have others than auto ips present. */
    else {
      mupnp_log_debug_s("Didn't have an exact subnet match, but non auto ip address anyway... (%u)\n", laddr);
      if (NULL != address_candidate)
        free(address_candidate);
      address_candidate = mupnp_strdup(
          inet_ntoa((struct in_addr)((struct sockaddr_in*)ifaddr->ifa_addr)->sin_addr));
    }
  }

  freeifaddrs(ifaddrs);

  if (NULL != address_candidate) {
    if (NULL != auto_ip_address_candidate)
      free(auto_ip_address_candidate);
    return address_candidate;
  }

  if (NULL != auto_ip_address_candidate) {
    if (NULL != address_candidate)
      free(address_candidate);
    return auto_ip_address_candidate;
  }

  /* Starting to feel desperate and returning local address.*/

  return mupnp_strdup("127.0.0.1");
}