Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: mester en 23 Marzo 2017, 12:51 pm



Título: Error en la estructura in_addr
Publicado por: mester en 23 Marzo 2017, 12:51 pm
Tengo los siguientes codigos:

cliente:
Código
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <netdb.h>
  10. #include <openssl/pem.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/rsa.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16.  
  17. int getipbyhostname ( const char *hostname, char *ip,
  18.                        size_t size, struct in_addr *in ) {
  19.  int fd;
  20.  char *r_host = (char *)hostname;
  21.  struct sockaddr_in *r_addr = NULL;
  22.  struct addrinfo info_addr, *res_addr, *r;
  23.  
  24.  info_addr.ai_family     = AF_INET;
  25.  info_addr.ai_socktype   = SOCK_STREAM;
  26.  info_addr.ai_flags      = AI_PASSIVE;
  27.  info_addr.ai_protocol   = 0;
  28.  info_addr.ai_addr       = NULL;
  29.  info_addr.ai_canonname  = NULL;
  30.  info_addr.ai_next       = NULL;
  31.  
  32.  if ( getaddrinfo ( r_host, NULL, &info_addr, &res_addr ) != 0 )
  33.    return -1;
  34.  
  35.  for ( r = res_addr; r != NULL; r = r->ai_next ) {
  36.    if ( r->ai_family == AF_INET ) {
  37.      if ( (fd = socket ( r->ai_family, r->ai_socktype, r->ai_protocol )) != -1 )
  38.        break;
  39.  
  40.      close ( fd );
  41.    }
  42.  }
  43.  
  44.  r_addr = (struct sockaddr_in *)r->ai_addr;
  45.  
  46.  if ( in != NULL ) {
  47.    memcpy ( (void *)in, (void *)&r_addr->sin_addr, sizeof ( struct in_addr ) );
  48.  } strncpy (ip, inet_ntoa ( r_addr->sin_addr ), size);
  49.  
  50.  freeaddrinfo ( res_addr );
  51.  
  52.  return 0;
  53. }
  54.  
  55.  
  56. int main (int argc, char **argv) {
  57.  if ( argc < 3 ) {
  58.    return fprintf ( stderr, "%s <host> <port>\n", argv[0] );
  59.  }
  60.  
  61.  int port = atoi ( argv[2] );
  62.  int sc, rd, e_size, pub_len = 2048;
  63.  char host_ip[15];
  64.  char *r_host = argv[1];
  65.  char msg[] = "This is a successful test text did in C";
  66.  char encrypt_msg[256];
  67.  char *pub_key = calloc ( pub_len, sizeof ( char ) );
  68.  struct in_addr *inaddr_st = (struct in_addr *)malloc ( sizeof ( struct in_addr ) );
  69.  struct sockaddr_in c_addr;
  70.  RSA *rsa = NULL;
  71.  
  72.  if ( getipbyhostname ( r_host, (char *)&host_ip, sizeof ( host_ip ), inaddr_st ) == -1 ) {
  73.    return fprintf ( stderr, "Error getting host %s\n", argv[1] );
  74.  }
  75.  
  76.  // opening the socket
  77.  if ( (sc = socket ( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
  78.    return fprintf ( stderr, "Error opening socket\n" );
  79.  }
  80.  
  81.  c_addr.sin_family = AF_INET;
  82.  c_addr.sin_port = htons ( port );
  83.  c_addr.sin_addr = *inaddr_st;
  84. //  memcpy ( (void *)&c_addr.sin_addr, (void *)inaddr_st, sizeof ( struct in_addr ) );
  85.  
  86.  printf ("%s\ndd", inet_ntoa ( c_addr.sin_addr ));
  87.  
  88.  if ( connect ( sc, (struct sockaddr *)&c_addr, sizeof ( c_addr ) ) == -1 ) {
  89.    return fprintf ( stderr, "Error connecting to the host\n" );
  90.  }
  91.  
  92.  // receiving the public key
  93.  rd = recv ( sc, pub_key, pub_len, 0 );
  94.  
  95.  // encrypting the message
  96.  d2i_RSAPublicKey ( &rsa, (const unsigned char **)&pub_key, rd );
  97.  if ( rsa == NULL ) {
  98.    return fprintf ( stderr, "Error creating RSA public key\n" );
  99.  }
  100.  
  101.  if ( (e_size = RSA_public_encrypt ( strlen ( msg ), (unsigned char *)msg,
  102.          (unsigned char *)&encrypt_msg, rsa, RSA_PKCS1_PADDING )) == -1 ) {
  103.    return fprintf ( stderr, "Error creating RSA key\n" );
  104.  }
  105.  
  106.  // sending encrypted message
  107.  send ( sc, encrypt_msg, e_size, 0 );
  108.  
  109.  close ( sc );
  110.  
  111.  return 0;
  112. }
  113.  

Servidor:
Código
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <openssl/bio.h>
  8. #include <openssl/pem.h>
  9. #include <openssl/x509.h>
  10. #include <openssl/rsa.h>
  11. #include <openssl/bn.h>
  12.  
  13. RSA *RSA_generate ( int bits ) {
  14.  RSA *rsa = RSA_new ();
  15.  BIGNUM *e = BN_new ();
  16.  
  17.  BN_set_word ( e, RSA_F4 );
  18.  
  19.  if ( RSA_generate_key_ex ( rsa, bits, e, NULL ) == -1 )
  20.    return NULL;
  21.  
  22.  BN_clear_free ( e );
  23.  
  24.  return rsa;
  25. }
  26.  
  27. char *getRSAPrivateKey ( RSA *rsa ) {
  28.  int i;
  29.  char *priv_key = NULL;
  30.  
  31.  if ( (i = i2d_RSAPrivateKey ( rsa, (unsigned char **)&priv_key )) == -1 )
  32.    return NULL;
  33.  
  34.  priv_key[i] = '\0';
  35.  
  36.  return priv_key;
  37. }
  38.  
  39. char *getRSAPublicKey ( RSA *rsa ) {
  40.  int i;
  41.  char *pub_key = NULL;
  42.  
  43.  if ( (i = i2d_RSAPublicKey ( rsa, (unsigned char **)&pub_key )) == -1 )
  44.    return NULL;
  45.  
  46.  pub_key[i] = '\0';
  47.  
  48.  return pub_key;
  49. }
  50.  
  51. int createListenSocket ( int port, struct sockaddr_in *s_addr, int size ) {
  52.  int sc;
  53.  
  54.  if ( (sc = socket ( AF_INET, SOCK_STREAM, 0 )) == -1 )
  55.    return -1;
  56.  
  57.  s_addr->sin_family = AF_INET;
  58.  s_addr->sin_port = htons ( port );
  59.  s_addr->sin_addr.s_addr = INADDR_ANY;
  60.  
  61.  if ( bind ( sc, (struct sockaddr *)s_addr, size ) == -1 )
  62.    return -1;
  63.  
  64.  if ( listen ( sc, 1 ) == -1 )
  65.    return -1;
  66.  
  67.  return sc;
  68. }
  69.  
  70. int main () {
  71.  int sc, cc;
  72.  int rd, len;
  73.  char *pub_key = NULL;
  74.  char *priv_key = NULL;
  75.  unsigned char encrypt_data[256];
  76.  unsigned char decrypt_data[2048];
  77.  struct sockaddr_in s_addr, c_addr;
  78.  RSA *rsa = NULL;
  79.  
  80.  // generate rsa 2048
  81.  if ( (rsa = RSA_generate ( 2048 )) == NULL )
  82.    return fprintf ( stderr, "Error creating RSA keys\n" );
  83.  
  84.  // getting public key
  85.  pub_key = getRSAPublicKey ( rsa );
  86.  priv_key = getRSAPrivateKey ( rsa );
  87.  
  88.  if ( pub_key == NULL || priv_key == NULL )
  89.    return fprintf ( stderr, "Error creating public and private key\n" );
  90.  
  91.  // creating listen socket
  92.  sc = createListenSocket ( 33177, &s_addr, sizeof ( s_addr ) );
  93.  
  94.  // waiting client connection
  95.  len = sizeof ( c_addr );
  96.  if ( (cc = accept ( sc, (struct sockaddr *)&c_addr, (socklen_t *)&len )) == -1 )
  97.    return fprintf ( stderr, "Error accept() calling\n" );
  98.  
  99.  // sending public key
  100.  send ( cc, pub_key, 2048, 0 );
  101.  
  102.  // receiving crypt message
  103.  rd = recv ( cc, encrypt_data, sizeof ( encrypt_data ), 0 );
  104.  if ( rd <= 0 )
  105.    return fprintf ( stderr, "Error receiving information\n" );
  106.  
  107.  if ( (len = RSA_private_decrypt ( rd, encrypt_data, decrypt_data, rsa, RSA_PKCS1_PADDING )) == -1 ) {
  108.    return fprintf ( stderr, "Error decrypting message\n" );
  109.  } decrypt_data[len] = '\0';
  110.  
  111.  printf ("%s\n", decrypt_data);
  112.  
  113.  RSA_free ( rsa );
  114.  close ( cc );
  115.  close ( sc );
  116.  
  117.  return 0;
  118. }
  119.  

El problema que tengo principalmente es que no quiero usar la funcion gethostbyname, y me he hecho una propia utilizando getaddrinfo. Lo que hago es intentar establecer una conexion con el servidor, pero nunca funciona. El error está en el connect, creo, pero no encuentro el por qué no conecta.
El localhost sí funciona, pero con una ip remota no.

Gracias de antemano.


Título: Re: Error en la estructura in_addr
Publicado por: xv0 en 23 Marzo 2017, 17:22 pm
Cual es el valor del error?, la funcion connect puede devolver distintos errores, utiliza errno para saber cua de ellos.

Un saludo.