/** * liefert Ip-Adresse(n) für einen Hostnamen. * * @param[in] hostname Ziel-Hostname/-ip * * @author FloSoft */ std::vector<HostAddr> Socket::HostToIp(const std::string& hostname, const unsigned int port, bool get_ipv6, bool useUDP) { std::vector<HostAddr> ips; std::string sPort = boost::lexical_cast<std::string>(port); HostAddr hostAddr; hostAddr.host = hostname; hostAddr.port = sPort; hostAddr.ipv6 = get_ipv6; hostAddr.isUDP = useUDP; // no dns resolution for localhost if(hostname == "localhost") { ips.push_back(hostAddr); return ips; } ResolvedAddr res(hostAddr, true); addrinfo* addr = &res.getAddr(); while(addr != NULL) { HostAddr h; h.host = IpToString(addr->ai_addr); h.port = sPort; h.ipv6 = (addr->ai_family == AF_INET6); h.isUDP = addr->ai_protocol == SOCK_DGRAM; ips.push_back(h); addr = addr->ai_next; } return ips; }
void CTSGetIp::Handle(void* Buffer, int Size) { //解包 CReGetUserIpPacket opPacket; opPacket.Unpack(Buffer, Size); int count = opPacket.UserList.size(); int TaskId = opPacket.nTaskId; int SeqNumber = opPacket.Header.nSeqNumber; Tracker.Back(SeqNumber); //消包 CTask *Task; for(int i = 0; i < count; i++) { if(! TaskPool.GetTask(TaskId,Task)) break; int UserIp = opPacket.UserList.at(i).nUserIp; int TcpPort = opPacket.UserList.at(i).wTcpPort; string FileCode = opPacket.strHashValue; Task->SetCode(FileCode); CResource *Resource = new CResource(IpToString(UserIp), TcpPort); CResourcePool* ResourcePool = Task->GetResourcePool(); ResourcePool->AddResource(Resource); } }
void TSysIpTest::TestIpToString() { ui8 ipArr[][4] = {{192, 168, 0, 1}, {87, 255, 18, 167}, {255, 255, 0, 31}, {188, 225, 124, 255}}; const char* ipStr[] = {"192.168.0.1", "87.255.18.167", "255.255.0.31", "188.225.124.255"}; for (size_t i = 0; i < ARRAY_SIZE(ipStr); ++i) { UNIT_ASSERT(IpToString(*ReinterpretCast<TIpHost*>(&(ipArr[i]))) == ipStr[i]); } }
/** * liefert die IP des Lokalen-Hosts. * * @return liefert @p buffer zurück oder @p "" bei Fehler * * @author OLiver * @author FloSoft */ std::string Socket::GetSockIP(void) { sockaddr_storage peer; socklen_t length = sizeof(sockaddr_storage); // Localhost-Adresse holen if(getsockname(socket_, (sockaddr*)&peer, &length) == SOCKET_ERROR) return ""; // in Text verwandeln return IpToString((sockaddr*)&peer); }
/** * versucht eine Verbindung mit einem externen Host aufzubauen. * * @param[in] hostname Ziel-Hostname/-ip * @param[in] port Port zu dem Verbunden werden soll * * @p true bei Erfolg, @p false bei Fehler * * @author FloSoft */ bool Socket::Connect(const std::string& hostname, const unsigned short port, bool use_ipv6, const Socket::PROXY_TYPE typ, const std::string& proxy_hostname, const unsigned int proxy_port) { if(typ == PROXY_SOCKS4) use_ipv6 = false; std::vector<HostAddr> proxy_ips; if(typ != PROXY_NONE) { proxy_ips = HostToIp(proxy_hostname, proxy_port, use_ipv6); if(proxy_ips.size() == 0) return false; } // TODO: socks v5 kann remote resolven std::vector<HostAddr> ips = HostToIp(hostname, port, use_ipv6); if(ips.size() == 0) return false; bool done = false; std::vector<HostAddr>::const_iterator start, end; // do not use proxy for connecting to localhost if(typ != PROXY_NONE && hostname != "localhost") { start = proxy_ips.begin(); end = proxy_ips.end(); } else { start = ips.begin(); end = ips.end(); } for(std::vector<HostAddr>::const_iterator it = start; it != end; ++it) { if(it->isUDP) throw std::invalid_argument("Cannot connect to UDP (yet)"); if(!Create(it->ipv6 ? AF_INET6 : AF_INET)) continue; // aktiviere non-blocking unsigned long argp = 1; #ifdef _WIN32 ioctlsocket(socket_, FIONBIO, &argp); #else ioctl(socket_, FIONBIO, &argp); #endif ResolvedAddr addr(*it); std::string ip = IpToString(addr.getAddr().ai_addr); //-V807 LOG.lprintf("Verbinde mit %s%s:%d\n", (typ != PROXY_NONE ? "Proxy " : ""), ip.c_str(), (typ != PROXY_NONE ? proxy_port : port)); // Und schließlich Verbinden if(connect(socket_, addr.getAddr().ai_addr, addr.getAddr().ai_addrlen) != SOCKET_ERROR) { done = true; break; } #ifdef _WIN32 if(WSAGetLastError() == WSAEWOULDBLOCK) #else if(errno == EINPROGRESS || errno == EWOULDBLOCK) #endif { int timeout = 0; while(!done) { SocketSet sw, se; sw.Add(*this); se.Add(*this); if(sw.Select(0, 1) == 1 || se.Select(0, 2) != 0) { unsigned int err; socklen_t len = sizeof(unsigned int); getsockopt(socket_, SOL_SOCKET, SO_ERROR, (char*)&err, &len); if(err != 0) { #ifdef _WIN32 WSASetLastError(err); #else errno = err; #endif break; } switch(typ) { default: break; case PROXY_SOCKS4: { union{ char proxyinit[18]; unsigned short proxyInitShort[9]; }; proxyinit[0] = 4; // socks v4 proxyinit[1] = 1; // 1=connect proxyInitShort[1] = htons(port); for(std::vector<HostAddr>::const_iterator it = ips.begin(); it != ips.end(); ++it) { if(!it->ipv6) sscanf(it->host.c_str(), "%c.%c.%c.%c", &proxyinit[4], &proxyinit[5], &proxyinit[6], &proxyinit[7]); } strcpy(&proxyinit[8], "siedler25"); // userid Send(proxyinit, 18); int proxy_timeout = 0; while(BytesWaiting() < 8 && proxy_timeout < 8) { Sleep(250); ++proxy_timeout; } if(proxy_timeout >= 8) { LOG.lprintf("Proxy error: connection timed out\n"); return false; } Recv(proxyinit, 8); if(proxyinit[0] != 0 || proxyinit[1] != 90) { LOG.lprintf("Proxy error: got %d: connection rejected or failed or other error\n", proxyinit[1]); return false; } } break; case PROXY_SOCKS5: { // not implemented return false; } break; } // Verbindung hergestellt done = true; } Sleep(50); ++timeout; if(timeout > 8) { #ifdef _WIN32 WSASetLastError(WSAETIMEDOUT); #else errno = ETIMEDOUT; #endif break; } } if(done) break; } LOG.getlasterror("Verbindung fehlgeschlagen\nFehler"); } if(!done) { LOG.lprintf("Fehler beim Verbinden mit %s:%d\n", hostname.c_str(), port); return false; } LOG.lprintf("Verbindung erfolgreich hergestellt mit %s:%d\n", hostname.c_str(), port); // deaktiviere non-blocking unsigned long argp = 0; #ifdef _WIN32 ioctlsocket(socket_, FIONBIO, &argp); #else ioctl(socket_, FIONBIO, &argp); #endif status_ = CONNECT; // Alles ok return true; }
VOID GetPacketInfo(unsigned short ptype, unsigned char *szBuffer, int size_framehdr, char *src, char *dst, char *type, char *info) { int size_iphdr; unsigned short src_port, dst_port; char *service; struct ethernet_802_3 *eth; struct iphdr *ip; struct udphdr *udp; struct icmphdr *icmp; struct tcphdr *tcp; struct arppkt *arp; if (ptype == 0x0800) //'tis IP packet { ip = (struct iphdr *) &szBuffer[size_framehdr]; size_iphdr = (ip->verlen & 0x0f) * 4; lstrcpy(src, IpToString(ip->sourceip)); lstrcpy(dst, IpToString(ip->destip)); if (ip->prot == R_UDP) { udp = (struct udphdr *) &szBuffer[size_framehdr + size_iphdr]; lstrcpy(type, "udp"); src_port = ntohs(udp->srcport); dst_port = ntohs(udp->dstport); if (service = find_in_table(src_port, 0)) lstrcpy(info, service); else if (service = find_in_table(dst_port, 0)) lstrcpy(info, service); else wsprintf(info, "port: %d --> %d", src_port, dst_port); } else if (ip->prot == R_ICMP) { lstrcpy(type, "icmp"); icmp = (struct icmphdr *) &szBuffer[size_framehdr + size_iphdr]; if (icmp->type <= 16) lstrcpy(info, ICMP_type[icmp->type]); } else if (ip->prot == R_TCP) { lstrcpy(type, "tcp"); tcp = (struct tcphdr *) &szBuffer[size_framehdr + size_iphdr]; src_port = ntohs(tcp->srcport); dst_port = ntohs(tcp->dstport); if (service = find_in_table(src_port, 1)) lstrcpy(info, service); else if (service = find_in_table(dst_port, 1)) lstrcpy(info, service); else wsprintf(info, "port: %d --> %d", src_port, dst_port); } else if (ip->prot == R_IGMP) { lstrcpy(type, "igmp"); } else return; } else if (ptype == 0x0806) // ARP { arp = (struct arppkt *) &szBuffer[size_framehdr]; wsprintf(src, "%d.%d.%d.%d", arp->sender_ip[0], arp->sender_ip[1],arp->sender_ip[2],arp->sender_ip[3]); if (ntohs(arp->operation) == 1) { wsprintf(type, "arp req."); wsprintf(dst, "Broadcast"); wsprintf(info, "%d.%d.%d.%d = ?", arp->target_ip[0], arp->target_ip[1],arp->target_ip[2],arp->target_ip[3]); } else if (ntohs(arp->operation) == 2) { wsprintf(type, "arp resp."); wsprintf(dst, "%d.%d.%d.%d", arp->target_ip[0], arp->target_ip[1],arp->target_ip[2],arp->target_ip[3]); wsprintf(info, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", arp->sender_ha[0], arp->sender_ha[1],arp->sender_ha[2],arp->sender_ha[3],arp->sender_ha[4],arp->sender_ha[5]); } } else if (ptype == 0x8035) // RARP { arp = (struct arppkt *) &szBuffer[size_framehdr]; wsprintf(src, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X = ?", arp->sender_ha[0], arp->sender_ha[1],arp->sender_ha[2],arp->sender_ha[3],arp->sender_ha[4],arp->sender_ha[5]); if (ntohs(arp->operation) == 3) { wsprintf(type, "rarp req."); wsprintf(dst, "Broadcast"); wsprintf(info, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X = ?", arp->sender_ha[0], arp->sender_ha[1],arp->sender_ha[2],arp->sender_ha[3],arp->sender_ha[4],arp->sender_ha[5]); } else if (ntohs(arp->operation) == 4) { wsprintf(type, "rarp resp."); wsprintf(dst, "%d.%d.%d.%d", arp->target_ip[0], arp->target_ip[1],arp->target_ip[2],arp->target_ip[3]); wsprintf(info, "%d.%d.%d.%d", arp->target_ip[0], arp->target_ip[1],arp->target_ip[2],arp->target_ip[3]); } } else // unknown type of packet { eth = (struct ethernet_802_3 *) szBuffer; wsprintf(src, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", szBuffer[6], szBuffer[7], szBuffer[8], szBuffer[9], szBuffer[10], szBuffer[11]); wsprintf(dst, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", szBuffer[0], szBuffer[1], szBuffer[2], szBuffer[3], szBuffer[4], szBuffer[5]); if (size_framehdr == 14) { wsprintf(type, "ethernet_II frame"); wsprintf(info, "type = 0x%.4x%", ptype); } else if ((eth->dsap == 0xf0) && (eth->ssap == 0xf0)) { wsprintf(type, "netbeui"); wsprintf(info, "type = 0x%.4x", ptype); } else if ((eth->dsap == 0xff) && (eth->ssap == 0xff)) { wsprintf(type, "ipx"); lstrcpy(info, "novell"); } else { wsprintf(type, "802.3 frame"); wsprintf(info, "type = 0x%.4x", ptype); } } }
/** * @brief 保存终端参数 * @return 0成功, 否则失败 */ int SaveParaTerm(void) { //SaveParaCamra(); //保存摄像头信息 XINREF pf; int bakup = 0; char str[48]; int i; DebugPrint(0, "Save paraterm\n"); pf = XinOpen(PARAM_SAVE_PATH "/term.xin", 'w'); if(NULL == pf) { PrintLog(0,"open param_path...\n"); goto mark_bakup; } mark_save: XinWriteInt( pf, "svraddrflag", ParaTermG.svraddr.net.chn, 0); IpToString(ParaTermG.svraddr.net.ip, str); XinWriteString(pf,"svraddrIp", str); XinWriteInt(pf, "svrport", ParaTermG.svraddr.net.port, 0); IpToString(ParaTermG.svraddr_1.net.ip, str); XinWriteString(pf,"svraddrIp2", str); XinWriteInt(pf, "svrport2", ParaTermG.svraddr_1.net.port, 0); IpToString(ParaTermG.svraddr_2.net.ip, str); XinWriteString(pf,"svraddrIp3", str); XinWriteInt(pf, "svrport3", ParaTermG.svraddr_2.net.port, 0); sms_phone_tounnor((unsigned char *)str, ParaTermG.sms_addr); XinWriteHex(pf,"smsaddr", (unsigned char *)str, 8); /*by ydl add 2011-03-21*/ IpToString(ParaTermG.gate_addr.net.ip, str); XinWriteString(pf,"gateaddr", str); XinWriteInt(pf, "gateport", ParaTermG.gate_addr.net.port, 0); XinWriteString(pf,"apn",ParaTermG.apn); XinWriteString( pf, "cdmausr", ParaTermG.cdma_usr); XinWriteString(pf, "cdmapwd", ParaTermG.cdma_pwd); XinWriteHex(pf, "norpwd", ParaTermG.nor_pwd,3); XinWriteHex(pf, "compwd", ParaTermG.com_pwd, 3); XinWriteHex(pf, "admpwd", ParaTermG.adm_pwd,3); XinWriteInt(pf,"search_device",ParaTermG.search_device,0); XinWriteInt(pf,"taskstarthour",ParaTermG.task_starthour,0); XinWriteInt(pf,"taskstartdev",ParaTermG.task_startdev,0); XinWriteInt(pf,"plcroutetype",ParaTermG.plc_route_type,0); XinWriteHex(pf,"gatelinewaste",ParaTermG.gate_linewaste,2); XinWriteInt(pf,"hourupimpdata",ParaTermG.hour_upimpdata,0); XinWriteInt(pf,"hourupdaydata",ParaTermG.hour_updaydata,0); XinWriteInt(pf,"alarmflag",ParaTermG.alarm_flag,0); unsigned char tmpbuf[16]; Cascade_AddrChange(ParaTermG.cascade_addr, tmpbuf,1); XinWriteHex(pf, "cascadeaddr", tmpbuf, 16); //XinWriteHex(pf, "cascadeaddr", ParaTermG.cascade_addr, 16); XinWriteInt(pf,"cascadeflag",ParaTermG.cascade_flag,0); XinWriteInt(pf,"dayreadmondata",ParaTermG.day_read_mondata,0); XinWriteInt(pf,"hourreadmondata",ParaTermG.hour_read_mondata,0); XinWriteInt(pf,"dayupmondata",ParaTermG.day_up_mondata,0); XinWriteInt(pf,"hourupmondata",ParaTermG.hour_up_mondata,0); //XinWriteHex(pf,"smsaddr", ParaTermG.sms_addr,16);//短信中心号码 Cascade_AddrChange(ParaTermG.peibian_addr,tmpbuf,0); XinWriteHex(pf,"peibian_addr",tmpbuf,4); XinWriteHex(pf, "ct", ParaTermG.ct,2); for(i=0;i<MAX_485PORT;i++){ sprintf((char *)tmpbuf,"baud%d",i); XinWriteInt(pf,(char *)tmpbuf,ParaTermG.port[i].baud,0); sprintf((char *)tmpbuf,"databits%d",i); XinWriteInt(pf, (char *)tmpbuf,ParaTermG.port[i].databits, 0);//默认为8 sprintf((char *)tmpbuf,"stopbits%d",i); XinWriteInt(pf, (char *)tmpbuf,ParaTermG.port[i].stopbits, 0);//默认为1 sprintf((char *)tmpbuf,"parity%d",i); XinWriteInt(pf, (char *)tmpbuf, ParaTermG.port[i].parity,0);//校验位 sprintf((char *)tmpbuf,"func%d",i); XinWriteInt(pf, (char *)tmpbuf, ParaTermG.port[i].func,0);//级联 } #if 0 int i; for (i=0; i<PORTCFG_NUM; i++) { printf(str, "port_valid%d", i); XinWriteInt(pf, str, ParaTermG.port[i].valid, 0); printf(str, "port_frame%d", i); XinWriteInt(pf, str, ParaTermG.port[i].frame, 0); printf(str, "port_baud%d", i); XinWriteInt(pf, str, ParaTermG.port[i].baud, 0); printf(str, "port_func%d", i); XinWriteInt(pf, str, ParaTermG.port[i].func, 0); } #endif XinClose(pf); /*end*/ mark_bakup: if(bakup) return 0; bakup = 1; pf = XinOpen(PARAM_BAK_PATH "/term.xin", 'w'); if(NULL == pf) { PrintLog(0,"open bak fail... \n"); return 1; } goto mark_save; }
void DumpRule(RuleNode *rule_node) { char str[128]; if (rule_node == NULL) { debug("Rule Node is NULL\r\n\r\n"); return; } wsprintf(str, "cur ptr:\t %d\r\n", rule_node); debug(str); wsprintf(str, "next ptr:\t %d\r\n", rule_node->next ? rule_node->next:0); debug(str); wsprintf(str, "src ip:\t\t %c%s\r\n", rule_node->sip_op, IpToString(rule_node->sip)); debug(str); wsprintf(str, "src mask:\t %s\r\n", IpToString(rule_node->smask)); debug(str); wsprintf(str, "dst ip:\t\t %c%s\r\n", rule_node->dip_op, IpToString(rule_node->dip)); debug(str); wsprintf(str, "dst mask:\t %s\r\n", IpToString(rule_node->dmask)); debug(str); wsprintf(str, "src ports:\t %d - %d\r\n", ntohs(rule_node->lsp), ntohs(rule_node->hsp)); debug(str); wsprintf(str, "dst ports:\t %d - %d\r\n", ntohs(rule_node->ldp), ntohs(rule_node->hdp)); debug(str); if (rule_node->dir == UNI_DIR) debug("direction:\t '->'\r\n"); else debug("direction:\t '<>'\r\n"); if (rule_node->offset_set) { wsprintf(str, "offset:\t\t %d\r\n", rule_node->offset); debug(str); } if (rule_node->depth_set) { wsprintf(str, "depth:\t\t %d\r\n", rule_node->depth); debug(str); } if (rule_node->dsize_set) { wsprintf(str, "dsize:\t\t %s\r\n", rule_node->dsize); debug(str); } if (rule_node->msg_set) { wsprintf(str, "msg:\t\t %s\r\r\n", rule_node->msg); debug(str); } if (rule_node->content_set) { wsprintf(str, "content:\t %s\r\n", rule_node->content); debug(str); } if (rule_node->ttl_set) { wsprintf(str, "ttl:\t\t %d\r\n", rule_node->ttl); debug(str); } if (rule_node->tos_set) { wsprintf(str, "tos:\t\t %d\r\n", rule_node->tos); debug(str); } if (rule_node->id_set) { wsprintf(str, "id:\t\t %d\r\n", rule_node->id); debug(str); } if (rule_node->ipopts_set) { wsprintf(str, "ipopts:\t\t %d\r\n", rule_node->ipopts); debug(str); } if (rule_node->fragbits_set) { wsprintf(str, "fragbits:\t 0x%x\r\n", rule_node->fragbits); debug(str); } if (rule_node->flags_set) { wsprintf(str, "flags:\t\t %x\r\n", rule_node->flags); debug(str); } if (rule_node->seqnum_set) { wsprintf(str, "seq:\t\t %d\r\n", rule_node->seqnum); debug(str); } if (rule_node->acknum_set) { wsprintf(str, "ack:\t\t %d\r\n", rule_node->acknum); debug(str); } if (rule_node->itype_set) { wsprintf(str, "itype:\t\t %d\r\n", rule_node->itype); debug(str); } if (rule_node->icode_set) { wsprintf(str, "icode:\t\t %d\r\n", rule_node->icode); debug(str); } if (rule_node->icmp_id_set) { wsprintf(str, "icmp_id:\t %d\r\n", rule_node->icmp_id); debug(str); } if (rule_node->icmp_seq_set) { wsprintf(str, "icmp_seq:\t %d\r\n", rule_node->icmp_seq); debug(str); } debug("\r\n------------------------------------------------------\r\n\r\n"); }
/* processDHCP(): * Actually this is processBOOTP, but we call it processDHCP here so that * the same code in enetcore.c can be used to call either function (depending * on what has been configured into the uMon build). */ int processDHCP(struct ether_header *ehdr,ushort size) { char getbootfile = 0; struct ip *ihdr; struct Udphdr *uhdr; struct bootphdr *bhdr; ulong ip, temp_ip, cookie; uchar buf[16], bootsrvrip[16], *op; ihdr = (struct ip *)(ehdr + 1); uhdr = (struct Udphdr *)((char *)ihdr + IP_HLEN(ihdr)); bhdr = (struct bootphdr *)(uhdr+1); /* Verify incoming transaction id matches the previous outgoing value: */ if (xidCheck((char *)&bhdr->transaction_id,1) < 0) return(-1); /* If bootfile is nonzero, store it into BOOTFILE shell var: */ if (bhdr->bootfile[0]) getbootfile++; /* Assign IP "server_ip" to the BOOTSRVR shell var (if non-zero): */ memcpy((char *)&temp_ip,(char *)&bhdr->server_ip,4); if (temp_ip) getbootfile++; IpToString(temp_ip,(char *)bootsrvrip); /* Assign IP address loaded in "your_ip" to the IPADD shell var: */ memcpy((char *)BinIpAddr,(char *)&bhdr->your_ip,4); memcpy((char *)&temp_ip,(char *)&bhdr->your_ip,4); printf("IP: %s\n",IpToString(temp_ip,(char *)buf)); /* If STANDARD_MAGIC_COOKIE exists, then process options... */ memcpy((char *)&cookie,(char *)bhdr->vsa,4); if (cookie == ecl(STANDARD_MAGIC_COOKIE)) { /* Assign subnet mask option to NETMASK shell var (if found): */ op = DhcpGetOption(DHCPOPT_SUBNETMASK,&bhdr->vsa[4]); if (op) { memcpy((char *)BinNetMask,(char *)op+2,4); memcpy((char *)&ip,(char *)op+2,4); printf("NETMASK: %s\n",IpToString(ip,(char *)buf)); } /* Assign first router option to GIPADD shell var (if found): */ /* (the router option can have multiple entries, and they are */ /* supposed to be in order of preference, so use the first one) */ op = DhcpGetOption(DHCPOPT_ROUTER,&bhdr->vsa[4]); if (op) { memcpy((char *)BinGipAddr,(char *)op+2,4); memcpy((char *)&ip,(char *)op+2,4); printf("GIPADD: %s\n",IpToString(ip,(char *)buf)); } } /* Call loadBootFile() which will then kick off a tftp client * transfer if both BOOTFILE and BOOTSRVR shell variables are * loaded; otherwise, we are done. */ /* If the bootp server gave us the bootfile and boot server IP, * then call loadBootFile()... */ if (getbootfile == 2) loadBootFile((char *)bhdr->bootfile,(char *)bootsrvrip); else DHCPState = BOOTPSTATE_COMPLETE; return(0); }