ClRcT SendArp(const char* host, const char* dev) { int i; char myMac[MacAddrLen]; unsigned int myIp = 0; EthIpv4ArpPacket pkt; ClRcT rc = CL_OK; DevToMac(dev,myMac); if( (rc = HostToIp(host,&myIp)) != CL_OK) return rc; for (i = ArpRequest; i<= ArpRequest; i++) { memcpy(pkt.myMac, myMac, MacAddrLen); memset(pkt.dstMac, 0xFF, MacAddrLen); pkt.type = htons(ETHERTYPE_ARP); pkt.hrd = htons(ArpHwTypeEthernet); pkt.pro = htons(IpProtoType); //htons(ETHERTYPE_IP); //htons(ETH_P_IP); //ArpAddressResolutionType; pkt.hln = MacAddrLen; /* ETH_HW_ADDR_LEN; Length in bytes of ethernet address */ pkt.pln = 4; // IP_ADDR_LEN; pkt.op = htons(i); // htons(ARPOP_REQUEST); memcpy(pkt.sha,myMac,MacAddrLen); memset(pkt.tha, 0xFF, MacAddrLen); memcpy(pkt.spa,&myIp,4); memcpy(pkt.tpa,&myIp,4); if (1) { int sd; struct sockaddr_ll sal; bzero(&sal,sizeof(sal)); sal.sll_family = AF_PACKET; sal.sll_protocol = htons(ETH_P_ARP); sal.sll_ifindex = 2; sal.sll_hatype = htons(i); sal.sll_pkttype = PACKET_BROADCAST; memcpy(sal.sll_addr, myMac, MacAddrLen); sal.sll_halen = MacAddrLen; if ((sd = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ARP))) < 0) { perror("socket"); return CL_ERR_LIBRARY; } if (sendto(sd,&pkt,sizeof(pkt),0,(struct sockaddr *)&sal,sizeof(sal)) < 0) { perror("sendto"); return CL_ERR_LIBRARY; } close(sd); } } return CL_OK; }
TcpClient::TcpClient ( char *ip ) { _ip = new char[100]; _hostname = new char[1024]; if ( isdigit ( ip[0] ) ) _ip = ip; else { strcpy ( _hostname, ip ); strcpy ( _ip, HostToIp ( _hostname ) ); } }
int TcpClient::ConnectToServer() { /* cream socketul */ if ( ( _Socket = socket ( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) { perror ( "Eroare la socket().\n" ); return errno; } if ( !isdigit ( _ip[0] ) ) { strcpy ( _hostname, _ip ); strcpy ( _ip, HostToIp ( _hostname ) ); } _CmdSocket = socket ( AF_INET, SOCK_STREAM, 0 ); /* umplem structura folosita pentru realizarea conexiunii cu serverul */ /* familia socket-ului */ server.sin_family = AF_INET; /* adresa IP a serverului */ server.sin_addr.s_addr = inet_addr ( _ip ); /* portul de conectare */ server.sin_port = htons ( _port ); /* ne conectam la server */ if ( connect ( _Socket, ( struct sockaddr * ) &server,sizeof ( sockaddr ) ) == -1 ) { perror ( "[client]Eroare la connect().\n" ); //ErrorHandler return errno; } char buffer[256]; strcpy ( buffer, ReceiveMessage() ); if ( GetCode ( buffer ) == 220 ) { cout << "\nConnected to server: " << this->_ip << endl; return 0; } else { cout << "\nConnection failed"; return 1; } return 1; }
/** * 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 TcpClient::SetHostName ( char *host ) { // cout << "sizeof(hostname)="<<sizeof(_hostname)<<" strlen(host)="<<strlen(host)<<endl; strcpy ( _hostname, host ); strcpy ( _ip, HostToIp ( _hostname ) ); }