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
Network Programming in CネットワークプログラミングLecture 8, Network Programming (3)第 8回「ネットワークとプログラミング (3)」
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!!!
Today
Makefiles & libraries Naming AF-independent coding: Itojun’s rules
struct sockaddr_storage getaddrinfo() v. gethostbyname() inet_aton() v. inet_pton()
Attendance Server
Did you get your program to connect to it properly?
Same this week, two weeks from now will be different!
Makefiles
Executables
Stop using “./a.out”! Stop doing “gcc myfile.c” by hand! 工学っぽくない。
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)
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));
実習: gethostname() printmyhostname()という関数をつくりましょう。 mylib.cに入れよう。 printmyhostnameというプログラムを作りましょう .
Makefileを使ってね。
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;
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;
}
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
練習 : ポイント 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);
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
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
use getaddrinfo() everywhere
use getaddrinfo() and getnameinfo() everywhere.
avoid gethostbyname(), inet_aton()
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)
itojun’s Rules: inet_pton()
avoid using when possible not very AF-independent stick with getaddrinfo()
実習: getpeername() printpeername()という関数をつくりましょう。 mylib.cに入れよう。 TCPサーバに使いましょう。 inet_ntop()か getnameinfo()をつかう。
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
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
次ページにポイント説明あり