• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開(kāi)心的工作
            簡(jiǎn)單、開(kāi)放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            inet_ntop(), inet_pton()

            Posted on 2011-01-04 22:41 S.l.e!ep.¢% 閱讀(1957) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Unix

            inet_ntop(), inet_pton()

            Linux/uClinux 2008-11-18 14:50:06 閱讀463 評(píng)論0 ??字號(hào): ? 訂閱

            Convert IP addresses to human-readable form and back.

            Prototypes

            #include <arpa/inet.h>
            
            const char *inet_ntop(int af, const void *src,
                                  char *dst, socklen_t size);
            
            int inet_pton(int af, const char *src, void *dst);

            Description

            These functions are for dealing with human-readable IP addresses and converting them to their binary representation for use with various functions and system calls. The "n" stands for "network", and "p" for "presentation". Or "text presentation". But you can think of it as "printable". "ntop" is "network to printable". See?

            Sometimes you don't want to look at a pile of binary numbers when looking at an IP address. You want it in a nice printable form, like 192.0.2.180, or 2001:db8:8714:3a90::12. In that case, inet_ntop() is for you.

            inet_ntop() takes the address family in the af parameter (either AF_INET or AF_INET6). The src parameter should be a pointer to either a struct in_addr or struct in6_addr containing the address you wish to convert to a string. Finally dst and size are the pointer to the destination string and the maximum length of that string.

            What should the maximum length of the dst string be? What is the maximum length for IPv4 and IPv6 addresses? Fortunately there are a couple of macros to help you out. The maximum lengths are: INET_ADDRSTRLEN and INET6_ADDRSTRLEN.

            Other times, you might have a string containing an IP address in readable form, and you want to pack it into a struct sockaddr_in or a struct sockaddr_in6. In that case, the opposite funcion inet_pton() is what you're after.

            inet_pton() also takes an address family (either AF_INET or AF_INET6) in the af parameter. The src parameter is a pointer to a string containing the IP address in printable form. Lastly the dst parameter points to where the result should be stored, which is probably a struct in_addr or struct in6_addr.

            These functions don't do DNS lookups—you'll need getaddinfo() for that.

            Return Value

            inet_ntop() returns the dst parameter on success, or NULL on failure (and errno is set).

            inet_pton() returns 1 on success. It returns -1 if there was an error (errno is set), or 0 if the input isn't a valid IP address.

            Example

            // IPv4 demo of inet_ntop() and inet_pton()
            
            struct sockaddr_in sa;
            char str[INET_ADDRSTRLEN];
            
            // store this IP address in sa:
            inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));
            
            // now get it back and print it
            inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
            
            printf("%s\n", str); // prints "192.0.2.33"
            // IPv6 demo of inet_ntop() and inet_pton()
            // (basically the same except with a bunch of 6s thrown around)
            
            struct sockaddr_in6 sa;
            char str[INET6_ADDRSTRLEN];
            
            // store this IP address in sa:
            inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &(sa.sin6_addr));
            
            // now get it back and print it
            inet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN);
            
            printf("%s\n", str); // prints "2001:db8:8714:3a90::12"
            // Helper function you can use:
            
            //Convert a struct sockaddr address to a string, IPv4 and IPv6:
            
            char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
            {
                switch(sa->sa_family) {
                    case AF_INET:
                        inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
                                s, maxlen);
                        break;
            
                    case AF_INET6:
                        inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
                                s, maxlen);
                        break;
            
                    default:
                        strncpy(s, "Unknown AF", maxlen);
                        return NULL;
                }
            
                return s;
            }
            人妻无码精品久久亚瑟影视| 久久伊人精品一区二区三区| 伊人久久大香线蕉AV色婷婷色| 亚洲国产成人久久综合一| 亚洲中文字幕久久精品无码喷水 | 亚洲愉拍99热成人精品热久久 | 亚洲国产成人精品女人久久久 | 精品久久8x国产免费观看| 亚洲欧美成人综合久久久 | 久久精品国产69国产精品亚洲| 久久久久亚洲AV无码专区体验| 无码专区久久综合久中文字幕| 少妇高潮惨叫久久久久久| 亚洲精品无码久久千人斩| 无码人妻久久一区二区三区免费丨 | 国产成人无码精品久久久免费 | 婷婷久久综合九色综合九七| 亚洲国产成人久久笫一页| 久久免费视频1| 日本强好片久久久久久AAA| 国产V综合V亚洲欧美久久| 久久综合综合久久97色| 国産精品久久久久久久| 婷婷久久五月天| 97久久久精品综合88久久| 青草影院天堂男人久久| 一本色道久久88综合日韩精品 | 91精品国产综合久久四虎久久无码一级| 一本久久a久久精品综合夜夜| 青青久久精品国产免费看| 亚洲精品美女久久久久99| 成人久久精品一区二区三区| 久久久久国产一级毛片高清板| 日本五月天婷久久网站| 国产精品久久波多野结衣| 久久久久亚洲AV成人网| 欧美喷潮久久久XXXXx| 国产伊人久久| 国内精品久久人妻互换| 亚洲欧美日韩久久精品| 粉嫩小泬无遮挡久久久久久|