Пример #1
0
void peisk_acceptTCPConnections() {
  int sin_size=sizeof(struct sockaddr_in);
  int sock;
  struct sockaddr_in peerAddr;
  PeisConnection *connection;
  PeisConnectMessage message;

  if(!peiskernel.tcp_isListening)  return;

  sock=accept(peiskernel.tcp_serverSocket,(struct sockaddr *)&peerAddr,(socklen_t*) &sin_size);
  if(sock <= 0) return;

  if(peisk_printLevel & PEISK_PRINT_CONNECTIONS)
    printf("peisk: incomming TCP connection ...\n");
  
  if(fcntl(sock,F_SETFL,O_NONBLOCK) == -1) {
    fprintf(stderr,"peisk: error setting socket nonblocking, closing connection\n");
    close(sock);
    return;
  }
  
  /* Create connection structure to use */
  connection = peisk_newConnection();
  if(!connection) return;
  
  /* Wait up to 200ms for data - otherwise drop connection */    
  if(!peisk_recvBlocking(sock,&message,sizeof(message),0,0.2) && (peisk_printLevel & PEISK_PRINT_CONNECTIONS)) {
    printf("Timeout on incomming connection (1)\n");
    peisk_freeConnection(connection);
    close(sock); return;
  }
  /* Check that it's ok to accept this connection */
  if(peisk_verifyConnectMessage(connection,&message) != 0) {
    peisk_freeConnection(connection);
    close(sock); return;
  }
    
  /* Store TCP information in connection */
  connection->type = eTCPConnection;
  connection->connection.tcp.socket=sock;
  
  /* Let P2P layer handle this connection */    
  peisk_incommingConnectFinished(connection,&message);
    
  if(peisk_printLevel & PEISK_PRINT_CONNECTIONS)
    printf("peisk: accepted new connection to %d with index: %d, flags=%d\n",
	   message.id,connection->id,message.flags);
  
}
Пример #2
0
void peisk_bluetoothAcceptConnections() {
  int i,len;
  PeisBluetoothAdaptor *adaptor;
  socklen_t opt = sizeof(struct sockaddr_l2);
  char str[256];
  PeisConnectMessage message;
  PeisConnection *connection;

  for(i=0;i<peisk_nBluetoothAdaptors;i++) {
    adaptor = &peisk_bluetoothAdaptors[i];
    if(adaptor->incomming.mode == 0) {
      adaptor->incomming.socket = accept(adaptor->listenSocket,(struct sockaddr*) &adaptor->incomming.remoteAddr, &opt);
      if(adaptor->incomming.socket == -1) continue;
      printf("Setting MTU of incomming socket\n");
      peisk_bluetooth_setMTU(adaptor->incomming.socket);

#ifdef OLD
      struct l2cap_options opts;
      struct l2cap_conninfo conn;
      socklen_t optlen;

      printf("Setting MTU of incomming socket\n");
      memset(&opts, 0, sizeof(opts));
      optlen = sizeof(opts);
      if (getsockopt(adaptor->incomming.socket, SOL_L2CAP, L2CAP_OPTIONS, &opts, &optlen) < 0) {
	perror("Can't get default L2CAP options");
      }

      /* Set new options */
      printf("Old L2CAP MTU: %d %d\n",opts.omtu,opts.imtu);
      opts.omtu = PEISK_MAX_PACKAGE_SIZE;
      opts.imtu = PEISK_MAX_PACKAGE_SIZE;
      /*        if (rfcmode > 0)
		opts.mode = rfcmode;*/
#endif

      ba2str(&adaptor->incomming.remoteAddr.l2_bdaddr,str);
      printf("Incomming connection from: %s\n",str);

      if(fcntl(adaptor->incomming.socket,F_SETFL,O_NONBLOCK) != 0) {
	perror("Failed to set incomming socket nonblocking\n");
      }

      adaptor->incomming.timeout = peisk_timeNow + 5.0;
      adaptor->incomming.mode = 1;
    }
    if(adaptor->incomming.mode == 1) {
      len = read(adaptor->incomming.socket,(void*)&message,sizeof(message));
      if(len == sizeof(message)) {
	/* Create connection structure to use */
	printf("Received connection message:\n"); peisk_hexDump(&message,len); printf("\n");
	connection = peisk_newConnection();
	if(!connection) {
	  fprintf(stderr,"peisk::error - failed to create connection structure\n");
	  return;
	}
	if(peisk_verifyConnectMessage(connection,&message) != 0) {
	  printf("do not accept him\n");
	  peisk_abortConnect(connection);
	  close(adaptor->incomming.socket);
	  adaptor->incomming.mode = 0;
	  continue;
	}
	connection->type = eBluetoothConnection;
	connection->connection.bluetooth.socket = adaptor->incomming.socket;
	connection->connection.bluetooth.adaptor = adaptor;

	printf("connection %x accepted\n",(int)connection);

	/* Let P2P layer handle this connection */
	peisk_incommingConnectFinished(connection,&message);

	if(1 || peisk_printLevel & PEISK_PRINT_CONNECTIONS)
	  printf("peisk: accepted new incomming BLUETOOTH connection to %d with index: %d, flags=%d\n",
		 message.id,connection->id,message.flags);

	adaptor->incomming.mode = 0;
      } else if(len > 0) {
	fprintf(stderr,"Warning, received incorrect length on incomming bluetooth connection\n");
	close(adaptor->incomming.socket);
	adaptor->incomming.mode = 0;
	continue;
      }
    }
    if(adaptor->incomming.mode == 1 && adaptor->incomming.timeout < peisk_timeNow) {
      /* peisk_abortConnect(adaptor->outgoing.connection); ?? */
      peisk_freeConnection(connection);
      close(adaptor->incomming.socket);
      adaptor->incomming.mode = 0;
    }
  }
}