Author Topic: how access to structs?  (Read 9471 times)

Offline grimoire

  • Jr. Member
  • *
  • Posts: 6
how access to structs?
« on: July 20, 2013, 04:55:30 PM »
hi

can somebody explain me how tuse strutcs?, please

for example

struct sockaddr_in
.sin_family = AF_INET
.sin_port = 0xB922
.sin_addr = 0x0100007f
endstruc

but, how access to "sin_family, sin_port and sin_addr"?

is something like this?

mov eax, [sockaddr.sin_family]

regards

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: how access to structs?
« Reply #1 on: July 20, 2013, 07:52:19 PM »
Well, the "struc" (not "struct") keyword is essentially a "typedef". It does not initialize any memory.
Code: [Select]
struc sockaddr_in
    .sin_family resw 1
    .sin_port resw 1
    .sin_addr resd 1
    .sin_zero resb 8
endstruc
Essentially, it just assigns values to the names. sockaddr_in.sin_family = 0, sockaddr_in.sin_port = 2, sockaddr_in.sin_addr = 4, sockaddr_in.sin_zero = 8. It also calculates the size - sockaddr_in_size is 16, and you can use this value in your code.

To initialize a structure, you need "istruc"...
Code: [Select]
section .data
;...
pop_sa istruc sockaddr_in
    at sockaddr_in.sin_family, dw AF_INET ; these constants defined elsewhere
    at sockaddr_in.sin_port, dw POPPORT
    at sockaddr_in.sin_addr, dd POPIP
    at sockaddr_in.sin_zero, dd 0, 0
iend
These names need to be "in order", but you don't need to initialize them all. Any "skipped" names will be initialized to zero.

To use them in your code...
Code: [Select]
section .text
;...
mov [pop_sa + sockaddr_in.sin_addr], eax
This is just an example, you probably wouldn't really want to do that.

Note that we're using Nasm's "local label" mechanism - symbols starting with a '.'. These are not "true local labels" since they can be used outside of their "scope" (from one non-local label to the next) by using the "full name" - "sockaddr_in.sin_addr". This is potentially confusing since it looks like similar C syntax, but does something different. The C syntax "mystruc.mymember" would include the "+", but in Nasm syntax, "sockaddr_in.sin_addr" is just the value 4. We need to say "[pop_sa + sockaddr_in.sin_addr]" to specify the full address.

There is, of course, more information in the Friendly Manual. If you have trouble using this, show us what you've tried and what happened, and we'll try to help more.

Best,
Frank


Offline grimoire

  • Jr. Member
  • *
  • Posts: 6
Re: how access to structs?
« Reply #2 on: July 21, 2013, 05:51:15 AM »

I've trying to learn nasm since 2 weeks ago and there are to many things i don't know how use them.

I tried to use sockets but I had a problem with struc sockkaddr_in, so I solved it using values.

I know, is very newbie my code jeje

Code: [Select]
[BITS 32]
 
section .data
 
wsdll: db 'ws2_32.dll',0
wsaddr: dd '0xFFFFFFFF'
Getpc: dd '0xFFFFFFFF'
 
WStp: db 'WSAStartup',0
WSaddr: dd '0xFFFFFFFF'
WSD: dd 'WSADATA',0
saveWSA: dd '0xFFFFFFFF'
soc: db 'socket',0
soadd: dd '0xFFFFFFFF'
conn: db 'connect', 0
conaddr: dd '0xFFFFFFFF'
sen: db 'send', 0
sendadd: dd '0xFFFFFFFF'
 
res1: db "WSAStartup %d", 10, 0
res2: db "Socket %d", 10, 0
res3: db "connect %d", 10, 0
res4: db "send %d", 10 ,0
 
sre: db "hola mundo",0
 
section .bss
sn resb 10
 
section .text
 
global _WinMain@16
extern _ExitProcess@4
extern _LoadLibraryA@4
extern _GetProcAddress@8
extern _printf
extern _WSAStartup@8
 
_WinMain@16:
 
xor eax, eax
 
push wsdll
call _LoadLibraryA@4
mov [wsaddr], eax
 
push WStp
push dword [wsaddr]
call _GetProcAddress@8
mov [Getpc], eax
 
push WSD
push 0x202
call [Getpc]
mov [saveWSA], eax
 
cmp dword [saveWSA], -1
jne mns1
je erro
 
mns1:
push dword [saveWSA]
push res1
call _printf
 
push soc
push dword [wsaddr]
call _GetProcAddress@8
mov [Getpc], eax
 
push 6
push 1
push 2
call [Getpc]
mov [soadd], eax
 
push dword [soadd]
push res2
call _printf
 
push conn
push dword [wsaddr]
call _GetProcAddress@8
mov [conaddr], eax
 
push dword [conaddr]
push res3
call _printf
 
push 0x0100007F
push 0xB9220002
mov esi, esp
push byte 16
push esi
push dword [soadd]
call [conaddr]
 
erro:
push 0
call _ExitProcess@4

but thanks for the answer, I will practice now with the struc

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: how access to structs?
« Reply #3 on: July 22, 2013, 05:36:47 PM »
I wouldn't worry too much about "struc"s. Ultimately, it's all just "values" anyway. The "struc" mechanism is just a convenient(?) way to keep values grouped together and give them names. You never "have to" use it.

Does the above code work? Do you get plausible output from the "_printf"s?

There are some things that look "strange" to me. In particular:
Code: [Select]
wsaddr: dd '0xFFFFFFFF'
and similar. By putting quotes around '0xxFFFFFFFF', you're getting the text "0xFFFFFFFF", not the number 0xFFFFFFF (aka -1). Since you don't seem to use these values initialized, but overwrite them with "real dwords", it's probably going to work, but to get a "number", remove the quotes.

I'm not familiar with "LoadLibrary" and "GetProcAddress". I think it may be "the hard way", but there may be advantages to it. I don't know enough Windows programming, and can't (won't) run Windows to test your code.

This seems a little "advanced" for someone who has only been at it for a couple weeks. You "might" want to take smaller steps, at first. But if you can do it - go for it!

Best,
Frank