getnameinfo prototype asks for sockaddr not sockaddr_in?

getnameinfo prototype asks for sockaddr not sockaddr_in?

By : user3861975
Date : November 21 2020, 07:01 PM
around this issue struct sockaddr is a "super-class" of the concrete protocol address structures like struct sockaddr_in, struct sockaddr_in6, and struct sockaddr_un, etc. The getnameinfo(3) dispatches to a specific execution path based of the address family (the sa_family member.)
As far as memory is concerned - the three members of struct sockaddr_in are overlaid with the struct sockaddr's sa_data member. Take a look at Chapter 3 of the UnP book.
code :

Share : facebook icon twitter icon
Why can we cast sockaddr to sockaddr_in

Why can we cast sockaddr to sockaddr_in

By : Prafulla Kumar Sahu
Date : March 29 2020, 07:55 AM
help you fix your problem It is possible because you normally cast pointers, not the structures themselves. You do what in natural language means "please treat this pointer to a socket structure as a pointer to an internet socket structure instead". Compiler has no problems to re-interpret the pointer.
Here is more detailed description taken up from comments:
C# sockaddr to sockaddr_in

C# sockaddr to sockaddr_in

By : Katie Beth Cunningha
Date : March 29 2020, 07:55 AM
I wish this help you Use an IntPtr in the p/invoke declaration, then you can use Marshal.PtrToStructure. First use sockaddr to find out sa_family, then call Marshal.PtrToStructure again with the more specific type.
How to cast sockaddr_in to sockaddr in swift 2.0?

How to cast sockaddr_in to sockaddr in swift 2.0?

By : Daniel
Date : March 29 2020, 07:55 AM
hop of those help? Similarly as in the referenced Q&A, you have to use withUnsafePointer()
code :
var sa = sockaddr_in()
let s =  socket(PF_INET,SOCK_STREAM,0)

let cn = withUnsafePointer(&sa) { 
    connect(s, UnsafePointer($0), socklen_t(sizeofValue(sa)))
let cn = withUnsafeMutablePointer(to: &sa) {
    $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
        connect(s, $0, socklen_t(MemoryLayout.size(ofValue: sa)))
Why does a cast from sockaddr_in to sockaddr work

Why does a cast from sockaddr_in to sockaddr work

By : user1463568
Date : March 29 2020, 07:55 AM
I wish this helpful for you The sockaddr struct basically has only one field, the address family. The code that receives this structure can use this field to determine what is the actual type of the structure. All the structures that are really used also have this field as the first one and therefore the value is deterministic.
The implementations also make the structures the same size with padding, so the memory usage is also completely deterministic. This makes it work properly.
code :
struct sockaddr {  
    unsigned short sa_family;  
    char sa_data[14];  
struct sockaddr_in{  
    short sin_family;  
    unsigned short sin_port;  
struct in_addr sin_addr;  
    char sin_zero[8];  
casting between sockaddr and sockaddr_in

casting between sockaddr and sockaddr_in

By : Remo Linter
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I'll add to @gsamaras answer by saying that Undefined Behaviour doesn't always means that bad things are about to happen. Undefined Behaviour actually says "we* don't provide any specifications on what should happen next if XYZ occurs".
(*the C++ standard).
Related Posts Related Posts :
  • C error conflicting types
  • How do I convert text to XML in C?
  • Call by Reference Function
  • C programming XML
  • Reading a directory
  • C error Array: array type has incomplete element type.
  • Compiling C code in Windows XP
  • Is there any char to hexadecimal function for C?
  • c compilation error
  • 5x5 matrix multiplication in C
  • parent process, and a child process
  • Linked list recursive reverse
  • C warning conflicting types
  • how to print a char from struct
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk