FLIPS and UDP broadcast
Tiago Jorge
tjpj at lasige.di.fc.ul.pt
Thu Mar 17 15:16:43 CET 2005
hello again to you all...
i'm trying to broadcast through a LAN using the socket interface
provided by FLIPS with a small test program, here is the code:
##################################################
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <l4/log/l4log.h>
#define PORTNUMBER 5824
/*Sets the socket to non-blocking*/
void unblock_socket(int sd);
void unblock_socket(int sd)
{
int flags;
/* get current socket flags */
if ((flags=fcntl(sd, F_GETFL)) == -1)
{
LOG("GETFL");
exit(1);
}
/* set socket to non-blocking */
flags |= O_NDELAY;
if (fcntl(sd, F_SETFL, flags) == -1)
{
LOG("SETFL");
exit(1);
}
}
int main (int argc, char** argv)
{
int socketDesc; /* Socket Descriptor */
int addrLength;
int i;
struct sockaddr_in destinationAddr,
myAddr,
responseAddr;
struct hostent *hostNameBufPtr;
char outMessageBuf[128];
char inMessageBuf[128];
int trueFlag = 0x1;
int size_sent = 0;
int recvfrom_error;
/* Create socket from which to send */
if ((socketDesc = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
LOG("open error on socket");
exit(1);
}
/* BIND the socket on our end to any available port (i.e.
leave it up to the system to assign a port (used for
replys */
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
myAddr.sin_port = htons(PORTNUMBER); /* The zero says bind to any
port */
if (bind(socketDesc, (struct sockaddr *) &myAddr, sizeof(myAddr))
< 0) {
perror("bind error");
exit(1);
}
/* And build BROADCAST address */
destinationAddr.sin_family = AF_INET;
destinationAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
destinationAddr.sin_port = htons(PORTNUMBER);
/* Set the socket to permit multicasts on the local LAN */
if (setsockopt(socketDesc, SOL_SOCKET, SO_BROADCAST, &trueFlag,
sizeof(trueFlag)) != 0) {
LOG("setsockopt error");
exit(1);
}
//COMMENTED
//unblock_socket(socketDesc);
for(i=0; i<2; i++){
/* Broadcast message */
sprintf(outMessageBuf,"%d", i);
if ((size_sent = sendto(socketDesc, outMessageBuf,
strlen(outMessageBuf)+1, 0,
(struct sockaddr *)&destinationAddr, sizeof(destinationAddr))) < 0) {
LOG("socket send error");
exit(1);
}
}
LOG("size sent: %d\n", size_sent);
return 0;
}
##################################################
i've tried out the mini_http example with mini_ifconfig and works out
fine with this menu.lst:
title Testes
root (hd0,0)
kernel /home/tiago/fiasco_builddir/rmgr -sigma0
modaddr 0x02000000
module /home/tiago/fiasco_builddir/main -nokdb -nowait
-serial_esc -comspeed 115200 -comport 1
module /home/tiago/fiasco_builddir/sigma0
module /home/tiago/fiasco_builddir/names
module /home/tiago/fiasco_builddir/dm_phys
module /home/tiago/fiasco_builddir/l4io
module /home/tiago/fiasco_builddir/name_server
module /home/tiago/fiasco_builddir/flips-lxdrv
module /home/tiago/fiasco_builddir/mini_ifconfig eth0
10.10.5.197 255.255.0.0
module /home/tiago/fiasco_builddir/mini_http
but when i add my program, replacing mini_http, built with the same libs
and configs as mini_http, gives me an error when sends the packet.
LOG("socket send error");
am i doing this right? does flips support broadcast UDP? i've hacked
mini_ifconfig and i see that the eth0 flags are ok for broadcasting...
what am i doing wrong?
thanks in advance
Tiago
More information about the l4-hackers
mailing list