コード例 #1
0
			//	Function responsible of connecting the socket.
			inline bool	TCPSocket::connect()
			{
				bool	return_value = false;



				lock_ref().enter();

				if ( socket_ref() != NULL  &&  address_ref() != "" )
				{
					create_socket_info();

					if ( WSAConnect(socket_ref(),reinterpret_cast<sockaddr*>(&socket_info_ref()),sizeof(socket_info_ref()),NULL,NULL,NULL,NULL) == 0 )
						return_value = true;
				}

				lock_ref().leave();


				return return_value;
			};
コード例 #2
0
NetworkInterface* networkinterface_new(Address* address, guint64 bwDownKiBps, guint64 bwUpKiBps,
		gboolean logPcap, gchar* pcapDir, gchar* qdisc, guint64 interfaceReceiveLength) {
	NetworkInterface* interface = g_new0(NetworkInterface, 1);
	MAGIC_INIT(interface);

	interface->address = address;
	address_ref(interface->address);

	/* interface speeds */
	interface->bwUpKiBps = bwUpKiBps;
	gdouble bytesPerSecond = (gdouble)(bwUpKiBps * 1024);
	interface->timePerByteUp = (gdouble) (((gdouble)SIMTIME_ONE_SECOND) / bytesPerSecond);
	interface->bwDownKiBps = bwDownKiBps;
	bytesPerSecond = (gdouble)(bwDownKiBps * 1024);
	interface->timePerByteDown = (gdouble) (((gdouble)SIMTIME_ONE_SECOND) / bytesPerSecond);

	/* incoming packet buffer */
	interface->inBuffer = g_queue_new();
	interface->inBufferSize = interfaceReceiveLength;

	/* incoming packets get passed along to sockets */
	interface->boundSockets = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, descriptor_unref);

	/* sockets tell us when they want to start sending */
	interface->rrQueue = g_queue_new();
	interface->fifoQueue = priorityqueue_new((GCompareDataFunc)_networkinterface_compareSocket, NULL, descriptor_unref);

	/* parse queuing discipline */
	if (qdisc && !g_ascii_strcasecmp(qdisc, "rr")) {
		interface->qdisc = NIQ_RR;
	} else {
		interface->qdisc = NIQ_FIFO;
	}

	/* open the PCAP file for writing */
	interface->logPcap = logPcap;
	interface->pcapDir = pcapDir;
	interface->pcapFile = NULL;
	if(interface->logPcap) {
		GString *filename = g_string_new("");
		if (interface->pcapDir) {
			g_string_append(filename, interface->pcapDir);
			/* Append trailing slash if not present */
			if (!g_str_has_suffix(interface->pcapDir, "/")) {
				g_string_append(filename, "/");
			}
		} else {
			/* Use default directory */
			g_string_append(filename, "data/pcapdata/");
		}
		/* log status */
		g_string_append_printf(filename, "%s-%s.pcap", address_toHostName(interface->address), address_toHostIPString(interface->address));
		interface->pcapFile = fopen(filename->str, "w");
		if(!interface->pcapFile) {
			warning("error trying to open PCAP file '%s' for writing", filename->str);
		} else {
			_networkinterface_pcapInit(interface);
		}
	}

	info("bringing up network interface '%s' at '%s', %"G_GUINT64_FORMAT" KiB/s up and %"G_GUINT64_FORMAT" KiB/s down using queuing discipline %s",
			address_toHostName(interface->address), address_toHostIPString(interface->address), bwUpKiBps, bwDownKiBps,
			interface->qdisc == NIQ_RR ? "rr" : "fifo");

	return interface;
}