1 / 16

第 11 回ネットワークプログラミング

第 11 回ネットワークプログラミング. 中村 修. 今日のお題. 講義 IPv6 IPv6 概要 プロトコルの独立性 練習 1 : getaddrinfo --------- 休憩 -------------------------------- 実習:ミニプロを作る&相談タイム 19:30 まで. IPv4 アドレスと IPv6 アドレス. IPv4 アドレス 32bit IPv6 アドレス 128bit. IPv4 アドレス. アドレスの数は 2 の 32 乗 ( 約 42 億 ) 世界の人口の数より少ない(約 64 億)

ashlyn
Download Presentation

第 11 回ネットワークプログラミング

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第11回ネットワークプログラミング 中村 修

  2. 今日のお題 • 講義 • IPv6 • IPv6概要 • プロトコルの独立性 • 練習1:getaddrinfo ---------休憩-------------------------------- • 実習:ミニプロを作る&相談タイム • 19:30まで

  3. IPv4アドレスとIPv6アドレス • IPv4アドレス32bit • IPv6アドレス128bit

  4. IPv4アドレス • アドレスの数は2の32乗(約42億) • 世界の人口の数より少ない(約64億) • アドレスはコンピュータだけ!? • IPアドレスの表記 32bits 133. 27. 4. 212

  5. IPv6アドレス • アドレスの数は2の128乗(約340澗(カン))(340,282,366,920,938,463,463,374,607,431,768,211,456 ) • 1人あたり5垓(ガイ)個 • アドレスはコンピュータ以外のものにも使える • IPアドレスの表記 • 128bit を 16進数で表す • 4桁(16bit)ごとに 「 : (コロン)」 で区切る 3ffe:501:100c:e320:2e0:18ff:fe98:936d

  6. 階層的なプロトコル設計 process process process process TCP UDP transport layer ICMP IPv4 IGMP network layer ARP RARP data link layer hardware interface media

  7. 階層的なプロトコル設計 process process process process TCP UDP transport layer ICMP IPv6 IGMP network layer ARP RARP data link layer hardware interface media

  8. IPv4とIPv6のプログラミングの違い • IPv4の時は • 以下の構造体のメンバの情報を • struct hostent *hp; • struct sockaddr_in servaddr; • 以下の関数を用いて集め • hp = gethostbyname(hostname)); • 以下のように使っていた • connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

  9. IPv6、IPv4両方に対応するには • 以下の構造体のメンバの情報を • struct addrinfo hints, *ai ; • 以下の関数を用いてあつめ • getaddrinfo(hostname, port, &hints, &ai)); • 以下のように使う • socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); • connect(socketfd, ai->ai_addr, ai->ai_addrlen) ;

  10. addrinfo構造体 • <netdb.h> struct addrinfo { int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ int ai_family; /* PF_xxx */ int ai_socktype; /* SOCK_xxx */ int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ size_t ai_addrlen; /* length of ai_addr */ char *ai_canonname; /* canonical name for hostname */ struct sockaddr *ai_addr; /* binary address */ struct addrinfo *ai_next; /* next structure in linked list */ };

  11. getaddrinfo • getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res); • nodename=ホスト名, servname=ポート, hintsは情報のタイプに関するヒント, resは結果のaddrinfoのリストが格納される • 返り値:成功すると 0 を返し、失敗すると以下の非 0 のエラーコードのいずれかを返す。 • サーバの場合は • nodenameはNULL • hintsにAI_PASSIVEフラグを設定する

  12. getaddrinfoの取り扱い方 • 複数のアドレスが戻ってくる(RFC2553) • 帰って来る順番はDNSの実装に依存 • 帰ってきたそれぞれに対してconnectしてみる for(ai = ai_save; ai != NULL; ai->ai_next){ sockfd = socket(ai->ai_family, ai->ai_socktype,ai->ai_protocol); if(sockfd < 0){continue;} if(connect(sockfd, ai->ai_addr, ai->ai_addrlen)){ break;} close(sockfd); }

  13. freeaddrinfo • void freeaddrinfo(struct addrinfo *ai) • getaddrinfoで動的に割り当てられた領域を開放する

  14. gaistrerror • char * gai_strerror(int ecode); • getaddrinfoの返り値のエラーコードを人間に可読な文字列に変換する

  15. 練習:getaddrinfoに慣れよう • まず、以下のソース(IPv4版echoクライアント)を • 入手し、コンパイルして、読む • /home/kaizaki/osamuNP/11/echo_client_v4.c • 次にgetaddrinfoだけをしたサンプルソースを • 入手し、コンパイルして、読む • /home/kaizaki/osamuNP/11/getaddrinfo.c • 最後に、echoクライアントをIPv6対応にしてみよう!

  16. 練習ヒント(というか答え) • 両方のサンプルソースのコメント部をよく見ると、、、 • どこから、どこまで変更すればいいか書いてある。。。 • 一応、答えは以下に(エラー処理もあり版) • /home/kaizaki/osamuNP/11/echo_client.c

More Related