Next: , Previous: , Up: posix socket addresses   [Index]


4.15.1.2 IPv4 addresses

Instances of struct sockaddr_in can be handled at the Scheme level as follows:

(import (vicare)
  (prefix (vicare posix) px.))

(define sockaddr
  (px.make-sockaddr_in '#vu8(1 2 3 4) 88))

(px.sockaddr_in.in_addr sockaddr)  ⇒ #vu8(1 2 3 4)
(px.sockaddr_in.in_port sockaddr)  ⇒ 88

and we can use constant values to initialise the fields:

(import (vicare)
  (prefix (vicare posix) px.)
  (vicare platform constants))

(define addr (make-bytevector 4))
(bytevector-u32-set! bv 0 INADDR_LOOPBACK (endianness big))

(define sockaddr (px.make-sockaddr_in addr 88))

(px.sockaddr_in.in_addr sockaddr)  ⇒ #vu8(127 0 0 1)
(px.sockaddr_in.in_port sockaddr)  ⇒ 88

care must be taken when handling such raw values.

Function: make-sockaddr_in host-address port

Given a bytevector host-address holding a struct in_addr and a fixnum port representing a network port number, build and return a bytevector holding a struct sockaddr_in initialised with this data.

Internally convert port to network byte order using htons() before storing the value in the data structure.

Function: sockaddr_in.in_addr socket-address
Function: sockaddr_in.in_addr.number socket-address
Function: sockaddr_in.in_port socket-address

Given a bytevector socket-address holding a struct sockaddr_in, extract the in_addr and in_port fields and return them. If the data in socket_address is invalid: an exception is raised.

sockaddr_in.in_addr returns in_addr as bytevector holding the corresponding struct in_addr.

(let ((sockaddr (px.make-sockaddr_in '#vu8(127 0 0 1) 88)))
  (px.sockaddr_in.in_addr sockaddr))
⇒ #vu8(127 0 0 1)

sockaddr_in.in_addr.number returns in_addr as 32-bit unsigned exact integer; internally it converts the address number to host byte order using ntohl() after the extraction of the value from the data structure.

(let ((sockaddr (px.make-sockaddr_in '#vu8(127 0 0 1) 88)))
  (px.sockaddr_in.in_addr.number sockaddr))
⇒ #x7f000001

sockaddr_in.in_port returns in_port as exact integer; internally it converts the port number to host byte order using ntohs() after the extraction of the value from the data structure.

(let ((sockaddr (px.make-sockaddr_in '#vu8(127 0 0 1) 88)))
  (px.sockaddr_in.in_port sockaddr))
⇒ 88

Next: , Previous: , Up: posix socket addresses   [Index]