Network library: Difference between revisions

From KolibriOS wiki
Jump to navigation Jump to search
(Created page with "=Network Library= ==inet_addr== '''Convert the string from standard IPv4 dotted notation to integer IP addr.'''<br> Input: ptr to ASCIIZ string containing standard dotted IPv4...")
 
No edit summary
Line 1: Line 1:
=Network Library=
=Network Library=
The KolibriOS network library implements some network related functions similar to those found in Unix/Windows.<br>
Their function and API closely resembles these functions on other platforms. <br>
Because of this, more information about these functions can be found in other guides suchs as [http://beej.us/guide/bgnet/ Beej's guide to network programming].<br><br>
All structures and error codes you need for these functions can be found in network.inc (in programs folder on SVN).<br>
==inet_addr==
==inet_addr==
'''Convert the string from standard IPv4 dotted notation to integer IP addr.'''<br>
'''Convert the string from standard IPv4 dotted notation to integer IP addr.'''<br>
Line 8: Line 14:
<syntaxhighlight lang="asm">
<syntaxhighlight lang="asm">
; Code section:
; Code section:
invoke inet_addr ip_address
        invoke inet_addr ip_address
; eax should now be 0X0100A8C0
; eax should now be 0x0100A8C0


; Data section:
; Data section:
ip_address db '192.168.0.1', 0
        ip_address db '192.168.0.1', 0
</syntaxhighlight>
</syntaxhighlight>
==inet_ntoa==
==inet_ntoa==
Line 18: Line 24:
Input: in_addr struct<br>
Input: in_addr struct<br>
Output: eax = pointer to resulting string (in static buffer)<br>
Output: eax = pointer to resulting string (in static buffer)<br>
<br>
Example:
<syntaxhighlight lang="asm">
<syntaxhighlight lang="asm">
; 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
</syntaxhighlight>
</syntaxhighlight>
==getaddrinfo==
==getaddrinfo==
Line 34: Line 48:
Example:
Example:
<syntaxhighlight lang="asm">
<syntaxhighlight lang="asm">
; 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 ?
</syntaxhighlight>
</syntaxhighlight>
==freeaddrinfo==
==freeaddrinfo==
Line 40: Line 81:
Input: ptr to addrinfo struct<br>
Input: ptr to addrinfo struct<br>
Output: /<br>
Output: /<br>
<br>
Example:
<syntaxhighlight lang="asm">
</syntaxhighlight>

Revision as of 20:17, 24 February 2015

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
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

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: /