UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » How can we send data when its pointer.

#1 2007-09-11 09:03 AM

santoshkumar
Member
Registered: 2007-08-21
Posts: 53

Re: How can we send data when its pointer.

How can we send data when its pointer.
i have 

struct client_hello_info
{
        char *username;
        char macid[MAC_LENGTH];
        int  osid;                      
}my_client;

struct data_packet {
        int command_id;
        char * data;
        int msg_length;
 }my_data;

username=malloc(7);
strcpy(username, "santosh")
strcpy(macid, "11:..90");
copied myclient structure into my_data.client
my_data.client=malloc(len)
memcpy(my_data.client, myclient, len);

I have to send data_packet from client to server!
but thinking how to send data bec pointers become invalid in diff machines!

copying in big buffer and sending! tats doing twice here ! any better ways?

Offline

#2 2007-09-11 03:40 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: How can we send data when its pointer.

Hello,

Please next time use code TAGS when posting code.

I am not sure that I understand what you write. Is it that you want to know how to send a structure without having to copy the data to a big buffer?

If that's so, then it has been covered in this FAQ several times. If you don't want to have a separate big buffer (and do the copy operations), and both client and server are yours, then you can send element by element... For example:

struct some_struct {
   char name[FIXED_LEN];
   int len;
   char *somevalue;
};

...

void send_my_struct(int sd, struct some_struct *s) {
   send(sd, s->name, FIXED_LEN, 0);
   send(sd, s->len, sizeof(int), 0);
   send(sd, s->somevalue, len, 0);
}

This code just tries to show the basics, for example, it doesn't do any error checking and/or verifies that the data was completely sent (for example, not just a part of it). And of course, you wouldn't send an "int" like that to another machine. This is a good thread to learn about sending objects over sockets.

Regards,

Hector

Offline

#3 2007-09-13 01:53 AM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: How can we send data when its pointer.

and can we send a a pointer of this struct to the other end ?

Offline

#4 2007-09-13 02:14 AM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: How can we send data when its pointer.

zhn636;22626 wrote:

and can we send a a pointer of this struct to the other end ?

A pointer is a local (as in the same PC) variable that holds the address of some object in the process' own memory space. It doesn't even make sense to other processes inside the same machine, so there is no point in being able to send it to another computer.

If you just want to know whether it is possible to send the pointer to other computers, indeed it is possible, but as I wrote before, it wouldn't make any sense. (It is possible, because the address is just a number)

When you deal with stuff like RMI (java's Remote Method Invocation), sending an object by means of passing a reference to it to a method in a remote object, may send the whole object (a copy of it, similar to what was presented earlier), or send a reference to a remote object (if the object can be remote - i.e. implements the Remote interface). In the later case, you'd have a behavior similar to what you are asking for. However, if you dig into the specifics of the RMI implementation you'll find out that it is not a "pointer" to a remote object, but instead a copy of a stub object.

OK, I think I said enough.

Good luck.

Offline

#5 2007-09-13 02:28 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How can we send data when its pointer.

Loco;22627 wrote:

If you just want to know whether it is possible to send the pointer to other computers, indeed it is possible, but as I wrote before, it wouldn't make any sense. (It is possible, because the address is just a number)

Just to play Devil's Advocate, I can think of one possible reason why you might want to send a pointer to another computer.... so that that computer can then at a later time send the pointer back to you as a "handle".... i.e. a way to indicate the object it is referring to.  You'd be crazy to do that without verifying that the returned pointer was valid before using it, however (by looking it up in a hash table or something), and even then it might not be such a good idea since that object that the pointer pointed to might have been deleted and another object allocated at the same location...

Offline

#6 2007-09-13 02:50 AM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: How can we send data when its pointer.

jfriesne;22628 wrote:

Just to play Devil's Advocate, I can think of one possible reason why you might want to send a pointer to another computer.... so that that computer can then at a later time send the pointer back to you as a "handle".... i.e. a way to indicate the object it is referring to.  You'd be crazy to do that without verifying that the returned pointer was valid before using it, however (by looking it up in a hash table or something), and even then it might not be such a good idea since that object that the pointer pointed to might have been deleted and another object allocated at the same location...

That is valid, although I wouldn't do it that way. I'd keep a structure to hold a handle so I can validate quickly whether it's valid or not... But your point is very valid.

Regards,

Hector

Offline

#7 2007-11-25 06:26 PM

zamb
Member
Registered: 2007-11-25
Posts: 5

Re: How can we send data when its pointer.

santoshkumar;22611 wrote:

How can we send data when its pointer.
i have 

struct client_hello_info
{
        char *username;
        char macid[MAC_LENGTH];
        int  osid;                      
}my_client;

struct data_packet {
        int command_id;
        char * data;
        int msg_length;
 }my_data;

username=malloc(7);
strcpy(username, "santosh")
strcpy(macid, "11:..90");
copied myclient structure into my_data.client
my_data.client=malloc(len)
memcpy(my_data.client, myclient, len);

I have to send data_packet from client to server!
but thinking how to send data bec pointers become invalid in diff machines!

copying in big buffer and sending! tats doing twice here ! any better ways?

OFF TOPIC: (sorry)
Your code have a grave bug.  Here:

username=malloc(7);
strcpy(username, "santosh");

The length of "snatosh" is 7 bytes and the size of the "username" variable is only 7.  You didn't account for the '\0' at the end of the string which will lead to all sort of trouble in both stability and security!.

You must allocate at least "strlen(string) + 1".  In other words: in the above code you must allocate 8 bytes, not 7 since strlen("snatosh") is 7 bytes and the '\0' at the end is 1 byte.

When using "malloc()" and text strings always add an extra "1" byte to it and set the last byte to '\0' like this:

username = malloc(8);
strcpy(username, "santosh");
username[strlen(username)] = '\0';

(Or you could use

username = calloc(8, sizeof(char));
strcpy(username, "santosh");

to save yourself from the last command.)

That's all.
Ziyad.

Offline

#8 2007-11-25 06:55 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,834
Website

Re: How can we send data when its pointer.

Very valid point...  However, your null-termination code is a bit overly paranoid and
not entirely necessary...  strcpy() by itself will always null-terminate the destination,
so there's no need to manually do so...  (And, if it didn't, then your strlen() of the
destination would be a very bad thing to do at that point, anyway, because it requires
null-termination!)  Perhaps you were thinking of strncpy(), which may fail to null-terminate?

Of course, the simplest way to do what's wanted in a guaranteed proper manner is:

username = strdup ("santosh");

(And, check for failure (NULL), just as with all *alloc() functions, too...)

Offline

#9 2007-11-25 07:13 PM

zamb
Member
Registered: 2007-11-25
Posts: 5

Re: How can we send data when its pointer.

RobSeace;23159 wrote:

Very valid point...  However, your null-termination code is a bit overly paranoid and not entirely necessary...  strcpy() by itself will always null-terminate the destination, so there's no need to manually do so...  (And, if it didn't, then your strlen() of the destination would be a very bad thing to do at that point, anyway, because it requires null-termination!)

You're right!  I simply didn't think of that.

RobSeace;23159 wrote:

Perhaps you were thinking of strncpy(), which may fail to null-terminate?

No, I simply overlooked the obvious.  That's all.

RobSeace;23159 wrote:

Of course, the simplest way to do what's wanted in a guaranteed proper manner is:

username = strdup ("santosh");

(And, check for failure (NULL), just as with all *alloc() functions, too...)

Wow, that is nice!  I didn't know that (yes, I'm not that much onto C.  Shame, I know!).


By the way: This is a very nice forum!  I didn't know about it until today and I  already learned a few things by only reading 3 or 4 threads!

Thanks to all of you.
Ziyad.

Offline

#10 2007-12-04 01:50 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: How can we send data when its pointer.

Not to be a contrarian but...

In response to using a memory pointer as a handle - my thought is that you should NEVER use a raw memory pointer is such a way... either a hash against the data or a secure one way hash against the pointer value should be used...

Here is the rationale... consider where you are sending raw pointers AND it turns out your code has a bug e.g. an overflow bug that allows a malicious user to force data in a specific memory location(s) to be sent back...

In such a situation, then by giving them a raw pointer you are giving the attacker the EXACT location in process memory they ( may ) want to access and make the job of compromising that data that much easier... additionally it may then give them the information to iterate through the rest of in memory records e.g. think of a linked list node where the next record pointer is contained in the structure...

On the other hand - hashing it securely first doesn't give away any 'secrets'... and means they will have to coerce your system into dumping EVERYTHING... or best case - dump it incrementally starting at one end of the heap - figure out where the first segment of desired data it... and then move forward... which is a much harder task...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#11 2007-12-04 02:01 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How can we send data when its pointer.

mlampkin;23232 wrote:

In response to using a memory pointer as a handle - my thought is that you should NEVER use a raw memory pointer is such a way... either a hash against the data or a secure one way hash against the pointer value should be used...

I agree, it's not a very good practice... especially since you have no guarantee that if you delete an object on the server side and then later allocate a new one, you won't get the old pointer value back again.  In that case, a client might accidentally(?) access the new data object via the old object's pointer/handle, which isn't what you want.

Better to just give the server a "handle generator" based on an incrementing counter (with sufficient bits that the chances of wrap-around causing trouble are minimized).

Offline

#12 2018-02-25 07:51 PM

John
Guest

Re: How can we send data when its pointer.

You can pass the heap address to another process. In that way you dont need to pass a big buffer. Other process can read the heap of other process , this is done as part of late binding.

  • Index
  • » C
  • » How can we send data when its pointer.

Board footer

Powered by FluxBB