Network library: Difference between revisions
(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...") |
Sweetbread (talk | contribs) m (Added link to network.inc) |
||
(6 intermediate revisions by 2 users not shown) | |||
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 [http://websvn.kolibrios.org/filedetails.php?repname=Kolibri+OS&path=%2Fprograms%2Fnetwork.inc 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 | ; 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== | ||
'''Convert the Internet host address to standard IPv4 dotted notation. '''<br> | '''Convert the Internet host address to standard IPv4 dotted notation. '''<br> | ||
Input: in_addr struct<br> | Input: in_addr struct (see network.inc for struct)<br> | ||
Output: eax = pointer to resulting string (in static buffer)<br> | Output: eax = pointer to resulting string (in static buffer)<br> | ||
<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== | ||
'''Get a list of IP addresses and port numbers for given host and service'''<br> | '''Get a list of IP addresses and port numbers for given host and service'''<br> | ||
Line 30: | Line 45: | ||
* ptr to addrinfo struct hints (optional)<br> | * ptr to addrinfo struct hints (optional)<br> | ||
* ptr to result addrinfo struct<br> | * ptr to result addrinfo struct<br> | ||
Output: eax = 0 on success / one of EAI codes on error<br> | Output: eax = 0 on success / one of EAI codes on error.<br> | ||
On success, the function will write the pointer to the addrinfo structure at the given address.<br> | |||
Structure definitions and error codes can be found in network.inc | |||
<br> | <br> | ||
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== | ||
'''Free one or more addrinfo structures returned by getaddrinfo. '''<br> | '''Free one or more addrinfo structures returned by getaddrinfo. '''<br> | ||
Input: ptr to addrinfo struct<br> | Input: ptr to addrinfo struct<br> | ||
Output: /<br> | Output: /<br> | ||
[[Category:Manuals]] | |||
[[Category:Network]] | |||
[[Category:Сеть]] | |||
[[Category:Руководства]] | |||
[[Category:Libraries]] | |||
[[Category:Библиотеки]] |
Latest revision as of 12:39, 19 January 2023
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: /