attach message at runtime


 
Thread Tools Search this Thread
Top Forums Programming attach message at runtime
# 1  
Old 06-08-2012
attach message at runtime

Hello,


I have a client C and server C, the client connects to the server and sends a message. At runtime I want to attach to the message sent, a couple (x, y)

Do you know a mechanism that allows me this?


Thank you.
Cordially.
# 2  
Old 06-08-2012
By couple(x,y) do you mean a struct of int?

Please define explictly what you mean by couple.

The answer is yes, but you probably want to start using a messge headers, that way everybody can decode what the incoming message is.

example:
Code:
the header template is nnnnXX[data]
where nnnn is the length of the packet,  XX  is a pre-agreed upon text code to tell you what the [data] is.

0010DT[raw integer] 
This says "I sent you a raw integer":  the way it is stored memory (4 bytes) 
 because the header is 6 bytes long 6 + 4 = 10 which is the whole length of the entire packet.

0011TXAABfd
I sent you an eleven byte message, the last five are text characters.

you decide what to use in XX. AK is commonly used as "I got it ok", NK (NAK) means I did not get your message. You could make your header template nnnnXXX[data] if you want more descriptive mesage types.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 06-08-2012
yes a struct of int,

To be more precise:

I want at run time, redirect the socket and attach on it a couple(struct (1,2) for example) and at the reception I will save this information

According to my research, I have to use the library interposition mechanism, LD-preload


Have you an idea about this mechanism if it really allows me to attach the couple at runtime?

Thank you so much for help
# 4  
Old 06-08-2012
Yes and no. LD_PRELOAD is a very ugly hack, and shouldn't be used if there's any other possible way. Why can't you just modify your original program to send headers?

LD_PRELOAD can replace entire system calls with your own code -- indiscriminately. If you override write(), you override every single write and any program probably calls write() for quite a few things other than that socket. It might even open sockets you're not aware of, like a UNIX domain socket for logging and that sort of thing.

So it's not as simple as "add header". You need to check if the file descriptor for your fake send() call is a socket or not, and if it is, send headers. Afterwards, call send() directly.

But, if it's not your program, how do you know it calls send() once for per data item? What if it packs ten into one send()? Then you'll have to actually process the data yourself in your fake send() call, inserting 10 headers.

What if it sends in indiscriminate blocks, or worse, one number at a time? Your fake send() call might get three and a half chunks, half a chunk, a single integer, and have to remember these jumbles for later somehow, making you have to worry about threading and all sorts of IPC junk.

Would it be possible to have this program just connect to a different socket instead? Have that program connect to the actual destination, and process data on the way through. This will be far more straightforward than LD_PRELOAD.

Last edited by Corona688; 06-08-2012 at 03:42 PM..
# 5  
Old 06-08-2012
Quote:
Why can't you just modify your original program to send headers?
i have juste the executable so i can't modify my original program

Quote:
But, if it's not your program, how do you know it calls send() once for per data item? What if it packs ten into one send()? Then you'll have to actually process the data yourself in your fake send() call, inserting 10 headers.
i'd like to use for example a library linked to my program , if there is a send or a receive it will redirect it and attach the couple

i want to do like that (i found it in a research)
Quote:
Therefore, we have developed a library which is dynamically loaded by each job using the library interposition mechanism available on Linux
systems during job submission. This mechanism works by initializing the LD PRELOAD environment variable with the desired shared library. Once submitted, all the job are dynamically linked against this library, and all communication-related system calls are redirected. In the used library we
currently redirect all the send() and receive() requests in
order to attach/extract the couple transparently and save it .
# 6  
Old 06-08-2012
Quote:
Originally Posted by chercheur857
i'd like to use for example a library linked to my program , if there is a send or a receive it will redirect it and attach the couple
I've already explained in detail why that's a bad idea -- please read it.

1) If you did this with a proxy, you could just recv() as much as you wanted and send() as much as you want, but when intercepting system calls, there's no guarantee you get what you actually want. Just because you're reading data in x-size chunks doesn't mean it's being written in x-size chunks. Your fake send() call may need to be extremely complex.

2) Just because send() is called doesn't mean it's a send() call you actually wanted to intercept.

3) It's plagued with security problems.

4) It's far harder than it looks, to the point that it's turned out to be literally impossible for most other folks I've tried to help with it. "overloading gettimeofday()", for instance, turned out to mean "overloading gettimeofday(), 9 other time-related functions, select(), half of lpthread, and recompiling". Altering the way a program works without recompiling it can cause it work strangely -- or, more to the point -- not work. Writing 17 bytes when it actually asked to write 12 can have more side-effects than you would have suspected.
Quote:
I want to do like that (i found it in a research)
Yes, you've made it clear. It's just not a good idea.

If you're hellbent on doing so, check out the source code to libkeepalive. It'll show you how to build a library you can LD_PRELOAD.

Last edited by Corona688; 06-08-2012 at 05:31 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Metadevice Too Small To Attach

Hi All, I am having trouble with my metadevices and I hope someone can help. when i try to attach my submirror i get the following error: root@xxxx>metattach d31 d21 metattach: tbair1: d21: submirror too small to attach root@xxxx> However, There is a submirror on the other disk which is... (3 Replies)
Discussion started by: A-Train
3 Replies

2. Emergency UNIX and Linux Support

xm block-attach

Hi guys. I'm studding XEN virtualization with 'The Book Of XEN'. in page 46 it has a statement about how to use xm block-attach command that I don't understand. here is the command: xm block-attach 0 tap:aio:/opt/xen/anthony.img /dev/xvda1 w 0and here is sentence: we are attaching anthony.img... (1 Reply)
Discussion started by: majid.merkava
1 Replies

3. Programming

attach is not a member of ofstream

Hi, Code written in C++ got compiled successfully using Sun 4.2 Compiler on Solaris 6. As part of migration, i am using same code and trying to compile using Sun 5.8 C++ compiler(Sun Studio11) on Solaris 10 and could not compile the below line, outStr.attach(1); // here outStr is declared... (1 Reply)
Discussion started by: shafi2all
1 Replies

4. Solaris

Help with Update on attach

Hi All, Can any-one explain me how update on attach works on solaris zones. Here is the situation . I am trying to migrate a zones xx-xxx-xxx from Global zone A to Global zone B. Here is the error message zoneadm is displaying zoneadm: zone 'xx-xx-xxx': ERROR: attempt to downgrade... (0 Replies)
Discussion started by: rama krishna
0 Replies

5. Shell Programming and Scripting

Can not attach using mailx

hi I know this topic has been discussed a lot in the forum but still couldnt get a exact solution to problem i am facing I have a CSV file genertaed on Unix box and want to be sent to my email . mailx -s "TEST" "xyz@hotmail.com" < 1.csv cat 1.csv | mailx -s "TEST" "xyz@hotmail.com" ... (10 Replies)
Discussion started by: ultimatix
10 Replies

6. Programming

How to limit max no of message in a posix message queue

Hii can anyone pls tell how to limit the max no of message in a posix message queue. I have made changes in proc/sys/fs/mqueue/msg_max But still whenever i try to read the value of max. message in the queue using attr.mq_curmsgs (where struct mq_attr attr) its giving the default value as 10.... (0 Replies)
Discussion started by: mohit3884
0 Replies

7. Shell Programming and Scripting

attach 2 files using mailx

if test.dat is the file cat test.dat|uuencode test.dat|mailx -s "subject" mailid can be used for attaching test.dat how can i attach more than one file to a mail using mailx (2 Replies)
Discussion started by: anumkoshy
2 Replies

8. AIX

Printers.hpJetDirect.attach

Can someone email me a copy of this file? Our AIX 5 cds are at our hotsite and I need a copy to try a fix on a printer. flyingdutchman1978@gmail.com Thanks! (0 Replies)
Discussion started by: schism25
0 Replies

9. UNIX for Dummies Questions & Answers

How to attach a file in mail?

Hi, I want to know how to send a mail in unix using mailx or mail or sendmail to send a mail with a file attached. I have read the answers from the other threads but I still don't understand. I want to do it in the command line as well as in script. Can anyone help? DO I need to use... (1 Reply)
Discussion started by: firebirdonfire
1 Replies
Login or Register to Ask a Question