Ejemplo n.º 1
0
void IPV6Address::setAddress(const char *host)
{
    if(hostname)
        delString(hostname);
    hostname = NULL;

    if(!host)  // The way this is currently used, this can never happen
        host = "::";

#ifdef  WIN32
    if(!stricmp(host, "localhost"))
        host = "::1";
#endif

    if(!setIPAddress(host)) {
        struct addrinfo hint, *list = NULL, *first;
        memset(&hint, 0, sizeof(hint));
        hint.ai_family = AF_INET6;
        struct in6_addr *addr;
        struct sockaddr_in6 *ip6addr;

        if(getaddrinfo(host, NULL, &hint, &list) || !list) {
            if(ipaddr)
                delete[] ipaddr;
            ipaddr = new struct in6_addr[1];
            memset((void *)&ipaddr[0], 0, sizeof(ipaddr));
            return;
        }

        // Count the number of IP addresses returned
        addr_count = 0;
        first = list;
        while(list) {
            ++addr_count;
            list = list->ai_next;
        }

        // Allocate enough memory
        if(ipaddr)
            delete[] ipaddr;    // Cause this was allocated in base
        ipaddr = new struct in6_addr[addr_count];

        // Now go through the list again assigning to
        // the member ipaddr;
        list = first;
        int i = 0;
        while(list) {
            ip6addr = (struct sockaddr_in6 *)list->ai_addr;
            addr = &ip6addr->sin6_addr;
            if(validator)
                (*validator)(*addr);
            ipaddr[i++] = *addr;
            list = list->ai_next;
        }
        freeaddrinfo(first);
    }
}
Ejemplo n.º 2
0
int splitCIDR(network_t *n, char *fullstring, char *ip, char *cidr) {

	ip = strtok(fullstring,"/");
	cidr = strtok(NULL, "/");

	// Validity checks:
	if ( ip == NULL || cidr == NULL ) {
		return 2;
	}
	int i_cidr = atoi(cidr);
	if ( i_cidr > 32 || i_cidr < 1 ) {
		return 2;
	}
	if ( setIPAddress(n, ip) != 1 ) {
		return 3;
	}
	convertCIDRToNetmask(n, i_cidr);
	return 1;
}
Ejemplo n.º 3
0
 void InetAddress::set (unsigned long ip, unsigned short port)
 {
     setIPAddress(ip);
     setPortNumber(port);
 }
Ejemplo n.º 4
0
void IPV6Address::setAddress(const char *host)
{	
	if(hostname)
		delString(hostname);
	hostname = NULL;

	if(!host)  // The way this is currently used, this can never happen
		host = "::";

#ifdef	WIN32
	if(!stricmp(host, "localhost"))
		host = "::1";
#endif

	if(!setIPAddress(host))
	{
		struct hostent *hp;
		struct in6_addr **bptr;
#if defined(__GLIBC__)
		char   hbuf[8192];
		struct hostent hb;
		int    rtn;

		if(gethostbyname2_r(host, AF_INET6, &hb, hbuf, sizeof(hbuf), &hp, &rtn))
			hp = NULL;
#elif defined(sun)
		char   hbuf[8192];
		struct hostent hb;
		int    rtn;

		hp = gethostbyname2_r(host, AF_INET6, &hb, hbuf, sizeof(hbuf), &rtn);
#elif (defined(__osf__) || defined(_OSF_SOURCE) || defined(__hpux))
		hp = gethostbyname(host);
#elif defined(WIN32) && (!defined(_MSC_VER) || _MSC_VER < 1300)
		hp = gethostbyname(host);
#elif defined(WIN32)
		hp = gethostbyname2(host, AF_INET6);
#else
		mutex.enterMutex();
		hp = gethostbyname2(host, AF_INET6);
		mutex.leaveMutex();
#endif
		if(!hp)
		{
			if(ipaddr)
				delete[] ipaddr;
			ipaddr = new struct in6_addr[1];
			memset((void *)&ipaddr[0], 0, sizeof(ipaddr));
			return;
		}
		
		// Count the number of IP addresses returned
		addr_count = 0;
		for(bptr = (struct in6_addr **)hp->h_addr_list; *bptr != NULL; bptr++)
		{
			addr_count++;
		}

		// Allocate enough memory
		if(ipaddr)
			delete[] ipaddr;	// Cause this was allocated in base
		ipaddr = new struct in6_addr[addr_count];

		// Now go through the list again assigning to 
		// the member ipaddr;
		bptr = (struct in6_addr **)hp->h_addr_list;
		for(unsigned int i = 0; i < addr_count; i++)
		{
			if ( validator )
				(*validator)(*bptr[i]);
			ipaddr[i] = *bptr[i];
		}
	}
}
Ejemplo n.º 5
0
bool ofxEpilog::connect(string ip, bool live)
{
    setIPAddress(ip);
    
    ofLog(OF_LOG_VERBOSE, "ofxEpilog::connect() start cnnecting to the laser cutter.");
    
    if(tcp_client.isConnected())
    {
        ofLog(OF_LOG_WARNING, "ofxEpilog: the connection is created already.You should disconnect first." );
        disconnect();
    }
    
    keep_alive = live;
    bool is_connected = tcp_client.setup(ip_address, PRINTER_SERVICE_PORT, true);
    
    if(!is_connected)
        return false;
    
    ofLog() << "ofxEpilog:connect(): start sending LPR handshake packets.";
    
    //
    // Send LPR Handshake Packets
    // See http://www.rfc-editor.org/rfc/rfc1179.txt
    //
    std::ostringstream stream;
    /*
     +----+-------+----+
     | 02 | Queue | LF |
     +----+-------+----+
     Command code - 2
     Operand - Printer queue name
     */
    tcp_client.sendRaw("\002Legend\n");
    
    char response;
    tcp_client.receiveRawBytes(&response, 1);
    if(response == 0)
    {
        /*
         +----+-------+----+------+----+
         | 02 | Count | SP | Name | LF |
         +----+-------+----+------+----+
         Command code - 2
         Operand 1 - Number of bytes in control file
         Operand 2 - Name of control file
         */
        stream << "\002" << hostname.length()+2 << " cfA" << hostname << "\n";
        tcp_client.sendRaw(stream.str());
        
        tcp_client.receiveRawBytes(&response, 1);
    }
    else
        return false;
    
    if(response == 0)
    {
        /*
         +---+------+----+
         | H | Host | LF |
         +---+------+----+
         Command code - 'H'
         Operand - Name of host
         */
        stream.str("");
        stream.clear();
        stream << "H" << hostname << "\n" << '\0';
        tcp_client.sendRaw(stream.str());
        
        tcp_client.receiveRawBytes(&response, 1);
    }
    else
        return false;
    
    if(response == 0)
    {
        /*
         +----+-------+----+------+----+
         | 03 | Count | SP | Name | LF |
         +----+-------+----+------+----+
         Command code - 3
         Operand 1 - Number of bytes in data file
         Operand 2 - Name of data file
         */
        stream.str("");
        stream.clear();
        if(machine_profile.name == ofxEpilog::FUSION_32.name)
            stream << "\003" << "125899906843000" << " dfA" << hostname << "\n"; // <- buffer size is correct?
        
        if(machine_profile.name == ofxEpilog::MINI_18.name)
            stream << "\003" << "65536" << " dfA" << hostname << "\n"; // <- 64MB?
        
        tcp_client.sendRaw(stream.str());
        
        tcp_client.receiveRawBytes(&response, 1);
    }
    else
        return false;
    
    ofLog() << "ofxEpilog::connect(): LPR handshake complete.";
    
    return true;
}