21
Network Programming in C ネネネネネネネネネネネネネ Lecture 8, Network Programming (3) ネ 8 ネ ネネネネネネネネネネネネネネ (3) 20 10 ネネネネ Rodney Van Meter

20 10 年秋学期 Rodney Van Meter

  • Upload
    ziven

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

20 10 年秋学期 Rodney Van Meter. Network Programming in C ネットワークプログラミング Lecture 8, Network Programming (3) 第 8 回「ネットワークとプログラミング (3) 」. 今期の授業スケジュール(予定). 第1回 9/28 : Introduction / イントロダクション 第2回 10/5 : C Basics ~ Functions, Variables, Data Types ・ Makefiles - PowerPoint PPT Presentation

Citation preview

Page 1: 20 10 年秋学期 Rodney Van Meter

Network Programming in CネットワークプログラミングLecture 8, Network Programming (3)第 8回「ネットワークとプログラミング (3)」

20 10年秋学期Rodney Van Meter

Page 2: 20 10 年秋学期 Rodney Van Meter

今期の授業スケジュール(予定) 第1回 9/28 : Introduction / イントロダクション 第2回 10/5: C Basics~ Functions, Variables, Data

Types・Makefiles 第3回 10/12: C Basics~ Command Line Arguments

・ Structures ・ Pointers 第4回 10/19: C Basics~ Pointers & Arrays ・ Lists 第5回 10/26 : file I/O ・ Network Protocols 第6回 11/2 : Network Programming (1) 第7回 11/9 : Network Programming (2) 第8回 11/16 : Network Programming ( 3) 11/23: No class! ORF! Please come! 第9回 11/30 : Applied Network Programming ( 1) 第10回 12/7 : Applied Network Programming (2) 第11回 12/14 : Work on Projects 第1 2 回 12/21 : Work on Projects 第13回 1/11 : Final Presentations!!!

Page 3: 20 10 年秋学期 Rodney Van Meter

Today

Makefiles & libraries Naming AF-independent coding: Itojun’s rules

struct sockaddr_storage getaddrinfo() v. gethostbyname() inet_aton() v. inet_pton()

Page 4: 20 10 年秋学期 Rodney Van Meter

Attendance Server

Did you get your program to connect to it properly?

Same this week, two weeks from now will be different!

Page 5: 20 10 年秋学期 Rodney Van Meter

Makefiles

Page 6: 20 10 年秋学期 Rodney Van Meter

Executables

Stop using “./a.out”! Stop doing “gcc myfile.c” by hand! 工学っぽくない。

Page 7: 20 10 年秋学期 Rodney Van Meter

Libraries

Big projects use more than one source file

Can be put into libraries made with ar linked into final executable using ld (or

gcc)

Page 8: 20 10 年秋学期 Rodney Van Meter

gethostname() Get your own name 自分のホスト名を取得する

#include <unistd.h> int gethostname(char *name, size_t len); 返り値 :成功した場合0,失敗した場合 -1が返る

例:char shostname[HOST_NAME_MAX+1];

gethostname(shostname, sizeof(shostname));

Page 9: 20 10 年秋学期 Rodney Van Meter

実習: gethostname() printmyhostname()という関数をつくりましょう。 mylib.cに入れよう。 printmyhostnameというプログラムを作りましょう .

Makefileを使ってね。

Page 10: 20 10 年秋学期 Rodney Van Meter

gethostbyname() ホスト名から IPアドレスを得る 古い方法なんだけど、知っているべき! #include <unistd.h> struct hostent gethostbyname(const char

*name); 例

struct sockaddr_in sin;struct hostent *shost;shost = gethostbyname(argv[1]); sin.sin_addr = *(struct in_addr *)hp->h_addr;

Page 11: 20 10 年秋学期 Rodney Van Meter

sample#include <netdb.h> int main (int argc, char *argv[] ) {

int sock_fd; struct sockaddr_in sin; char buf[BUF_SIZE]; int readlen; struct hostent *hp;

hp = gethostbyname( argv[1] ); /* add */

sock_fd = socket(AF_INET, SOCK_STREAM, 0);

sin.sin_family = AF_INET; sin.sin_port = htons(SERV_PORT); sin.sin_addr = *(struct in_addr *)hp->h_addr;

}

Page 12: 20 10 年秋学期 Rodney Van Meter

hostent構造体struct hostent{

char *h_name; /* official name of host */

char **h_aliases; /* alias list */int h_addrtype /* host address type */

int h_length /* length of address */char **h_addr_list; /* list of addresses

*/

}#define h_addr h_addr_list[0] /* for backward

compatibility

Page 13: 20 10 年秋学期 Rodney Van Meter

練習 : ポイント sock_fd = socket(AF_INET, SOCK_STREAM, 0);

sin.sin_family = AF_INET; sin.sin_port = htons(SERV_PORT); sin.sin_addr = *(struct in_addr *)hp->h_addr;

connect(sock_fd, (struct sockaddr *)&sin, sizeof(sin));

fgets(buf,sizeof(buf),stdin);

write(sock_fd, buf, readlen);

readlen = read(sock_fd, buf, sizeof(buf));

printf("%s\n",buf);

Page 14: 20 10 年秋学期 Rodney Van Meter

AF-independent Code: itojun’s Rules

avoid struct in_addr and struct in6_addr.

use getaddrinfo() and getnameinfo() everywhere.

do not hardcode knowledge about particular AF.

http://www.kame.net/newsletter/19980604/ http://www.faqs.org/rfcs/rfc3493.html http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html

Page 15: 20 10 年秋学期 Rodney Van Meter

avoid struct in_addr

avoid struct in_addr and struct in6_addr

use sockaddr_storage instead more generic, but we still run into

trouble: see the Mac problems from last week

Page 16: 20 10 年秋学期 Rodney Van Meter

use getaddrinfo() everywhere

use getaddrinfo() and getnameinfo() everywhere.

avoid gethostbyname(), inet_aton()

Page 17: 20 10 年秋学期 Rodney Van Meter

no AF-dependent code

Bad example:/* BAD EXAMPLE */switch (sa->sa_family) {case AF_INET: salen = sizeof(struct sockaddr_in);break;}

Instead, use res->ai_addrlen returned by getaddrinfo(3)

Page 18: 20 10 年秋学期 Rodney Van Meter

itojun’s Rules: inet_pton()

avoid using when possible not very AF-independent stick with getaddrinfo()

Page 19: 20 10 年秋学期 Rodney Van Meter

実習: getpeername() printpeername()という関数をつくりましょう。 mylib.cに入れよう。 TCPサーバに使いましょう。 inet_ntop()か getnameinfo()をつかう。

Page 20: 20 10 年秋学期 Rodney Van Meter

Homework (Two Weeks!) More detailed project proposal Program that prints all addresses for a

given name, using getaddrinfo() Must be done using a function in a separate

library file, mylib.c Must have Makefile for library mylib.a

TCP client that reads index.html from a web server Should use same functions to print out info

about host & connection

Page 21: 20 10 年秋学期 Rodney Van Meter

More Complete Proposal for Term Project

“Value Proposition” (why do I care?) Related work (has it been done

before?) Key Idea How you will evaluate Schedule with technical milestones

次ページにポイント説明あり