void forwarding_actor_proxy::enqueue(mailbox_element_ptr what,
                                     execution_unit*) {
  CAF_PUSH_AID(0);
  CAF_ASSERT(what);
  forward_msg(std::move(what->sender), what->mid,
              what->move_content_to_message(), &what->stages);
}
bool forwarding_actor_proxy::link_impl(linking_operation op,
                                       abstract_actor* other) {
  switch (op) {
    case establish_link_op:
      if (establish_link_impl(other)) {
        // causes remote actor to link to (proxy of) other
        // receiving peer will call: this->local_link_to(other)
        forward_msg(ctrl(), invalid_message_id,
                    make_message(link_atom::value, other->address()));
        return true;
      }
      break;
    case remove_link_op:
      if (remove_link_impl(other)) {
        // causes remote actor to unlink from (proxy of) other
        forward_msg(ctrl(), invalid_message_id,
                    make_message(unlink_atom::value, other->address()));
        return true;
      }
      break;
    case establish_backlink_op:
      if (establish_backlink_impl(other)) {
        // causes remote actor to unlink from (proxy of) other
        forward_msg(ctrl(), invalid_message_id,
                    make_message(link_atom::value, other->address()));
        return true;
      }
      break;
    case remove_backlink_op:
      if (remove_backlink_impl(other)) {
        // causes remote actor to unlink from (proxy of) other
        forward_msg(ctrl(), invalid_message_id,
                    make_message(unlink_atom::value, other->address()));
        return true;
      }
      break;
  }
  return false;
}
bool remote_actor_proxy::link_impl(linking_operation op,
                                   const actor_addr& other) {
  switch (op) {
    case establish_link_op:
      if (establish_link_impl(other)) {
        // causes remote actor to link to (proxy of) other
        // receiving peer will call: this->local_link_to(other)
        forward_msg(address(), invalid_message_id,
                    make_message(atom("_Link"), other));
        return true;
      }
      return false;
    case remove_link_op:
      if (remove_link_impl(other)) {
        // causes remote actor to unlink from (proxy of) other
        forward_msg(address(), invalid_message_id,
                    make_message(atom("_Unlink"), other));
        return true;
      }
      return false;
    case establish_backlink_op:
      if (establish_backlink_impl(other)) {
        // causes remote actor to unlink from (proxy of) other
        forward_msg(address(), invalid_message_id,
                    make_message(atom("_Link"), other));
        return true;
      }
      return false;
    case remove_backlink_op:
      if (remove_backlink_impl(other)) {
        // causes remote actor to unlink from (proxy of) other
        forward_msg(address(), invalid_message_id,
                    make_message(atom("_Unlink"), other));
        return true;
      }
      return false;
  }
  return false;
}
void remote_actor_proxy::enqueue(const actor_addr& sender, message_id mid,
                                 message m, execution_unit*) {
  forward_msg(sender, mid, std::move(m));
}
示例#5
0
int main(int argc, char ** argv) {
  int ec_fifo, ce_fifo, es_fifo, se_fifo;
  FILE* fp;
  ssize_t msg_size;
  uint8_t *buff;
  char client_nm[NM_LENGTH], client_nm_tmp[NM_LENGTH];
  uint8_t rsa_tmp[RSA_LENGTH], rsa_tmp2[RSA_LENGTH];
  BIGNUM /**bn_n, *bn_d,*/ *bn_client_e, *bn_client_n, *bn_r;
  char client_cipher_suite;
  uint8_t sym_id, hash_id, public_id;
  uint8_t k[K_SIZE] = {0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1};
  int i, done;
  int k_len, c_hex_len, c_len;
  uint8_t g[HASH_LENGTH], g1[HASH_LENGTH];
  uint8_t c[MSG_SIZE_MAX];
  unsigned int tmp;

  assert(K_SIZE == 8);

  bn_client_e = BN_new();
  bn_client_n = BN_new();
  bn_r = BN_new();


  /* Mandatory arguments */
  if (!argv[1] || !argv[2] || !argv[3] || !argv[4] ||
      !argv[1] || !argv[2] || !argv[3] || !argv[4]) {
    fprintf(stderr, "%s [server->eve fifo] [eve->server fifo] [eve->client fifo] [client->eve fifo]\n", argv[0]);
    exit(1);
  }

  se_fifo = open_channel(argv[1]);
  es_fifo = open_channel(argv[2]);
  ec_fifo = open_fifo(argv[3]);
  ce_fifo = open_fifo(argv[4]);

  /* wait real client to connect */
  fprintf(stderr,"(E) Waiting connection...\n");
  if (wait_connection(ce_fifo) < 0) {
    fprintf(stderr,"(E) Communication error...\n");
    goto next;
  }
  /* connect to server */
  write_msg(es_fifo, (const u_int8_t *)CONNECTION_STRING, strlen(CONNECTION_STRING));

  forward_string(se_fifo, ec_fifo, OK_STRING);

  /* Server authentication */
  /* client challange */
  forward_msg(ce_fifo, es_fifo);
  /* server response to challange */
  forward_msg(se_fifo, ec_fifo);

  /* Client authentication */
  /* Client name */
  msg_size = forward_msg_read(ce_fifo, es_fifo, &buff);

  /*  vvvv  copy-paste from server  vvvv  */
  buff[msg_size] = '\0';
  strncpy((char *)client_nm, (const char *) buff, NM_LENGTH);
  /* EXTRACT from (names[],c_puk[],n[]) the pair (c_puk[i],n[i]) where names[i] = nm */
  if ((fp = fopen("server_folder/clients_rsa64_public_keys.txt", "r")) == NULL) {
    fprintf(stderr, "Error while getting clients RSA public keys...\n");
    goto next;
  }
  done = 0;
  while (!feof(fp)) {
    fscanf(fp, "%129s %129s %129s", client_nm_tmp, rsa_tmp, rsa_tmp2);
    if (strcmp(client_nm_tmp, client_nm) == 0) {
      done = 1;
      break;
    }
  }
  if (done == 0) {
    fprintf(stderr, "Error: unrecognized client\n");
    goto next;
  }
  fclose(fp);

  BN_hex2bn(&bn_client_n, (const char *) rsa_tmp);
  BN_hex2bn(&bn_client_e, (const char *) rsa_tmp2);

  /*  ^^^^  copy-paste from server  ^^^^  */
  /* now I know client name and pub key */

  /* server challange */
  forward_msg(se_fifo, ec_fifo);
  /* client response */
  forward_msg(ce_fifo, es_fifo);


  /* Negotiation of the cipher suite */
  /* cipher suite c -> s */
  forward_msg_read(ce_fifo, es_fifo, &buff);

  /*  vvvv  copy-paste from server  vvvv  */
  client_cipher_suite = buff[0];
  cipher_suite_table(client_cipher_suite, &sym_id, &hash_id, &public_id);
  /*  ^^^^  copy-paste from server  ^^^^  */


  /* Negotiation of the private key */
  /* k already set as an arbitrary key */

  /*  vvvv  copy-paste from server  vvvv  */

  if (sym_id == 1) {
    k_len = 3;
  }
  else {
    k_len = K_SIZE;
  }
  BN_bin2bn(k, k_len, bn_r);

  /* If we're using RSA512 read the correct key (we have the 64bit one) */
  if (public_id == 6) {
    if ((fp = fopen("server_folder/clients_rsa512_public_keys.txt", "r")) == NULL) {
      fprintf(stderr, "Error while getting clients RSA public keys...\n");
      goto next;
    }
    done = 0;
    while (!feof(fp)) {
      fscanf(fp, "%129s %129s %129s", client_nm_tmp, rsa_tmp, rsa_tmp2);
      if (strcmp(client_nm_tmp, client_nm) == 0) {
        done = 1;
        break;
      }
    }
    if (done == 0) {
      fprintf(stderr, "Error: unrecognized client\n");
      goto next;
    }
    fclose(fp);

    BN_hex2bn(&bn_client_n, (const char *) rsa_tmp);
    BN_hex2bn(&bn_client_e, (const char *) rsa_tmp2);
  }

  /* ENCRYPT key */
  rsa_encrypt(bn_r, bn_client_e, bn_client_n);

  /* WRITE encrypted k to C */
  buff = (uint8_t *) BN_bn2hex(bn_r);
  if ((write_msg(ec_fifo, buff, strlen((char *) buff))) < 0) {
    fprintf(stderr, "Error while sending C to the client...\n");
    goto next;
  }
  OPENSSL_free(buff);

  /* Encrypted communication */
  if ((msg_size = read_msg(ce_fifo,&buff)) < 0) {
    fprintf(stderr, "Error while reading message from the client...\n");
    goto next;
  }

  c_hex_len = msg_size - HASH_LENGTH * 2;
  if (c_hex_len <= 0) {
    fprintf(stderr, "Error, malformed message...\n");
    goto next;
  }

  c_len = c_hex_len / 2;
  for (i=0; i<msg_size; i+=2) {
    if (i < c_hex_len) {
      sscanf((char *) (buff+i), "%02x", &tmp);
      c[i/2] = (uint8_t) tmp;
    }
    else {
      sscanf((char *) (buff+i), "%02x", &tmp);
      g[(i - c_hex_len) / 2] = (uint8_t) tmp;
    }
  }

  /* Decrypt C */
  decrypt(sym_id, c, c_len, k);

  /* COMPUTE G' = H(M) */
  sponge_hash(c, c_len, g1);

  c[c_len] = '\0';

  /* CHECK G' = G */
  done = 1;
  for (i=0; i<HASH_LENGTH; i++) {
    if (g[i] != g1[i]) {
      done = 0;
    }
  }

  /* If the check fails print error message */
  if (done == 0) {
    if ((write_msg(ec_fifo, (uint8_t *) CORRUPTED_STRING, strlen(CORRUPTED_STRING))) < 0) {
      fprintf(stderr, "Error while writing to the client...\n");
      goto next;
    }
  }

  /* PUT M' on a file */
  if ((fp = fopen("eve_folder/received_messages.txt", "a+")) == NULL) {
    fprintf(stderr, "Error while saving message...\n");
    fclose(fp);
    goto next;
  }
  fprintf(fp, "%s", c);
  fflush(stdout);
  fprintf(stdout, "MESSAGGIO SEGRETO >>>%s<<< FINE MESSAGGIO SEGRETO\n", c);
  fflush(stdout);
  fclose(fp);

  /* WRITE ok message to C */
  if ((write_msg(ec_fifo, (uint8_t *) DECRYPTED_STRING, strlen(DECRYPTED_STRING))) < 0) {
    fprintf(stderr, "Error while writing C to the client...\n");
    goto next;
  }

  /*  ^^^^  copy-paste from server  ^^^^  */



 next:

  /*close_channels ...!*/

  BN_free(bn_client_n);
  BN_free(bn_client_e);
  BN_free(bn_r);

  exit(0);
}
示例#6
0
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: $ ./server 1234\n");
        exit(0);
    }
 
    int   sevrfd;
    int   peerfd[MAX_CLIENT];
    char *peername[MAX_CLIENT];
    char *peerip[MAX_CLIENT];
    
    struct sockaddr_in sevr_addr;
    struct sockaddr_in peer_addr;
    
    char buf[MAX_BUF];
    char buf2[MAX_BUF];
    bzero(buf, MAX_BUF);
    bzero(buf2, MAX_BUF);
    
    
    //SO_REUSEADDR
    int optval = 1;
    setsockopt(sevrfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); 
    
    //sevr_addr
    bzero(&sevr_addr, sizeof(sevr_addr));
    
    sevr_addr.sin_family      = AF_INET;
    sevr_addr.sin_port        = htons((uint16_t)atoi(argv[1]));
    sevr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    //server socket
    if ( (sevrfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        perror("create sevrfd socket.");
        exit(1);
    }
    
    //bind
    if ( bind(sevrfd, (struct sockaddr *) &sevr_addr, sizeof(sevr_addr)) < 0 )
    {
        perror("bind sevrfd.");
        exit(1);
    }
    
    //listen
    if ( listen(sevrfd, 5) < 0 )
    {
        perror("listen.");
        exit(1);
    }
    
    printf("------ Chat Room Server Started ------\n");
    
    int            i, j, result, maxfd, newfd;
    struct timeval tv;
    fd_set         rset;
    socklen_t      peer_addr_len;
    char           ipstr[30];
    
    
    peer_addr_len = sizeof(peer_addr);
    bzero(&peer_addr, peer_addr_len);
    
    int conn_clients = 0;
    
    for(i=0; i<MAX_CLIENT; i++)
    {
        peerfd[i]   = 0;
        peername[i] = NULL;
        peerip[i]   = NULL;
    }
    
    while(1)
    {
        FD_ZERO(&rset);
        FD_SET(0, &rset);
        FD_SET(sevrfd, &rset);
        
        maxfd = MAX(maxfd, sevrfd);
        
        tv.tv_sec  = 1;
        tv.tv_usec = 0;
        
        for(i=0; i<MAX_CLIENT; i++)
        {
            if (peerfd[i] != 0)
            {
                FD_SET(peerfd[i], &rset);
                maxfd = MAX(maxfd, peerfd[i]);
            }
        }
        
        if ( select(maxfd+1, &rset, NULL, NULL, &tv) < 0 )
        {
            perror("select");
            exit(1);
        }
        
        //peerfd[]
        for(i=0; i<MAX_CLIENT; i++)
        {
            if(peerfd[i] != 0 && FD_ISSET(peerfd[i], &rset))
            {
                result = read(peerfd[i], buf, MAX_BUF);
                
                if (result == 0)
                {
                    FD_CLR(peerfd[i], &rset);
                    close(peerfd[i]);
                    peerfd[i] = 0;
                    
                    sprintf(buf2, "[SERVER]: [%s][%s] LEFT. BYE-BYE. [%s]\n", peerip[i], peername[i], get_time());
                    forward_msg(peerfd, MAX_CLIENT, buf2, MAX_BUF);
                    bzero(buf2, MAX_BUF);
                    bzero(buf, MAX_BUF);
                    
                    conn_clients--;
                }
                else if (result == -1)
                {
                    perror("read");
                    exit(1);
                }
                else
                {
                    sprintf(buf2, "[%s-%s]: %s (%s)\n", peerip[i], peername[i], buf, get_time());
                    forward_msg(peerfd, MAX_CLIENT, buf2, MAX_BUF);
                    bzero(buf2, MAX_BUF);
                    bzero(buf, MAX_BUF);
                }
            }
        }
        
        //sevrfd
        if( FD_ISSET(sevrfd, &rset) )
        {
            if ( (newfd=accept(sevrfd, (struct sockaddr *) &peer_addr, &peer_addr_len)) == -1 )
            {
                perror("accept.");
                continue;
            }
            
            read(newfd, buf, MAX_BUF);
            bzero(ipstr, 30);
            inet_ntop(AF_INET, (void *) &peer_addr.sin_addr, ipstr, 30);
            
            if (conn_clients >= MAX_CLIENT)
            {
                write(newfd, "Too many clients. Please wait then try again...", 60);
                printf("[SERVER]: NEW CLIENT [%s] FROM [%s] COME, BUT WE ARE ALREADY FULL.\n", buf, ipstr);
                close(newfd);
                continue;
            }
            
            
            write(newfd, "[SERVER]: WELCOME TO OUR CHATROOM!\n", 50);
            
            for(i=0; i<MAX_CLIENT; i++)
            {
                if(peerfd[i] == 0)
                {
                    peerfd[i] = newfd;
                    peername[i] = (char *)malloc(MAX_BUF);
                    strcpy(peername[i], buf);
                    
                    peerip[i] = (char *)malloc(16);
                    strcpy(peerip[i], ipstr);
                    
                    bzero(buf, MAX_BUF);
                    
		            sprintf(buf2, "[SERVER]: [%s][%s] JOINED. WELCOME!\n", peerip[i], peername[i]);
                    forward_msg(peerfd, MAX_CLIENT, buf2, MAX_BUF);
                    bzero(buf2, MAX_BUF);
                     
                    conn_clients++;
                    break;
                }
            }
        }
        
        //STDIN
        if (FD_ISSET(0, &rset))
        {
            read(0, buf, MAX_BUF);
            buf[strlen(buf) - 1] = '\0';
            sprintf(buf2, "[SERVER]: %s (%s)\n", buf, get_time());
            forward_msg(peerfd, MAX_CLIENT, buf2, MAX_BUF);
            bzero(buf, MAX_BUF);
            bzero(buf2, MAX_BUF);
        }
    }
    
    return 0;
}