boost::tuple<std::string, std::string, std::string, std::string> HttpRequest::ParseUrl(const std::string & url) { //for remove regex lib size_t pos_protocol = url.find("://"); std::string protocol("http"); if (pos_protocol != std::string::npos) { protocol = url.substr(0, pos_protocol); } size_t pos_host_beg = (pos_protocol == std::string::npos ? 0 : pos_protocol +3); size_t pos_path_beg = -1; std::string host_str(""); std::string port_str("80"); size_t pos_host = url.find_first_of(':', pos_host_beg); size_t pos_slash = url.find_first_of('/', pos_host_beg); if (pos_host != std::string::npos && pos_host < pos_slash) { host_str = url.substr(pos_host_beg, pos_host - pos_host_beg); size_t pos_port_beg = pos_host + 1; size_t pos_port = url.find_first_of('/', pos_port_beg); if (pos_port != std::string ::npos) { port_str = url.substr(pos_port_beg, pos_port - pos_port_beg); pos_path_beg = pos_port; } } else { pos_host = url.find_first_of('/', pos_host_beg); if (pos_host != std::string::npos) { host_str = url.substr(pos_host_beg, pos_host - pos_host_beg); pos_path_beg = pos_host; } } std::string path_str(""); if (pos_path_beg > 0) path_str = url.substr(pos_path_beg); //boost::regex reg("^(([A-Za-z]+)://)?([^:/]+)(:([0-9]+))?(.*)"); //boost::smatch sm; //if (false == boost::regex_match(url, sm, reg)) //{ // return boost::make_tuple("", "", "", ""); //} //std::string protocol(sm[2].matched ? std::string(sm[2].first, sm[2].second) : "http"); //std::string host_str(sm[3].first, sm[3].second); //std::string port_str(sm[5].matched ? std::string(sm[5].first, sm[5].second) : ""); //std::string path_str(sm[6].matched ? std::string(sm[6].first, sm[6].second) : "/"); if (path_str.empty()) path_str = "/"; return boost::make_tuple(protocol, host_str, port_str, path_str); }
rnb_err_t RNBSocket::Connect (const char *host, uint16_t port) { Disconnect (false); // Create the socket m_fd = ::socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_fd == -1) return rnb_err; // Enable local address reuse SetSocketOption (m_fd, SOL_SOCKET, SO_REUSEADDR, 1); struct sockaddr_in sa; ::memset (&sa, 0, sizeof (sa)); sa.sin_family = AF_INET; sa.sin_port = htons (port); if (host == NULL) host = "localhost"; int inet_pton_result = ::inet_pton (AF_INET, host, &sa.sin_addr); if (inet_pton_result <= 0) { struct hostent *host_entry = gethostbyname (host); if (host_entry) { std::string host_str (::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list)); inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr); if (inet_pton_result <= 0) { Disconnect (false); return rnb_err; } } } if (-1 == ::connect (m_fd, (const struct sockaddr *)&sa, sizeof(sa))) { Disconnect (false); return rnb_err; } // Keep our TCP packets coming without any delays. SetSocketOption (m_fd, IPPROTO_TCP, TCP_NODELAY, 1); return rnb_success; }
int CapturePacketThread::RunCapture() { Releaseinfo("RunCapture input"); pcap_t *adhandle; char errbuf[PCAP_ERRBUF_SIZE]; u_int netmask=0xffffff; struct bpf_program fp; if((adhandle= pcap_open(devname.c_str(), // name of the device 65536, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode 1000, // read timeout NULL, // authentication on the remote machine errbuf // error buffer ) ) == NULL) { Releaseinfo("pcap_open fails"); fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", devname.c_str()); return -1; } string filter(""); string host_str(""); host_str = AppConfig::SharedInstance()->GetHostIp(); filter+="host "; filter+=ipstr; filter+=" and (tcp or udp) and (not host "; filter+=host_str; filter+=")"; //char test_filter[100]={0}; //strcpy(test_filter,filter.c_str()); //Releaseinfo(test_filter); if(pcap_compile(adhandle, &fp, filter.c_str(), 0, netmask) == -1) { Releaseinfo("pcap_compile fails"); fprintf(stderr, "Error calling pcap_compile\n"); return -1; } if(pcap_setfilter(adhandle, &fp) == -1) { Releaseinfo("pcap_setfilter fails"); fprintf(stderr, "Error setting filter\n"); return -1; } char user_ip[20]={0}; strcpy(user_ip,ipstr.c_str()); pcap_loop(adhandle, 0, PacketHandler, (u_char*)user_ip); return 0; }