Thread: magic could happen on recvfrom

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    magic could happen on recvfrom

    Ok, this is so weird that i can't even think why this could happen:

    So I am trying to send a struct, which is a frame over UPD using the sendto, here's the code of my sender that shows where it sends and I am not given the code of the receiver, but I am asked to create one based on the binary executable that is given

    Code:
    /*Here's my struct*/
    typedef struct FrameHdr {
        u_char	type;		
        SeqNum	seq;		
        u_char	size;		     
        u_char	flags;		
    } FrameHdr;
    
    typedef struct Frame {
        FrameHdr	hdr;		
        u_char	body[256];	
    } Frame;
    
    
    /*Here's a snippet of my code*/
    Frame* frame = (Frame*) calloc(1, sizeof(Frame));
    fread(frame->body, 1 , 256, stdin);
    frame->hdr.type = TYPE_DATA;
    frame->hdr.seq = (SeqNum) LFS;
    sendto(sock_fd,(void*)frame ,sizeof(Frame),0,(struct sockaddr *)&sockin, sizeof(sockin));
    I'll provide more code if necessary. But here's where the weird thing started to happen. When I typed in from the stdin the following string and press enter:

    Tom Sawyer is a nice, playful boy, a natural showoff who likes to show his authority over other boys. He is around twelve years old as gathered from hints in Twain's works. Tom is supposed to represent the carefree and wonderful world of boyhood in the early-mid 1800s.

    clearly this sentence is more than 256 bytes, so I believe the sendto won't be able to send all the words. I think the sentence will be cut to (i.e: this is what I get when printing frame->body after the fread):

    Tom Sawyer is a nice, playful boy, a natural showoff who likes to show his authority over other boys. He is around twelve years old as gathered from hints in Twain's works. Tom is supposed to represent the carefree and wonderful world of boyhood in the ear

    However, here's the magical thing. When the receiver receives and prints this frame body I sent (the incomplete one), it could print the full sentence that I input in stdin although what I sent was not the complete one, the one I sent was missing the "ly-mid 1800s". Any idea how this could happen? Oh and yes I am pretty sure I only called sendto once..

    I can't find out as I don't know what the source code for the binary looks, the binary is already provided.
    Last edited by -EquinoX-; 03-26-2009 at 08:53 PM.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Does the receiver get the correct FrameHdr info as well? Could you provide more code?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I don't know if it gets the frame header, as I have no access to the receiver code... but I am pretty sure it does get the right FrameHdr info. Which part of the code would you like to see more? I can't post codes about the receiver as I don't have it....

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    here's more if you really want to:

    Code:
    while (checkFullSlots() ||  (!feof(stdin)))
    	{
    	  /*read data from stdin and fill any empty slots*/
    		while (checkEmptySlots() && (!feof(stdin))){
    			sigprocmask(SIG_BLOCK, &set , &oset);
    			LFS++;
    			Frame* frame = (Frame*) calloc(1, sizeof(Frame));
    			size = fread(frame->body, 1 , 256, stdin);
    			printf("size read to body is %d\n\n", size);
    			fwrite(frame->body , 1 , 256 , stdout);
    			/*
    			if (size != 256){
    				printf("Error: Can't read the frame body from stdin %d\n", (int) sizeof(frame->body));
    				close(sock_fd);
    				exit(1);
    			}
    			*/
    			frame->hdr.type = TYPE_DATA;
    			frame->hdr.seq = (SeqNum) LFS;
    			if (size == 256)
    				frame->hdr.size = 0;
    			else
    				frame->hdr.size = (u_char) size;
    
    			printf("seq number %d \nsize is %d \n", (int) frame->hdr.seq, (int) size);
    			if (feof(stdin)) 
    				frame->hdr.flags = FLAG_END;
    			
    			time_t frame_timeout = time(NULL) + timeout;
    			if (insertTimeoutAndFrame(frame, frame_timeout, LFS)){
    				perror("Error: Failed inserting frame and timeout into window slot \n");
    				exit(1);
    			}
    
    			flag = sendto(sock_fd,(void*)frame ,sizeof(Frame),0,(struct sockaddr *)&sockin, sizeof(sockin));
    			if (flag == -1)
    			{
    				perror("Error: Sending frame failed \n");
    				close(sock_fd);
    				exit(1);
    			}
    			printf("sending frame with size %d \n", flag);
    			sigprocmask(SIG_SETMASK, &oset, NULL);
    		}
    		
    		/*read ACK from socket (recv)*/
    	
    		
    			printf("Trying to receive ACK here!\n");
    			Frame *ACK = (Frame*) malloc(sizeof(Frame));
    			flag = recvfrom(sock_fd, ACK, sizeof(Frame), 0,  (struct sockaddr *) 0, (socklen_t *) 0);
    			if (flag == -1)
    			{
    				perror("Error: Receiving ACK failed \n");
    				close(sock_fd);
    				exit(1);
    			}
    			printf("Receiving ACK for seq %d\n", ACK->hdr.seq);
    			sigemptyset(&set);
    			sigaddset(&set, SIGALRM);
    
    			sigprocmask(SIG_BLOCK, &set , &oset);
    			if ((int) ACK->hdr.seq > LAR){
    				int i;
    				do {
    					printf("emptying slot!\n");
    					/*empty slot containing frame LAR*/
    					LAR++;
    					for (i = 0; i < SWS; i++){
    						if (win_slots[i].frame->hdr.seq == LAR){
    							free(win_slots[i].frame);
    							win_slots[i].frame = NULL;
    							win_slots[i].timeout = -1;
    						}
    					}	
    				} while (LAR != ACK->hdr.seq);
    			}
    			free(ACK);
    			printf("ready to send as there's an empty slot available!\n");
    			sigprocmask(SIG_SETMASK, &oset, NULL);
    		
    	}

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    You write out the body of the frame after reading it in. Does it print back out the truncated string?

    [edit: Scratch that, reread your post... still thinking]
    Last edited by neandrake; 03-26-2009 at 09:25 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    you mean this one?

    Code:
    fwrite(frame->body , 1 , 256 , stdout);
    if you mean that one then it prints out:
    Tom Sawyer is a nice, playful boy, a natural showoff who likes to show his authority over other boys. He is around twelve years old as gathered from hints in Twain's works. Tom is supposed to represent the carefree and wonderful world of boyhood in the ear

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    You do run this in a loop, so it should try to send the first 256 bytes, and then the next. Your check on the number of bytes read should fail at that point and not send the rest. Is this check somehow not passing?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by neandrake View Post
    You do run this in a loop, so it should try to send the first 256 bytes, and then the next. Your check on the number of bytes read should fail at that point and not send the rest. Is this check somehow not passing?
    Of course the check doesn't work because it's commented!

    Your code loops and sends the whole thing in 2 frames.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so are you saying it performs sendto twice?? I just uncommented that test and it goes through it once only... if it were sending it in 2 frames then it would perform sendto twice, but in fact it doesn't
    Last edited by -EquinoX-; 03-26-2009 at 09:36 PM.

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Yup, it should. Double check if your fwrites are not showing up if stdout needs flushed.

    [edit: also for printf()]
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    also if it sends it twice then it should print this part twice:

    Code:
    printf("seq number %d \nsize is %d \n", (int) frame->hdr.seq, (int) size);
    but it doesn't

  12. #12
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I made an edit above. Try calling fflush(stdout) after the loop.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    you mean after that sigprocmask?

  14. #14
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Outside the loop. After the } after the sigprocmask. Before the comment about /*Read ACK*/
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I really don't think that would have any affect, the whole thing inside the loop body is only executed once

    EDIT: I just tried and nothing really changed

    The code doesn't even reach the point after it breaks out of the while loop.. it still stays inside the loop waiting for my next input into stdin
    Last edited by -EquinoX-; 03-26-2009 at 09:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rubik's Magic?
    By nonoob in forum Game Programming
    Replies: 2
    Last Post: 09-27-2008, 10:17 AM
  2. Windows crashing Magic Square
    By KoshiB in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2006, 09:02 PM
  3. help on magic square
    By katway in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 06:44 PM
  4. Magic Squares!
    By ZAM777 in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2004, 03:34 PM
  5. magic square whoes
    By caws in forum C Programming
    Replies: 9
    Last Post: 03-30-2003, 10:36 PM