From owner-freebsd-current@freebsd.org Thu Aug 4 07:15:06 2016 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 1E85BBAE754 for ; Thu, 4 Aug 2016 07:15:06 +0000 (UTC) (envelope-from ed@nuxi.nl) Received: from mail-yw0-x234.google.com (mail-yw0-x234.google.com [IPv6:2607:f8b0:4002:c05::234]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id C94361899 for ; Thu, 4 Aug 2016 07:15:05 +0000 (UTC) (envelope-from ed@nuxi.nl) Received: by mail-yw0-x234.google.com with SMTP id u134so246339352ywg.3 for ; Thu, 04 Aug 2016 00:15:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nuxi-nl.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=pRi8MD35VdpxTfj38xA+lv2WIiW2wHBoskvjb/L6ER8=; b=zrAykTFYnKQfmI12jUwMskWllp+0+Crte4BuvGE7Ko+WX2JE0BzkIPy+u2f0v7NTWE NsaqmapFKFhnMPb9H3O+F0+IDB9q5u+18LwyEN00gwBS7UBI6G5B4J6RbxSPOvRzV2a8 7ZGiKhbxp1eXrwmeFvL7/ITXVpBAioKYjFUKQLuOXHX2vLJbbS5gzg0TACaii7I74Aek D8tebJi1T6w9XoFgrZIkrE6v8bK/PkC6XYs9W++a/4k9vv16CVFdVdAFBu4Gb+4xnCAG K82JwXvkhEhJoBCKj4fFB0tldoM4Kq83bdC/cmQx2RlfQga6I3lK/F9XMUJXSTZlA5aX vzkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=pRi8MD35VdpxTfj38xA+lv2WIiW2wHBoskvjb/L6ER8=; b=Jai5v1TrjRgzNiBowssouCfITZnlzq0/nOmWanlEs+HxxXcwh1cyPHX1U3XTDIghu4 njVBFM+2V6GYhy5Zzzlsq8yiXK9ySfl6ONJ64VJ6vq96p8KINrhY+elKXmcvSWyXt2nh T6b9xzQm9S+GPlTCa1KaW0azZy20Vw97CKrXpvymeIRAt2ZCkCJGSfdntIlrHT02ykqH /1bnpoKufcZk0BVsdL6trm1udh+HdKMU+pOeOx8ArEm25aOj8SGyZbpXy7RQgXPKqBCq jl+2Cxl3pmiBxsIPLUuHfbaivdlYpIzZqU9TXKfn2sb25lYl+B5uMgXeNjwfjawLVrZT 7VCQ== X-Gm-Message-State: AEkoousSp6J6ggMG4idtr1Ym3dd5WVNQnSvvXKjbbrKNrqMY5NzmNoQEoGc9uojFUjweQDolE6pCCz21/o1o4Q== X-Received: by 10.13.229.1 with SMTP id o1mr58028323ywe.95.1470294904747; Thu, 04 Aug 2016 00:15:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.13.201.71 with HTTP; Thu, 4 Aug 2016 00:15:03 -0700 (PDT) In-Reply-To: References: From: Ed Schouten Date: Thu, 4 Aug 2016 09:15:03 +0200 Message-ID: Subject: Re: Socket sendmsg() porting question To: "Lundberg, Johannes" Cc: Alan Somers , FreeBSD Current Content-Type: text/plain; charset=UTF-8 X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Aug 2016 07:15:06 -0000 2016-08-03 19:18 GMT+02:00 Lundberg, Johannes : > Even if I set cmsg_level and cmsg_type it won't let me send it. The problem > is having a zero length attachment on freebsd.... > I can't send -1 as fd because that errors to invalid file descriptor. Well, it makes sense. If you're attaching a message to a sendmsg() call, it should have contents that make sense. Also, msg_controllen should correspond with the actual space consumed by the message. Not a single byte more. Change the logic to one of the following two. Solution 1: Simply set msg_controllen to zero entirely, so there is no message attached when sending. struct cmsghdr *cmsg = CMSG_FIRSTHDR(&message); if (fd >= 0) { message.msg_controllen = CMSG_SPACE(sizeof(fd)); cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); } else { message.msg_controllen = 0; } Solution 2: Send a SCM_RIGHTS message that contains no file descriptors. struct cmsghdr *cmsg = CMSG_FIRSTHDR(&message); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; if (fd >= 0) { message.msg_controllen = CMSG_SPACE(sizeof(fd)); cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); } else { message.msg_controllen = CMSG_SPACE(0); cmsg->cmsg_len = CMSG_LEN(0); } Also worth mentioning: char control[CMSG_SPACE(sizeof(int))]; That line is incorrect. The reason for this is that it may not be sufficiently aligned. You have to make sure that the control message buffer is aligned to at least 'struct cmsghdr', as CMSG_FIRSTHDR() will do nothing more than return the buffer directly. Change that line to one of the following two: #include alignas(struct cmsghdr) char control[CMSG_SPACE(sizeof(int))]; Or: union { struct cmsghdr a; char b[CMSG_SPACE(sizeof(int))]; } control; Best regards, -- Ed Schouten Nuxi, 's-Hertogenbosch, the Netherlands KvK-nr.: 62051717