This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
A problem with sendto
- To: "'bug-gcc at prep dot ai dot mit dot edu'" <bug-gcc at gnu dot org>
- Subject: A problem with sendto
- From: "Eerola, Olavi" <olavi dot eerola at insta dot fi>
- Date: Wed, 13 Sep 2000 12:52:44 +0300
- Cc: "Rouhu, Ilpo" <ilpo dot rouhu at insta dot fi>, "Piipponen, Jari" <jari dot piipponen at insta dot fi>
Hello,
The following test program stopped working in the new operating system.
Compilers:
2.95.2 and 2.7.2
Makefile:
# 2.7.2
CC = /usr/local/bin/gcc
CFLAGS = -g -ansi -Wall -Wshadow -Wcast-align -Wpointer-arith \
-Waggregate-return -Wmissing-prototypes -Wmissing-declarations
mcast: mcast.o
$(CC) -o $@ mcast.o -lsocket -lnsl
Operation system:
Solaris6 and Solaris8; Solaris5 is OK.
Result:
> uname -a
SunOS uj003sa 5.6 Generic_105181-14 sun4u sparc SUNW,Ultra-60
>
> mcast
sendto: Address family not supported by protocol family
sendto: Address family not supported by protocol family
sendto: Address family not supported by protocol family
sendto: Address family not supported by protocol family
sendto: Address family not supported by protocol family
>
Program:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sockio.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
typedef unsigned long net_addr_t;
static char *buf;
static int passive_ip_socket( char *service, int qlength, char *protocol );
static int passive_ip_socket( char *service, int qlength, char *protocol )
{
short port;
int sockout, type;
struct servent *sent;
struct protoent *pent;
struct sockaddr_in saddr;
(void)memset( &saddr, 0, sizeof( saddr ) );
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl( INADDR_ANY );
if ( !service || *service == '\0' )
saddr.sin_port = htons( (unsigned short)0 );
else if ( sscanf( service, "%hd", &port ) == 1 )
saddr.sin_port = htons( (unsigned short)port );
else if ( (sent = getservbyname( service, protocol )) )
saddr.sin_port = htons( (unsigned short)sent->s_port );
else
return -1;
if ( !(pent = getprotobyname( protocol )) )
return -1;
if ( !strcmp( protocol, "udp" ) )
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
if ( (sockout = socket( PF_INET, type, pent->p_proto )) < 0 )
return -1;
if ( bind( sockout, (struct sockaddr *)&saddr, sizeof( saddr ) ) < 0 ) {
(void)close( sockout );
return -1;
};
if ( type == SOCK_STREAM && listen( sockout, qlength ) < 0 ) {
(void)close( sockout );
return -1;
};
return sockout;
}
static int udp_write( int fd, const void *buffer, unsigned int nbyte,
unsigned long addr, unsigned short port );
static int udp_write( int fd, const void *buffer, unsigned int nbyte,
unsigned long addr, unsigned short port )
{
int len;
struct sockaddr_in saddr;
saddr.sin_addr.s_addr = addr;
saddr.sin_port = htons( (unsigned short)port );
len = sendto( fd, buffer, (int)nbyte, 0,
(struct sockaddr *)&saddr, sizeof( saddr ) );
if ( len < 0 )
perror( "sendto" );
else if ( len != nbyte )
(void)fprintf(stderr, "sendto: data truncated %d --> %d", nbyte, len
);
return len;
}
int main ( int argc, char *argv[] )
{
int v, i, fd;
short uport;
net_addr_t uaddr;
struct ip_mreq ipm;
assert( sizeof(net_addr_t) == sizeof(struct in_addr) );
uport = 9876;
uaddr = inet_addr( "235.235.235.235" );
fd = passive_ip_socket( "", 0, "udp" );
if ( fd < 0 ) {
perror( "passive_ip_socket" );
return 1;
};
ipm.imr_multiaddr.s_addr = uaddr;
ipm.imr_interface.s_addr = INADDR_ANY;
v = setsockopt( fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char *)&ipm, sizeof( ipm ) );
if ( v )
perror( "setsockopt/IP_ADD_MEMBERSHIP" );
v = 1000;
buf = (char *)malloc( v );
for ( i = 0; i < v; i++ )
buf[i] = (i % 10) + '0';
for ( i = 0; i < 5; i++ )
udp_write( fd, buf, v, uaddr, uport );
return 0;
}
Olavi Eerola
olavi.eerola@insta.fi