static BOOL cg_socket_getavailablelocaladdress(T_IPV4EP *localAddr) { ER ret; char *ifAddr; int localPort; cg_log_debug_l4("Entering...\n"); CgNetworkInterfaceList *netIfList; CgNetworkInterface *netIf; int netIfCnt; netIfList = cg_net_interfacelist_new(); netIfCnt = cg_net_gethostinterfaces(netIfList); if (netIfCnt <= 0) { cg_net_interfacelist_delete(netIfList); return FALSE; } netIf = cg_net_interfacelist_gets(netIfList); ifAddr = cg_net_interface_getaddress(netIf); localPort = cg_socket_getavailableport(); localAddr->ipaddr = ascii_to_ipaddr(ifAddr); localAddr->portno = htons(localPort); cg_net_interfacelist_delete(netIfList); return FALSE; }
BOOL cg_bittorrent_client_createpeerid(CgBittorrentClient *cbc, CgByte *peerId) { CgNetworkInterfaceList *netIfList; CgNetworkInterface *netIf; char hostName[CG_HTTP_SEVERNAME_MAXLEN]; CgByte hostNameHash[CG_SHA1_HASH_SIZE]; CgSHA1Context sha; int err; memset(peerId, 0, CG_BITTORRENT_CLIENT_PEERID_SIZE); /* Client ID/Vertion */ peerId[0] = '-'; memcpy(peerId + 1, CG_BITTORRENT_CLIENT_ID, 2); memcpy(peerId + 3, CG_BITTORRENT_CLIENT_VER, 4); /* Host Name */ cg_http_getservername(hostName, CG_HTTP_SEVERNAME_MAXLEN); err = cg_sha1_reset(&sha); if (!err) { err = cg_sha1_input(&sha, hostName, cg_strlen(hostName)); if (!err) err = cg_sha1_result(&sha, hostNameHash); } if (err) return FALSE; memcpy(peerId + 7, hostNameHash, 7); /* Mac Address */ netIfList = cg_net_interfacelist_new(); if (!netIfList) return FALSE; cg_net_gethostinterfaces(netIfList); netIf =cg_net_interfacelist_gets(netIfList); if (!netIf) { cg_net_interfacelist_delete(netIfList); return FALSE; } cg_net_interface_getmacaddress(netIf, (peerId + 14)); cg_net_interfacelist_delete(netIfList); return FALSE; }
BOOL cg_http_serverlist_open(CgHttpServerList *httpServerList, int port) { CgNetworkInterfaceList *netIfList; CgNetworkInterface *netIf; CgHttpServer *httpServer; char *bindAddr; BOOL result =FALSE; cg_log_debug_l4("Entering...\n"); netIfList = cg_net_interfacelist_new(); #ifndef CG_NET_USE_ANYADDR cg_net_gethostinterfaces(netIfList); #else netIf = cg_net_interface_getany(); cg_net_interfacelist_add(netIfList, netIf); #endif for (netIf = cg_net_interfacelist_gets(netIfList); netIf; netIf = cg_net_interface_next(netIf)) { bindAddr = cg_net_interface_getaddress(netIf); if (cg_strlen(bindAddr) <= 0) continue; httpServer = cg_http_server_new(); if (cg_http_server_open(httpServer, port, bindAddr) == FALSE) { /* Theo Beisch - why break off, we may be running ok on at least some IF??? (at least WINCE does...) */ continue; } cg_http_serverlist_add(httpServerList, httpServer); result = TRUE; //at least one IF found ok } if (result==FALSE) cg_http_serverlist_clear(httpServerList); cg_net_interfacelist_delete(netIfList); return result; }
BOOL cg_socket_setmulticastinterface(CgSocket *sock, char *ifaddr) { struct sockaddr_in sockaddr; BOOL sockAddrSuccess; int optSuccess; CgNetworkInterfaceList *netIfList; CgNetworkInterface *netIf; int netIfCnt; cg_log_debug_l4("Entering...\n"); netIfList = NULL; if (cg_strlen(ifaddr) <= 0) { netIfList = cg_net_interfacelist_new(); netIfCnt = cg_net_gethostinterfaces(netIfList); if (netIfCnt <= 0) { cg_net_interfacelist_delete(netIfList); return FALSE; } netIf = cg_net_interfacelist_gets(netIfList); ifaddr = cg_net_interface_getaddress(netIf); } sockAddrSuccess = cg_socket_tosockaddrin(ifaddr, 0, &sockaddr, TRUE); if (netIfList != NULL) cg_net_interfacelist_delete(netIfList); if (sockAddrSuccess == FALSE) return FALSE; optSuccess = ka_setsockopt(sock->id, IP_PROTOIP, IPO_MULTICAST_IF, (const char *)&sockaddr.sin_addr, sizeof(sockaddr.sin_addr)); if (optSuccess != 0) return FALSE; cg_log_debug_l4("Leaving...\n"); return TRUE; }
BOOL cg_upnp_ssdpresponse_serverlist_open(CgUpnpSSDPResponseServerList *ssdpServerList, int bindPort) { CgNetworkInterfaceList *netIfList; CgNetworkInterface *netIf; CgUpnpSSDPResponseServer *ssdpServer; char *bindAddr; BOOL result = FALSE; cg_log_debug_l4("Entering...\n"); netIfList = cg_net_interfacelist_new(); #ifndef CG_NET_USE_ANYADDR cg_net_gethostinterfaces(netIfList); #else netIf = cg_net_interface_getany(); cg_net_interfacelist_add(netIfList, netIf); #endif for (netIf = cg_net_interfacelist_gets(netIfList); netIf; netIf = cg_net_interface_next(netIf)) { bindAddr = cg_net_interface_getaddress(netIf); if (cg_strlen(bindAddr) <= 0) continue; ssdpServer = cg_upnp_ssdpresponse_server_new(); if (cg_upnp_ssdpresponse_server_open(ssdpServer, bindPort, bindAddr) == FALSE) { cg_upnp_ssdpresponse_server_delete(ssdpServer); continue; } cg_upnp_ssdpresponse_serverlist_add(ssdpServerList, ssdpServer); result = TRUE; } if (result == FALSE) cg_upnp_ssdpresponse_serverlist_clear(ssdpServerList); cg_net_interfacelist_delete(netIfList); return result; }
BOOL cg_upnp_controlpoint_ipchanged(CgUpnpControlPoint *ctrlPoint) { CgNetworkInterfaceList *current, *added, *removed; CgNetworkInterface *netIf; CgUpnpDevice *dev, *tmp; CgUpnpSSDPPacket *ssdpPkt; char *address; cg_log_debug_l4("Entering...\n"); current = cg_net_interfacelist_new(); added = cg_net_interfacelist_new(); removed = cg_net_interfacelist_new(); if (current == NULL || added == NULL || removed == NULL) { if (current != NULL) cg_net_interfacelist_delete(current); if (added != NULL) cg_net_interfacelist_delete(added); if (removed != NULL) cg_net_interfacelist_delete(removed); return FALSE; } /* Get Interface changes */ cg_net_gethostinterfaces(current); cg_net_interfacelist_getchanges(ctrlPoint->ifCache, current, added, removed); /* Remove all devices registered through old interface */ for (netIf = cg_net_interfacelist_gets(removed); netIf != NULL; netIf = cg_net_interface_next(netIf)) { cg_upnp_controlpoint_lock(ctrlPoint); tmp = cg_upnp_controlpoint_getdevices(ctrlPoint); while (tmp != NULL) { dev = tmp; tmp = cg_upnp_device_next(dev); ssdpPkt = cg_upnp_device_getssdppacket(dev); address = cg_upnp_ssdp_packet_getlocaladdress(ssdpPkt); if (address != NULL && cg_strcmp(address, cg_net_interface_getaddress(netIf)) == 0) { /* This device has been received from the removed interface, so it does not exist */ cg_upnp_controlpoint_unlock(ctrlPoint); cg_upnp_controlpoint_removedevicebyssdppacket(ctrlPoint, ssdpPkt); cg_upnp_controlpoint_lock(ctrlPoint); address = NULL; dev = NULL; ssdpPkt = NULL; } } cg_upnp_controlpoint_unlock(ctrlPoint); } /* Launch new M-SEARCH */ cg_upnp_controlpoint_search(ctrlPoint, CG_UPNP_ST_ROOT_DEVICE); /**** Cache current interfaces ****/ cg_net_gethostinterfaces(ctrlPoint->ifCache); cg_net_interfacelist_delete(current); cg_net_interfacelist_delete(added); cg_net_interfacelist_delete(removed); cg_log_debug_l4("Leaving...\n"); return TRUE; }