(* The result of this may be subsequently retrieved using InetDomainSocket().
Assume that any existing socket was deleted when its owner terminated. Create
a fresh Inet Domain socket and save the handle, on failure save an error code.
Both success and failure are silent.
*)
procedure CreateInetDomainSocket(port: integer);
var
sockAddr: TSockAddr;
begin
// TODO : This could usefully announce what IP addresses are being listened on.
// Scenario: the host computer is restarted, and comes up with a DHCP-allocated
// address which no longer corresponds to that being assumed by other WatchXxx
// instances assuming it's the supervisor.
Write(StdErr, 'Creating new Internet domain socket (UDP, port ', port, ')... ');
inetDomainSocketHandle := fpSocket(PF_INET, SOCK_DGRAM, 0);
try
sockAddr.sin_family:= AF_INET;
sockAddr.sin_port:= hToNS(port);
sockAddr.sin_addr.s_addr:= 0;
FillChar(sockAddr.sin_zero[0], SIZEOF(sockAddr.sin_zero), #0);
if fpBind(inetDomainSocketHandle, @sockAddr, SIZEOF(sockAddr)) = 0 then
WriteLn(StdErr, 'OK')
else begin
WriteLn(StdErr, 'failed, might need CAP_DAC_OVERRIDE,CAP_NET_BIND_SERVICE,CAP_NET_RAW=p+e');
inetDomainSocketHandle := -Abs(ErrNo)
end
except
WriteLn(StdErr, 'failed, might need CAP_DAC_OVERRIDE,CAP_NET_BIND_SERVICE,CAP_NET_RAW=p+e');
inetDomainSocketHandle := -Abs(ErrNo)
end
(* Assume that this will be deleted automatically at program termination. *)
end { CreateInetDomainSocket } ;