From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 870E9BC6B for ; Wed, 10 Oct 2007 10:55:17 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.21,253,1188770400"; d="scan'208";a="2811835" Received: from estephe.inria.fr (HELO [128.93.11.95]) ([128.93.11.95]) by mail2-relais-roc.national.inria.fr with ESMTP; 10 Oct 2007 10:55:17 +0200 Message-ID: <470C9375.70807@inria.fr> Date: Wed, 10 Oct 2007 10:55:17 +0200 From: Xavier Leroy User-Agent: Thunderbird 1.5.0.7 (X11/20060915) MIME-Version: 1.0 To: cedilla+ocaml@gmail.com Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] UDP broadcast from Windows References: <470C2B72.3090704@gmail.com> In-Reply-To: <470C2B72.3090704@gmail.com> X-Enigmail-Version: 0.94.0.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; setsockopt:01 printf:01 printf:01 ocaml:01 notation:01 byte:01 bug:01 byte:01 wrote:01 unix:01 unix:01 caml-list:01 strings:01 data:02 defined:02 Reed Wilson wrote: > Hi guys! > > I'm in the middle of writing a program which does a UDP broadcast, but I > noticed it doesn't work on Windows. Here's the source: > > try ( > let sock = Unix.socket Unix.PF_INET Unix.SOCK_DGRAM 0 in > Unix.setsockopt sock Unix.SO_BROADCAST true; > let sent = Unix.sendto sock "ping" 0 4 [] (Unix.ADDR_INET > ((Unix.inet_addr_of_string "255.255.255.255"), 12345)) in > Printf.printf "Sent %d\n" sent; > ) with > | Unix.Unix_error (x,y,z) -> Printf.printf "%S - %s,%s\n" > (Unix.error_message x) y z > ;; > I looked around in the OCaml source, and it > looks like the broadcast address is specifically disallowed unless > HAS_INET_ATON is defined? Why is this? Quoting the Linux man page: The inet_addr() function converts the Internet host address cp from numbers-and-dots notation into binary data in network byte order. If the input is invalid, INADDR_NONE (usually -1) is returned. This is an obsolete interface to inet_aton(), described immediately above; it is obsolete because -1 is a valid address (255.255.255.255), and inet_aton() provides a cleaner way to indicate error return. Unfortunately, Windows does not provide any of the better substitutes for inet_addr, namely inet_aton and inet_pton. Send a bug report to Microsoft. > I noticed that when I force the address by replacing: > Unix.inet_addr_of_string "255.255.255.255" > > with: > Obj.magic "\255\255\255\255" > > it _seems_ to work fine. [...] > And am I breaking anything by using magic? Currently not. Inet addresses are represented internally as strings (of length 4 for IPv4, 16 for IPv6) containing the bytes of the address in network byte order. This is of course not guaranteed but unlikely to change soon. - Xavier Leroy