LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2009, 05:35 AM   #1
bizoo
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Rep: Reputation: 0
segmentation fault while sending UDP packets!


The following piece of code is suppose to send a UDP packet.
but inside function udpsocketinit , i get a segmentation fault and i can not understand why!!
please help me

Code:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
     
#define BUFLEN 512
#define NPACK 10
#define PORT 9930
#define SRV_IP "127.0.0.1"   
void udpsocketinit (struct sockaddr_in *sok)
{
 	struct in_addr *xyz;
//	memset((char *) sok, 0, sizeof(sok));
        sok->sin_family = AF_INET;
        sok->sin_port = htons(PORT);
	xyz=&(*sok).sin_addr;
        if (inet_aton(SRV_IP, xyz)==0)
 	{
          printf("the ip address is wrong\n");
	}

} 

     
int main(void)
 {
         struct sockaddr_in *si_other;
         int s, i, slen=sizeof(si_other);
         char buf[BUFLEN];
     
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
            printf("socket is not initialised\n");
	udpsocketinit(si_other);

    
        for (i=0; i<NPACK; i++)
	{
          printf("Sending packet %d\n", i);
          sprintf(buf, "This is packet %d\n", i);
          if (sendto(s, buf, BUFLEN, 0, si_other, slen)==-1)
       	  printf("socket bind failed!\n");
	}
    
        close(s);
        return 0;
}
 
Old 07-16-2009, 08:30 AM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Try this:
Code:
int udpsocketinit(struct sockaddr_in *sok)
{
    struct in_addr xyz;

    if (inet_aton(SRV_IP, &xyz) == 0)
    {
        printf("the ip address is wrong\n");
        return -1;
    }

    sok->sin_addr = xyz;
    sok->sin_family = AF_INET;
    sok->sin_port = htons(PORT);
    return 0;
}

int main(void)
{
    struct sockaddr_in si_other;
    int s, i, slen = sizeof(si_other);
    char buf[BUFLEN];
    int ret;

    ret = -1;

    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        printf("socket is not initialised\n");
        goto socket_err;
    }

    if (udpsocketinit(si_other))
    {
        goto init_err;
    }

    for (i = 0; i < NPACK; i++)
    {
        printf("Sending packet %d\n", i);
        sprintf(buf, "This is packet %d\n", i);
        if (sendto(s, buf, BUFLEN, 0, &si_other, slen) == -1)
        {
            printf("socket bind failed!\n");
            goto send_err;
        }
    }

    ret = 0;
    goto done;

send_err:
init_err:
done:
    close(s);

socket_err:
    return(ret);
}

Last edited by David1357; 07-16-2009 at 10:38 AM. Reason: Try to fix errors in main and add some error handling.
 
Old 07-16-2009, 08:42 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by bizoo View Post
struct sockaddr_in *si_other;
That just defines a pointer to that structure type. It does not make the pointer point at anything.

Quote:
int s, i, slen=sizeof(si_other);
That sets slen to the size of a pointer. Is that what you intend, or did you intend slen to be the size of the type of object that the pointer would point to?

Quote:
udpsocketinit(si_other);
You use that pointer as if it is the address of an actual object, but it is just an uninitialized pointer.
 
Old 07-16-2009, 08:43 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
In main you declare a pointer to struct sockaddr_in (si_other) that is never initialized (so pointing to somewhere in memory). Next you try (in udpsockinit) to assign a value to an element of that struct so writing to memory that more than likely is not 'yours'.
 
Old 07-16-2009, 11:19 AM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
bizoo,

I made some changes to my earlier post. They should fix your problems. However I have not tested them.

Good luck.
 
Old 07-17-2009, 03:33 AM   #6
bizoo
LQ Newbie
 
Registered: Jul 2009
Posts: 17

Original Poster
Rep: Reputation: 0
thanks all esp. David
Yup the problem indeed was initializing a pointer but not directing it to some struct
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Udp packets dropped while sending in RHEL4 chinoy Linux - Networking 1 08-21-2008 10:07 PM
UDP: Short Packets: and UDP bad checksum: entries in dmesg minutes2memories Linux - Networking 2 02-26-2006 07:28 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
encapsulating TCP packets in UDP packets... yoshi95 Programming 3 06-03-2004 02:53 PM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration