Error en la estructura in_addr

Iniciado por mester, 23 Marzo 2017, 12:51 PM

0 Miembros y 1 Visitante están viendo este tema.

mester

Tengo los siguientes codigos:

cliente:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>

int getipbyhostname ( const char *hostname, char *ip,
                       size_t size, struct in_addr *in ) {
 int fd;
 char *r_host = (char *)hostname;
 struct sockaddr_in *r_addr = NULL;
 struct addrinfo info_addr, *res_addr, *r;

 info_addr.ai_family     = AF_INET;
 info_addr.ai_socktype   = SOCK_STREAM;
 info_addr.ai_flags      = AI_PASSIVE;
 info_addr.ai_protocol   = 0;
 info_addr.ai_addr       = NULL;
 info_addr.ai_canonname  = NULL;
 info_addr.ai_next       = NULL;

 if ( getaddrinfo ( r_host, NULL, &info_addr, &res_addr ) != 0 )
   return -1;

 for ( r = res_addr; r != NULL; r = r->ai_next ) {
   if ( r->ai_family == AF_INET ) {
     if ( (fd = socket ( r->ai_family, r->ai_socktype, r->ai_protocol )) != -1 )
       break;

     close ( fd );
   }
 }

 r_addr = (struct sockaddr_in *)r->ai_addr;

 if ( in != NULL ) {
   memcpy ( (void *)in, (void *)&r_addr->sin_addr, sizeof ( struct in_addr ) );
 } strncpy (ip, inet_ntoa ( r_addr->sin_addr ), size);

 freeaddrinfo ( res_addr );

 return 0;
}


int main (int argc, char **argv) {
 if ( argc < 3 ) {
   return fprintf ( stderr, "%s <host> <port>\n", argv[0] );
 }

 int port = atoi ( argv[2] );
 int sc, rd, e_size, pub_len = 2048;
 char host_ip[15];
 char *r_host = argv[1];
 char msg[] = "This is a successful test text did in C";
 char encrypt_msg[256];
 char *pub_key = calloc ( pub_len, sizeof ( char ) );
 struct in_addr *inaddr_st = (struct in_addr *)malloc ( sizeof ( struct in_addr ) );
 struct sockaddr_in c_addr;
 RSA *rsa = NULL;

 if ( getipbyhostname ( r_host, (char *)&host_ip, sizeof ( host_ip ), inaddr_st ) == -1 ) {
   return fprintf ( stderr, "Error getting host %s\n", argv[1] );
 }

 // opening the socket
 if ( (sc = socket ( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
   return fprintf ( stderr, "Error opening socket\n" );
 }

 c_addr.sin_family = AF_INET;
 c_addr.sin_port = htons ( port );
 c_addr.sin_addr = *inaddr_st;
//  memcpy ( (void *)&c_addr.sin_addr, (void *)inaddr_st, sizeof ( struct in_addr ) );

 printf ("%s\ndd", inet_ntoa ( c_addr.sin_addr ));

 if ( connect ( sc, (struct sockaddr *)&c_addr, sizeof ( c_addr ) ) == -1 ) {
   return fprintf ( stderr, "Error connecting to the host\n" );
 }

 // receiving the public key
 rd = recv ( sc, pub_key, pub_len, 0 );

 // encrypting the message
 d2i_RSAPublicKey ( &rsa, (const unsigned char **)&pub_key, rd );
 if ( rsa == NULL ) {
   return fprintf ( stderr, "Error creating RSA public key\n" );
 }

 if ( (e_size = RSA_public_encrypt ( strlen ( msg ), (unsigned char *)msg,
         (unsigned char *)&encrypt_msg, rsa, RSA_PKCS1_PADDING )) == -1 ) {
   return fprintf ( stderr, "Error creating RSA key\n" );
 }

 // sending encrypted message
 send ( sc, encrypt_msg, e_size, 0 );

 close ( sc );

 return 0;
}


Servidor:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/rsa.h>
#include <openssl/bn.h>

RSA *RSA_generate ( int bits ) {
 RSA *rsa = RSA_new ();
 BIGNUM *e = BN_new ();

 BN_set_word ( e, RSA_F4 );

 if ( RSA_generate_key_ex ( rsa, bits, e, NULL ) == -1 )
   return NULL;

 BN_clear_free ( e );

 return rsa;
}

char *getRSAPrivateKey ( RSA *rsa ) {
 int i;
 char *priv_key = NULL;

 if ( (i = i2d_RSAPrivateKey ( rsa, (unsigned char **)&priv_key )) == -1 )
   return NULL;

 priv_key[i] = '\0';

 return priv_key;
}

char *getRSAPublicKey ( RSA *rsa ) {
 int i;
 char *pub_key = NULL;

 if ( (i = i2d_RSAPublicKey ( rsa, (unsigned char **)&pub_key )) == -1 )
   return NULL;

 pub_key[i] = '\0';

 return pub_key;
}

int createListenSocket ( int port, struct sockaddr_in *s_addr, int size ) {
 int sc;

 if ( (sc = socket ( AF_INET, SOCK_STREAM, 0 )) == -1 )
   return -1;

 s_addr->sin_family = AF_INET;
 s_addr->sin_port = htons ( port );
 s_addr->sin_addr.s_addr = INADDR_ANY;

 if ( bind ( sc, (struct sockaddr *)s_addr, size ) == -1 )
   return -1;

 if ( listen ( sc, 1 ) == -1 )
   return -1;

 return sc;
}

int main () {
 int sc, cc;
 int rd, len;
 char *pub_key = NULL;
 char *priv_key = NULL;
 unsigned char encrypt_data[256];
 unsigned char decrypt_data[2048];
 struct sockaddr_in s_addr, c_addr;
 RSA *rsa = NULL;

 // generate rsa 2048
 if ( (rsa = RSA_generate ( 2048 )) == NULL )
   return fprintf ( stderr, "Error creating RSA keys\n" );

 // getting public key
 pub_key = getRSAPublicKey ( rsa );
 priv_key = getRSAPrivateKey ( rsa );

 if ( pub_key == NULL || priv_key == NULL )
   return fprintf ( stderr, "Error creating public and private key\n" );

 // creating listen socket
 sc = createListenSocket ( 33177, &s_addr, sizeof ( s_addr ) );

 // waiting client connection
 len = sizeof ( c_addr );
 if ( (cc = accept ( sc, (struct sockaddr *)&c_addr, (socklen_t *)&len )) == -1 )
   return fprintf ( stderr, "Error accept() calling\n" );

 // sending public key
 send ( cc, pub_key, 2048, 0 );

 // receiving crypt message
 rd = recv ( cc, encrypt_data, sizeof ( encrypt_data ), 0 );
 if ( rd <= 0 )
   return fprintf ( stderr, "Error receiving information\n" );

 if ( (len = RSA_private_decrypt ( rd, encrypt_data, decrypt_data, rsa, RSA_PKCS1_PADDING )) == -1 ) {
   return fprintf ( stderr, "Error decrypting message\n" );
 } decrypt_data[len] = '\0';

 printf ("%s\n", decrypt_data);

 RSA_free ( rsa );
 close ( cc );
 close ( sc );

 return 0;
}


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.
Justicia es dar a cada uno lo que se merece

cpu2

Cual es el valor del error?, la funcion connect puede devolver distintos errores, utiliza errno para saber cua de ellos.

Un saludo.