Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!