/// <summary> /// Get the peer credentials from a given socket /// </summary> /// <param name="socket">Socket</param> /// <param name="pid">Process ID</param> /// <param name="uid">User ID</param> /// <param name="gid">Group ID</param> public static void GetPeerCredentials(this Socket socket, out int pid, out int uid, out int gid) { ucred cred = new ucred(); int credSize = Marshal.SizeOf <ucred>(); int error = Interop.getsockopt(socket.Handle.ToInt32(), Interop.SOL_SOCKET, Interop.SO_PEERCRED, ref cred, ref credSize); if (error < 0) { throw new IOException($"Failed to query peer credentials (error {Marshal.GetLastWin32Error()})"); } pid = cred.pid; uid = cred.uid; gid = cred.gid; }
internal static extern int getsockopt(int sockfd, int level, int optname, ref ucred optval, ref int optlen);