Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > e7402dc2d35e679dfa225853dfccfb35 > files > 62

UpTools-devel-8.6.2-1.fc15.i686.rpm

/* UpTools v8.6
 *
 * Copyright (c) 2005-2012 Fundacion Universidad de Palermo (Argentina).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment: 'This product includes software developed by the
 *    "Universidad de Palermo, Argentina" (http://www.palermo.edu/).'
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <cerrno>
#include <iostream>
#include <cstring>
#include <UpTools/UpInet.h>

int main() {

	/////////////////////////////////////////////////////////////////////////
	// UpAddress test

	std::cout << "--UpAddress ipv4 test-------------------------------------\n";
	std::string str1("127.0.0.1"),str2("200.69.193.1"),str3("100.300.0.1");
	UpAddress a1(str1),a2(str2),a3(str3);
	UpAddress a1b(a1),a2b,a3b,a1c,a2c,a3c;
	a2b=a2;
	a3b=a3.c_str();
	a1c=*(in_addr*)a1b.ptr();
	a2c.set("0.0.0.0",AF_INET);a2b.write(a2c.ptr());
	a3c.set(a3b.c_str(),AF_INET);
	bool opeq1=(a1==a1b);
	bool opeq2=(a1==a2);
	bool opless1=(a1<a2);
	bool opmore1=(a1>a2);
	std::cout <<
	"std::string str1(\"127.0.0.1\"),str2(\"200.69.193.1\"),str3(\"100.300.0.1\");\n"
	"UpAddress a1(str1),a2(str2),a3(str3);\n"
	"UpAddress a1b(a1),a2b,a3b,a1c,a2c,a3c;\n"
	"a2b=a2;\n"
	"a3b=a3.c_str();\n"
	"a1c=(in_addr*)a1b.ptr();\n"
	"a2c.set(AF_INET,\"0.0.0.0\");a2b.write(a2c.ptr());\n"
	"a3c.set(AF_INET,a3b.c_str());\n"
	"bool opeq1=(a1==a1b);\n" 
	"bool opeq2=(a1==a2);\n"
	"bool opless1=(a1<a2);\n"
	"bool opmore1=(a1>a2);\n"
	<<"a1="<<a1<<" a2="<<a2<<" a3="<<a3<<"\n"
	<<"a1b="<<a1b<<" a2b="<<a2b<<" a3b="<<a3b<<"\n"
	<<"a1c="<<a1c<<" a2c="<<a2c<<" a3c="<<a3c<<"\n"
	<<"opeq1="<<opeq1<<" opeq2="<<opeq2<<" opless1="<<opless1<<" opmore1="<<opmore1
	<<std::endl<<std::endl;
	
	std::cout << "--UpAddress ipv6 test-------------------------------------\n";
	std::string str4("abcd:4001:1234:abcd::1111:1"),str5("abcd:4001:abcd:1234::1111:1"),str6("::127.0.0.1");
	UpAddress a4(str4),a5(str5),a6(str6);
	UpAddress a4b(a4),a5b,a6b,a4c,a5c,a6c;
	a5b=a5;
	a6b=a6.c_str();
	a4c=*(in6_addr*)a4b.ptr();
	a5c.set("::0",AF_INET6);a5b.write(a5c.ptr());
	a5b.write(a5c.ptr());
	a6c.set(a6b.c_str(),AF_INET6);
	bool opeq4=(a4==a4b);
	bool opeq5=(a4==a5);
	bool opless2=(a1<a2);
	bool opmore2=(a1>a2);
	std::cout<<
	"std::string str4(\"abcd:4001:1234:abcd::1111:1\"),str5(\"abcd:4001:abcd:1234::1111:1\"),str6(\"::127.0.0.1\");\n"
	"UpAddress a4(str4),a5(str5),a6(str6);\n"
	"UpAddress a4b(a4),a5b,a6b,a4c,a5c,a6c;\n"
	"a5b=a5;\n"
	"a6b=a6.c_str();\n"
	"a4c=(in6_addr*)a4b.ptr();\n"
	"a5c.set(AF_INET6,\"::0\");a5b.write(a5c.ptr());\n"
	"a6c.set(AF_INET6,a6b.c_str();\n"
	"bool opeq4=(a4==a4b);\n"
	"bool opeq5=(a4==a5);\n"
	"bool opless2=(a1<a2);\n"
	"bool opmore2=(a1>a2);\n"
	<<"a4="<<a4<<" a5="<<a5<<" a6="<<a6<<"\n"
	<<"a4b="<<a4b<<" a5b="<<a5b<<" a6b="<<a6b<<"\n"
	<<"a4c="<<a4c<<" a5c="<<a5c<<" a6c="<<a6c<<"\n"
	<<"opeq4="<<opeq4<<" opeq5="<<opeq5<<" opless2="<<opless2<<" opmore2="<<opmore2
	<<std::endl<<std::endl;
	


	
	

	
	/////////////////////////////////////////////////////////////////////////
	// UpHost test
	std::cout<<"--UpHost test--------------------------------------"<<std::endl;

	std::string hstr1("www.google.com"), hstr2("localhost");
	UpHost h1(hstr1), h2(hstr2), h3("10.0.0.251"), h4("www.baddomain.combi");
	UpHost h1b(h1), h2b, h3b, h4b;
	h2b=h2;
	h3b=h3.hostName();
	h4b=h4;
	std::cout<<
	"std::string hstr1(\"www.google.com\"), hstr2(\"localhost\");\n"
	"UpHost h1(hstr1), h2(hstr2), h3(\"10.0.0.251\"), h4(\"www.baddomain.combi\");\n"
	"UpHost h1b(h1), h2b, h3b;\n"
	"h2b=h2;\n"
	"h3b=h3.hostName();\n"
	"h4b=h4;\n\n";
	std::cout<< "h1.status():"<<h1.status()<<" h1.addrSize():"<<h1.addrSize()<<
	" h1.hostName:"<<h1.hostName()<<" h1.address():"<<h1.address()<<"\n"<<
	" h1.#aliases:"<<h1.aliases().size()<<" h1.#addresses:"<<h1.addresses().size()<<"\n\n"<<
	"h2.status():"<<h2.status()<<" h2.addrSize():"<<h2.addrSize()<<
	" h2.hostName:"<<h2.hostName()<<" h2.address():"<<h2.address()<<"\n"<<
	" h2.#aliases:"<<h2.aliases().size()<<" h2.#addresses:"<<h2.addresses().size()<<"\n\n"<<
	"h3.status():"<<h3.status()<<" h3.addrSize():"<<h3.addrSize()<<
	" h3.hostName:"<<h3.hostName()<<" h3.address():"<<h3.address()<<"\n"<<
	" h3.#aliases:"<<h3.aliases().size()<<" h3.#addresses:"<<h3.addresses().size()<<"\n\n"<<
	"h4.status():"<<h4.status()<<" h4.addrSize():"<<h4.addrSize()<<
	" h4.hostName:"<<h4.hostName()<<" h4.address():"<<h4.address()<<"\n"<<
	" h4.#aliases:"<<h4.aliases().size()<<" h4.#addresses:"<<h4.addresses().size()<<"\n\n"<<
	"\nh1b:\n"<<h1b<<
	"\nh2b:\n"<<h2b<<
	"\nh3b:\n"<<h3b<<
	"\nh4b:\n"<<h4b<<
	std::endl<<std::endl;


	/////////////////////////////////////////////////////////////////////////
	// UpSockAddr test
	std::cout<<"--UpSockAddr test--------------------------------------"<<std::endl;

	UpSockAddr d1("127.0.0.1 80"), d2("abcd::321a 636"), d3("/tmp/sockunix");
	UpSockAddr d4("192.168.0.1"), d5("xcvbxcv.com 22"), d6("300.56.2.2",56);
	UpSockAddr d1b(d1.ptr()), d2b((sockaddr_in6*)d2.ptr()), d3b;
	UpSockAddr d4b, d5b, d6b;
	d3b = d3.c_str();
	d4b = d4.ptr();
	d5b = d5.string();
	d6b = d6;
	UpSockAddr d1c, d2c, d3c;
	d1c.hostPort("localhost 80");
	d2c.hostPort("abcd::321a",636);
	d3c.set("a",0,AF_UNIX); d3.write(d3c.ptr());
	
	std::cout <<	
	"UpSockAddr d1(\"127.0.0.1 80\"), d2(\"abcd::321a 636\"), d3(\"/tmp/sockunix\");\n"
	"UpSockAddr d4(\"192.168.0.1\"), d5(\"xcvbxcv.com 22\"), d6(\"300.56.2.2\",56);\n"
	"UpSockAddr d1b(d1.ptr()), d2b((sockaddr_in6*)d2.ptr()), d3b;\n"
	"UpSockAddr d4b, d5b, d6b;\n"
	"d3b = d3.c_str();\n"
	"d4b = d4.ptr();\n"
	"d5b = d5.string();\n"
	"d6b = d6;\n"
	"UpSockAddr d1c, d2c;\n"
	"d1c.hostPort(\"localhost 80\");\n"
	"d2c.hostPort(\"abcd::321a\",636);\n"
	"d3c.set(AF_UNIX,\"a\"); d3.write(d3c.ptr());\n"
	<< "\n\nd1="<<d1<< "    d2="<<d2<<   "    d3="<<d3 << "    d4="<<d4<< "    d5="<<d5<<   "    d6="<<d6
	<< "\n\nd1b="<<d1b<<"    d2b="<<d2b<<"    d3b="<<d3b << "    d4b="<<d4b<<"    d5b="<<d5b<<"    d6b="<<d6b
	<< "\n\nd1c="<<d1c<<"    d2c="<<d2c<<"    d3c="<<d3c
	<< std::endl << std::endl;
	





	/////////////////////////////////////////////////////////////////////////
	// UpSocket test
	// UpSelect
	std::cout<<"--UpSocket test--------------------------------------"<<std::endl;
	UpSocket sq;

	if( ! sq.bindInterface("eth0", 2000,AF_INET) ) {
		std::cerr << "error binding to " << sq.getLocalSockAddr() << std::endl;
		return 1;
	}
	if( ! sq.listen(10) ) {
		std::cerr << "error seting the listen queue to socket" << std::endl;
		return 1;
	}

	UpSocket sp;
	UpTimeVal maxRecvWait(10.5);
	sp.setRecvTimeout(maxRecvWait);
	sq.setRecvTimeout(maxRecvWait);
	std::cout << "setting receive timeout for sockets = " << maxRecvWait << std::endl;

	std::cout << "accepting connections at " << sq.getLocalSockAddr()
				 << std::endl;
	if( ! sq.accept(sp) ) {
		std::cerr << "error accepting connections at " << sq.getLocalSockAddr()
					 << std::endl;
		return 1;
	} else {
		std::cout << "connection accepted from " << sp.getRemoteSockAddr()
					 << std::endl;
	}

	std::cout << "expecting data from connection..." << std::endl;

	char line[10240];

	while(1) {
		int n = sp.read(line,10240);
		if(n < 0 ) {
			std::cerr << "sd " << sp.fd() << " read error"<< std::endl;
			return 1;
		}
		if( n==0 ) {
			std::cerr << "sd " <<  sp.fd()
					 << " zero bytes read: eof" << std::endl;
			return 1;
		}
		line[n]='\0';
			std::cout << "read from sd " << sp.fd() << ":\n"
						 << std::endl << line << std::endl;
	}

	/////////////////////////////////////////////////////////////////////////

	return 0;
}