0

I try to memcpy ipv4 address in *ai(struct addrinfo)

struct addrinfo *ai;
char *p = (char *)(void *)(ai->ai_addr);

memcpy(p + afd->a_off, "d83adcca", (size_t)afd->a_addrlen); // "d83adcca ipv4 address is hex data - not correct.."

So, i need verify that i have been correctly assigned. I use this code :

struct sockaddr_in ipv4 = (struct sockaddr_in *)ai->ai_addr;
inet_ntop(AF_INET, &(ipv4->sin_addr), ipAddress, INET_ADDRSTRLEN);

BUT, ipAddress and ai->ai_addr is not matched!! If you know how to assigned these struct, Please help me. Thanks.

hyunsang park
  • 11
  • 1
  • 3
  • 1
    Note that `"d83adcca"` is a null-terminated string of nine bytes (null-terminator included), which on ASCII systems will be an array containing the bytes (in decimal) `100` (for `'d'`), `56` (for `'8'`), etc. Not really what's expected here. – Some programmer dude Feb 21 '19 at 07:01
  • 1
    And if you have an IP address you want to use, why don't you use `inet_pton` to convert the *string* (in dotted-decimal form!) to a proper encoded address in the correct byte-order? – Some programmer dude Feb 21 '19 at 07:02
  • So, Can i use inet_pton function? @Someprogrammerdude – hyunsang park Feb 21 '19 at 07:24
  • inet_pton(AF_INET, "192.168.0.1", ai->ai_addr); -> Is this right? – hyunsang park Feb 21 '19 at 07:25
  • OKAY. I will try right now. – hyunsang park Feb 21 '19 at 07:27
  • 1
    Not quite, the destination should be a pointer to an `in_addr` structure. Please read an [`inet_pton` manual page](http://man7.org/linux/man-pages/man3/inet_pton.3.html). – Some programmer dude Feb 21 '19 at 07:28
  • So, I use - struct in_addr addr; inet_pton(AF_INET, "192.168.0.1", &addr.s_addr); – hyunsang park Feb 21 '19 at 07:35
  • How can I allocate from in_addr to addrinfo? @Someprogrammerdude – hyunsang park Feb 21 '19 at 07:36
  • I think it's time you expand on your question, to explain what your *actual* problem is. *Why* do you need to do this (which is well-documented in many, many tutorials)? Why do you need an `addrinfo` structure? Why do you need to define it as a pointer? And please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly don't forget how to create a [mcve] to show us. – Some programmer dude Feb 21 '19 at 07:40
  • I also recommend that you ask about your real problem as a new question, as `inet_pton` should solve *this* one. If you feel that this won't be useful for others in the future, you could delete it in favor or the new question about the real problem. Oh and please read about [the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Some programmer dude Feb 21 '19 at 07:43

0 Answers0