recv

å称
     recv, recvfrom, recvmsg — ã½ã±ããããã¡ãã»ã¼ã¸ãåä¿¡ãã

ã©ã¤ãã©ãª
     Standard C Library (libc, -lc)

æ¸å¼
     <sys/types.h> <sys/socket.h> ssize_t recv(int s, void *buf, size_t len,
     int flags) ssize_t recvfrom(int s, void * restrict buf, size_t len, int
     flags, struct sockaddr * restrict from, socklen_t * restrict fromlen)
     ssize_t recvmsg(int s, struct msghdr *msg, int flags)

解説
     recvfrom() 㨠recvmsg()
     ã·ã¹ãã ã³ã¼ã«ã¯ãã½ã±ããããã®ã¡ãã»ã¼ã¸ãåä¿¡ããã®ã«ä½¿ç¨ããã¾ãã
     ã½ã±ãããæ¥ç¶æåã§ãããã©ããã«ããããããã½ã±ããä¸ã®ãã¼ã¿ã åä¿¡ããã®ã«ä½¿ç¨ã§ãã¾ãã

     from ã NULL ãã¤ã³ã¿ã§ãªããã½ã±ãããæ¥ç¶æåã§ãªãå ´åã
     ããã«ã¯ã¡ãã»ã¼ã¸ã®ã½ã¼ã¹ã¢ãã¬ã¹ãä¿åããã¾ãã fromlen å¼æ°ã¯å¤ã¨çµæã®å¼æ°ã§ããã
     from ã«å¯¾å¿ãããããã¡ã®ãµã¤ãºã«åæåããã æ»ãæã«ã¯ä¿å‐
     ãããã¢ãã¬ã¹ã®å®éã®ãµã¤ãºã示ãããã«å¤æ´ããã¾ãã

     recv() ã·ã¹ãã ã³ã¼ã«ã¯ãé常 æ¥ç¶ããã ã½ã±ããä¸ã ãã§ä½¿ç¨ãã (connect(2)
     ãåç§)ã from å¼æ°ã« NULL ãã¤ã³ã¿ãæå®ãã recvfrom() ã¨åä¸ã§ãã
     ããã¯åé·ãªã®ã§ãå°æ¥ã®ãªãªã¼ã¹ã§ã¯ãµãã¼ããããªãå¯è½æ§ãããã¾ãã

     ããã 3 ã¤ã®ã«ã¼ãã³ã¯æ£å¸¸ã«å®äºããã¨ã¡ãã»ã¼ã¸ã®é·ããè¿ãã¾ãã
     ã¡ãã»ã¼ã¸ãé·ããã¦æå®ã®ãããã¡ã«åã¾ããªãå ´åã
     ã¡ãã»ã¼ã¸ãåä¿¡ããã½ã±ããã®ã¿ã¤ãã«ãã£ã¦ã¯ è¶éåã®ãã¤ããç ´æ£ããããã¨ãããã¾ã
     (socket(2) ãåç§)ã

     ã½ã±ããã«ã¡ãã»ã¼ã¸ãç¡ãå ´åã¯ãã½ã±ãããéããããã³ã° (fcntl(2) ãåç§)
     ã®å ´åãé¤ããå¼ã³åºãã¯ã¡ãã»ã¼ã¸ãå°çããã®ãå¾ã¡ã¾ãã ã½ã±ãããéããããã³ã°ã®å ´åãå¤
     -1 ãè¿ãããå¤é¨å¤æ° errno ã EAGAIN ã«è¨å®ããã¾ãã
     é常ãåä¿¡å¼ã³åºãã¯è¦æ±ãããéãåä¿¡ããã¾ã§å¾ããã«ã
     è¦æ±ãããéãä¸éã¨ãã¦å¾ããããã¼ã¿ãè¿ãã¾ãã ãã®åä½ã¯ã getsockopt(2)
     ã§è§£èª¬ããã¦ããã½ã±ããã¬ãã«ã®ãªãã·ã§ã³ SO_RCVLOWAT ããã³ SO_RCVTIMEO
     ã«ãã£ã¦å½±é¿ãåãã¾ãã

     次ã®ãã¼ã¿ããã¤å°çããããå¤å®ããã«ã¯ select(2) ã·ã¹ãã ã³ã¼ã«ã使ããã¨ãã§ãã¾ãã

     recv() é¢æ°ã¸ã® flags å¼æ°ã¯ã次ã®å¤ã® 1 ã¤ã¾ãã¯è¤æ°ã®è«çå (or) ããæãã¾ã:

           MSG_OOB         ããã»ã¹å¸¯åå¤ãã¼ã¿
           MSG_PEEK        çä¿¡ã¡ãã»ã¼ã¸ã®è¦ãè¦ (peek)
           MSG_WAITALL     è¦æ±ã®å®å¨ãªå®è¡ãã¾ãã¯ã¨ã©ã¼ãå¾ã¤
           MSG_DONTWAIT    ãããã¯ããªã

     MSG_OOB ãã©ã°ã¯å¸¯åå¤ãã¼ã¿ã®åä¿¡ãè¦æ±ãã
     é常ã®ãã¼ã¿ã¹ããªã¼ã ããã¯åä¿¡ãã¾ããã
     æ¥éãã¼ã¿ãé常ã®ãã¼ã¿å¾ã¡è¡åã®åé ã«éç½®ãããããã³ã«ãããã¾ããã
     ãã®ãã©ã°ã¯ãã®ãããªãããã³ã«ã§ã¯ä½¿ç¨ã§ãã¾ããã MSG_PEEK
     ãã©ã°ã¯åä¿¡å¾ã¡è¡åã®åé ãããã¼ã¿ãé¤å»ãããã¨ãªãã ãã®ãã¼ã¿ãè¿ãã¾ãã
     ãããã£ã¦ãå¾ç¶ã®åä¿¡å¼ã³åºãã¯åããã¼ã¿ãè¿ãã¾ãã MSG_WAITALL
     ãã©ã°ã¯è¦æ±ãå®å¨ã«æºããããã¾ã§ãããã¯ããããã«è¦æ±ãã¾ãã
     ããããã·ã°ãã«ãææãããå ´åãã¨ã©ã¼ã¾ãã¯åæãçºçããå ´åã
     ã¾ãã¯åä¿¡ãã次ã®ãã¼ã¿ãè¿ãããã¿ã¤ãã¨ç°ãªã
     å ´åãå¼ã³åºãã¯è¦æ±ãããããå°ãªããã¼ã¿ãè¿ãå¯è½æ§ãããã¾ãã MSG_DONTWAIT
     ãã©ã°ã¯ããã©ã°ãæå®ããã¦ãªãã£ãããããã¯ãããããªæã«ã æ»ããã¨ãè¦æ±ãã¾ãã
     å©ç¨å¯è½ãªãã¼ã¿ãç¡ãå ´åã«ã¯ã errno ã EAGAIN ã«è¨å®ããã¾ãã
     ãã®ãã©ã°ã¯ãå³æ ¼ãª ANSI ã¾ã㯠C99 ã®ã³ã³ãã¤ã«ã¢ã¼ãã§ã¯å©ç¨ã§ãã¾ããã

     recvmsg() ã·ã¹ãã ã³ã¼ã«ã¯ãç´æ¥ã«æå®ãããå¼æ°ã®æ°ãæå°ã«ããããã« msghdr
     æ§é ä½ã使ç¨ãã¾ãã ãã®æ§é ä½ã¯ <sys/socket.h>
     ã§å®ç¾©ããã¦ããããã«ã次ã®å½¢å¼ã«ãªã£ã¦ãã¾ã:

     struct msghdr {
             caddr_t msg_name;       /* ã¢ãã¬ã¹ (ãªãã·ã§ã³) */
             u_int   msg_namelen;    /* ã¢ãã¬ã¹ã®ãµã¤ãº */
             struct  iovec *msg_iov; /* ã¹ãã£ãã¿ / ã®ã£ã¶ã¼éå */
             u_int   msg_iovlen;     /* msg_iov ã®è¦ç´ æ° */
             caddr_t msg_control;    /* è£å©ãã¼ã¿ãå¾è¿° */
             u_int   msg_controllen; /* è£å©ãã¼ã¿ã®ãããã¡é· */
             int     msg_flags;      /* åä¿¡ãããã¡ãã»ã¼ã¸ä¸ã®ãã©ã° */
     };

     ãã㧠msg_name 㨠msg_namelen
     ã¯ãã½ã±ãããæ¥ç¶ããã¦ããªãå ´åã«ãå®åã¢ãã¬ã¹ãæå®ãã¾ãã
     ååãè¦æ±ããªãå ´åãå¿è¦ã§ãªãå ´åã msg_name 㯠NULL ãã¤ã³ã¿ã¨ãã¦æå®ã§ãã¾ãã
     msg_iov 㨠msg_iovlen å¼æ°ã¯ read(2) ã§èª¬æããã¦ããããã«ã¹ãã£ãã¿ /
     ã®ã£ã¶ã¼ã®å ´æãè¨è¿°ãã¾ãã msg_control å¼æ°ã¯ãé·ãã msg_controllen
     ã®ãä»ã®ãããã³ã«å¶å¾¡ã«é¢é£ããã¡ãã»ã¼ã¸ã¾ãã¯ãã®ä»ã®
     å種è£å©ãã¼ã¿ç¨ã®ãããã¡ãæãã¦ãã¾ãã ã¡ãã»ã¼ã¸ã¯æ¬¡ã®å½¢å¼ã§ã:

     struct cmsghdr {
             u_int   cmsg_len;       /* ãã¼ã¿ãã¤ãã«ã¦ã³ããhdr ãå«ã */
             int     cmsg_level;     /* ã¡ãã»ã¼ã¸ãçæãããããã³ã« */
             int     cmsg_type;      /* ãããã³ã«ã«åºæã®ã¿ã¤ã */
     /*      u_char  cmsg_data[]; ãå¾ã«ç¶ã */
     };

     ãã¨ãã°ãããã使ç¨ã㦠XNS/SPP ã«ãã㦠ãã¼ã¿ã¹ããªã¼ã ã®å¤åãç¥ããã¨ãã§ãã¾ãã
     ã¾ããISO ã«ãã㦠accept() ã·ã¹ãã ã³ã¼ã«ã®ç´å¾ã«ããã¼ã¿ãããã¡ãä¼´ããã«
     recvmsg() ãè¦æ±ãã¦ã ã¦ã¼ã¶æ¥ç¶è¦æ±ãã¼ã¿ãå¾ããã¨ãã§ããã§ãããã

     ãªã¼ãã³ãã¡ã¤ã«è¨è¿°åã¯ãã㧠AF_UNIX
     ãã¡ã¤ã³ã½ã±ããç¨ã®è£å©ãã¼ã¿ã¨ãã¦å¼ã渡ããããã®éã cmsg_level ã SOL_SOCKET
     ã«è¨å®ããã cmsg_type ã SCM_RIGHTS ã«è¨å®ããã¾ãã

     SCM_CREDS ã® cmsg_type ã使ç¨ãã¦ãããã»ã¹ã®èªè¨¼æå ±ã AF_UNIX
     ãã¡ã¤ã³ã½ã±ããç¨ã®è£å©ãã¼ã¿ã¨ã㦠渡ããã¨ãã§ãã¾ãã ãã®ã±ã¼ã¹ã§ã¯ã cmsg_data
     ã¯ãæ§é ä½ cmsgcred ã§ããå¿è¦ãããã¾ãã ããã¯æ¬¡ã®ããã« <sys/socket.h>
     åã§å®ç¾©ããã¦ãã¾ã:

     struct cmsgcred {
             pid_t   cmcred_pid;             /* éä¿¡ããã»ã¹ã® PID */
             uid_t   cmcred_uid;             /* éä¿¡ããã»ã¹ã®å® UID */
             uid_t   cmcred_euid;            /* éä¿¡ããã»ã¹ã®å®å¹ UID */
             gid_t   cmcred_gid;             /* éä¿¡ããã»ã¹ã®å® GID */
             short   cmcred_ngroups;         /* ã°ã«ã¼ãã®æ° */
             gid_t   cmcred_groups[CMGROUP_MAX];     /* ã°ã«ã¼ã */
     };

     ã«ã¼ãã«ã¯éä¿¡ããã»ã¹ã®èªè¨¼æå ±ãè¨å¥ãããããåä¿¡å´ã¸éä¿¡ãã¾ãã

     msg_flags ãã£ã¼ã«ãã¯åä¿¡æ¸ã¿ã¡ãã»ã¼ã¸ã«å¾ã£ã¦æ»ãæã«è¨å®ããã¾ãã MSG_EOR ã¯
     end-of-recordãã¤ã¾ãè¿ããããã¼ã¿ã§ã¬ã³ã¼ãã å®çµãã¦ãããã¨ã示ãã¾ã
     (ä¸è¬ã«ã¯ãã¿ã¤ã SOCK_SEQPACKET ã®ã½ã±ããã¨ã¨ãã«ä½¿ç¨ããã¾ã)ã MSG_TRUNC
     ã¯ãæä¾ããããããã¡ãããã¼ã¿ã°ã©ã ã大ããã£ãã®ã§ã
     ãã¼ã¿ã°ã©ã ã®å¾ãã®é¨åãåãæ¨ã¦ããããã¨ã示ãã¾ãã MSG_CTRUNC
     ã¯ãè£å©ãã¼ã¿ç¨ã®ãããã¡åã®ç©ºéã®ä¸è¶³ã®ããã«ããããã®å¶å¾¡ãã¼ã¿ã
     åãæ¨ã¦ããããã¨ã示ãã¾ãã MSG_OOB ã¯ãæ¥éã¾ãã¯å¸¯åå¤ãã¼ã¿ãåä¿¡ããããã¨ã示ãã¾ãã

æ»ãå¤
     ãããã®å¼ã³åºãã¯åä¿¡ãããã¤ãæ°ãè¿ããã¨ã©ã¼ãèµ·ããå ´å㯠-1 ãè¿ãã¾ãã

ã¨ã©ã¼
     å¼ã³åºãã¯æ¬¡ã®å ´åã«å¤±æãã¾ã:

     [EBADF]            å¼æ° s ãæå¹ãªè¨è¿°åã§ã¯ããã¾ããã

     [ENOTCONN]         ã½ã±ããã¯æ¥ç¶æåãããã³ã«ã¨çµã³ä»ãããã¦ãã¾ãããæ¥ç¶ããã¦ãã¾ãã
                        (connect(2) 㨠accept(2) ãåç§)ã

     [ENOTSOCK]         å¼æ° s ã¯ã½ã±ãããåç§ãã¦ãã¾ããã

     [EMSGSIZE]         recvmsg() ã·ã¹ãã ã³ã¼ã«ã¯ãæ¥ç¶æã«ãã¼ã¿ãããã¨ããã権å©
                        (ãã¡ã¤ã«è¨è¿°å) ãåä¿¡ããããã«ä½¿ç¨ããã¾ããã
                        ããããªãããåä¿¡ããã°ã©ã ã«ã¯åãä»ãããã㮠空ããã¡ã¤ã«è¨è¿°å‐
                        ã¹ããããããã¾ããã§ããã ãã®å ´åãè¨è¿°åãã¯ãã¼ãºããã recvmsg()
                        ã¸ã®å¥ã®å¼ã³åºãã«ãã£ã¦ã ã©ããªãã³ãã£ã³ã° (æªè§£æ±º)
                        ã®ãã¼ã¿ãè¿ããã¨ãã§ãã¾ãã

     [EAGAIN]           ã½ã±ãããéããããã³ã°ã¨ãã¼ã¯ããã¦ããã¨ãã åä¿¡æä½ã§ãããã¯ãã¾ããã
                        ãããã¯ãåä¿¡ã¿ã¤ã ã¢ã¦ããè¨å®ããã¦ãã¦ã
                        ãã¼ã¿ãåä¿¡ãããåã«ã¿ã¤ã ã¢ã¦ãã«ãªãã¾ããã

     [EINTR]            ãã¼ã¿ãåä¿¡å¯è½ã«ãªãåã«ãåä¿¡ãã·ã°ãã«ã«ãã£ã¦å²è¾¼ã¾ãã¾ããã

     [EFAULT]           åä¿¡ãããã¡ãã¤ã³ã¿ããããã»ã¹ã«å²ãå½ã¦ãããã¢ãã¬ã¹ç©ºéã®
                        ç¯å²å¤ãæãã¦ãã¾ãã

é¢é£é ç®
     fcntl(2), getsockopt(2), read(2), select(2), socket(2)

æ´å²
     recv() é¢æ°ã¯ 4.2BSD ã§ç»å ´ãã¾ããã