Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
Wrong output inet_ntop

I'm trying to print an ip address from inet_ntop, but the output seems pretty weird.

The program seems to work well, I succeed to connect the socket but it print this:

H��H9�u�H�[]A\A]A^A_�ff.�

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char *argv[]){
    int sock = socket(AF_INET, SOCK_STREAM, 0); 
    struct addrinfo hints, *result;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    hints.ai_flags = 0;

    int s = getaddrinfo("irc.root-me.org", "6667", &hints, &result);
    if( s != 0){ 
        printf("erreur\n");
        exit(EXIT_FAILURE);
    }   

    int f = connect(sock, result->ai_addr, result->ai_addrlen);
    if(f != 0){ 
        printf("erreur connect\n");
    }   

    struct sockaddr_in *sockin;  
    sockin = (struct sockaddr_in *)result->ai_addr;
    char  *dst;
    inet_ntop(AF_INET, &sockin->sin_addr, dst, sizeof(char *));
    printf("%s\n", dst);
    freeaddrinfo(result);

    exit(0);
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Two issues here:

  1. The 3rd parameter to inet_ntop() should be a char-array or a pointer to the 1st element of a char-array.

  2. The 4th parameter to inet_ntop() should be the size of the destination buffer, whose address is passed as 3rd parameter.

What you do is to pass the uninitialised char-pointer dst as destination and tell the function it would point to sizeof(char*) bytes memory.

An AF_INET address (which has the form xxx.xxx.xxx.xxx) uses a maximum of 4x3 characters plus 3 delimiting . chars which sums up to 15 chars plus 1 additional char used as 0-terminator to make it a C-"string", so the corrected code would look like this:

char dst[16] = ""; /* inet_ntop() does not necessarily 0-terminates the result. */
inet_ntop(AF_INET, &sockin->sin_addr, dst, sizeof dst);

As pointed out by Filipe Gonçalves in his comment one can use INET_ADDRSTRLEN (if available) instead of the hard coded "magic number" 16 to define the buffer size for the textual representation of a IPv4 address. It's a good thing.


Documentation for inet_ntop() is here:

  • POSIX in general
  • Specific to Linux
over 4 years ago · Santiago Trujillo Denunciar

0

The Problem

The problem is with the size of the destination char* you're trying to save the result in, and the fact that it is uninitialized, what you want to do is save it in a char[].

sizeof(char*) on x86 is 4B and 8B on x64, IP Addresses are usually larger than that (an IPv4 address is between 7 to 15 bytes) + 1 for null terminator.

Solution

You can fix the code as follows:

char dst[16] = {0};
inet_ntop(AF_INET, &sockin->sin_addr, dst, sizeof(dst));

After the fix:

$ ./main
212.83.153.145

Source Code

If you want the full fixed main.c, I've uploaded it to github.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda