getprotoent_r

GETPROTOENT_R(3)           Linux Programmer's Manual          GETPROTOENT_R(3)



åå
       getprotoent_r, getprotobyname_r, getprotobynumber_r - ãããã³ã«
       ã¨ã³ããªãåå¾ãã (ãªã¨ã³ãã©ã³ãç)

æ¸å¼
       #include <netdb.h>

       int getprotoent_r(struct protoent *result_buf, char *buf,
                       size_t buflen, struct protoent **result);

       int getprotobyname_r(const char *name,
                       struct protoent *result_buf, char *buf,
                       size_t buflen, struct protoent **result);

       int getprotobynumber_r(int proto,
                       struct protoent *result_buf, char *buf,
                       size_t buflen, struct protoent **result);

   glibc åãã®æ©è½æ¤æ»ãã¯ãã®è¦ä»¶ (feature_test_macros(7)  åç§):

       getprotoent_r(), getprotobyname_r(), getprotobynumber_r():
           _BSD_SOURCE || _SVID_SOURCE

説æ
       é¢æ° getprotoent_r(), getprotobyname_r(), getprotobynumber_r() ã¯ããããã
       getprotoent(3), getprotobyname(3), getprotobynumber(3)
       ã®ãªã¨ã³ãã©ã³ãçã§ããã protoent
       æ§é ä½ã®è¿ãæ¹ã¨ãé¢æ°å¼ã³åºãæã®å¼ãæ°ã¨è¿ãå¤ãç°ãªãã
       ãã®ããã¥ã¢ã«ãã¼ã¸ã§ã¯ããªã¨ã³ãã©ã³ãã§ãªãé¢æ°ã¨ã®éãã ãã 説æããã

       ãããã®é¢æ°ã¯ãé¢æ°ã®çµæã¨ãã¦éçã«å²ãå½ã¦ããã protoent æ§é ä½
       ã¸ã®ãã¤ã³ã¿ãè¿ãã®ã§ã¯ãªãã protoent æ§é ä½ã result_buf ã
       æãå ´æã«ã³ãã¼ããã

       éå buf ã¯ãè¿ããã protoent æ§é ä½ãæãæååãã£ã¼ã«ãã æ ¼ç´ããã®ã«ä½¿ç¨ããã
       (ãªã¨ã³ãã©ã³ãã§ãªãé¢æ°ã®å ´åã¯ã ãããã®æååã¯éçãªé åã«æ ¼ç´ããã)ã
       ãã®éåã®å¤§ãã㯠buflen ã§æå®ãããã buf ãå°ããããå ´åãé¢æ°å¼ã³åºãã¯ã¨ã©ã¼
       ERANGE ã§å¤±æãã å¼ã³åºãå´ã§ã¯ãã£ã¨å¤§ããªãããã¡ã§å度å¼ã³åºãå¿è¦ããã
       (ã»ã¨ãã©ã® ã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãé·ã 1024 ãã¤ãã®ãããã¡ã§ååãªã¯ãã§ãã)ã

       é¢æ°å¼ã³åºãã§ãããã³ã«ã¬ã³ã¼ãã®åå¾ã«æåããã¨ã *result 㯠result_buf
       ãæãããã«è¨å®ãããã ãã以å¤ã®å ´å㯠*result ã« NULL ãè¨å®ãããã

è¿ãå¤
       æåããã¨ããããã®é¢æ°ã¯ 0 ãè¿ããã¨ã©ã¼ã®å ´åããã¨ã©ã¼ãã®ç¯ã® ãªã¹ãã«ããæ‐
       £ã®ã¨ã©ã¼çªå·ã®ãããããè¿ãã

       ã¨ã©ã¼ã®å ´åãã¬ã³ã¼ããè¦ã¤ãããªãã£ãå ´å (getprotobyname_r(),
       getprotobyaddr_r()) ããã以ä¸ã¬ã³ã¼ãããªãå ´å (getprotoent_r())ãresult
       ã«ã¯ NULL ãè¨å®ãããã

ã¨ã©ã¼
       ENOENT (getprotoent_r())  ãã¼ã¿ãã¼ã¹ã«ãã以ä¸ã¬ã³ã¼ãããªãã

       ERANGE buf ãå°ããããããã£ã¨å¤§ããªãããã¡ã«ã㦠(ã¾ã㯠buflen ãå¢ããã¦)
              å度å¼ã³åºããã¨ã

æºæ
       ãããã®é¢æ°ã¯ GNU ã«ããæ¡å¼µã§ããã ä»ã®ã·ã¹ãã ã«ãåæ§ã®ååã®é¢æ°ãå‐
       å¨ããå ´åããããã é常ã¯é¢æ°ã®å¼ãæ°ãç°ãªãã

ä¾
       以ä¸ã®ããã°ã©ã ã¯ã getprotobyname_r() ã使ã£ã¦ãæåã®ã³ãã³ã
       ã©ã¤ã³å¼ãæ°ã§æå®ãããååã®ãããã³ã«ã®ã¬ã³ã¼ããåå¾ããã
       äºçªç®ã®ã³ãã³ãã©ã¤ã³å¼ãæ° (æ´æ°å¤) ãæå®ãããå ´åã¯ã ãã®å¤ã buflen
       ã®åæå¤ã¨ãã¦ä½¿ç¨ãããã getprotobyname_r() ãã¨ã©ã¼ ERANGE ã§å¤±æããã¨ããã‐
       ã°ã©ã 㯠ãã大ããªãããã¡ãµã¤ãºã§å度 getprotobyname_r ãå¼ã³åºãã
       ä¸è¨ã®ã·ã§ã«ã®ã»ãã·ã§ã³ã¯ãå®è¡ä¾ã示ãã¦ããã

           $ ./a.out tcp 1
           ERANGE! Retrying with larger buffer
           getprotobyname_r() returned: 0 (success)  (buflen=78)
           p_name=tcp; p_proto=6; aliases=TCP
           $ ./a.out xxx 1
           ERANGE! Retrying with larger buffer
           getprotobyname_r() returned: 0 (success)  (buflen=100)
           Call failed/record not found

   ããã°ã©ã ã®ã½ã¼ã¹

       #define _GNU_SOURCE
       #include <ctype.h>
       #include <netdb.h>
       #include <stdlib.h>
       #include <stdio.h>
       #include <errno.h>
       #include <string.h>

       #define MAX_BUF 10000

       int
       main(int argc, char *argv[])
       {
           int buflen, erange_cnt, s;
           struct protoent result_buf;
           struct protoent *result;
           char buf[MAX_BUF];
           char **p;

           if (argc < 2) {
               printf("Usage: %s proto-name [buflen]\n", argv[0]);
               exit(EXIT_FAILURE);
           }

           buflen = 1024;
           if (argc > 2)
               buflen = atoi(argv[2]);

           if (buflen > MAX_BUF) {
               printf("Exceeded buffer limit (%d)\n", MAX_BUF);
               exit(EXIT_FAILURE);
           }

           erange_cnt = 0;
           do {
               s = getprotobyname_r(argv[1], &result_buf,
                            buf, buflen, &result);
               if (s == ERANGE) {
                   if (erange_cnt == 0)
                       printf("ERANGE! Retrying with larger buffer\n");
                   erange_cnt++;

                   /* Increment a byte at a time so we can see exactly
                      what size buffer was required */

                   buflen++;

                   if (buflen > MAX_BUF) {
                       printf("Exceeded buffer limit (%d)\n", MAX_BUF);
                       exit(EXIT_FAILURE);
                   }
               }
           } while (s == ERANGE);

           printf("getprotobyname_r() returned: %s  (buflen=%d)\n",
                   (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
                   strerror(s), buflen);

           if (s != 0 || result == NULL) {
               printf("Call failed/record not found\n");
               exit(EXIT_FAILURE);
           }

           printf("p_name=%s; p_proto=%d; aliases=",
                       result_buf.p_name, result_buf.p_proto);
           for (p = result_buf.p_aliases; *p != NULL; p++)
               printf("%s ", *p);
           printf("\n");

           exit(EXIT_SUCCESS);
       }

é¢é£é ç®
       getprotoent(3), protocols(5)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



GNU                               2010-09-10                  GETPROTOENT_R(3)