Example #1
0
Sender::Sender(QString host,int port)
{
    connection = 0;
    //connect
    sock=new QTcpSocket(this);
    connect(sock,SIGNAL(connected()),this,SLOT(connSock()));
    connect(sock,SIGNAL(disconnected()),this,SLOT(dconSock()));
    connect(sock,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(dconSock()));
    connect(sock,SIGNAL(readyRead()),this,SLOT(readSock()));
    sock->connectToHost(host,port);
    qDebug("connected???");
}
bool PageNetTest::netTestStart_r( bool amThread, long num ) {
	long long endTime   = 0;
	long long calcTime  = 0;
	long      count;
	long      index     = 0;

	m_running = true;
	m_startTime = gettimeofdayInMilliseconds();
	endTime = gettimeofdayInMilliseconds();
	
	if( (m_sock[num] = openSock( num, m_type[num], &m_name[num], 
				     m_port[num])) == -1 ) 
		return false;	

	while( (endTime - m_startTime) < (m_testDuration * 1000) ) {
		count = 0;
		calcTime = gettimeofdayInMilliseconds();

		if( m_type[num] == TEST_READ ) 
			count = readSock( m_sock[num] );

		else if( m_type[num] == TEST_SEND )
			count = sendSock( m_sock[num] ); 

		endTime = gettimeofdayInMilliseconds();
		float secs = (endTime - calcTime)/1000.0;
		float mb   = (float)count * 8.0 / (1024.0 * 1024.0);
		float mbps = mb/secs;
		log( LOG_INFO, "net: nettest: took %lli ms to %s %li bytes at "
		     "%.2f Mbps", endTime - calcTime, 
		     (m_type[num] == TEST_READ)?"receive":"send", count, mbps );
		log( LOG_INFO, "net: nettest: run time %lli s", 
		     (endTime-m_startTime)/1000 );

		m_calcTable[num][index] = (unsigned long)mbps;
		if( ++index >= AVG_TABLE_SIZE ) index = 0;

		if( !m_runNetTest ) break;
	}

	m_sock[num] = closeSock( m_sock[num] );	
	return true;
}
Example #3
0
void tapHandler(void* tapSock) {


	/*Get tap fd and mac address*/
	localTapMac = malloc(  sizeof(uint8_t)*6  );
	if ( (tap_fd = allocate_tunnel(TAP_NAME, IFF_TAP | IFF_NO_PI, localTapMac)) < 0 ) {
		perror("tapHandler");
		exit(1);
	}

	int socket_fd = (int)tapSock;
	int ret_status;
	unsigned short int data_size = 0;



  /*Read from tap */
  while(globalStatus)
  {
    char* tapPayload = malloc(sizeof(char) * PAYLOAD_SIZE);

    data_size = readSock(tap_fd, &tapPayload, PAYLOAD_SIZE);

    if(data_size == 0) {
      continue;
    }

    /*SNIP*/

    /* Create a Data packet */
    struct data_packet* pktFromTap = malloc(sizeof(struct data_packet));
    pktFromTap->head.packet_type = 0xABCD;
    pktFromTap->head.packet_length = data_size;
    pktFromTap->datagram = tapPayload;

   /*Send from tap to dest*/
    broadcast(routesList, (void*)pktFromTap);
  }
}
Example #4
0
int main(int argc, char *argv[])
{
  int sockfd,
      portno,
      n,
      receivedNum = 0, // int representing the data size sent
      returnStatus,    // value returned from read or write
      dataSizeNum,
      convertedNum,
      connectStatus;
  struct sockaddr_in serv_addr;
  struct hostent *server;        // Defines a host computer
  char txtBuffer[BUFF_SIZE],
       keyBuffer[BUFF_SIZE],
       ciphBuffer[BUFF_SIZE];
  FILE* filePtr;

  // Check for correct arguments
  if (argc < 4)
  {
    fprintf(stderr, "usage: %s plaintext key port\n", argv[0]);
    exit(1);
  }

  // Read the plaintext file
  filePtr = fopen(argv[1], "r");
  if (filePtr == NULL)
  {
    fprintf(stderr, "could not open plaintext file\n");
    exit(1);
  }
  bzero(txtBuffer, BUFF_SIZE);
  fgets(txtBuffer, BUFF_SIZE, filePtr);
  fclose(filePtr);

  // Read the key file
  filePtr = fopen(argv[2], "r");
  if (filePtr == NULL)
  {
    fprintf(stderr, "could not open key file\n");
    exit(1);
  }
  bzero(keyBuffer, BUFF_SIZE);
  fgets(keyBuffer, BUFF_SIZE, filePtr);
  fclose(filePtr);

  // Check for bad characters or if key file is too short
  if (strlen(keyBuffer) < strlen(txtBuffer))
  {
    fprintf(stderr, "ERROR: key %s is too short\n", argv[2]);
    exit(1);
  }

  if (!validChars(txtBuffer))
  {
    fprintf(stderr, "ERROR: bad characters in %s\n", argv[1]);
    exit(1);
  }
  if (!validChars(keyBuffer))
  {
    fprintf(stderr, "ERROR: bad characters in %s\n", argv[2]);
    exit(1);
  }

  /******** Connect to server ********/

  // Set port number and host name
  portno = atoi(argv[3]);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  server = gethostbyname("localhost");
  if (server == NULL)
  {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
  }

  // Set server address
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr,
        (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(portno);

  // Connect to the server
  if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on initial connect");

  // Check if trying to connect to otp_dec_d; if so, reject
  returnStatus = read(sockfd, &receivedNum, sizeof(receivedNum));
  if (returnStatus > 0)
  {
    receivedNum = ntohl(receivedNum);
    if (receivedNum == 2)
    {
      fprintf(stderr, "ERROR: could not contact otp_dec_d on port %s\n",
              argv[3]);
      exit(2);
    }
  }

  // Receive the new port number from server after initial connect
  returnStatus = read(sockfd, &receivedNum, sizeof(receivedNum));
  if (returnStatus > 0)
    receivedNum = ntohl(receivedNum);
  else
    fprintf(stderr, "ERROR receiving port number: %d\n", returnStatus);

  // Restart socket on new port number

  close(sockfd);
  // Set port number and host name
  portno = receivedNum; // set to new port number
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  server = gethostbyname("localhost");
  if (server == NULL)
  {
    fprintf(stderr,"ERROR, no such host\n");
    exit(1);
  }
  // Set server address
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr,
        (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(portno);
  // Connect to the server
  do
  {
  connectStatus =
    connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  if (connectStatus < 0)
    perror("ERROR on secondary connect");
  } while (connectStatus < 0);

  /******** Begin data exchange with server *********/

  // Write the data size of plaintext to the socket
  dataSizeNum = strlen(txtBuffer);
  convertedNum = htonl(dataSizeNum);
  returnStatus = write(sockfd, &convertedNum, sizeof(convertedNum));
  if (returnStatus < 0)
    error("ERROR writing data size");
  // Write plaintext to the socket
  writeSock(sockfd, txtBuffer);

  // Write the data size of the key to the socket
  dataSizeNum = strlen(keyBuffer);
  convertedNum = htonl(dataSizeNum);
  returnStatus = write(sockfd, &convertedNum, sizeof(convertedNum));
  if (returnStatus < 0)
    error("ERROR writing data size");
  // Write key to the socket
  writeSock(sockfd, keyBuffer);

  // Read the data size of ciphertext
  returnStatus = read(sockfd, &receivedNum, sizeof(receivedNum));
  if (returnStatus > 0)
    receivedNum = ntohl(receivedNum);
  else
    fprintf(stderr, "ERROR receiving data size: %d\n", returnStatus);
  // Read ciphertext from the socket
  readSock(sockfd, ciphBuffer, receivedNum);

  printf("%s", ciphBuffer);

  close(sockfd);
  return 0;
}
Example #5
0
void Dijkstra(int **graph, int source, int vert, int destination,int sock)
{
	int *dist = (int*)malloc(vert * sizeof(int*));
	int *prev = (int*)malloc(vert * sizeof(int*));
	int *visited = (int*)malloc(vert * sizeof(int*));
	int curr = source;
	int i = 0;
	while( i < vert)
	{
		dist[i] = 99999;
		prev[i] = -1;
		visited[i] = 0;
		i++;

	}
	dist[source] = 0;
	i = 0;
	while(i < vert)
	{
		visited[curr] = 1;
		int w = 0;
		while(w < vert)
		{
			if((graph[curr][w] != 0) && (graph[curr][w] != 99999) && (graph[curr][w] + dist[curr] < dist[w]) && (visited[w] != 1))
			{
				dist[w] = dist[curr] + graph[curr][w];
				prev[w] = curr;
			}
			w++;
		}
		curr = lowestIndex(dist,visited,vert);
		i++;
	}
	int buffer[vert];
	int tbuffer[vert];
	int count = 0;
	int dest = 0;
	int temp = destination;
	i = 0;
	if(dist[destination] != 99999)
	{
		writeSock(sock,-1);
		printf("%d to %d: ",source,destination);
		while(dest != -1)
		{
			buffer[count] = temp;
			dest = prev[temp];
			temp = dest;
			count++;
		}
		i = count-1;
		while( i >= 0)
		{
			printf("%d... ", buffer[i]);
			i--;
		}
		printf("->%d\n",dist[destination]);
		exit(0);
	}
	else
	{
		sleep(2);
		writeSock(sock,destination);
		temp = 8;
		while(dest != -1)
		{
			buffer[count] = temp;
			dest = prev[temp];
			temp = dest;
			count++;
		}
		i = 0;
		dist[destination] = readSock(sock);
		int t = readSock(sock);
		while(i < t)
		{
			tbuffer[i] = readSock(sock);
			i++;
		}
		i = t;
		temp = 0;
		while(i < (count +t))
		{
			tbuffer[i] = buffer[temp];
			temp++;
			i++;
		}
		i = (count + t) - 1;
		while(i >= 0)
		{
			printf("%d..", tbuffer[i]);
			i--;
		}
		printf("->%d\n",dist[8] + dist[destination]);
	}
}
Example #6
0
int Peer::incomeHandler(int peer_num){
	int sock = this->peer_connection[peer_num].peer_sock;
	int read_len;
	unsigned int head_4_bytes;
	char buffer[36000];
	memset(buffer, 36000, 0);
	readSock(sock, &head_4_bytes, 4, &read_len);

	readSock(sock, buffer, 1, &read_len);

	if(buffer[0] <= '8' && buffer[0] >= '0'){                        //if the fifth char of the message indicate a normal message type

		if(this->log == 1){
			char log[200];
			sprintf(log, "[%d] RECV A %s MSG FROM %s", (int)this->getCurrentTime(), this->getMsgType(buffer[0]).c_str(), this->peer_connection[peer_num].peer_name);
			this->writeToLogFile(log);
		}
		unsigned int len = head_4_bytes;
		if(len > 1)
			readSock(sock, buffer+1, len - 1, &read_len);
		this->msgHandler(buffer, len, peer_num);
		return 0;
	}
	else if(buffer[0] == 'T'){
		readSock(sock, buffer+1, 15, &read_len);
		char head_20[20];
		char handshake_head_20[20];
		char hs_0 = (char)19;
		char hs_1_19[] = "BitTorrent Protocol";
		memcpy(handshake_head_20, &hs_0, 1);
		memcpy(handshake_head_20+1, hs_1_19, 19);
		memcpy(head_20, &head_4_bytes, 4);
		memcpy(head_20+4, buffer, 16 );
		handshake_head_20[20] = '\0';
		head_20[20] = '\0';

		if(strcmp(head_20, handshake_head_20) == 0)  //we have a handshake message
		{
			//test
			if(this->verbose){
				std::cout << "RECV A HANDSHAKE MSG FROM " << this->peer_connection[peer_num].peer_name << std::endl;
			}
			if(this->log){
				char log[200];
				sprintf(log, "[%d] RECV A HANDSHAKE MSG FROM %s", (int)this->getCurrentTime(), this->peer_connection[peer_num].peer_name);
				this->writeToLogFile(log);
			}
			//test
			memset(buffer, 0, sizeof(buffer));
			readSock(sock, buffer, 8, &read_len);
			readSock(sock, buffer, 20, &read_len);
			readSock(sock, buffer+20, 20, &read_len);
			if(this->type == 0){
				this->assemSeederHandShake();
				this->sendHandShakeMsg(sock);
				return 0;
			}
			else{
				//std::cout<<"leecher reaceived a send-back handshake message" << std::endl;
				char info_hash[20];
				char id[20];
				char info_hash_local[20];
				char id_local[20];
				memcpy(info_hash, buffer, 20);
				memcpy(id, buffer+20, 20);
				memcpy(info_hash_local, this->info_hash, 20);
				memcpy(id_local, this->peer_connection[peer_num].peer_id, 20);

				info_hash_local[20] = '\0';
				info_hash[20] = '\0';
				id[20] = '\0';
				id_local[20] = '\0';

				int check_info_hash;
				int check_id;


				check_info_hash = strcmp(info_hash, info_hash_local);
				check_id = strcmp(id, id_local);
				if(check_info_hash != 0 || check_id != 0){   //if the info_hash or id information don't match the handshake, the leecher will disconnect
					if(this->verbose){
						std::cout << "HANDSHAKE FAILED WITH " << this->peer_connection[peer_num].peer_name << std::endl;
					}
					if(this->log){
						char log[200];
						sprintf(log, "[%d] HANDSHAKE FAILED WITH peer: %s", (int)this->getCurrentTime(), this->peer_connection[peer_num].peer_name);
						this->writeToLogFile(log);

					}
					close(this->peer_connection[peer_num].peer_sock);
					this->peer_connection[peer_num].active = 0;
					return 1;
				}
				else{								//if the info_hash and id infomation of the handshake match, the leecher will do..
					if(this->verbose){
						std::cout << "HANDSHAKE FAILED WITH " << this->peer_connection[peer_num].peer_name << std::endl;
					}
					if(this->log){
						char log[200];
						sprintf(log, "[%d] HANDSHAKE SUCCEED WITH peer: %s", (int)this->getCurrentTime(), this->peer_connection[peer_num].peer_name);
						this->writeToLogFile(log);
					}
					if(this->verbose){
						std::cout << "Checking if the client is intersted in the file" << std::endl;
					}
					if(this->getNextIndex() >= 0){
						if(this->verbose){
							std::cout << "SEND INTERESTED MSG TO " << this->peer_connection[peer_num].peer_name << std::endl;
						}
						if(this->log){
							char log[200];
							sprintf(log, "[%d] SEND INTERESTED MSG TO %s", (int)this->getCurrentTime(), this->peer_connection[peer_num].peer_name);
							this->writeToLogFile(log);
						}
						this->sendInterestMsg(this->peer_connection[peer_num].peer_sock);
					}
					else{
						std::cout << "NOT INTERESTED IN THE FILE, DISCONNECT" << std::endl;
						if(this->log){
							char log[200];
							sprintf(log, "[%d] NOT INTERESTED IN THE FILE, DISCONNECT FROM peer: %s", (int)this->getCurrentTime(), this->peer_connection[peer_num].peer_name);
							this->writeToLogFile(log);
						}
						this->peerClose(peer_num);
						exit(0);
					}

				}
			}
		}
		else{
			Helper::LiveWithUserMessage("incomeHandler()", "encounter an unknow income message(expecting a handshake)");
			return -1;
		}
	}
	else{
		Helper::LiveWithUserMessage("incomeHandler()", "encounter an unknow income message");
		return -2;
	}
	return -3;
}
Example #7
0
void Dijkstra(int **graph, int source, int vert,int sock)
{
	int destination;
	int *dist = (int*)malloc(vert * sizeof(int*));
	int *prev =(int*)malloc(vert * sizeof(int*));
	int *visited =(int*)malloc(vert * sizeof(int*));
	int curr = source;
	int i = 0;
	while( i < vert)
	{
		dist[i] =99999;
		prev[i] = -1;
		visited[i] = 0;
		i++;
	}
	dist[source] = 0;
	i = 0;
	while(i < vert)
	{
		
		visited[curr] = 1;
		int w = 0;
		while(w < vert)
		{
			if((graph[curr][w] != 0) && (graph[curr][w] != 99999) && (graph[curr][w] + dist[curr] < dist[w]) && (visited[w] != 1))
			{
				dist[w] = dist[curr] + graph[curr][w];
				prev[w] = curr;
		
			}
			w++;
		}
		curr = lowestIndex(dist,visited,vert);
		i++;
	}
	destination = readSock(sock);
	if(destination < 0)
	{
		exit(0);
	}
	else
	{
		int buffer[vert];
		int count = 0;
		int dest = 0;
		int temp = destination;
		while(dest != -1)
		{
			buffer[count] = temp;
			dest = prev[temp];
			temp = dest;
			count++;
		}
		readSock(sock);
		sleep(1);
		writeSock(sock,dist[destination]);
		writeSock(sock,count);
		sleep(1);
		i = 0;
		sleep(1);
		while(i < count)
		{
			writeSock(sock,buffer[i]);
			i++;
		}
	}
}