D$EFHGHEIJ3JLKHK MON Autunno 2003 2 PRATICA (Lez. 1) #include <stdio.h> int main(int argc, char **argv) { union { short s; char c[sizeof(short)]; } un; un.s = 0x0102; printf("CPU = %s - byte ordering: ",getenv("CPU")); ! #"$%& ' ( if (sizeof(short) == 2) { if ( un.c[0] == 1 && un.c[1] == 2 ) printf ("big-endian\n"); else if ( un.c[0] == 2 && un.c[1] == 1 ) printf ("little-endian\n"); else printf("unknown\n"); } else printf("size of short: %d.\n",sizeof(short)); PRATICA - Lezione 1 )*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*: ;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9 N#FHJQP$K R ISHK FHM$TQTQUVIWTXK R T exit(0); } UIWTXK R T Autunno 2003 3 Autunno 2003 4 PRATICA (Lez. 1) Y[Z\]^`_ a?bdcHebgfihjgkH^`lnm?o \ j#e^ PRATICA (Lez. 1) #Makefile ALL = lib-errori lib-corso-reti \ daytimesrv daytimecli daytimesrv-ric \ echosrv echocli echosrv-sigh Ypfq^`lgm?o \ jq_rbis%ctm%_ ^?a`_ujqlvlwjgkH^`cH]?o ]ebxcHbq\ yHz|{ EI}~XFHE~JLIET3K RH}FHEG$TM { T3NK MOFH$K TLFG$G$T {u{ F z M$K ET { IR TLFH$K TLFG$G$T {r{ FwFHMLR TLR K $ETEK TQ R K M$W all: $(ALL) .c: Ypa?kHk|~ffq^`lgm?o \ bq_ G$R }H G$%FwR }wR } G$%J`R K $P$I { ?R J`R K ? J`K M$P$I { %?$ 6¡%¢ lib-errori: lib-errori.c gcc -g -O0 -Werror -c lib-errori.c lib-corso-reti: lib-corso-reti.c gcc -g -O0 -Werror -c lib-corso-reti.c > clean: Y[£Vj¤b#¥o \ b lib-errori.o lib-corso-reti.o @echo compiling $< with rule 1 gcc $< -g -O0 –Werror -o $@ lib-errori.o lib-corso-reti.o rm -f $(ALL) rm -f *~ rm -f *.o JQIWTL XI {{u{ F $}TJQP$K F K $ETEK TLTLK M$R $TdXK R T Autunno 2003 5 Autunno 2003 PRATICA (Lez. 1) robdep@zircone:~/Corsi/Reti/C> gmake gcc -g -O0 -Werror -c lib-errori.c gcc -g -O0 -Werror -c lib-corso-reti.c compiling daytimesrv.c with rule 1 gcc daytimesrv.c -g -O0 -Werror -o daytimesrv lib-errori.o lib-corso-reti.o compiling daytimecli.c with rule 1 gcc daytimecli.c -g -O0 -Werror -o daytimecli lib-errori.o lib-corso-reti.o compiling daytimesrv-ric.c with rule 1 gcc daytimesrv-ric.c -g -O0 -Werror -o daytimesrv-ric liberrori.o lib-corso-reti.o compiling echosrv.c with rule 1 gcc echosrv.c -g -O0 -Werror -o echosrv lib-errori.o libcorso-reti.o …… 6 PRATICA (Lez. 1) Y[¦Qo cHk%\ sH§?bi¨LcH^`lwbd©~ª« N#TEILK RXK R TQ$IQK M$R $TETQM$TR R TL$K ET { FE ¬$KEK TEI } { IM$$IEw$TRHFJQP$K R I { FET ® }E K M$R $T¯ }E R K K M$R $T¯° Y[¦±o cHk%\ sH§?bg²cH^`lwbd©~ª%³ N#TEILK RXK R TQ$IQK M$R $TETQM$TR R IL~´w YpµA\?lO^`lwbqc$e^|§`ob#]b#a`s%o _ubio \?\ o c%¤¶o \kH^`lgm?o \ j#e^`_rbgkHbd_rkj o \kH^?§`o kHbicHb#kHb]]jd_o ^·cHbd\ \ bi\ o ¸?_ bd_o b R K $ETEK TQ}P$TK XK I { TLFHM`%RHM$TRFHJLIM$HFw$KHFHJQP$K R ISHK FHM$T R ILEK TEIL$K { IR KHR K $ETEK TQ¹XI {~{ IQK MwP$FH} { KH} { IM$$IE }E R K $¯ }E R FHIR R K $¯° qTLM$TR R T3$K ET { FHE ¬}P$TK XK I { T FHM% R K $ETEK IQ$K$TXI R { FM { K TM$T3P$EK M { X I}K Autunno 2003 7 PRATICA (Lez. 1) Y #ifndef __BASIC__ #define __BASIC__ #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <time.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> /* basic system data types */ /* basic socket definitions */ /* timeval{} for select() */ /* timespec{} for pselect() */ /* sockaddr_in{} and other Internet defns */ /* inet(3) functions */ Y /* for Unix domain sockets */ Y #endif ! "$# %'&(&(%)%* +-, +.%/0, %1/3204-, % "$57 698 :;%%* +, +.<%/=, %>/?204', % "$#%'&(&(%>@(/7+A8 :&(+ ? `sHb]e ji\ o ¸?_ bd_o jim$bq_VsH]j_ b R M$T { TE~JLK M$IM$FwR T}T S$K FHM$TL$TRP$E~FHG$EIJQJQI #o \ bi\ o ¸ rbd__ ^`_o © k K $ETEK ILFHE}FLET { K 8 PRATICA (Lez. 1) $^`cH^v¥ s%c °o ^`c?ok%ªHbg]e jqlnmjqcH^s%ctlwb#]]j#a?a`o ^|§`o bd__ ^_ b #define MAXLINE 256 #define PORT 12345 #define BACKLOG 5 #define MAX(a, b) ((a) > (b) ? (a) : (b)) Y j#kk%o jqlw^o \?\ o c%¤QkH^`c e jd\ o¥ s%c °o ^`c%o Y <unistd.h> <sys/wait.h> <sys/un.h> Autunno 2003 b#¥ o c%o ]kHbghjd_o bg¥sc #o ^`c%o?mbq_V\ jga%b#]eo ^`c$bg§?b°a`\ o bd__ ^_o …… #include #include #include K $ETEK ILG$T} { K FHM$KTEEFEK } { I JQP$IL}FR FwR TE~EFHET K IJQIM$FwT K { TEE %JQ}G K { ¯°TEE %}~} TEE Autunno 2003 9 PRATICA (Lez. 1) b#¥o % c o ]kHbghjd_o bg¥ s%c °o ^`c%o?mbd_V\ ji\ b#ees_ jnb ]k?_o e e% s _ jg§%bdo]^?k?¤ be Ypfq^`cHhb #o ^`cHbg]?s%\?cH^`lwb ET { K & '( !#"$% H]bqlnm?o§`o¥s%c #o ^`co§?bq\ \ ji\ o ¸?_ b_o j ET { K %ETI$M ET { K H´LEK { TM ET { K %ETI$R K M$T PRATICA - Lezione 2 b#eej#a`\ o?c$bq\¥ o \ bi\ o ¸ rk^`_ ]^ _ b#e o © k C D E < F HG AFI F B )#*+ ,-./+ 01Q2-4H5 +6/ 072+62+6895 -.*: ;97.-93-Q<6+ =5 :>?93+ *A@*B:.>?90+ C9 FWT { Autunno 2003 11 PRATICA (Lez. 2) Y b#ee bd_rjq\ lObdcHe bg]°o a`c%o ¥ o kHjg²m%_rb]j³3~§`ok^`__ bqc$ebq Y t\ j#]e_uj °o ^`cHbg§`o?sckHjdcHjd\ bg§`okH^`lgs%co kHj °o ^`cHbn¥_ j §`sHbgkH^`lgm?s$ebq_kH^`c%cHb#]]?o§?jis%c$ji_ b#eb Y < bd_ f HZ |s%c¬]^?k%¤b#eLo §?bqcHeo ¥o kj·o§`sHbim?s%c$e o §?bd\ \ jnk^`ccHb]]?o ^`cHb ¡%MOK M$$K EK SSHFL L D T ¡%MOK M$$K EK SSHFL L D T MS$K FHM$KHP$TEKH}FHWT { M$IQP$FE { I3} O M FH} { M$IQP$FE { 3 I } R R IR { E F FH} { Autunno 2003 12 PRATICA (Lez. 2) socket() bind() SKT L RM N O L P-UTQ R PN V O P W U X L ' N Q R P R N L U R N S S T T YP O U Z [ US \ UN L O P ] YP O U Z [ UL M T L O P ] ^ T O U_ S P ` U T R Na U _ U R N S T b Q R US P ` UT R N listen() socket() H^`cH^|§?b#¥o c%o eo?mbd_hjq_o?m?_ ^%e ^?kH^`\ \ o Y J c : d(:6e+>: /=, %8 +'fg:h/=%>: /i@(/0+6jh//7%*7*=:h/=% c<k9l CLIENT Y # connect() write() read() close() accept() read() write() read() close() SERVER Y B struct in_addr { in_addr_t } struct sockaddr_in { uint8_t sin_len; sa_family_t in_port_t struct in_addr char } struct sockaddr { uint8_t sin_len; sa_family_t char } FHWI$$E %K M M$G TSS$TQ} { E {~{ ETL}FHWT { Autunno 2003 13 Autunno 2003 14 PRATICA (Lez. 2) s_addr; /* 32-bit, network byte ordered */ sin_family; sin_port; sin_addr; sin_zero[8]; /* tipo di protocollo, AF_INET */ /* 16-bit, network byte ordered */ /* struttura indirizzo IP */ PRATICA (Lez. 2) " *=: / f7% 8 h !#" $&%'( " *=: / *+/)%!/& !, ! 01 !& ! &&-! . 32 sin_family; sa_data[14]; /* tipo di protocollo: AF_XXX */ /* indirizzo specifico del protocollo */ J I MS$K FM$TL}FWT { 15 M ?416798 : ; PRATICA (Lez. 2) > FN O]bnbq__r^`_rb QPSR B UT B B"$8 %-+Z=[\i8 :, %3[j6 h* %1F* Wh/=h %1* Y, %*=* %U]=@(/7f(: h/(:Z(X%W :(]=: % W ) ) ) MS$K FM$TLFHM$M$T { *V Autunno 2003 : /7,: /76j6 h@ /((/7% %'6j, *4: /=*, '* h6 * %-,e,8 @06e,* h-6 + 8 * h6 %/ , + h8 % :8 :, h8 /7h 3* % %8 h8 % * % F E 'F G 1F E L 3F B J B * B C 17 PRATICA (Lez. 2) bd_lOb#ee bnj#§·s%c¬k%\ o bqc$e`§`ojdm%_o _rbxs%cHjnk^`ccHb]]#o ^`cHb kH^`cto \]bd_ hbd_ Y[Z\?¤ bq_cbq\]kHba`\ o biscHjim^`_ ejgb#¥ ¥o lwbd_rji~bi\ o cH§`o _o ^ f : /7,: /062(: @/ (% : /0<, * 4 * '** h-,=68 @76j%,, * h6 + 8 * h6 %/ , + h8 % :-8 :, h8 /0h 3* % %8 h8 % * % F G AF E W J UVHd¡ $N ° ¡ q¡ ° $ N MS$K FHM$TQ $K MH Autunno 2003 18 ~ ) Z q 1i lu Z=j b1u rq ZuZ (tLu([ q ) Cuu n ) Z =J 3[ h v Y PRATICA (Lez. 2) bq_lbee bvj§s%c¬]bd_rh6bd_§`oj#]]b#a cHjd_ bis%cto c$§`o _o ^ mbq_Vo \]bd_rhb_jq\]^?k%¤b#e Ypf^`c f v\ o cH§`o _o ^m?s vb#]]bd_ b K M$$K EK SS$FLDO $T #T3 T }}TET M$IL$TR R TLK M { TE XIT P$FHE { I TM { EIJQ $K M$T}} M$F T E -FT J¥ ho bqcHbn¥j#ee ^ \ ª$jqcH§?]?ªHjd¤o c$a do c¬kHj]^v§`obd__ ^`_ubx_o e ^`_cHj ¯ { EIM$M$TL TLP$TE J Z ? Y[bq\kHj#]^|§`o?s%cHjnk^`c?c$b]]°o ^cHb IR T`XIJLK G$R K IL$KPHEF { FHFHR R KH}K Y[o cHe ~ ) (Z rq Hi lu Hj C( u q tZuZ tLu([ rq ) buu n ) Z =J J[ h v Y 3^ c f g c Hi kj rq ) ) ) ¡%MOK M { TEFL TQ$K TLK R { K P$FL$KH}FHWT { T MOK M { TEFQ¹ }I { FwFJLTdXK R TL$T}EK P { FHEM$FHMOP T}}TET }I { FwFJLTQ}FHWT { $T}EK P { FET #K T TE}I FHWT { T`XK R TQ}FHM$F #K } { KHP$K OFQJLTM$FQIR R Fw} { T}}FwJQFH$F J 16 PRATICA (Lez. 2) Y[o cHe H^?k%¤b#e`§%b#]k?_o me ^`_ gkH^`lwbis%c¬¥ o \ bn§?b]k%_o mHe ^`_ FHM$FLPHET}KH$IR R Fw} { T}}FwK M$}K TJLT Autunno 2003 "`"`_b_baa **cc d=d=ee ccCg ll ;; "`"`_b_baa *o# hhbkp _c # e d(57h8 h-6 , %-,eh-*6j+ d7%% 8 86 h:@=%,e/=: /7,& %>* %8 ;%8*7@ ) )hA* , %*7* hml7h* ,n " + ),=8 t: s ?> ;=@DA "" u55uhh kkvv *5y*c z*oo e__bw w d7d7%%88LZ(@(+'/7,h1+'& * ,e8 +8 %. +.x. :tZ j :p Z(y +-l,e:tn j c<k l n " u5 h k v *o _{ d7%8+d(d ) :6e+'fg:h/(: Z : 8 %, %>*=@ c l D@FEG>1GIJG: }| u5 h k v Jo _{ s%c]^?k%¤b#e`§?b#]k?_o me^`_]b ¡%MOK M { TEFL TQ}P$TK XK I K M { TM$$T }IET 54(6798 : ;=<Ao c$e?> ;=@#ACBdo cHeD@FEG>HGIJG:#LK jq\ ^`_ bg§`o?_o e ^`_cH^ MSHK FHM$TQ}FHWT { Y[o cHe ¦Lo cHk%\ s$§?bi¨] ] ]^?k%¤b#e ©ª%« o c$e`]^?k?¤ beo cHe J Autunno 2003 ¡ ? cHbgho bqcb `sHbd\ \ ^£¢go \ §?kjd_r§1 =¤¯ | Hbi\ jxm^`_ ejicH^`c g]?mbk%o ¥o kj#e jih6jd\ ^`_ b ]kHbd\ e jis%cHjnb¥¥o lObd_rj Y Hbi\ o cH§`o _o ^·Z ho bdcHbisH]j#e^ `s$bd\ \ ^v§?b#]?o a`cHj#e v ^ kH^`lwbiZ §?b]eo cHj #o ^`cHbicHbd\ V§?bd\k?\ o d b cHe -F Y I 3< J 2 MS$K FM$TLR K } { TM 6IWR FHG Autunno 2003 19 6I=: GdLK M Ao cHe > )G 1F FN O]bgbd__ ^`_ubBF ]b9PSR Y[]j#e jg]^`\ ^|§?jis%c¬]bd_ hbq_ f # B ]Hbd_rhbnj fq^`cHhbd_reo _ bio \]^?k%¤b#e`§%jgj°e eo h^|jxmj#]]°o h^FdB mb_¥j_ ]?k%ªHbio \?¤bd_cHbd\j#kkHb#eeok^`ccHb]]?o ^`c%o]?s?\]^?k%¤b#e D$TE$TXI R { Mw}FWT { ¹LETI { FwI {u{ K W FH¯°TQK RHWTEM$TRH}K I}P$T {u{ IL T3}K ILK RH}FHWT { $K MOR K TM { TRH$K IG$EIJLJQIQIQ} { I { K N6DXIQJ FtW TETL$I h euy I c eud # 5c jq\ ^`_ bg§`o?_o e^`_cH^ Server k# 5 MSHK FHM$TLITP { : /7,: /7+-6 6e@6e(%% d=, : */74, * '* h6 * ,e%-8 @7, 6j,* h-6 + 8 * h(6 %/ , + h8 % :-8 :, h8 /7h 3* % %8 h8 % * h6 % (%* 698 : d7, h8* %h v F B IJ: 8 6#$E T jg]^`lglwjv§?b#a`\ o bq\ bqlObqce o?o c¬bqcHe_ jdln¸bx\ bgkH^?§%bic$^`c m?s v]?s%mbd_ j_ bio \ W &% 22 PRATICA (Lez. 2) daytimesrv.c if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0) err_sys("bind error"); if( listen(listenfd, BACKLOG) < 0 ) err_sys("listen error"); I { K JLTQ}TE TE H Autunno 2003 if( (sp = getservbyname("daytime", "tcp")) == NULL ) { fprintf(stderr, "getservbyname error for daytime, tcp."); exit(-1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = sp->s_port; is%ctmj_ jlwb°e _r^vhjq\ ^`_ b _o ]°s%\ e je ^ MO K IJQI { IQFHM { K TM$TQK RHR K } { TM$K M$GO}FHWT { RHEK { FHEM$FLFM { K TM$TLK RH}FHWT { FHM$M$T}}FLIRHP$IE { K FR IET R K TM { W #include "basic.h" #include <time.h> int main(int argc, char **argv) { pid_t pid; int listenfd, connfd; struct sockaddr_in servaddr; char buff[MAXLINE]; time_t ticks; struct servent *sp; if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); TQM$FHMOT3M$TL}FM$FL}K $R FHI 6I=: G I { K JQTL}TE TE 21 bd_lOb#ee bnj#§·s%c¬]bq_uhb_§`o?m?_rbdcH§%bd_ bi\ j·m?_o lj Y * PRATICA (Lez. 2) kH^`c%cHb#]]°o ^`cHbnk^`lnm?\ b#ej#ejg§%jd\ \ jgkH^?§?j connect dal client Autunno 2003 ~ ) (Z rq Hi lu Hj b(u q tZuZ (! " tuL([ q ) btLu n ) Z =J 3[ rq ZZ Y 7Y S KT b S TM RX N O NP LO N U T R U Z L PS T R M S NT [ bO Q M[ PX N O P O P O P O T V K V Y] 7Y U RK S Tb R M X N OL N U T R U Z V S T ^9R PN ML N [UTO QR [N P L O P O T V ^ 0Y] accept C jk%¤\ ^?av]?mb#k?o ¥o kHj `sHjdcHebgkH^`c%cHb#]]?o ^`c?oj#kHkHbe e jq_ubgb lwb#ee b_ bio c¬j#ee b]Hjimb_b]]bq_ubg]b_ ho eb J 20 PRATICA (Lez. 2) ¦Lo cHk%\ sH§?bi¨`] ] ]^?k%¤b#e©~ª%« o cHeL\ o ]ebdc%o cHe Autunno 2003 PRATICA (Lez. 2) /* backlog = 5 */ I { K JLTQR K TM { H Autunno 2003 23 Autunno 2003 24 PRATICA (Lez. 2) for ( ; ; ) { if( (connfd = accept(listenfd, (struct sockaddr *) NULL, NULL)) < 0) err_sys("accept error"); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); write(connfd, buff, strlen(buff)); close(connfd); } B (' Hbd_ hbq_Vo ebd_rj#eo h^ TE TLKR K TM { M$FLIR R I FHR { I IM$$F MOR K TM { ¹3FHM$M$T}}FwK RH}TG TM { TLR K TM { $T #T I}P$T {~{ IET T {~{ I $K R TLP$TE}TE TE}TJQP$R K KFHJLT3K RH$I { K JLT W W XW #include "basic.h" daytimecli.c int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; struct in_addr **pptr; struct hostent *hp; struct servent *sp; } Y PRATICA (Lez. 2) W if (argc != 3) err_quit("usage: daytimecli <hostname> <service>"); if ( (hp = gethostbyname(argv[1])) == NULL) err_quit("hostname error for %s: %s", argv[1], hstrerror(h_errno)); if ( (sp = getservbyname(argv[2], "tcp")) == NULL) err_quit("getservbyname error for %s", argv[2]); pptr = (struct in_addr **) hp->h_addr_list; &% I { K JQTLR K TM { H B W W TE TEEK FHE}K K Autunno 2003 25 PRATICA (Lez. 2) Autunno 2003 26 PRATICA (Lez. 2) Y[c¬]bd_ hb_V_o k^`_r]°o h^·s$]jiscHjgkH^`m?o jg§`o]bg]e b]]^ for ( ; *pptr != NULL; pptr++) { if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) err_sys("socket error"); mbd_]b_ ho _rbxs%cHji_o k%ª%o b#]e j bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = sp->s_port; memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr)); pid_t int if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == 0) break; /* success */ err_ret("connect error"); close(sockfd); /* riempi la struttura sockaddr_in (es. numero di porta) */ pid; listenfd, connfd; listenfd = socket(….); bind(listenfd,….) listen(listenfd, LISTENQ) } if (*pptr == NULL) err_quit("unable to connect"); for ( ; ; ) { connfd = accept(listenfd,…); if ( (pid = fork()) == 0) { close(listenfd); /* figlio chiude il socket di ascolto */ DOIT(connfd); /* serve la richiesta */ close(connfd); /* chiude il socket */ exit(0); /* il figlio termina */ } close(connfd); /* il padre chiude il socket della connessione */ } while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ fputs(recvline, stdout); } exit(0); } B W W TE #TEEK FHE}K K B Autunno 2003 27 PRATICA (Lez. 2) Client Server W Autunno 2003 28 PRATICA (Lez. 2) Client listensd connect() W TE TEEK FHE}K K Server listensd Richiesta di connessione Connessione stabilita connect() connsd W RH}TE TE K IJLIQITP { K TM$TLETI { F MOM F Fw}FHWT { $ T}EK P { FHEM$TRH}TE TEP$TE R ILFHM$M$T}}K FHM$TQFHM T } { FLP$IE { K FR IE~TLR K TM { tW Server W listensd connsd Client W Server RH}TE TE K IJLI`XFEW D$I$ET3T`XK G$R K FwM$TR}TE TEFHMH$K K $FM$FLK RH}FHWT { listensd connect() Connessione stabilita B connsd W W TE #TEEK FHE}K K Client 29 PRATICA (Lez. 2) Server connect() connsd " l @ +'6e6j%', +8 %>/(@7h(;%)6jh/(/7%*=*7:h/(: " l @ &%* ,e: 8 % )+6jh//7%*7*=:h/7% 6=h/i: ) 6 ) : %/7, RP$IHETL K TLK RH}FHWT { $TR R ILFHM$M$T}}K FM$T tW Autunno 2003 "" )# - :- /(: /(Z Z : 8: 8:f=:f=f=f7h h) hZ(6=% + ) ) )-%+ ) ,=+8 h*7* h-)+-69, :h+', Z(h3% + ) )+) * 6jh-h6/(q %/7,%*e*=:h/=%)+*=* h69:+, + + ) * h-6q%-, B W p/6 :%/=,/6. l0%1/=h/6rl(: +.<+12: /(Z /=h/3* +10@7+ %Ad7h8 , +12A* , +-, +1@(* +', + "} "} p/6 )) : %/=,/=h/3* + ) - : /Z : 8 : fef7h c l @(* +', h1* % 69:-* h) /=h1d(: 33: /=, %8 ] +'6e69: % "} "" p57y ,h/7%+)d7*7h1*6+l(@g6j: +/7h+ .<* +A%+-=', +d7%'%6 +>8 *=)2(:-- : :(/d(/(Z Z3@ A: 86e:f=h8 fe: /i*h + d7) c:h8l % 8 , j ++'5 +7& 469) :8): +(/8;Z*=:9* :%-8 6t:f7& +7fg/0<:+AZ(n % @()/7)+)+>6ed7hh/g8 ,/0+ %%*7*=] ]=: h: . /7% %8 + +7>/(& ! !@?!& .1 °K { FHEM$IM$F listensd R XK G$R K Fw K $TLK R}FHWT { P$TER IT {u{ ISK FHM$TQ$KHM F T FM$M$T}}K FHM$K W 30 PRATICA (Lez. 2) padre figlio figlio !31'( ! ( r!( u!1 ! r!1 " #$% & u !1 ' ! ! !)(H* "3H ! u !1 & ( !+, Server connsd W ¢T { }FHWM$IJQTLT3G$T { P$TTEM$IJLT Autunno 2003 listensd Connessione stabilita padre TE TQP$TE W $ Fw}TE TE H W &% $ Fw}TE TE H Autunno 2003 31 Autunno 2003 32 PRATICA (Lez. 2) #include #include "basic.h" "echo.h" PRATICA (Lez. 2) for ( ; ; ) { cliaddr_len = sizeof(cliaddr); if( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &cliaddr_len)) < 0) err_sys("accept error"); echosrv.c int main(int argc, char **argv) { pid_t childpid; int listenfd, connfd; struct sockaddr_in servaddr, cliaddr; socklen_t cliaddr_len; if( (childpid = fork()) == 0 ) { close(listenfd); str_echo(connfd); exit(0); } close(connfd); if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); } } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); /* daytime server */ void str_echo(int sockfd) { ssize_t n; char line[MAXLINE]; for ( ; ; ) { if ( (n = read(sockfd, line, MAXLINE)) == 0) return; /* connection closed by other end */ write(sockfd, line, n); } } if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0) err_sys("bind error"); if( listen(listenfd, LISTENQ) < 0 ) err_sys("listen error"); $ FLR K TM { H &% $ FwR K TM { H Autunno 2003 33 Autunno 2003 34 PRATICA (Lez. 2) #include "basic.h" #include "echo.h" int main(int argc, char **argv) { int sockfd, n; struct sockaddr_in servaddr; echocli.c PRATICA (Lez. 2) void str_cli(FILE *fp, int sockfd) { char sendline[MAXLINE], recvline[MAXLINE]; while (fgets(sendline, MAXLINE, fp) != NULL) { reti_writen(sockfd, sendline, strlen(sendline)); if (reti_readline(sockfd, recvline, MAXLINE) == 0) err_quit("str_cli: server terminated prematurely"); fputs(recvline, stdout); } } if (argc != 2) err_quit("usage: echotcpcli <IPaddress>"); if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); /* echo server */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_quit("inet_pton error for %s", argv[1]); if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) err_sys("connect error"); str_cli(stdin, sockfd); /* svolge tutto il lavoro del client */ exit(0); } W $ Fw}TE TE " l %8* %. d ) :69:, ] +'6j69:+. h & : 8 +8 %>* %8 ;%8(%6 ) :%/=, *=@ ) ) +>* , %*e* +1.+'6e6l(: /7+ W $ FL}TE TE Autunno 2003 35 Autunno 2003 36 PRATICA (Lez. 2) PRATICA (Lez. 2) prompt > echoserver & [1] 21130 prompt > echoclient 127.0.0.1 Ciao server Ciao server Arrivederci Arrivederci ^D prompt > prompt > netstat –a Proto Recv-Q Send-Q Local address Tcp 0 0 *.9877 Foreign address *.* (state) LISTEN " c /3@(/ -+ ),=8 +U]=: /0%* ,=8 + Foreign address localhost.1052 localhost.9877 *.* " _ 0@7%* , hAd@(/=, h )+6jh/(/7%*=*=:h/=% 2A* , +2: ) : , + (state) ESTABLISHED ESTABLISHED LISTEN Risposta del server Risposta del server Digitata al terminale prompt > netstat –a | grep 9877 Tcp 0 0 localhost.1052 Tcp 0 0 *.9877 prompt > echoclient 127.0.0.1 prompt > netstat –a Proto Recv-Q Send-Q Local address Tcp 0 0 localhost.9877 Tcp 0 0 localhost.1052 Tcp 0 0 *.9877 Digitata al terminale Digitata al terminale localhost.9877 *.* TIME_WAIT LISTEN " 6ec %)t)8-+ , ,h1h3d76%l08 % :ht6Z(hml(: @(j Z(L% w5()+#6jn hd7/(%/78 c%c*=w *=:he /={F%>_8 : .<c c +/=%>: /$0 @0%* , h * , +, hAd7%8 @(/ ! !( !& !@6 ,01 H ! ! W RH}TE TE IL K } FwK RH}FHWT { RHR K TM { ¹L$ M TR R Fw} { I { FL$K - ! (0( ( !H(5+ H H !! !& !& 0((!H ! W $ Fw}TE TE "" yc ) :&q%:, +8 /7/(% Z) h6lg: @(y Z([ % : ) ,=6 @7) ,:%e:-/0,:(]e, :%)%U8 .Z(%: /7* +698 6r: d7l(, :h+8.<[+0 /(@(Z(: /(h3Z %9: =+:,/76rl7%1:(* h(6q %,Z(%* 698 : d=, h8 " " <# +@6: (/ 1l(Z: @(: *7: )(@(*8h-+1 6! :q!. %,3dZ) :H6=% + ) 6 65 9+X) :,%/=, ;5 : %+01/7% 6l(: @(* h "" _ /7# +1% 0) )d(h@78%: *.<* , , +-hA+>, hkd7d+@(a 8/=c, ,%UdLh Zg{#: %) _* %+ c c8 ;6r %l(8: @21*=@(/78 %+U) )Zh :* @(, +-/0+, h 6ehk #/h/=%5 *ee1*7 :h{F/7_% c cc k9.<l %2/=,=6e8h%A/=6: ) 6@()* :%+ /7,2 )) ) " " 0; <c :]e@(%@0::+)/7U%(/ %AZ(/ ZgZ: *7%hA6 d7*l(%t69: ):Z+8 * ::.<,%d7h , +8 h;+ %%8) =;8 6 %:8,) ::%/06e&g%'/=;-h, %>/7h3: ) 6 a lc :d @(*721: [/=0 % @() ):+A(/ Z 8 %': ++ Z/=r6 ) : l=/=%1%r6: ) l7* %Ah-6 8 q:, %h,8 /7% +Z e @(hC/ a a % c d " _;+10/7@7% %) *)hA, hA* ,d+@(, h/=, hc c )+w e16jh{F/7%_ *7c*=c :h./=% %2/=,=6e8 %Ah.: ) d * %)%-8, ;+%.<C8 %l0/7+, %6 l(, %: @(8 *. h : /=)+ +-,6=+h%/(Z/=%: *=) *76 : h) :%/0/0% , " 6 y l(h: d7(@ h1Z(% @g/)+ 6e6=%h8 /(, h /=%d7*=%*78:h :Lh /0Z(% mh j w+ = : . @. 57%-&.<%/=, #:] %,=: . J% n : ) 6 ) :%/7, B Autunno 2003 37 PRATICA (Lez. 2) Y $TX M$ { k%ªHbge bq_lgo cHjx\ j#]k%o jxs%cH^ K M$$K I M$F`S$FJQ $K T robdep@zaffiro:~/Corsi/Reti/C> echocli 127.0.0.1 ciao ciao ^D robdep@zaffiro:~/Corsi/Reti/C> echocli 127.0.0.1 pippo pippo ^D robdep@zaffiro:~/Corsi/Reti/C> ps PID TTY TIME CMD 1077 pts/0 00:00:00 cat 22084 pts/2 00:00:00 bash 27162 pts/3 00:00:00 ssh 30007 pts/6 00:00:00 bash 30331 pts/11 00:00:00 bash 30761 pts/11 00:00:00 echosrv 30765 pts/11 00:00:00 echosrv <defunct> 30767 pts/11 00:00:00 echosrv <defunct> 30768 pts/6 00:00:00 ps e bd_lgo cHj W FHM { EFR R IETLK R #IR FHETL$K }K { IL$TRP$EFHT}}FLT $TR R TLEK }FHE}T3$TRXK G$R K Fw JQTJLFEK I¯°N6D$¡?¯°T { PQhho jqlbqcHebxcH^`c¬h^?a`\ o jqlw^\ j#]k?o jq_ b Y PQkHk^`__ bg]k?_o hbd_ bis%c¬]?o a`cHjd\?ªHjdcH§ Y ¢wjdo e B 39 { K R K SS$F ^`lv¸?o b \ bq_k%ªHbgk%ª%o jqlwj K G$M$IR IM$$R TE Autunno 2003 40 PRATICA (Lez. 2) ^`lg¸?o b void sig_child(int signo) { pid_t pid; int stat; while ( pid = waitpid(-1,&stat,WNOHANG)) > 0) { printf(“Child %d terminated\n”,pid); } Il client viene ucciso } Yi%eo \ o jdcH§?^·o \a?b]e ^ _ bg§`o]b°a`cHjd\ o]?obho e jqcH^o m?_ ^?kHb]]°o ^`lg¸?o b Il client viene ucciso FW P$P$TM$IQK RXK G$R K F`XK M$K }T K TM$TQ K IJLI { I´wIK { P$K Prompt > echoserver & [2] 19287 prompt > echoclient 127.0.0.1 Ciao server Ciao server ^D Child 19293 terminated accept error: interrupted system call 2003 IR Autunno R ¡M$IQP$FH}}K $K R TQ}FHR S$K FHM$T 41 Autunno 2003 42 PRATICA (Lez. 2) PRATICA (Lez. 2) for ( ; ; ) { clilen = sizeof(cliaddr); if ( (connfd = accept(listenfd, &cliaddr, &clilen)) < 0) { if (errno = EINTR) continue; else { perror(“accept error”); exit(1); } } } Y[Z\a?b#]e^`_ubg§%bd\]b#a`cHjd\ bvho bdcHbgb#]Hb#a`so e ^ 1=U QT Y ^`o k%ª g]e j#e jio cHebd__r^?eejx\ jg¥ s%c ?o ^`cHbnjkHkHbqme _o e^`_cHjgkH^`cto \kH^?§`o kHbg§`obd__ ^`_rbi$ Y ^`o k%ª x\ jga?b#]eo ^`cHbg§`oe jq\ bnbd__ ^ _rbxcH^`c o \ ]bd_ hbd_e b_lno cHji\ b]b#k?s #o ^`c$b PRATICA (Lez. 2) E 9T Y C ZrVf jd\?mj°§`_rbgbio \¥o a`\ o ^v§`o hbqc$ej ^`lg¸?o b FHJL $K TL}FM$FL$TKP$EFHT}}K { TEJLK M$I { KHPHTEK IR K WTM$G$FHM$FwJQIM { TM { K$TK$I { KHM$TRH}K } { TJLIQFHP$TEI { K WF FHJL $K TL}FM$FLM$TT}}IEKP$TEP$TE~JLT {u{ TETQIRHP$I$ET3$K Y[Z\]b#a`cHjq\ b g]eje ^|kHj#e es%_uje ^|§%jq\?mj#§ _ bv§`s%_ jdc$e b \ b#]bk%s °o ^`cHbg§`oj#kHkHbqmHe 38 PRATICA (Lez. 2) mbd_a?b#]eo _ubi\ jnk^`ccHb]]?o ^`cHb Autunno 2003 M { TE~E SHK FHM$T3$TR R TL}~} { TJ F F Autunno 2003 Y[Z\]°o ]e bdlwjg^`mb_ j°eo h^lOjdcH§?jisc]b#a`cHjd\ bg§`o PQa`c%\ ok?\ o bqcHe C ¢N `s$jqcH§?^\ jnk^`ccHb]]?o ^`cHbgho bdcHbgk%ª%o s$]jio \¥o a`\ o ^ Y FB B Y[Zcts%c¬]bd_ hb_V_o kH^`_ ]?o h6^ do \]bq_ h6bq_k%_ b#jis%c¬¥ o a`\ o ^ S$FHJQ $K T TG$M$IR T E P3kHkH^`__ bnebdcHb_Vm?_rb#]bdcHeb Tim?_ b#ho ]ej `s$b]He ^·m?_r^`¸?\ blwj MOIR M$KH}K } { TJLKHR TQ}~} { TJ IR RH}FHM$F I { FJLI { K IJLTM { TQEK K IJLI { TQK MwIR { EKM$F B E Y $bi\ jgk%ª%o jqlOjejgj#§tjkHkHbdmHeL_o e ^`_c$j ITP { K TM$TQEK K IJLI { I Y W $bi\ bd__ ^`_rbQTg§`o h6bd_ ]^|§?j ! )G ! )G Z ' Z KHG$T} { K }TQR TEEFHET3 M$TR R T}TJQP$K Fw}KH K IJLIQT K { °T}T { FHM$M$T}}K FHM$TLTQITP { TEJQK M$ISHK FHM$T3$TRH}TE WTE Autunno 2003 43 FT W N#FH}IL} T$T3}TQK RH}TE TE { TE~JLK M$IQPHE~TJLI { EIJLTM { T _ b#]b°e §?bq\ \ jgkH^`c%cHb#]]°o ^`cHbim?_o lwjg§%bd\ \ jgk%ª%o jdlwj#ej j#§vj#kHkbme B B B W ILFHM$M$T}}K FHM$TQ$K TM { L I RR K TM { }P$T$K }T M R}TE TE K IJQILITP { x W Prompt > echoclient 127.0.0.1 Ciao Ciao Il server viene ucciso Arrivederci Il server non risponde (dipende dal codice) H } " p/a c d ; :%/=%>*=d7%Z :, h3+ W ) 6 *) W:%/=, W " 3d7%8 .<%*=* hAd0%8 6rl 1: ) 6 ) :%/=,/=h/l7+ 6l(: @(* h13: J) * h6q %-, " %d 8hh/38 %*7:7%+*7, d7%%8 ,. + : /7Z+ :8 :6j%';%8 % e hba 0 @(: (/ Z :* , +. d7+>: ) .<%*7* +'&(& :hZ : RHWK R RKH}FHWT { $T}EK P { FHE TM$G$FHM$Fw K }K YpµqkHkbdmeL_o e ^`_cHjisckH^?§`o kbn§`obq__ ^`_ b $N $ 6 RHR K TM { }P$T$K }T J Y[Z\]bd_ h6bd_Vm?s ·_o k%ª%o jdlwj_ bvjkHkHbqmeOmbd_\ jxm?_ ^?]]?o lwj B W " @7%11*, h1 d((!@6j+d(:,+8%A* %>!%: 01) 6ehLZ:6j% l=+$Z : @7%\i8 : , %6eh/g* %'69@=,=:;% " :c& ) /7d(h8 h-8 +-6j,%h *7* h3, %8 . : /7+>* %1: ) * %'& /7+ ) %>B /=h/; : %/7% 6=+-, e@(8 +, h3h B B B W c &/7h6L8 +8 k% j5 (c zu*c !z*d !n (: ) !* %' &/= + ) %Z :(5 c z l c l e 1( H & % k h/0(,e 8 h !+ 8 % %8 h8 U% Z : l l *=@ % \i8 : , %)%A/7% 6e+* h Z : %8 h8 % , %8 . : /0+) ) 8 % j/7) -h/i* 6g8 :;%8 e%1d(c : 3ue n ) ) ) ¢DH D$ 45 PRATICA (Lez. 2) K TM$TLG$TM$TEI { F ¢DH DH C Y W TE #TE} ${ $F´wMLIM$wET $FHF { Y ( 47 Y Y[Z\k%\ o bdcHe`]?mb§`o ]kHbicsH^?ho§?j#e ojd\]bq_uhbq_§?^`m^o \ _ bd¸^?^%e R}TE TEM$FHM IQP$K OK RH}FWT { IP$TE { F N#DwEK }P$FMH$TLIK$I { KHFHM M W Ypf #T Y B ? B L 3 G \ o bqcHe io ct_ b#j°§`\ o cHb `sHjdcH§?^_o kHb°hb °TI$R K M$TLEK { FEM$I3$N # * Autunno 2003 J H W #T W E kR B J C ¶¥ o cH^|j#§ jg¥sc #o ^`cHbg§`o?\ b#ees%_ v j §?jq\]^?k%¤b°eL_o e^`_cHjis%c¬b__ ^`_ub qUVHd¡ ¡ # N ¯° ¡ ° N Y[Z\]bd_ h6bd_h6jga`o bn¥jxo \?_ bd¸^?^?e`]bc jgk%ªHbxo \k%\ o bqcHe ]bicHbgj#kHk^`_ra?j FHMO ¹LFHJ M$K IS$K FHM$T3 E~IM { TLR FL} H{ $F´LMO }TE TE }FHR R TG$I { Fw$IR R ILET { T3IR { EK JLTM { KH}PHT$K }T f v_o e_ j#]°lbee b_ i o§%j#eo?mHbd__o kHb°hb_ bi\ µqf sckHbd_ e ^veo lwb^`s$e PRATICA (Lez. 2) jgkH^`c%c$b]]°o ^`cHbgho bqc$bg]e jd¸?o \ o e j J H B W *V G AF C L Y H B 46 PRATICA (Lez. 2) 9T W Y[Z\k%\ o bdcHe i¸?\ ^?kHkHje ^o ct_ b#j§`\ o cHb IM$FwM$FMO}KHHT TETIG$K ET FHR SHK FHM$TQ}TJQP$R K T¯ IR R TEEFET H W W T ¢D$D$O¹LK G$M$FEI { FQR FP$TEIS$K FM$TQHK´wEK { TLGHTM$TEI R TEEFHET3$KH$D$D$ $ Y[c d j \ e_rjxm^?]]?o ¸?o \ bgkHjdsH]jn§`obq__r^`_rb g]bi\ jilOj#kHk?ªo cHj ]bd_ h6d b _VcH^`ct_o ]?m^`cH§?bim%_r^`m?_o ^ TRHI}F K TE}FL$I K $TETQK RP$EFHT}}FL}TE TE K M TM$G$FHM$Fw}P$T$K { K %¯ q D $K P$TM$$TETQ$IR R I3ET { T $IR R IQJQI K M$IL}TE TE Mw}TG$M$IR TQ$K EEK T$TEK dIR}TE TE 2003 UVI K M$IQ}TE TEM$FHMwEIGHG$K M$G$K $Autunno KRT E Autunno 2003 ETI$R K M$TLT RHR K TM { K IJLI3ETI$R K M$TQ TQEK { FHEM$I kH^`c%cHb#]]°o ^`cHb N#F}IL} T$TQ}TLK RHR K TM { K G$M$FEI3R TEEFETQ} }EK TLM$TRH}FWT { 44 PRATICA (Lez. 2) Y[cjd\ e_ ^|bd__ ^`_ubgeo m?o kH^|§?jga%b#]eo _ bnkH^`c¬jkHkHbdme io \ Autunno 2003 PRATICA (Lez. 2) * t B W TE TE}FJQJQI Autunno 2003 48 PRATICA (Lez. 2) $^`\ ^\ jg¥ s%c °o ^`cHjgk?ªHbga?b#]e o ]kHbio \k?\ o bdcHe void server_somma(int sockfd) { int i, arg1, arg2; ssize_t n; char sendline[MAXLINE], rcvline[MAXLINE]; char c; for ( ; ; ) { if ( (n = reti_readline(sockfd, rcvline, MAXLINE)) == 0) return; /* connection closed by other end */ /* legge dalla stringa passata dal client i due interi da sommare */ if( sscanf(rcvline, "%d %d", &arg1, &arg2) == 2 ) /* converte il risultato in stringa e lo scrive nel buffer */ sprintf(sendline, "%d\n", arg1 + arg2); else sprintf(sendline, "input error\n"); n = strlen(sendline); reti_writen(sockfd, sendline, n); } } sommasrv.c N#R K TM { }FHJQJLI D$EFH $R TJQI Autunno 2003 49 PRATICA (Lez. 2) 9Tisc|m3m?o Y YgfA\ o bdcHe`bn]bd_ h6bd_ #]e b]]^veo m^|§`o?lOj#kHk%ª%o cHj sunos5 > sommacli 206.62.226.33 11 22 33 -11 -44 -55 b#hbna?b#]eo _rbg§`sHbxo c%m?sHe q$I { KK MOIEEK Fw$IRH}FHWT { q$I { K$K G$K { I { K$IR R { TM { TLIR R I { I} { K TEI W B B YgfA\ o bdcHe`bn]bd_ h6bd_ dlOjkHk%ª%o c$bg§`oeo mH^|§`o hbd_ ]^ P$IER IR { EIL M { TR ¡%M$I s$b]e ^·m?_ ^`¸?\ bqljgh6bd__ n j#¥¥ _ ^`cHej°e ^·o c¬]b#a`s%o e ^ |J R { K P$R T K M$G TR T { Y B QTg§`o ]?m^`c%o ¸?o \ bg]?s%\ \ jimj#a`o cHj Y[Z\kH^?§`o kb }FHJLJQIR K 50 PRATICA (Lez. 2) B Y[Z\kH^?§`o kbn§?bq\k%\ o bqc$e`]^`lglwj kH^`lgm?\ b#]]^ Autunno 2003 bsdi > sommacli 206.62.226.33 11 22 33 -11 -44 -16542537 bq¸ Y ? N &B 3 N %mjq_rk V¸?o a rbdcH§`o jdc ZcHe bq\ \ o e e\ b rbcH§`o jqc G AF 1FB Y ¡D Autunno 2003 52 PRATICA (Lez. 3) f qEI}P$FE { FwFEK TM { I { FwIR R ILFM$M$T}}K FHM$T¯°IXrXK $I $K R T Y[ ! #"$%& ' ( Ypf TMSHILFHM$M$T}}K FHM$T¯#K M$IXrXK $I $K R T Y 3T 1F $]bdlgm?o PRATICA - Lezione 3 o]^`cH^n]?o esHj #o ^`cHbio ck%s?o g]bdcH]j#e ^·sH]jd_rbi ! U B )J B B UD )*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*: ;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9 J MSHK FHM$KHP$TEK}FHWT { Autunno 2003 53 PRATICA (Lez. 3) qK P$K ILK M { TEIS$K FHM$T3P$TEK RHP$EF { FHFR R Fw¡%D socket() K a L P M O NP O P[ bQ R recvfrom() YP O U Z [ U S \ U N L O P ] YP O U Z [ U L M T L O P ] K a L P M O NP O P[ Qb R sendto() SERVER CLIENT sendto() recvfrom() W 7 W 0 J^ "! " * h 6q %,Z(%* 69 8 : d7, h8 [ :(Z(+',j:Z(+1* 6g8 :;%8 %h : 2(@1] ] %8: /69@(: %'&(&(%8 % "} % jc$;) %t)+ # Z )8 @(%| /7.<&=hl7%'+ f=f=6+Ul7%AZ(%* %:(Z(8 ;-+'%),e:'6j2(h@(/ ] ] %%8 ]e@/7fg:h/(:Z : ) h?+-;+/7fe+', h*n ) " = ;5(%d7%'8%69+:]e/(:6e/0 !+h /7 !*h 698 )+>:, =*:(,=Z(8 +@= ,) =)H+@( ]=8 +)@(/76f( l=:h%U)/7Z(% %* 6g 8 : ;%1: ) * h!c 6&q % ,1![ :)'& ( %X W IR FET3$KHEK { FHEM$F close() ¯#P$TEFHEI ·F " 5(: . : ) :0+'& ) :@ ),e!: . &:01Z @7 %1d=+!&8 +.< % ,= 8 :Z :7+'6e6e%d=, 54 PRATICA (Lez. 3) w}TLTEEFET¯# { TLR T u{ { H K Fw}EK {u{ KH}T3 T close() W 2003 P$T$K ETQTQEK T TET3$I { IG$EIJLJQAutunno K `K M$R $ T }u#} }FWT { K M { ET X EFHJLK M { FHK ¯°K M { ¯°K M { ¯°} { E { }FHWI$HE A¯°}FHWR TM { HR TM$ K M { }TM$ { FHK M { FHM$} { FHK ¯°K M { ¯#K M { ¯ FHM$} { } { E { }FH WIH$E ¯#}FWR TM { R TM$ bind() socket() B B W TE TET FLFHMO¡?D N#R K TM { T FwFHMO¡?Dw H Autunno 2003 55 Autunno 2003 56 PRATICA (Lez. 3) int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr, cliaddr; if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); echoudpsrv.c PRATICA (Lez. 3) #include #include "basic.h" "echo.h" echoudpcli.c int main(int argc, char **argv) { int sockfd; struct sockaddr_in if( bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) err_sys("bind error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); inet_pton(AF_INET, argv[1], &servaddr.sin_addr); server_echo_udp(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)); } void server_echo_udp(int sockfd, struct sockaddr *p_cliaddr, socklen_t clilen) { int n; socklen_t len; char mesg[MAXLINE]; for ( ; ; ) { len = clilen; if( (n = recvfrom(sockfd, mesg, MAXLINE, 0, p_cliaddr, &len)) < 0) err_sys("recvfrom error"); if( sendto(sockfd, mesg, n, 0, p_cliaddr, len) != n ) err_sys("sendto error"); } } &% NR K TM { T FwFMO¡?Dw servaddr; if (argc != 2) err_quit("usage: udpclient <IPaddress>"); if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) err_sys("socket error"); client_echo_udp(stdin, sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); exit(0); } NFHM { EFHR R Fw} RJLK {u{ TM { T Autunno 2003 57 Autunno 2003 58 PRATICA (Lez. 3) PRATICA (Lez. 3) Y[Z\k%\ o bdcHe k^`c$e _r^`\ \ jnk°ªHbxo \§?j#ej#a _ jl void client_echo_udp(FILE *fp, int sockfd, const struct sockaddr *p_servaddr, socklen_t servlen) { int n; char sendline[MAXLINE], recvline[MAXLINE + 1]; char buff[MAXLINE]; socklen_t len; struct sockaddr *p_replyaddr; h6bqcHajg§%jd\]bd_ h6bd_ Y[ZcH¥jeeo?m^?e_ b¸?¸Hbi_o kHbhbq_ubis%c §?j#ej#a _ jl p_replyaddr = malloc(servlen); qIR TL$I { IG$EIJ `sHjd\ ]?o j#]°ojd\ e_ ^ }IET $ $T3K M { TEPHEI { I { FQFHJLTQR I EK }P$FH} { IQ$TRH}TE #TE while (fgets(sendline, MAXLINE, fp) != NULL) { sendto(sockfd, sendline, strlen(sendline), 0, p_servaddr, servlen); len = servlen; §`o?_o ]°m^?]e j W ! tW " B l %8 .<%',( %UZ:*=d=%Z : 8 %1@/Z+-, +-& 8 +. ; %8 * hA@(/=+1d7h8 , +kp y l if( (n = recvfrom(sockfd, recvline, MAXLINE, 0, p_replyaddr, &len)) < 0 ) err_sys("recvfrom error"); Y $]bd_ k%o °o ^ D$EF IET3ILETIET M$IL}K { IS$K FM$TQ$TRG$TM$TET RH}K { FL ¹QK RHFH$K TL$KH}P$T$K }T %$G$ if( (len != servlen) || memcmp(p_servaddr, p_replyaddr, len) != 0 ) { struct sockaddr_in *sin = (struct sockaddr_in *) p_replyaddr; err_msg("risposta da %s ignorata\n", inet_ntop(AF_INET, &sin->sin_addr, buff, sizeof(buff))); continue; } recvline[n] = 0; fputs(recvline, stdout); } } I { IGHEIJQJQKHP$TE { K Autunno 2003 59 PRATICA (Lez. 3) F Autunno 2003 60 PRATICA (Lez. 3) Y Ypf Y[^`c¬]?ok%_ b#jis%c$jgkH^`c%cHb#]]?o ^`cHbxªjdcH§?]?ªHjq¤b ª%o?\ ^v]e jgj#]?mb#e ejdcH§?^u]bq_uhb_^vk%\ o bqc$e 3_o lwjdcHb F Y bd_b°ho ejq_ub O M IR M$KHI}KM$FHMw $I} { I FHMO}IPHP$K IJLFw}TQK RJLT}}IG$G$K FL$TRHR K TM { M$FHMO¹QJQIK IEEK #I { FwIRH}TE #TEFP$P ETQ}TQR IQEK }P$F} { IL$TRH}TE TE M$FHMO¹QIEEK I { IQIRHR K TM { MOIR M$KHI}K T} { EIM$}ISHK FHM$KH $IM$IEK TXILJQFHR { I * W $K XrXTETMS$I Y W W W G 1F E 'F f ? $bqlgm?\ o kHbdlwbqce bio \?¤b_c$bq\?lwblw^`_o ji\ o cH§`o _o ^Z `sHb#]e ^m%_ ^`¸?\ bdlOjg]°o?m?s ·sH]jd_ bis%c eo lwb#^`sHe ]°o jg]bdc jgkH^`c%cHb#]]?o ^`cHb k%ª%o jqlOjq_ bx\ jg¥ s%c °o ^`cHbnk^`ccHbkHe ]#sts%c¬]^°k%¤b#eL ¸?\ ^?kHkHj#e^o c¬j#e eb#]j 1F QTim^?]]?o ¸?o \ b Ypfq^?]jn]°sHkHkHb#§?bg]bxs%c¬§?je j°a`_rjdl ]?o?mbd_ §%b D$TET}TJQP$K F MwEF { TER Fw {u{ I #K I W $bq¸?¸HbdcHbi NFHM$M$T { T3¡%D bx\ jim^`_uejnk^`c¬k%s%o]°ohsH^`\ bnk^`l B B Y ¶s%o c$§`o§?^`¸?¸?o jdlw^v§`o ]eo cHa`s%o _rbge_ j FHWT { ¡%DLFHM$M$T}}F FHWT { ¡%DLM$FHMOFHM$M$T}}F sc%o kHjd_ b B J FHWT { ¡%DLFHM$M$T}}F FNT Autunno 2003 D F * 61 PRATICA (Lez. 3) Y[^`c¬]°o?m?s v]°mb#k%o ¥ o kHjd_ bio \§?b#]e o cHj#ejd_o ^ `s$bd\ \ ^ ]?mbk%o ¥ o kHj#e ^·o ckH^`c?cHb#kHe FHMO}K }IL}TMH { FQJLI`´QEK { TLFL}TM$ qP$I T {r{ K #TEEIM$FLI { FJLI { K IJQTM { T3}P$T$K { K IR R K M$$K EK SS$Fw}P$TK XK I { FwM$TR R IQ K IJQI { ILIQFHM$M$T { E W ! Y W Y 62 PRATICA (Lez. 3) tW J' & B " ld(h-8 h,e82 %)%2(.20%1+ 8 :, h8 /=+8 % ) -%8 h8 % e _ba d h 5up ll h o c [ .<+1/7h/$21@(/ ?s vb#]]bd_ bisH]je ^mHbd_ LW W Autunno 2003 tm^?]]°o ¸?o \ ok%ª%o jqlOjq_ bnk^`ccHbkHewm?o ¬ §`o?s%cHjgh6^`\ e j Y Y[Z§?j°e j#a`_ujql \ b#e eo]^`cH^ `s$bq\ \ ok?ªHbgjd__o hjqc$^ §?jd\ \ o cH§`o _o ^|²kH^`c%c$b]]^`³ FHMO}K }3 I ET XEFHJL¯JLI3}K }IQETILFwETI NK R K JQK { I MO}TE #TE¡?DOIQFJ M$K IETQFHM MO}FHR F R K TM { V FHM$M$T { IJQ $K IETLR K M$$K EK SSHFLFHMO KH}K FHRFJ M$K IET K }FHM$M$T {~{ TET3K RH}FWT { }P$TK XK IM$$F %¡ D$$N FHJQT`XIJQK G$R K IL$KHPHEF { FHFHR R KHM$TRIJQPHFw}K M HXIJQK R JV %__r^`_oj]?o c$k?_r^`c%o?m^?]]^`cH^vb]]bd_ bvk^`c$e _r^`\ \ j#eo ¡%MO}FWT { ¡%DwM$FHMOFM$M$T}}FwM$FMwP FM { E FHR R IET TEEFEKHI}K M$EFHM$K ¡DOR K TM { WTE}K FHM$T3FHM$M$T { Autunno 2003 63 PRATICA (Lez. 3) Y[ M$IXrXK $I $K R K { 1F Autunno 2003 64 PRATICA (Lez. 3) |cH^`c¬§ g jq\ k%s%cHjga?jd_ jdc #o jg]?s%\ \ jgkH^`cH]b#a`cHjg§?bdo §?j#ej#a _ jl void client_echo_udp_conn(FILE *fp, int sockfd, const struct sockaddr *p_servaddr, socklen_t servlen) { $KH¡?D < 1FW W " l* %-@& /0%+ *7)% * %68 %1l7%1: /=* ,%%. 8 dh-,) :6eh3%.<6eh%/ /=, ke%>c * o , +#. k d7[j+ 6 -0% -@7+@(//7,e&g:%Z(+-* , , h+'&8 %U8 +Z . : * h/=h * , +-,e:-8 :6j%'; @=,e: Ypfq^`cH]?o §?bd_o jqlw^\ jn]b#a`s$bdcHe bnjdm%m?\ o kHj °o ^`cHbgk%\ o bdcHe int n; char sendline[MAXLINE], recvline[MAXLINE + 1]; ]bd_ h6bd_V if( connect(sockfd, (struct sockaddr *) p_servaddr, servlen) < 0 ) err_sys("connect error"); RH}TE TEEK T TQ$I { IG$EIJ while (fgets(sendline, MAXLINE, fp) != NULL) { write(sockfd, sendline, strlen(sendline)); n = read(sockfd, recvline, MAXLINE); recvline[n] = 0; /* null terminate */ fputs(recvline, stdout); TQ}TJQP$R K TJLTM { TQR KHFM { I H R R K TM { } P$T$K }T Mw}TEK TQ$KHP$I T ~{ { K ¯°}TMS$I I}P$T {u{ IE3 T IR M$ILEK }P$FH} { I } } echoudpcli-connect.c W ¡DOT FQ}TE TEF M { W 65 PRATICA (Lez. 3) #include #include "basic.h" "echo.h" echoudpcli-count.c void server_echo_udp_count(int sockfd, struct sockaddr *p_cliaddr, socklen_t clilen); static void gestisci_interrupt(int signo); void server_echo_udp_count(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) { int n; socklen_t len; char mesg[MAXLINE]; n = 240 * 1024; setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)); int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr, cliaddr; for ( ; ; ) { len = clilen; recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); count++; sleep(1); /* rallentiamo il server */ } if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) err_sys("socket error"); if( bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) err_sys("bind error"); signal(SIGINT, gestisci_interrupt); server_echo_udp_count(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr)); } Autunno 2003 66 PRATICA (Lez. 3) int count = 0; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); &% ¡%DOT FL}TE #TEF M { H Autunno 2003 } static void gestisci_interrupt(int signo) { printf("\nDatagrams ricevuti: %d\n", count); exit(0); } ¡%DOT FLR K TM { F M { $EEFHEKHM$FMO}TG$M$IR I { K Autunno 2003 67 Autunno 2003 68 PRATICA (Lez. 3) #define NDG 2000 /* #datagrams to send */ #define DGLEN 1400 /* length of each datagram */ robdep@zircone:~/Corsi/Reti/C> netstat -s | grep -C4 "Udp" | tail -5; Udp: 6686 packets received 2012 packets to unknown port received. 9674 packet receive errors 18634 packets sent echoudpcli-count.c void client_echo_udp_count(FILE *fp, int sockfd, const struct sockaddr *pservaddr, socklen_t servlen) { int i; char sendline[MAXLINE]; for (i = 0; i < NDG; i++) { sendto(sockfd, sendline, DGLEN, 0, pservaddr, servlen); } robdep@zircone:~/Corsi/Reti/C> echoudpsrv-count Client in un’altra shell; dopo un pòCTRL-C Datagrams ricevuti: 11 } FT robdep@zircone:~/Corsi/Reti/C> netstat -s | grep -C4 "Udp" | tail -5; Udp: 7206 packets received 2012 packets to unknown port received. 11154 packet receive errors 20634 packets sent Y[Z\k%\ o bdcHe gkH^`lwbna`\ ojd\ e_o NIJL $K IL}FR FLR IX MS$K FM$TL T3}P$T$K }TLKH$I { IGHEIJ Ypfq^?]jn]°sHkHkHb#§?bg]bxsH]°o jqlO^ PRATICA (Lez. 3) 9 `sHb#]e^|k%\ o bqcHe u]bd_ hbd_ robdep@zircone:~/Corsi/Reti/C> echoudpcli-count 127.0.0.1 robdep@zircone:~/Corsi/Reti/C> D$EFH $R TJQI Autunno 2003 70 PRATICA (Lez. 4) B Y[ctm?_ ^?a`_rjdlgljg§%b#h6bga%b#]e o _ bn§`sbio cm?sHe ]?o lns%\ e jdcHbjdlObdcHeb { IM$$IE~OK M$P { R TG$G$TETL$I { I} { K TEI ¡%MO}FHWT { R TG$G$TET3$IRH}FHWT { Ypµ Y )*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*: ;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9 UVFH$TR R KH$KH ) h6q: /7& % H B h /(2. h@ 6 ,e: d: /7%& : /7& @(:(+', h +:-* %-&/7+ ) : _<*=: /7698 h/=h d h ) q = c Z ) Z ) z J^ % 6R FHWK M$GO 71 Autunno 2003 72 PRATICA (Lez. 4) PRATICA (Lez. 4) applicazione kernel sincroni System call Recvfrom = Non ci sono datagram pronti FASE 1: attesa 9_ k ,h%d:*++1Z(d7%%:8Z(+:(Z(,=:+'Z(,j+:Z() +1q %d08 +/7%8 , %) + Z() % d() 8 hq%6=%8 /7*e% * h ) 6l7%Z(%;%1@* +8 ) : M M$I3FHP$TEISHK FHM$TQ$KR T ~{ { EIQ$I P FH}}K IJLFw$K } { K M$G TETQ TXI}K $ H H tW Autunno 2003 K M$EFHM$F K RHP$EFHT}}FQ}KH $R FIL IMH$Fw K IJQI R H F P$TEIS$K FHM$TL$KHR T {u{ EIXK M$FwIR R ILFHM$R }K FHM$T $TR R FHP$TEIS$K FHM$T ]?o?¸?\ ^°kHkjn]b $bq_ hbis%ctlw^?§?^mb_m^?ebd_²j]?mHb°e e d j _rbd³§%jim?o kHjdcHjd\ o§`o?o cm?sHe RHP$EK JLFL TQP$EF T3$I { K K TM$TLR T {u{ F BLOCCATA ILP IEKHJ3FH$TR R KH$KHMHP { { P { H H % W Y[^`_lOjq\ lObdcHe bis%cHjg¥ s%c #o ^`c$bg§`o?Z c$^`ck%o]^`c^v§?j#eo§?ji\ b°a?a%bd_rb PRATICA - Lezione 4 J ( ¸?¸?o jqlw^|ho ]e ^s%c¬b]bqlvm?o ^o c¬k?s%o?o \k%\ o bdcHe bq_ j ¸?\ ^?kHkj#e ^vjx\ b#a?a%bq_ubg§%jg]e jqc$§%jq_u§ o c%m?sHe FMOP$F { T ILR TG$G$TE~TLK R v} RH}FHWT { ! #"$%& ' ( datagram pronto Copia datagram MwIM$IR TQ$KH FASE 2: copia Processo continua (elabora il datagram) Ritorna OK Copia completata FHM$ $R FHWK M$GO tJ Autunno 2003 73 applicazione kernel FASE 1: attesa EWOULDBLOCK System call Recvfrom Recvfrom Copia completata t G K $I { FQ$IKH}TG$M$IR K datagram pronto System call Copia datagram BLOCCATA BLOCCATA FASE 2: copia Ritorna OK FASE 1: attesa Ritorna “pronto” datagram pronto Copia datagram Processo continua (elabora il datagram) Non ci sono datagram pronti BLOCCATA System call 74 kernel System call select EWOULDBLOCK Recvfrom Autunno 2003 PRATICA (Lez. 4) applicazione Non ci sono datagram pronti System call Recvfrom ' R { K P$R T K M$G PRATICA (Lez. 4) Processo continua (elabora il datagram) FASE 2: copia Ritorna OK Copia completata t I}K M$EFHM$F Autunno 2003 75 Autunno 2003 76 PRATICA (Lez. 4) applicazione PRATICA (Lez. 4) kernel applicazione Non ci sono datagram pronti System call signal kernel System call aio_read Non ci sono datagram pronti FASE 1: attesa SEGNALE GESTORE SEGNALE Recvfrom datagram pronto datagram pronto Copia datagram Copia datagram BLOCCATA System call FASE 2: copia Ritorna OK Processo continua (elabora il datagram) J MS$K FHM$TL}TR T { FASE 2: copia GESTORE SEGNALE Processo continua (elabora il datagram) Copia completata 77 PRATICA (Lez. 4) FT * ,e8 @06j)h,/7,j& : .<%';+, ; ) * %6 ) h/7& , ; @* %-6r Autunno 2003 (l57%,=@(8 */7@769, , +'8=:,,@(h8h+8 %A8 %6eh/(2A@/i)d()h*@(8 h/+/7,=, :*7hJ.<d7n%-%',h+>@=, * /=%h/=/f=+ fe%,=:8 .h=%'h+@0*7,&d=%-j ,]e: +/7h3]=:+)/0h 6l=+ %1) ,e@(: .</ %h@7,1[ d7h:-8 :, h8 /7+ +/76l7%A* %A/7h/6g:* h/7hZ(%* 698 :, h8 :gd(8 h/0,e: 6t ! !!! ! " &H ( ! !! ! 1 6t !& 1 .!! !&! ! ! !7 01 57: .,=8 .<@7,%t=Z@(:8+'+, +6eh.</i%/=@(, /%,=j: d7.<h %') h) : /7@=&=, n d7+8 :7k+ t/7h/ +*=d7%-, +8 3% [ 8 : , h8 /0+ %'+ Z * %-, 1d(8 h/=,e:-d=%8 + %, e@8 + { o 8 : , %* %-,Hd(8 h/=,e:-d7%8 ) +A) * ) 6g8 : , e@8 + = e 6e%6td7 ,e 01: h!&/(* %1, 6eh / !Z : f(%: h/(:d7+8 ,=: 6jh&) +!8 : ( qK JQTF { H %H Z §?b#]k%_o me^_]?o jdcH^m?_ ^`cHeo?mb_b]]b_ bx\ b#ee o Y[Z\eo lwb^`s$e n§?j°e ^|§?jq\ \ jg]e_sHe es_ j Copia completata 78 PRATICA (Lez. 4) bd_lOb#ee bn§`oj#]°mb°e ejq_ubgk%ªHbis%c$^v^m?o ¬ ¥ o \ b Y SEGNALE D$IEIJQT { EKH}TR T { Autunno 2003 : /76 @ (% * 4* '* % %'6jH, i lu : /7,: /76* % @ %-(6j% , : /=* ,=] 4Z * ,e* %: .<,&) u%3 Li lL] !Z * %'" ,C((uu u[6=h/g[]* Z, * ,=* 8%'@7, 6j,,= ": . %-u;' + [ ) " m( n + h8 % :8 :, h8 /7*h J 3* %%8 h8 3% [ * % ,e: .<%'h@=1, [/(@.<%8 h Z : Z%* 6g8 :, h8 :(d(8 h/7,e: F ~ ) (Z ~ ) (Z ) 1j ) Z FASE 1: attesa " "" J K R TQ$T}EK P { FE$I3FHM { EFR R IET 5 ! !! ! !&!& ! D$IEIJLT { EKH}TR T { W Autunno 2003 " ;h:Zm6La yr =euc o hb j] Z * %-, &&u% r n " ;h:Zmayr 5kXe "Jj: /=%, r& [ H] Z 01 !&* %-H, & uu n ! " ;h:Zmayr # &o c j: /=%, r& [ H] Z 01 !&* %H, & ur n ! " : /7,aLyL!= c 5(!5 e j: /7, %& [] Z 0(* !%-, & L r n ( ! b,X" J B L B |% W*^ "} " ;pe + *(* +-i)%, * 8hA%Ak% d7*=t%:+8(.<jt% :h ] ]=]e: ::69)%/0:,%%Z(/0%8 fe%*U+ 69*7* Z(8 +': +d7,j, :0) h+q 8%:(Z(:8 /(/7%:%f9* :6g)+8 /7: ,h hZ(8 + :uLL[ fJn [ t[ 3.<+ =] ZZ(%';'% D$TE$T}EK #TEKHG$R KHK M$}K TJLK}K } ILR IQ} { E {~{ EI`X %}T { T3¹ MOK M$}K TJQTL$K $K { Y T}EK {u{ FHEKP$EFM { K Y R}FHWT { ¹Q} { I { FQ K }FLK MO}EK {u{ EI N ¹ HkHkHb °o ^`c%o?mbd_]^?k%¤b#e EEK #FQ$KH$I { KX FEKH $IM$$I WTE}K FM$TL}TR T { H% Autunno 2003 83 if (n == 0) err_quit(“%s [%d]: server disconnesso“,__FILE__,__LINE__); fputs(recvline, stdout); } if( FD_ISSET(fileno(fp), &rset) ) { if( fgets(sendline, MAXLINE, fp) == NULL) return; if( (reti_writen(sockfd, sendline, strlen(sendline))) < 0) err_sys("write error"); } B " 6 : %/7W , hA8 :6e%';-%)% , %W 8 . : /7+ +6jh/(/7%*=*e:h/7% 3J " c57)%/0) f=+A* % ) )%-6e,: ) 6 ) :%/0,* %A/0%A* )+8 %2(27% +'6e6jh8 , hZ(hd7h } echocli-slct.c T FHR K TM { TQ}TR T { Autunno 2003 84 PRATICA (Lez. 4) Ypfq^`cH§`o #o ^`c%oa?b]eo e bg§%jn]bd\ b#kHeLo ct\ be es_ jn]°s¬]e§`o c b#§s%c¬]^?k%¤b#e Client Data o EOF stdin socket H R R K TM { EK T}TLI3G$T} { K ETL}K ILR K M$P { $I { I} { K TEIQ TQR K M$P { $IR}FHWT { TLK RH}TE TE { T EJQK M$I #K TM$TL}P$T$K { F 82 PRATICA (Lez. 4) if( FD_ISSET(sockfd, &rset) ) { if ( (n = reti_readline(sockfd, recvline, MAXLINE)) < 0) { if( errno == EPIPE ) { err_msg(“%s [%d]: server disconnesso“, __FILE__,__LINE__); break; } else err_sys("readline error"); } PRATICA (Lez. 4) 2003 HAutunno FD_ZERO(&rset); for( ; ; ) { FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdl = MAX(fileno(fp), sockfd) + 1; if( select(maxfdl, &rset, NULL, NULL, NULL) < 0 ) err_sys("select error"); MOTEEFET T FHR K TM { } WTE}K FHM$TQ}TR T { void client_echo_select(FILE *fp, int sockfd) { int maxfdl; fd_set rset; char sendline[MAXLINE], recvline[MAXLINE]; int n; % | T FHR K TM { 81 PRATICA (Lez. 4) "$"$## +>-hd7* h-%& 8 +-) f(:+1: h*7/0:(%d(Z@ :*6j69+8 .:, =2(@(:+8 +18 %8 :6j, hh8 / /7+1) %: h) /(d7fg@(:.<h%/(:8 hZ(%Z :g:(* 20h4'6r, q% %-, t% ] ] %', e: ;'+.<%/=, %1d7+*7* +-,e:7+ )L) : ;% ) ) h Z :7,=8 +*=d7h8 , h " < p/ -hd7%8 +'f9:h/7% Z:* 6g8 : , e@8 +&(%/=%8 +15 c z l c l e "$#) -%-h8 d7h%88 %+-f(: h/0%Z :* 698 :, =@(8 +18 : , h8 /7%8 % %8 /7h *=d0%-69:]=:6l0%8 ! < W Y H R }FHWT { ¹ M R K } { TM$K M$G d }FHWT { TLK}FHM$Fw$TR R T FHM$M$T}}K FHM$KHFHJQP$R T { I { T N ¹ MOTEEFET Autunno 2003 H^?k%¤b#eLo c]k%_o e es%_ j RM JLTEFL$K { 3 T $KH}P$IS$K FQ$K }P$FM$K $K R TQM$TRH XrXTE$TR WTEM$TRH¹QJLIG$G$K FHETQ$K MO { T3$ILR TG$G$TET RH}FHWT { ¹L} { I { FL K }FwK MOR T {u{ EI JLI X ¹3K RHM Q J TE~FwJLI}}K JLFL$K$T}EK {~{ FHEK TXrXT { K IJLTM { T }I { K " ' # +1* h& ) (: +> *=:(d@ 6j+. 2(:+8 %)6jh/ )%hd0fg: h/(:Z%:g* h6q %, "" e# -(*hid72>%8* +', fg+,:hh1/7%8 :6=Z%:; )%@7,, h1e@(: 8) +>a c8 d: , h8 /=+ e hCa " #) -%-h8 d7h%88 %+'fg:h/7% Z: )%, e@(8 +>8 : , h8 /=%8 <% %8 /7h *=d7%-6g:]=:r6 l7%8 $^?k?¤ bewo ct\ b#e e s_rj IM$$FQ ¹QIR JQTM$F ILF} { I M { T L }TR T { ¹QK RM JLTEFL$K $T}EK {~{ H F EKHK MX ?}T { }H F R K { IJLTM { TQ ? 80 PRATICA (Lez. 4) UT b#]]bd_ bisH]j°e ^ Y Autunno 2003 ¶sHjdcH§?^·sc]^°k%¤be §%b#]k%_o me^`_ xm?_ ^`cHe ^·m$bq_ T}EK {u{ FHEKP$EFHM { K 79 PRATICA (Lez. 4) TCP Mw$ Q} RH}FWT { RST data FIN B ? ¢ { FHP IM$ B ´LIK { Autunno 2003 N Y[Z\k%\ o bdcHe`^`mbd_ jio c|lO^?§%jq\ o e g]e ^`m rjdcH§ wjdo e P$T$K }T M$ILR K M$TI3$KHK M$P { TL}KH $R FHILK MOI {u{ T}I $TR R ILEK }P$F} { IQ$TRH}TE TET F W Tempo 0 C Y K M$P { Autunno 2003 86 PRATICA (Lez. 4) o]°mb#§`o ]kH^`cH^x\ bi_o k%ª%o b]e bgkH^`cH]b6k%sHeo hjdlwbdc$e b B ]bdc jgj#]%mb°eejd_rbx\ bi_o ]°m^?]e b #k%ªHbvjq__o hbq_ujqc%c$^ §?^`m^ Tempo 4 Tempo 0 dati Tempo 4 d1 C d5 C d4 d3 d2 r1 S S d5 d4 r1 d3 r2 S S d6 r1 d5 r2 d4 r3 S S d7 r2 d6 r3 d5 r4 S S S echo Tempo 1 6I { 85 PRATICA (Lez. 4) Tempo 1 Tempo 5 dati Tempo 5 d2 C d1 d6 C echo Tempo 2 Tempo 2 Tempo 6 dati Tempo 6 d3 C d2 d1 d3 d2 d7 C echo Tempo 3 Tempo 7 dati C B Y Tempo 3 ${ $F´LMw$TR R ILFHM$M$T}}K FHM$T xW#TE}K FHM$TL} ${ $F´QM 87 void client_echo_shutdown(FILE *fp, int sockfd) { int maxfdl, stdineof; fd_set rset; char sendline[MAXLINE], recvline[MAXLINE]; int n; F´ { F ¡ °¯ ¡ °¯ T FHR K TM { ¡ | w}TLTEEFET¯ ° if( FD_ISSET(sockfd, &rset) ) { if ( (n = reti_readline(sockfd, recvline, MAXLINE)) < 0) { if( errno == EPIPE ) { err_msg("%s [%d]: server disconnesso",__FILE__,__LINE__); break; } else err_sys("readline error"); } }TQ WTE}K FHM$TQ} ${ $F´QM 89 PRATICA (Lez. 4) } if( FD_ISSET(fileno(fp), &rset) ) { if( fgets(sendline, MAXLINE, fp) == NULL) { stdineof = 1; shutdown(sockfd, SHUT_WR); FD_CLR(fileno(fp), &rset); continue; } if( (reti_writen(sockfd, sendline, strlen(sendline))) < 0) err_sys("write error"); } } B F Autunno 2003 if (n == 0) { if( stdineof == 1 ) return; else { err_msg("%s [%d]: server disconnesso",__FILE__,__LINE__); exit(-1); } } fputs(recvline, stdout); echocli-shtd.c FD_ZERO(&rset); for( ; ; ) { FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdl = MAX(fileno(fp), sockfd) + 1; if( select(maxfdl, &rset, NULL, NULL, NULL) < 0 ) err_sys("select error"); "! IR FET3$KHEK { FHEM$F 88 PRATICA (Lez. 4) %o§?b#hbgk%ª%o sH§?bq_ubio \]^?k%¤b#e`]^`\ ^o c¬]k%_o ees%_rjgb \ j#]k?o jq_\ ^|jqmbd_re ^o ct\ b#e e s_rj P$T$K ETQK R g}FHR FLK M M$IQ$K ETSHK FHM$T K M { Autunno 2003 PRATICA (Lez. 4) B J H 9 J^ # B 3 & B 3 £ B 3< d8 r1 C C S T FR K TM { Autunno 2003 J W K M$R $T }~} }FHWT { K M { } ${ $F´LM$ K M { d1 echo s$jqcH§?^o \k?\ o bqc$e`¥ o c%o ]kHbg§`o]?mb§`o _ bicH^`ctm?s k%ª%o s$§?bq_ bxo \]^?k?¤ be NKHP$FH}}FHM$FwT}}TEIM$FEIQ$I { KHK MOIEEK #F Y Tempo 7 d4 C S Y W TR T { P$TEK R}TE TE Autunno 2003 90 PRATICA (Lez. 4) ^?]]?o jdlw^·s$]jd_ bg]bq\ bkHe jqcHk%ªHbxcHbq\]bd_ h6bd_ Ypµ B \?m^?]e ^v§`ok%_ b#jq_rbis%c¬¥o a`\ o ^·mbd_^?a`c%o kH^`c%c$b]]#o ^`cHb TR T { P R TGHG$TETQ$I {{~{ KHKR K TM { FM$M$T}}K Y *V $e _sHe e s_ bn§?j°eo?sHeo \ o j°e b EEIE}T { ¯FHM { K TM$T`XK R TQ$T}EK P { FEHTKH}FWT { { K R K SS$I { KH$IRH}TE TE }K ILR K } { TM$K M$GO T FM$M$T}}K EEIR K TM { ¯°FHM { K TM$TQK M { TEK TQK M$$K IM$F`X W client 1 -1 2 -1 3 -1 FD_SETSIZE-1 -1 } rset fd0 fd1 fd2 fd3 fd4 fd5 0 0 0 0 0 0 … B W { E {u{ ETQ$I { KHP$TEK R}TE TE B 91 PRATICA (Lez. 4) B " 5(@(d(d0h/(:+. h 6l=%1: ) ] Z 8 :, h8 /7+, h 2f client rset fd0 fd1 fd2 fd3 fd4 fd5 0 0 0 1 0 0 92 PRATICA (Lez. 4) IM$$F MOR K TM { } { I $K R K }T M$ILFHM$MT}}K FHM$T #TEE ETI { F Mw } FHWT { P$ T ER IQFHM$M$T}}K FM$T3FHMOR I`X MH S K FHM$T ITP { \ o ]e bdc $ P P$FHM$K IJLFQ T3G$R KH} { IM$$IE~XK R TL}K IM$FwIP$TE { KHTQ T K RXOEK { FHEM$I { FwHIQR K } { TMO}K I IR TQK MXFHEJLIS$K FHM$T TE~E JQTJQFHEK SSHI { ILK MOE}T { XW Autunno 2003 XW ' Y[Z\]bd_ h6bd_k?_rb#jxo \?\ o ]e bdc%o c$av]^°k?¤ bek%ª%o jdlwjdcH§?^ W { E {~{ ETL$I { KHP$TEK RH}TE TE Autunno 2003 … 1 4 2 -1 3 -1 rset fd0 fd1 fd2 fd3 fd4 fd5 0 0 0 1 1 0 … Y[Z\]bd_ h6bd_k?ªo jdlwjg]bq\ bk$ewmbd_V\ ba%abq_ bv§?jnesHe eo?o ' ( ]^?k?¤ bewu¥ o \ bg§?b]k%_o m$e^`_qjmbd_re o R R K M$K S$K FL ¹Q}FHR FLK RHK RHR K } { TM$K M$GO}FHWT { ¯?} X K M$$KK RHP$IE~IJQT { EFLJLI Xw$KH}TR T { T L T T}}TET B B W W { E {u{ ETQ$I { KHP$TEK R}TE TE tW W "} "h<<8@+: /(69Z:-:* :h) /7d7hU+8 Z+@=.<%1%,=* 8h-h 6q.<%+,1=[ ] *7Z@Z ] Z:-* j% )%-)6e: * ,3, %Zg/(%';: /7% &=%ng%*=* %] Z8 %fk j 6eh//=%*7*=: h/7%3n B B 93 PRATICA (Lez. 4) P$P$FHM$K IJLFQ T MwIR { EFwR K TM { }KHFM$M$T {u{ T¯ TE~E ETI { F MLM F w F }FHWT { tW 1 5 2 5 3 -1 3 -1 fd0 fd1 fd2 fd3 fd4 fd5 0 0 0 1 1 1 tW rset -1 W "}Z h @08 +%6g6eh:*/(h/=/7%h3*7*=,e:h8 %A/g:* h-6q%-,H[ *=@] Zj ) : * , %/(: /0&=n [H%] Zf g %] Z d7%8 ) % " @(: (/ Z :: ) d0+8 +.<%-,e8 h . + =] ZZ :* % )%6e, Zg%-;% %*=* %8 % KM F FwK RH}TE TE K IJLIQ}TR T { P$TER TG$G$TETQ$I {{u{ KK }FWT { XK R TQ$T}EK P { FHE IP$TE { K W =W 2003 $ Fw}TE TE TE}K FHM$T3}TR T { Autunno echosrv-slct.c if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0) err_sys("bind error"); if( listen(listenfd, BACKLOG) < 0 ) err_sys("listen error"); TR R IQ T fd0 fd1 fd2 fd3 fd4 fd5 0 0 0 1 0 1 … 95 -1 W "} "hj 6e8@(h+:/(/69Z/7:-%:**=:h*=/0:d7hh+/78 %3Z+n@=.<%1%,=* h8 h 6q.<%+,1=[ ] *=Z@Z] Z:* j% %) 6e: * ,, %Zg/(%-;: /7% &=%n [H*=*%U%]8 %Zg H R }TE TE K IJLIQ}TR T { P$TER TG$G$TET3HI {{u{ KHKH}FWT { XK R T $T}EK P { FHEIP$TE { K ) PRATICA (Lez. 4) int main(int argc, char **argv) { int listenfd, connfd, sockfd; int i, maxi, maxfd; int ready, client[FD_SETSIZE]; char buff[MAXLINE]; fd_set rset, allset; ssize_t n; struct sockaddr_in servaddr, cliaddr; socklen_t cliaddr_len; 94 PRATICA (Lez. 4) … FD_SETSIZE-1 FD_SETSIZE-1 Autunno 2003 -1 2 rset e bW 4 1 W { E {~{ ETL$I { KHP$TEK RH}TE TE P$P$FHM$K IJLFLFEI3 TLR I3P$EK JLI3FHM$M$T}}K FHM$TL }IX % TMHG$IQ K } I client client -1 KHM F FwK RH}TE TE K IJLIL}TR T { P$TER TG$G$TET3$I {{u{ KHK }FHWT { XK R TL$T}EK P { FHE IP$TE { K Autunno 2003 W " 5(@d(d7h/:+.<h 6l=%>: ) ] Z 8 : , h8 /7+-, h 2 FD_SETSIZE-1 ) W CW &% 2003 $ FL}TE TE TE}K FHM$TQ}TR T { HAutunno 96 PRATICA (Lez. 4) maxfd = listenfd; /* inzializza il numero di descrittori */ maxi = -1; for ( i = 0; i < FD_SETSIZE; i++) client[i] = -1; /* inizializza l'array client a -1 */ FD_ZERO(&allset); /* inizializza a zero tutti i descrittori */ FD_SET(listenfd, &allset); /* setta il descrittore di ascolto */ for ( ; ; ) { rset = allset; /* insieme descrittori da controllare per la lettura */ if( (ready = select(maxfd+1, &rset, NULL, NULL, NULL)) < 0 ) err_sys("select error"); if( FD_ISSET(listenfd, &rset) ) { /* richiesta ricevuta dal listening socket */ cliaddr_len = sizeof(cliaddr); if( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &cliaddr_len)) < 0) err_sys("accept error"); for(i = 0; i < FD_SETSIZE; i++) if( client[i] < 0 ) { /* cerca il primo posto libero per il nuovo il descrittore */ client[i] = connfd; break; } if( i == FD_SETSIZE ) err_quit("troppi client"); FD_SET(connfd, &allset); /* setta connfd per la select */ if( connfd > maxfd ) maxfd = connfd; /* aggiorna maxfd */ if( i > maxi ) maxi = i; /* aggiorna maxi */ if( --ready <= 0 ) continue; /* se non ci sono altri socket pronti riprendi da select */ } W =W $ Fw}TE TE TE}K FHM$T3}TR T { Autunno 2003 BW TM$K IRHFX 97 TE K T PRATICA (Lez. 4) {u{ IW Autunno 2003 98 PRATICA (Lez. 4) J Ypfq^`cH]?o §?bd_o jqlw^\ jn]b#a`s$bdcHe bg]?o esHj #o ^`cHbgk%ªHbim?s j#kHkHj§?b_ bgkH^`cto \]bd_ hbd_ho ]e ^·m^?kH^|¥j ¡%MOR K TM { }KHFHM$M$T {u{ T¯°}PT$K }T MO}FHR Fw { TL T M$FHMO}K I MOM$T´wR K M$TqTQM$FHMXILP$K wM R R I RH}TE TE K IJLI3ETI$R K M$TL TQR TG$G$TE K RH}K M$G$FHR Fw { T JLIQ}KH $R F TE M$TR R IQPHEFH}}K JQIL K IJQI { ILI3ETIwK M I {u{ T}IL$K MwM$T´wR K M$T for( i = 0; i <= maxi; i++ ) { /* controlla tutti i socket attivi per controllare se sono leggibili */ if( (sockfd = client[i]) < 0 ) continue; Y W if ( FD_ISSET(sockfd, &rset) ) { /* se sockfd è leggibile invoca la readline */ if ( (n = reti_readline(sockfd, buff, MAXLINE)) == 0) { /* connessione chiusa dall'altro endpoint */ close(sockfd); /* rimuovi sockfd dalla lista di socket che la select deve controllare */ FD_CLR(sockfd, &allset); client[i] = -1; /* cancella sockfd da client */ } else reti_writen(sockfd, buff, n); if ( --ready <= 0 ) break; } W Y W W H R }TE TE¹Q $R FHI { FLTLM$T}} MwIR { EFwR K TM { EK T TE }TE K S$K F bdc%o jq\^?¥ KR $bq_ ho kHbgµqe ejk%¤ ¡%MOR K TM { EK T}TLIXIEKJQFH$FL TQK RH}TE #TEM$FHM EK }P$FHM$$I3P$K OIwIR { EKHR K TM { W Y } } } TM$K IRHFX BW TE #K T {~{ IW Autunno 2003 Y[Z\?m?_r^`¸?\ blwjg§%bd_o hjg§%jd\¥j#e e ^|k?ª$bxo \]bd_ hbd_]?o lwbdcHe_ g b k%o]^`cH^|jqcHk%ªHbnjq\ e _ok?\ o bdce D$F { E T H $TQIMH$IET3 $TM$T3}TLK RH}TE TE$T TQG$T} { K ET }FHR FwR K TM { F Y W W ]bd_ h6bd_]^`lglj M 100 PRATICA (Lez. 4) E Y[£A^?§`o ¥ o kHjq_rbnb#k?ª$^?]°_ h r]?\ kHeLo cvlw^?§?^|k%ªHbg]e jqlgm?o \ o cH§`o _o ^|§%bd\k?\ o bdcHe `s$jqcH§?^ K RHR K TM { }KFHM$M$T {u{ T RHR K TM { }K$K }FM$M$T {u{ T ^?]]°o ¸?o \ o]^\ s °o ^`c%o ¡%}IET MO}K M$G$FHR FLP$EFT}}FLP$TEFG$M$KHR K TM { ¡ { K R K SS$IET M { K JQTF { } R R TQFHP$TEISHK FHM$KH$KH ¡%}IETQ tM$FM $R FHWK M$G Autunno 2003 Y[£A^?§`o ¥ o kHjq_rbnb#k?ª$^?]°_ h r]?\ kHeLo cvlw^?§?^|k%ªHbg]?o jisc ¸?\ ^?kHkHjio c|s?c$jg¥sc #o ^`cHbi_ bd\ j#eo hjvj§s%c¬k%\ o bqcHe $}TEK S$K ? 99 PRATICA (Lez. 4) B FHWT { FHP { K FHM$} Autunno 2003 102 PQa`c%o]^#k%¤b#e`jmb_ e ^ªHjn§?bd\ \ bim%_ ^`m?_o b°enk?ª$bicHb Y §?b#ebd_lgo cHjqc$^vjd\ k%s%c%okH^`lgm^`_rejdlObdcHeo F C E Y Y ! #"$%& ' ( jqcH^`\ ^?a?^·mb_Vo¥ o \ bn]^`cH^·\ bim?_r^`m%_o b#e g k%ªHb P Y *V Qa`c%o^`m #o ^`cHbiªHjxs%c¬hjq\ ^`_ bg§`o§%b#¥jqs%\ e R M$TLFHPSHK FHM$KH}FHM$FL $K M$IEK TQ FHMwFLFX X R { ET IM$M$F M IR FHETQK M { FwIM$ TL} { E {u{ E TQP$K FHJQP$R T}}T )*+ ,-./+ 0132-4$5 +6/ 072+62+895 -.*: ;97.-93-3<+ =5 :>?93+ *A@*B:.>?90+ C9 h6bqcHa?^`c$^va%b#]e o e bne_rjdlgo ebx\ jg¥ s%c ?o ^`cHbn¥k%cHe\ I`X MS$K FHM$T`XM { RP T}}TE~T }I { I3FHMOKH}FHWT { PRATICA - Lezione 5 N ^?]]^`cH^vb#]]bd_ bgkHjqlv¸?o j#eb ^`m °o ^`c%o DW Y PRATICA (Lez. 5) B e B ! t * b * b * b * b J Y W K TR R K d D$D D$D D$D D$D dN %D %D %N6UVD N#D W R K TR R FL}FHWT { R K TR R FL D R K TR R FL DOP$TER I TE}K FHM$T R K TR R FLJLT}}IG$G$KH$KHFM { EFHR R F R K TR R F qND W W FW W W F Livello: §?b#¥jds%\ e`§?bd\ \ bn^`m #o ^`c%o]°sm?m^`_ e j#e b Livello: IPPROTO_TCP G$T { }FWFHP { TQ}T { }FHWFP { :: /7/7,,: /7&(6* %'%-@,=,e(* * hh(% 66 hh* 4d0d0,,* '::*/=/=h,, 6 %-, ::/7/7,, :: /=/0,, jj;;'hh:: ** hh-66 %%// ,, ) %)/u%n /u n + h8 % :8 :, h8 /7h 3* % %8 h8 %J[ * %h v l +8 +.<%-,=8 : * %',=* h+'6 , =h:;d=+ , hd75 f(<: h# /0% 5 k: %'c %d75 + :;% d7l %<8 # .<+/=, %/7*=%:f=8%'% h +: /=6e,h/(/=%*=*7:h/=% +', =:;+ ;* h-h6 : %/ +'&, *=:f=% &(%',e* h8 6 :, hh8 d0/7,+ + 5 & 8 +# / 5 %-f7k fe+ c :-5@(/i: /0, %8 hAl <: /?# *7:f=%% +-& +-& -*=* :+f=% 8 * % ) -hd7fg:h/7%12 : * +2(: :, +-, + 'hd(d(@(8 % :;%8 * h (+ * % hd7fg:h/=% )+2: : , +, + C B Y ° E B W 9 J - Y F tW ?sJ|b]]bq_ bn]^`\ ^·\ be e jd B c$^`c]k%_o e ej ¸?_r^?j#§?kHj#]e §%b#h6bgj¸?o \ o e jd_rb Y 106 PRATICA (Lez. 5) `sHb#]ejn^`m #o ^`cHb $bq_ hbgjim%_rb#hbqco _ bi¸_ ^?j§?k$j#]e`j#k$k?o §?bqc$ejd\ o $} MOPEFHGHEIJQJQIQPHETM$$TQK MOK M$P { R K M$$K EK SS$FL$K $T} { K M$IS$K FHM$T { TM { TLP$F { ET $ $TQK M$}TEK ET MLK M$$K EK SSFw$KH HEFHI$I} { RHP$EFG$EIJLJQIQ$F #ET $ $T3FHM { EFHR R IET TLR FHPS$K FHM$TL¹3$K }I $K R K { I { IQK RH $EFHI$I} { M$FHM K TM$T XI {u{ FwFHJ M T 9 B W " :%/7%>8 : , h8 /=-+ , h ) -%8 h8 % e _ k k e 5 B ! Y T 6$$D DW ?G AE T F Autunno 2003 108 PRATICA (Lez. 5) ¶sHjdcH§?^ njq¸?o \ o e j°e jimHbd_Vs%c¬]^?k%¤b#e f ]?mb°§`o ]kb sc|lOb]]j#a?a`o ^|§`okH^`cHe_ ^`\ \ ^v]bic^`c¬k g]kjdlg¸?o ^ §`o§?j#eo?mHbd_sc|m$bq_o ^?§?^v§`o ^`_ b B ! N Autunno 2003 Y[c¬jqm?m?\ o kHj #o ^`c$bgk%ªHbio c$ebdcH§%bn]°mHb#§`o _ubio c B Y[Z\?m?_r^?kb#]]^m?s \ b#a?a?bd_rbio \hjd\ ^`_ubg§`o]^ ?bq__r^`_ e_ jlgo e b `sHb]ejg^`m °o ^`cHb K M { TEFwEK { FHEM$I { FL¹LK R IR FHETL$KH}F %TEE~FHE N #bdcH§%bxm^?]]?o ¸?o \ bi\ jge_ j]?lgo ]]?o ^`cHbx¸?_ ^?j°§?kj#]e Y 107 B % ° PRATICA (Lez. 5) B C B Autunno 2003 jic$^?e o ¥o kHjg§?bq\ \ b__ ^`_ubgj°hho bqc$bio c |lw^?§`o TQK RHP$EFT}}FL¹QK M M$I3 K IJQI { ILI3}TR T { ¯°}K IQP$TER I R T {u{ EIL T3P$TER IQ}EK {u{ EI¯EK { FHEM$ILFMOK RH$T}EK {u{ FET P$EFHM { FLIOT}}TETLR T {~{ F TQ}KH} { I }IMH$FL tG K $I { FQ$IKH}TGHM$IR K ¯ ¢ K TM$TLG$TM$TEI { F 105 B * PRATICA (Lez. 5) s$jqcH§?^|]?ohbq_o ¥ o kHjis%c¬bd__ ^`_rb ds%c¬kH^?§`o kHbg§`o bd__ ^ _ bvho bdcHbilbqlO^`_o j#e ^·o cts%cHjghj_o jq¸?o \ b k%ª%o jdlwj#e jg]^ ?b__r^`_ Autunno 2003 ~ ) (Z rq Hi lu q Hj b1L C t [ bukH[ Z?t t rH[ q q Hj C(u b t [ Cu mH[ Z?L t r1[ q ) ) ) Z =J q Hj t[ Lh Lh v e [ uh v eue _ c e [ t[ L]1j 1n n ) - UZ Lq ) ) Z b] ) = q ) J v q Hj L[ uh uh e [ uh v ere _ c e [ ] ) *[ 3n Z ) ) 3[ (Z UUZ Z Z k ) - ] ) 2 k) Y IPPROTO_IP Y[Z\?m?_r^?a _ jdlnlwjg]^?k%¤^`mHe ] rk%ªHb#k?¤Q]ejdlnmjxoh6jd\ ^`_o§`o % Tg§?bee ^|k%ªHbgesHe ebg]?o jdcH^v]?s%m?m^`_ ej#eb Y[^`c 104 PRATICA (Lez. 5) Autunno 2003 % %! ! !% ! ! !1&! 0(! ! ((!! !! (! !! ! 01 % % & ( ! ! !! %% & ( !! !! H !! !&!&! ! ! !& !(! 1 ! @ ! 1(H !! !(! 5- H t * !* 1 0( !! ! & ! ! @% .( ! !+ Livello: SOL_SOCKET P3a`co?\ o h6bq\ \ ^xª$jghjd_o bg^m #o ^`co Y +;+;>98: /+;>L698 96 +;8;++ : +3 / +;/:9: +: +;,L//-6 5 ' / +; 5 +/: +;: ' L> +; +78)L> +;: ' + 6 +;+78 + 6 +;: ' 5 /+ +; +78 5 / + +;u: / /698)8): +;:u/ /-+: +; < -/ +; / +;3 + ->96 9, 55 --;3+ 8)- : 5 5 3+ + + 5 - t-t, //-6 5 ' / t- 6 : t- 6 / t-+7+8/ 69< 103 PRATICA (Lez. 5) H^`cH^|§`o ho ]bio c¬hjd_o?\ o hbd\ \ o dPSHK FHM$K Autunno 2003 Y $bq_ hbgjiljqcHebdcHbd_rbgj°e eo hji\ jgkH^`c%c$b]]°o ^`cHb Y |m^?]]°o ¸?o \ okHj#]°o N $KEK }P$FH} { I R IQFHM$M$T}}K FHM$TL¹3IMHFHEIQI {u{ K I v$KHEK }P$FH} { I R ILFM$M$T}}K FHM$TQTEI3} { I { IL K } I¯ K TM$TQG$TM$TEI { FLR TEEFHETQ$N6 ° { { { FMO ¹QEK }P$FH} I¯ TM$G$FHM$FQ}P$T$K K R TEK FHEK JLT}}IG$G$KHFG$M$K w}TFHM$$K ^ ^ W ( W B " &(d %%/7*=%*7@(8 +/7, +Ah 8 ) :-%d7k h8 *h,l +8 % Z(ehc d0chmw ueu y hC. p : /(c @7,=:7%k 3* %'6jh/1Z : -; :%/=% " we%h*=* 5+-c &(p&d*: coe w _ k Z :7%8 h8 %A*=@ ) )+18 %-, %3[; :%/7%&(%/7%8 +', h ) -%8 h8 % W B B H * % ¢ 109 PRATICA (Lez. 5) Y ]?mb#§`o #o ^`cHb r !! &J! !u! H ! J!! R %FM$FXrX |¯ Z Z ) q ) Z )Z ) Z R %FM$FXrX |≠ ¯#R %R K M$G$TE | J[ Z Z UZ Z e ) c w e {D_ c [ l ) )) R %FM$FXrX |≠ ¯#R %R K M$G$TE ≠ | (Z t] ] l2 ) Z ) J) Z Z l UZ (Z (Z ) ) ) ) UZ ) ) ) *) B & °N B B ¢ ¢ vT < J2 ¢wjebd_ljq_¤ R$TXI R { ¹ 32 ¢ % | BB !99! B dN N d M Y D$ ° °¢ F UVHT B B C 2003 % UHAutunno 112 PRATICA (Lez. 5) * ,=8 @76j,,e: . )h%-;/7+&3) , ; * %'6 * %'6jh/(Z: ) h/7&3, ; @(* %'6 . : 698 h* %-6eh/(Z : 9T (W LW W W QT W F T -F B > AC -F P FG P -F G)G1C 113 PRATICA (Lez. 5) Y[Z U B °N bd_§%b#¥jds%\ eLo \eo lwb^`s$e qK JLTF { $K }I $K R K { I { FH¯ ?s |b]]bq_ bn]^`\ ^·\ be e j dc$^`c]k%_o e ej B & Y[Z\eo lwb#^`sHeLo c¬]°mb#§`o °o ^`cHb imb_\ bn¥ s%c °o ^`c%o tEK { T¯´wEK { T ¯°}TM$H¯°}TM$ { F Autunno 2003 o bdcHbisH]j#ejn§%jis%ctm?_ ^?kHb#]]^vk%ªbgbd_rb#§`o ejxs%c F J Y U ]^?k?¤ be E < 9T M J P ? W Y[Z\eo w l b#^`sHeLo ct_o kHb #o ^`c$b imHbd_V\ bg¥s%c °o ^`c%o °TI$¯#ETI ¯ET #¯#ET XEFHQ J ¯#ET JL}G o§ i o \eo m^v§`o]^?k%¤be Ypf < Y 110 PRATICA (Lez. 5) #o ^`cHbgb#§·scH^v§`o tW o?¸ 6e bn§`o ]°m^`co ¸?o \ o°cHbq\?¸?s$¥¥bq_ B JC¤ W §`o]?mb§`o #o ^`c$bg§%b°hbgb#]]b_ bgjd\ lbdc$^·mjd_ojd\?\ ^ Autunno 2003 o mbd_lOb#ee ^`cH^|§`o?sH]jd_ bisceo lwb°^`sHeLmbd_V\ j ? _o kHb °o ^`cHbnbi\ jg]?mb°§`o °o ^`cHbg]?s%\]^°k?¤ be wjebd_lOjd_¤Qjd\ \ ^`_ j Y[Zctlw^?§?^|]?o lgo \ bimbd_Vm^%e bd_]k%_o hb_ bxu]^?k%¤be m?_ ^`cHe ^o c¬]k?_o e es%_ jd D ]bq\ bkHeL_o e ^`_cHjv²]^?k%¤b#ewm?_ ` ^ c$e^o ct\ be es%_rjd³ D$TE NDOT3¡%DwK RH$TXI R { Q ¹ = % 6¡ #f wb % wk%o?mbq_lbee ^`cH^|§`o kHjdlg¸?o j_ bx\ jga _rjqc§?b n j §%bqo?¸?s$¥¥ bd_ Ypf s$jqcH§?^o ct_o kHb °o ^`cHbg]^`cH^m?_ b#]bdcHeojq\ lwbdcH^·sc c%s%lObq_ ^|§`o?¸ ebxmjd_ojq\?\ ^ *P Y 111 Y Y[Z\?¸?sH¥¥bd_Vo c¬]°mb°§`o #o ^`c$b isH]je ^ mbd_¥ jd_]k%_o h6bd_o §?j#eo§%jd\ \ jm?m?\ o kHj °o ^`cHbxm%_o lOjg§`o]%mb§`o _\ o PRATICA (Lez. 5) H^`cH^\ bn]^?a`\ o bisH]je bg§?jd\ \ jg¥ s%c °o ^`cHbg]bd\ b#ke tW Autunno 2003 Y ¡ LT Y[Z\?¸?sH¥¥bd_§`o?_o kHb #o ^`cHb isH]j°e ^·mb_VlwjdcHebqc$bd_rbio §?j#eo?cHbd\?¤ bd_c$bd\?m?_o ljg§`o?mj#]]j_\ ojd\ \ jqm?m?\ o kHj #o ^`c$b TR R FwP $ $R K K SS$I { F N#FHM ND$¯#R Fw}P$ISHK Fw$K }P$FHM$K $K R TL¹ K M$$KM$FHMw$F E~T $ $TQT}}TEK M$TR R I`XK M$T} { EIQ$K N#D F TE XR F´ N#FHMO¡%D$¯°T TM { IR KHPHI T {~{ KHK MOF TE XR F´ #TM$G$FHM$F IM$TR R I { K " 6(+'h,=* :%A8 %# 8 *=:, :h8@(/7: +1 : . .<% :+# , +.%/7, %% : %8 /7% *ed7% : 8 %';'%/7,=@=+ : "" # +; :,6=+>h/(: /=c %*=*7: h/0% ; :%c /=%1.<8 +1%* 8%': ,* 6 +-(, :++ +>: (8 +-%,e: /=:-6e8 %+*78 /7:+-@(f9::h/7/7h% /3*=% d=% + :,e: 6eh/(/7%*=*7: h/0% " (k +'h,=*:%18 %/7*=h: /i@(8:0 :;, h%8/7/7&g+1+/7*7h@(2(*=:, d7h % .<:,e+: +*7d7%-, + : /7&g%8 * %-6=h / : + e: /76 1: " 3: :. /0&(d7%h88 , *+%-/76e, h%)/ 6jh:-/7: ,=(8 h+,=+:0;8 %1%/7: &g;-h+ /0h3h86e% +/06j:-% 8 : , +-h,e8 :/7hd7%8 6 hd7h #N PQa`c%o]^#k%¤b#eLª$jis%ct¸?sH¥¥bd_§`o?_o kHb P TK XK IQK RX MS$K FHM$IJQTM { FL$TR R ILR FH}TQP$TEPHEF { FFHR R K $ FHEK TM { I { KHIR R IQFHM$M$T}}K FM$TL ND$ B & J B B J 9T E tW Autunno 2003 Y[Z LW dPSHK FHM$K$KH D Autunno 2003 114 PRATICA (Lez. 5) dZf T ¹Q}T {u{ I { I3P$TE MwEI´ }FHWT { $K DLIR R FHEI L R IP$P$R K IS$K FM$TL$T TQFH} { E K ET3IM$ TQR D TI$TE T M$FHEJQIR JQTM { T3¹L}EK {u{ Fw$IRWTEM$TR W AZ ¶ D$TEJLT ~{ { TQ$KK M$}TEK ETLR TLFPSHK FHM$KH$TR R D Y[Z K M$$KHR IX MS$K FM$TL}K $R FHI t W TI$TE D$TEJLT {~{ TQ$K}P$TK XK IETQK R K JLT F K TL K M $I { IG$E~IJ RH$TXI R { ¹ FP$}q$TR t dPSHK FHM$K$K qN6D " lu5 % h !8 .<v %'eu, e %Ul _<Z# :c .< hte ]e:6j+8 % ) % <h8 %Z :7+', %* +>@(* +', %Z(+ "} t[ 8 : ,=8 +*=.%', =:d7%8 * %. d(8 % "} " 6eDth[ /(@(/7* t%+1[ *=:8*7) ::,=Z(h8 %+/0]% *7+.<@ %),3, jZ :-:-d7*=%: *8, %1.<* %-+ 6jh/(Z :-d(8 : .<+UZ :0+2(20+/(Zh/7+8 % )+ " l %8 .< %', B%UZ:.<htZ :]=:6j+8 %>: ) ;'+ ) h8 %UZ% ) ) - w5(5Z(% ) )+6eh/(/=%*=*=:h/7% " 6ey & +:+* Uh 2: Z) : , :7+ 6eh) -+ /7 &()&(¤%h* 8 ,e:,e:h. /7h % Z : d +'& )%6 l=% Z : .: /(@: * 6j%A: Z(+',=:(*=d7% Z :,e:: / $}TJLP$K Fw$KH}T { TLGHT { }FHWFHP { Autunno 2003 115 Autunno 2003 PRATICA (Lez. 5) N#D 6$$D N#D ?U N#D ?U N#D 116 PRATICA (Lez. 5) #include "basic.h" #include <netinet/tcp.h> int main(int argc, char **argv) { int sockfd, mss, sendbuff, ttl; socklen_t optlen; sockopts-set.c if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) err_sys("socket error"); optlen = sizeof(ttl); if( getsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, &optlen) == -1 ) err_ret("getsockopt error"); printf("getsockopt: TTL = %d\n", ttl); $¢ ttl = ttl + 16; if( setsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) == -1 ) err_ret("getsockopt error"); printf("TTL modificato\n", ttl); if( getsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, &optlen) == -1 ) err_ret("getsockopt error"); printf("getsockopt: TTL = %d\n", ttl); exit(0); %d$ }