Ticket #13 (new defect)

Opened 2 years ago

Last modified 2 years ago

Permission denied on broadcast

Reported by: gdurand Assigned to: chris
Priority: major Milestone:
Component: core-vm Version:
Keywords: socket broadcast Cc:

Description

When I execute the code below with broadcast address as parameter (127.255.255.255 for instance) I have the following exception:

java.io.IOException: send failed: Permission denied
        at java.net.PlainDatagramSocketImpl.send(Native Method)
        at java.net.DatagramSocket.send(Unknown Source)
        at Test.main(Test.java:25)
        at Test.main(Test.java:11)
        at java.lang.reflect.Method.invoke0(Native Method)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at wonka.vm.Init.main(Unknown Source)

It does not occur with non-broadcast addresses, and it works fine on Sun jvm, whatever the address.

import java.io.*;
import java.net.*;

public class Test {
	
  public static void main(String[] args){
   int port = 2010;
   String addr = "127.255.255.255";
   if(args.length == 2){
     port = Integer.parseInt(args[1]);
   }
   if(args.length > 0){
     addr = args[0];
   }
		
   try{
     DatagramSocket socket = new DatagramSocket(port);
     InetAddress address = InetAddress.getByName(addr);
     byte[] buf = {0, 0, 0, 0, 0};
     DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
     socket.send(packet);
     socket.close();
    	
    } catch (Exception e){
      e.printStackTrace();
    }
  }
}

Change History

07/04/07 10:24:22 changed by gdurand

I found the problem: the SO_BROADCAST option was not set when creating the socket. The w_socket definition used in network.h was not the inline one (which sets SO_BROADCAST) but the #define w_sochet(x,y,z) socket(x,y,z).

If I add setsockopt(sock,SOL_SOCKET, SO_BROADCAST, &i, sizeof(int)); in PlainDatagramSocketImpl?_nativeCreate my problem is fixed. But it might not be the proper solution...