子網路遮罩轉換為IP範圍

來源:互聯網
上載者:User

子網路遮罩轉換為IP範圍 在網路應用中,經常需要將子網路遮罩轉換為IP範圍,以便進行進一步的計算.以下是轉換的原理及代碼:  www.2cto.com  一 子網路遮罩的作用, 就是將某個IP地址劃分為'子網編號'和'主機地址'   掩碼格式       [子網編號:26bit               ]主機地址:6bit   172.16.2.64/26 [10101100 00010000 00000010 01]000000   172.16.2.96/26 [10101100 00010000 00000010 01]100000   6bit的主機地址, 可以容納63個編號. 則上面兩個掩碼的範圍是   掩碼格式       IP範圍   172.16.2.64/26 [172.16.2.64 - 172.16.2.127]   172.16.2.96/26 [172.16.2.96 - 172.16.2.127] *上一條的子集 二 演算法  www.2cto.com  typedef struct{u_int32_t min;u_int32_t max;}ip_range_t;int str2iprange( ip_range_t *ipr, char *sipr){char sip[16];char smask[3];sscanf( sipr, "%[^/]/%s", sip, smask);ipr->min = ntohl(inet_addr( sip));ipr->max = (ipr->min & ~( (0x1 << (32 - atoi(smask))) - 1)) + ( (0x1 << (32 - atoi(smask))) - 1);}三 測試  www.2cto.com  ip_range_t ipr;str2iprange( &ipr, "172.16.2.94/24");struct in_addr min = { htonl( ipr.min)};printf("%s - ", inet_ntoa( min));struct in_addr max = { htonl( ipr.max)};printf("%s \n", inet_ntoa( max));輸出結果:172.16.2.94 - 172.16.2.127END
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.