0

I need to create a message and send it over a unix socket.

I have a socket defined as such: socket(AF_UNIX, SOCK_RAW, 0)

The header structure of the message/packet i want to send is as follows:

struct map_msghdr {
    uint8_t  map_msglen;    /* to skip over non-understood messages */
    uint8_t  map_version;   /* future binary compatibility */
    uint16_t map_type;  /* message type */

    uint32_t map_flags; /* flags, incl. kern & message, e.g. DONE */
    uint16_t map_addrs; /* bitmask identifying sockaddrs in msg */
    uint16_t map_versioning;/* Mapping Version Number */

    int     map_rloc_count;/* Number of rlocs appended to the msg */
    pid_t   map_pid;    /* identify sender */
    int map_seq;    /* for sender to identify action */
    int map_errno;  /* why failed */

};

I need to build a buffer containing the map_msghdr{} structure followed by a socket address structure. The socket address structure will have an ip address. How do i do this? Can you please show me an example? Thank you.

Pheonix7
  • 2,131
  • 5
  • 21
  • 38
  • [raw socket examples](http://www.tenouk.com/Module43a.html) – Alex Jan 18 '16 at 14:10
  • 1
    @DarkFalcon Nothing stops you from sending an IP address over a unix socket - which is what the question asks. – nos Jan 18 '16 at 14:16

1 Answers1

1

Allocate (statically or dynamically) sizeof(struct map_msghdr) + sizeof(sockaddr_storage) bytes. Copy the map_msghdr to the beginning of the allocated memory and copy the socket address structure to the buffer after the header structure (i.e. buffer + sizeof(map_msghdr)). Send the buffer.


Simple pseudo-ish code:

struct map_msghdr hdr;
struct sockaddr_storage addr;

fill_in_header(&hdr);     // You need to write this
fill_in_sockaddr(&addr);  // You need to write this

// Create a buffer to send the header and address
int8_t buffer[sizeof hdr + sizeof addr] = { 0 };

memcpy(buffer, &hdr, sizeof hdr);  // Copy header to beginning of buffer
memcpy(buffer + sizeof(hdr), &addr, sizeof addr);  // Copy address after header

write(your_socket, buffer, sizeof buffer);  // Write buffer to socket
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • can you please show me a more concrete example. I'm fairly new to c socket programming. – Pheonix7 Jan 18 '16 at 14:22
  • @Pheonix7 It doesn't really have much to do with socket programming, but I have added a simple pseudo-ish example to my answer – Some programmer dude Jan 18 '16 at 14:33
  • Thanks a lot, it works except for the sockaddr_storage addr, Can you show me just how to set the address statically say to 10.10.10.10 – Pheonix7 Jan 18 '16 at 16:07
  • 1
    @Pheonix7 I used the `sockaddr_storage` because it's big enough to fit all address structures. If you pass it to a function as a pointer, you should do it and cast it to the socket address you really need. Or just use the actual socket address structure you actually need (e.g. `sockaddr_in` or `sockaddr_un`). – Some programmer dude Jan 18 '16 at 16:21