Network library

From KolibriOS wiki
Jump to: navigation, search

Network Library

The KolibriOS network library implements some network related functions similar to those found in Unix/Windows.
Their function and API closely resembles these functions on other platforms.
Because of this, more information about these functions can be found in other guides suchs as Beej's guide to network programming.

All structures and error codes you need for these functions can be found in network.inc (in programs folder on SVN).

inet_addr

Convert the string from standard IPv4 dotted notation to integer IP addr.
Input: ptr to ASCIIZ string containing standard dotted IPv4 address.
Output: eax = converted IP address / -1 on error

Example:

; Code section:
        invoke inet_addr ip_address
; eax should now be 0x0100A8C0
 
; Data section:
        ip_address db '192.168.0.1', 0

inet_ntoa

Convert the Internet host address to standard IPv4 dotted notation.
Input: in_addr struct (see network.inc for struct)
Output: eax = pointer to resulting string (in static buffer)

; Code section:
        invoke ntoa sockaddr1
; eax will now point to following data: db "192.168.0.1", 0
 
; Data section:
sockaddr1:
        dw ?           ; address family
        dw ?           ; port
        dd 0x0100A8C0  ; IP address
        rb 8           ; zero
ends

getaddrinfo

Get a list of IP addresses and port numbers for given host and service
Input:

  • ptr to hostname (optional)
  • ptr to servname (optional)
  • ptr to addrinfo struct hints (optional)
  • ptr to result addrinfo struct

Output: eax = 0 on success / one of EAI codes on error.
On success, the function will write the pointer to the addrinfo structure at the given address.
Structure definitions and error codes can be found in network.inc
Example:

; Code section
; resolve name
        invoke  getaddrinfo, hostname, 0, 0, result
; test for error
        test    eax, eax
        jnz     fail
 
; convert IP address to decimal notation
        mov     edi, [result]
  addrloop:
        mov     eax, [edi+addrinfo.ai_addr] 
        invoke  inet_ntoa, [eax+sockaddr_in.sin_addr]
 
; write result
        invoke  con_write_asciiz, eax
 
; advance to next item
        mov     edi, [edi+addrinfo.ai_next]
        test    edi, edi
        jnz     addrloop
 
; free allocated memory
        invoke  freeaddrinfo, [result]
 
; Data section:
        hostname db 'kolibrios.org', 0
;        hostname db '192.168.0.1', 0
        result   dd ?

freeaddrinfo

Free one or more addrinfo structures returned by getaddrinfo.
Input: ptr to addrinfo struct
Output: /