News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

structures and winsock!

Started by marco1974, March 30, 2005, 05:44:19 PM

Previous topic - Next topic

marco1974

Hi ,

I have try`d for 8 hours to get some data from win32 functions that return a structure.
Now i realize i am stuck!

ahhh zeroes and ones everywhere, man i feel so confused.

Can someone point me back on the track again?
Thanx!

here is some sample:
-----------------------

; define some stuff
m_servent servent <>
m_hostent hostent <>
buf db 255 dup (?)                          ;buffer pour le MessageBox
SMTP_buffer db 255 dup (?)

push offset m_service      ; db "smtp",0
push offset m_protocol   ; db "tcp",0
call  getservbyname

; returns a servent structure in EAX
; the interesting stuff is in servent.s_name

assume EAX:ptr servent
mov SMTP_buffer,[EAX].s_name

invoke MessageBox,0,addr SMTP_buffer, addr SMTP_buffer, MB_OK
some strange thing:
stos [EAX].s_name works really strange!
i feel so confused!

bye and happy coding!

mnemonic

I´m not really sure but it looks like you´re having some pointer confusion...

If servent.s_name is a pointer to a string and you save that value in SMTP_buffer, you should not call MessageBox with "addr SMTP_buffer" but with "SMTP_buffer" since that already is the address of the string.

change:
invoke MessageBox,0,addr SMTP_buffer, addr SMTP_buffer, MB_OK
to:
invoke MessageBox,0, SMTP_buffer, SMTP_buffer, MB_OK
and it should do the expected output.

Regards
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

Ghirai

Quote from: marco1974 on March 30, 2005, 05:44:19 PMmov SMTP_buffer,[EAX].s_name

You should do a:

invoke lstrcat,addr SMTP_buffer,[eax].s_name
MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

marco1974

Thanx for your support.

I have some real strange error, Internal assembler error.
U should see the source, its a funny error crashing the assemler.

:-(

Its so hard to get data from a function returning a structure;
tcp db "tcp"" ,0
smtp db "smtp",0

buffer1 db 256 dup(0)
buffer2 db 256 dup(0)

push offset smtp
push offset tcp
call getservicebyname
assume eax:ptr servent
mov buffer1,[eax].s_name
mov buffer2,[eax].s_port

if this works i could use data from this type of functions that return a structure.

Look at the source.

;orginal = the original smtp source that emails a smtp message to hotmai; :-)













[attachment deleted by admin]

Ghirai

You have in your code:


push offset m_service
push offset m_protocol
call  getservbyname


The api looks like this:

struct servent* FAR getservbyname(
  const char* name,
  const char* proto
);


You are pushing params in the wrong order.

Why don't you use invoke?
MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

thomasantony

Hi,
  s_name is a DWORD value. You don't need a buffer of 256 bytes for that, a register or a DWORD variable will be suffice. You are not storing the whole string but only a pointer to it. So it is enough to do

mov eax,[eax].s_name
invoke MessageBox,0,eax,eax,MB_OK


Thomas
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

marco1974

I try`d to get it to work but i dont get any data.

here the sample
--------------------------
push offset m_service
push offset m_protocol
call  getservbyname
; struct * sp=getservicebyname("smtp","tcp");

assume eax:ptr servent
;mov eax,[eax].s_name      ;s_port etc
assume edi:BYTE
mov edi,[eax].s_name
mov SMTP_zeelandnet,byte ptr [edi]

invoke messagebox,null, ADDR SMTP_zeelandnet, ADDR SMTP_zeelandnet,MB_OK

it seems so hard to get the values.
maybe am i overlooking something?





marco1974

So far i try`d a different way:

Buffer dw ? dup(0)


push offset m_service
push offset m_protocol
call  getservbyname
; struct * sp=getservicebyname("smtp","tcp");
;==============================
assume eax:ptr servent
assume edi:dword
mov edi,[eax].s_name      ; << fatal error

;===================================
; s_name is a simple char * s_name = "mail.server.com",0
;===================================
invoke lstrcpy,offset buffer,edi

Hmm, what am i missing inhere?
any clues?

mnemonic

Quote from: marco1974 on March 31, 2005, 07:07:03 PMHmm, what am i missing inhere?
any clues?
If I had the full source I could help you test it. This way I can only guess.
Btw, there are some open questions by board members you didn´t answer.
Invoke is a good thing to know and have.


;Buffer dw ? dup(0)  << what the heck is this?
Buffer dw 256 dup(?) ; that way it should be ok

push offset m_service
push offset m_protocol
call  getservbyname
; struct * sp=getservicebyname("smtp","tcp");
;==============================
assume eax:ptr servent
assume edi:dword            ; << Unnecessary, edi IS dword size. What are you trying to achieve here?
mov edi,[eax].s_name      ; << fatal error

;===================================
; s_name is a simple char * s_name = "mail.server.com",0
;===================================
invoke lstrcpy,addr buffer,edi

There are some things which I don´t really understand...
I didn´t look up the API for getservicebyname so you have to make sure you push the parameters in the correct order.
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

marco1974

Ok u are right.


I like to use push and call sometimes.
I could use invoke more often. :-)

here is the source.

Its good documented and easy to read.
I am really gratefull about all the comments and help i get from u guys!
Thanx again!


Here is the updated source.


Next version will be flavored with invoke ;-)


[attachment deleted by admin]

mnemonic

Ok, I made a quick debugging session and one thing is for sure: getservbyname returns NULL which means that an error occured.
You really should do some error checking. That´s just a good style of coding. You neither check WSAStartup nor getservbyname for a successful return what I really urge you to do.
You just have to insert some IF blocks to check whether there was success or not.

Do you have a debugger? I use OllyDebug which I think is a handy toy. Get it and use it :wink

I can´t do more for you at the moment because I have to get some sleep (5 ´o clock in the morning here...).
But now you know where the failure comes from.

Regards
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

marco1974

I have reworked the source. Its v10+ by now  :P

Anyway i have some output from getservicebyname!
see the source.

Its not the comlete server-string i was looking for but its a start!
I have ported it from my cpp source.
almost done with that time consuming process  :eek

Quote
struct servent * sp = ::getservbyname("smtp", "tcp");
; result in [eax].member ;
this one is easy!
;-----------------------
m_sockADDR.sin_port = sp->s_port;  //PORT 25 could be hard-coded.
becomes: mov m_sockADDR.sin_port,[eax].s_port
; sort of :-)
;=======================================
; this is tricky another structure 2 deal with
m_servent=gethostbyname(sp->s_name);   //"mail.zeelandnet.nl"); or system smtp???   
;
assume eax:ptr servent

; this is hard to specify
assume eax:ptr somestruc
mov [eax].sin_addr.s_addr,dword ptr [m_servent].h_addr;

m_sockADDR.sin_addr.s_addr=*((unsigned long *) m_servent->h_addr);

decoding from cPP2asm to masm systax is hard sometimes.
But when this works in the near future, i dont need to search for a server,
just ask the os and set it up for the user.






[attachment deleted by admin]

marco1974

sudden breaktrough!

see the source.
now working on this part:

c++
m_sockADDR.sin_port = sp->s_port;  //PORT << working on it!

how do i store a ushort (s_port) in m_sin.sin_port wich is a
sockaddr_in structure.?


;mov m_sin.sin_port,[eax].s_port
; mov edi,[eax].s_port
; mov m_sin.sin_port,edi

if this is finished There wil be no need to setup COMPLICATED mail server.
just enter mail to , mail from , subject and your message and mail.!!!

The cPP code finds the smtp server and sets up stuff.
:U  hide technical stuff from the user.



Man u guys are so GREAT! U have helped me numerous of times.

Here is v11.000000 haahahaha
have fun!





[attachment deleted by admin]

mnemonic

Since you cannot get to the low byte of EDI as with EAX/AH/AL how about using the stack?
Alternatively you could use some different general purpose register like EDX where you can access the low byte easy.
The following assembles just fine:
push [eax].s_port
pop [m_servent].s_port

Also there is a macro out there called m2m which you might use as if you were moving from register to register.
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way