Author Topic: Win64 Get ip address by name NASM, GCC, GOLINK  (Read 3057 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Win64 Get ip address by name NASM, GCC, GOLINK
« on: November 24, 2013, 09:53:08 AM »
Hello!

; ------------------------------------------------------------ ;
; This is: Win64 Get ip address by name NASM, GCC, GOLINK.
; Author: J.K. Encryptor256
; Date: November 24, 2013
; Why: Why not!
;
; ------------------------------------------------------------ ;
; Description:
; Initialize network library.
; Get ip address from name.
; Print result.
; De-Initialize network library.
;
; ------------------------------------------------------------ ;
; Using:
;
; 1. NASM: (http://nasm.us/)
;
;     "The Netwide Assembler, NASM,"
;     "is an 80x86 and x86-64 assembler designed"
;     "for portability and modularity."
;
; 2. MINGW64-GCC: (http://mingw-w64.sourceforge.net/)
;
;     "Mingw-w64 delivers runtime, headers and libs for"
;     "developing both 64 bit (x64) and 32 bit (x86) windows"
;     "applications using GCC and other free software compilers."
;
; 3. GoLink: (http://www.godevtool.com/)
;
;     "Jeremy Gordon's Go Tools for Win32 and Win64:"
;     "assembler, resource compiler,"
;     "linker, debugger and information"
;
; ------------------------------------------------------------ ;
; Compile:
;
; 1. NASM.EXE:
;     "-f win64 main.asm -o main.obj"
;
;           Output size: 3.07 KB (3,148 bytes)
;
; Choose your linker:
;
; 2. GCC.EXE:
;     "main.obj -m64 -o main.exe -lWs2_32"
;
;           Output size: 47.3 KB (48,463 bytes)
;
; 3. GoLink.EXE:
;     "/entry main /console main.obj MSVCRT.dll Ws2_32.dll"
;
;           Output size:  2.50 KB (2,560 bytes)
;
; ------------------------------------------------------------ ;

...

; Code section contains:
; 1. Procedure: main - entry point
; 2. Procedure: netInit
; 3. Procedure: netDeInit
; 4. Procedure: netGetIpByName

Code: [Select]
; -----------------------------------------------------------;
;
; Procedure: netGetIpByName
;
; RCX - name.
; RDX - name len.
;
; Usage:
;           ...
;           txt_name: db "www.google.com",0
;           txt_name_len equ ($-txt_name)
;           ...
;
;           mov rcx,qword (txt_name)
;           mov rdx,qword (txt_name_len)
;           call netGetIpByName
;           jc .quit
;
;           mov rcx,rax
;           call inet_ntoa
;
;           mov rcx,rax
;           call printf
;
; Returns ip4 address into RAX (sockaddr_in.sin_addr).
; Set's carry flag on error.
; Clear's carry flag on success.
;
; -----------------------------------------------------------;
align 8
netGetIpByName:

      ; Early check
      xor rax,rax
      cmp rcx,rax
      je .earlyquit
      cmp rdx,rax
      jne .proceed
.earlyquit:
      stc
      ret
.proceed:

      ; Save arg
      mov qword [rsp+8*1],rcx
      mov qword [rsp+8*2],rdx

      ; Create stack
      push rbp
      mov rbp,rsp
      push rbx
      push r12
      push r13
      lea rsp,[rsp-(8*5)]

      ; 1. Save regsiters
      ; Save src name address
      ; Save src name len
      mov rbx,rcx
      mov r12,rdx
      xor r13,r13

      ; 1.1. Allocate memory
      mov rcx,r12
      add rcx,byte (1)
      call malloc
      jc .quitError

      ; Save dst name address
      mov r13,rax

      ; 1.2. Copy src name into dst name buffer
      mov r8,r12 ; rcx - len
      mov rdx,rbx ; rdx - src name address
      mov rcx,r13 ; rax - dst name address
      call memcpy

      ; 2. getaddrinfo
      lea r9,[rsp+(8*4)] ; Reference
      xor r8,r8
      xor rdx,rdx
      mov rcx,r13
      call getaddrinfo

      ; 2.1. Check against zero, means -> success:
      xor rdx,rdx
      cmp rax,rdx
      jne .quitError

      ; Check result
      xor rax,rax
      mov rbx,qword [rsp+(8*4)]
      cmp rbx,rax
      je .quitError
      mov rbx,qword [rbx+addrinfo.ai_addr]
      cmp rbx,rax
      je .quitError
      mov rbx,qword [rbx+sockaddr_in.sin_addr]
      cmp rbx,rax
      je .quitError

.quitSuccess:

      ; Set error indicator
      mov r12b, byte (0)
      jmp .quit

.quitError:

      ; Set error indicator
      mov r12b, byte (1)

.quit:

      ; Release dst name:
      xor rax,rax
      cmp r13,rax
      je .releaseDN
      mov rcx,r13
      call free
.releaseDN:

      ; Release reference:
      xor rax,rax
      mov r13,qword [rsp+(8*4)]
      cmp r13,rax
      je .releaseRef
      mov rcx,r13
      call freeaddrinfo
.releaseRef:

      ; Check for error
      cmp r12b, byte (0)
      je .onSuccess
.onError:
      ; Set error
      stc
      jmp .finalize
.onSuccess:

      ; Set return value
      mov rax,rbx
      ; Set success
      clc
.finalize:

      ; Clear stack n quit
      lea rsp,[rsp+(8*5)]
      pop r13
      pop r12
      pop rbx
      pop rbp
      ret

This output is produced by main.exe file:
Code: [Select]
[code begin]

173.194.40.244

[code end]


Added attachment, named: "WIN64_GETIPBYNAME_NASM_GCC_GOLINK.zip", contains a source file, "main.asm".

Video demonstration: Watch full source code and runtime video on youtube, named: Win64 Get ip address by name NASM GCC GOLINK

Bye, Encryptor256!!!
Encryptor256's Investigation \ Research Department.

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Win64 Get ip address by name NASM, GCC, GOLINK
« Reply #1 on: November 24, 2013, 01:32:11 PM »
Hello!

Main example code is all right, tested, but,
after posting it...

I was programming also some other socket things and i found, that,
when stack is not aligned properly, then socket functions might fail,
and it did, spent some hour before i found that my stack were not
aligned, like it should be, during socket function calls.

Function, getaddrinfo, returned bad result: 11001, until i found that alignment problem.

Bye, Encryptor256!!!
Encryptor256's Investigation \ Research Department.