Distributed Programming I (Socket - Nov'09)
Total Page:16
File Type:pdf, Size:1020Kb
Distributed programming I (socket - nov'09) Warning for programmers network programming is dangerously close to O.S. Network programming: sockets kernel, and therefore: It can easily hang the O.S. verify the results of every operation, without assuming anything as granted Antonio Lioy < [email protected] > APIs can vary in details that are minimal but important consider every possible situation to create english version created and modified by “portable” programs Marco D. Aime < [email protected] > we will try to use Posix 1.g Politecnico di Torino Dip. Automatica e Informatica ISO/OSI, TCP/IP, network programming Exercise – copying data copy the content of file F1 (first parameter on the application command line) into file F2 (second parameter on the 7. application details application command line) user 6. presentation (l6: XDR/XML/... process l5: RPC/SOAP/...) 5. session network programming 4. transport TCP UDP SCTP interface 3. network IPv4, IPv6 kernel communication 2. data link device driver details 1. physical and hardware OSI model IP suite ref. UNP Intro copyfile.c Error messages Error functions must contain at least: best to define standard error reporting functions [ PROG ] program name which accept: [ LEVEL ] error level (info, warning, error, bug) a format string for the error [ TEXT ] error signalling, the most specific as possible a list of parameters to be printed (e.g. input file name and line where the problem has UNP, appendix D.4 (D.3 in 3rd edition) occurred) errno? termination? log level [ ERRNO ] system error number and/or name (if applicable) err_msg no no LOG_INFO err_quit no exit(1) LOG_ERR suggested format: err_ret yes no LOG_INFO err_sys yes exit(1) LOG_ERR ( PROG ) LEVEL - TEXT : ERRNO err_dump yes abort( ) LOG_ERR errlib.h errlib.c © A.Lioy - Politecnico di Torino (2009) B-1 Distributed programming I (socket - nov'09) stdarg.h stdarg.h usage example variable list of arguments (ANSI C) create a function named my_printf declared with an ellipsis (. .) as the last argument of … behaving like printf (thus accepting a variable a function number of parameters) arguments used altogether (ap) with special … but printing the string “(MY_PRINTF)” before its functions (e.g. vprintf, vfprintf, vsprintf, vasprintf, output vsnprintf), or separately (va_ arg); but in the second case you must know their number by other means #include <stdarg.h> void va_start (va_list ap, RIGHTMOST); TYPE va_arg (va_list ap, TYPE); void va_end (va_list ap); va_test.c Exchanging data between From 32 to 64 bit: problems heterogeneous nodes migration from 32-bit to 64-bit architecture has problem: changed data sizes when exchanging complex data (i.e., not single ASCII in particular, do not assume anymore that characters), do not assume they are encoded in the | int | = | pointer | same way on different nodes therefore, pay attention to correctly use the data encoding depends on HW + OS predefined types to avoid problems (e. g. size_ t) solution: use a neutral format (the network format) ILP32 LP64 some functions automatically do the conversion… char 88 short 16 16 … but often it is explicit task for the programmer int 32 32 long 32 64 pointer 32 64 datasize.c Sources of data incompatibility “host to network” functions different floating-point formats (IEEE-754 / non-IEEE) to pass parameters to network functions alignment of structs on word boundaries not designed for application data passing byte order of integers (little-endian or big-endian) #include <sys/types.h> #include <netinet/in.h> n = 25810 = 010216 uint32_t htonl (uint32_t hostlong); big-endian little-endian uint16_t htons (uint16_t hostshort); uint32_t ntohl (uint32_t netlong); 01 memory address X 02 uint16_t ntohs (uint16_t netshort); 02 memory address X+1 01 byteorder.c © A.Lioy - Politecnico di Torino (2009) B-2 Distributed programming I (socket - nov'09) Notes on integer types Network addresses sometimes you may still find the old types: IPv4 networks directly use 32-bit addresses u_long (= uint32_t) ... not names (e.g. www.polito.it), that are translated u_short (= uint16_t) into addresses by DNS in old versions of cygwin, they were defined as: ... and neither dotted addresses (e.g. 130.192.11.51) u_ int32_ t (= uint32_t) ... but their 32-bit numerical value u_int16_t (= uint16_t) example: 130.192.11.51 24 16 8 suggestion: = 130*2 + 192*2 + 11*2 + 51 write programs with the types uint32_t and uint16_t = (((130*256) + 192)*256 + 11)*256 + 51 when necessary, map them with a conditioned #define = 130<<24 + 192<<16 + 11<<8 + 51 = 2,193,623,859 Network address conversion inet_aton ( ) for generality, standard functions require numerical converts an IPv4 address … addresses to be expressed in struct format … from “dotted notation” string to convert numerical addresses from/to strings: … to numerical (network) format [IPv4] inet_ntoa( ) and inet_aton( ) returns 0 if the address is invalid [IPv4 / v6] inet_ntop( ) and inet_pton( ) the string may be composed by decimal numbers other functions (e.g. inet_addr) are deprecated (default), octal (start with 0) or hexadecimal (start with 0x) thus 226.000.000.037 is equal to 226.0.0.31 #include <arpa/inet.h> #include <arpa/inet.h> struct in_addr { in_addr_t s_addr }; int inet_aton ( const char *strptr, struct in_addr *addrptr struct in6_addr { uint8_t s6_addr[16]; }; ); inet_ntoa ( ) Example: address validation converts an IPv4 address … write a program that: … from numerical (network) format accepts a dotted notation IPv4 address on the … to “dotted notation” string command line returns the pointer to the string or NULL if the returns its numerical (network) value address is invalid signals an error in case of erroneous format or illegal beware! the pointer being returned points to a static address memory area internal to the function verify: #include <arpa/inet.h> remember that A.B.C.D = A<<24 + B<<16 + C<<8 + D char *inet_ntoa ( struct in_addr addr avrfy.c ); avrfy2.c © A.Lioy - Politecnico di Torino (2009) B-3 Distributed programming I (socket - nov'09) inet_pton( ) inet_ntop( ) converts an IPv4 / IPv6 address … converts an IPv4 / IPv6 address … … from “dotted notation” string … from numerical (network) format … to numerical (network) format … to “dotted notation” string returns 0 if the address is invalid, -1 if the address returns the pointer to the string or NULL if the family is unknown address is invalid address family = AF_INET or AF_INET6 #include <arpa/inet.h> #include <arpa/inet.h> int inet_pton ( char *inet_ntop ( int family, int family, const char *strptr, const void *addrptr, void *addrptr char *strptr, size_t length ); ); Example: address validation Address sizes write a program that: to size the string representation of v4/v6 addresses, accepts an IPv4 or IPv6 address on the command use macros defined in <netinet/in.h> line tells if it is valid in one of the two IP versions #include <netinet/in.h> #define INET_ADDRSTRLEN 16 #define INET6_ADDRSTRLEN 46 avrfy46.c IPv6 addresses Data structure initialization (ANSI) 128 bit = 8 16-bit groups, separated by “ : ” to handle data structures as byte sequences complete form = 1080:0:0:0:8:800:200C:417A … which may include NUL, and therefore cannot be zeros compression = 1080::8:800:200C:417A handled with “str…” functions (strcpy, strcmp, …) #include <string. h> void *memset ( void *dest, int c, size_t nbyte ) void *memcpy ( void *dest, const void *src, size_t nbyte ) int *memcmp ( const void *ptr1, const void *ptr2, size_t nbyte ) © A.Lioy - Politecnico di Torino (2009) B-4 Distributed programming I (socket - nov'09) Data structure initialization (BSD) Signal management (*) pre-ANSI functions, defined in Unix BSD signals are asynchronous events still frequently used every received signal corresponds to an implicit default behaviour often the receiving process is terminated few signals are ignored by default #include <strings. h> to change the response to a signal (i.e. its void bzero ( disposition): void *dest, size_t nbyte ) reset its default behaviour void bcopy ( const void *src, void *dest, size_t nbyte ) ignore it int bcmp ( catch (i.e. intercept) the signal and register a signal const void *ptr1, const void *ptr2, size_t nbyte ) handler Timeout sleep ( ) sometimes timeouts are needed to: starts a timer and suspends the process for the wait in idle state for a fixed time (sleep) selected time know when a certain time elapsed (alarm) if terminates because the selected time has expired returns zero if terminates for being interrupted by a signal returns the time missing to the requested term #include <unistd.h> unsigned int sleep ( unsigned int seconds ); sveglia.c alarm ( ) Signal management starts a timer which generates the SIGALRM signal at pay attention to semantic differences in various expiration time operating systems the process is not suspended for ‘signum’ use the signal logical names (SIGCHLD, note: the default response to SIGALRM is terminating SIGSTOP, …) the receiving process the handler can be: a new call replaces the current timer a user-defined function alarm(0) (e.g. use to clear the current timer) SIG_IGN (ignore the signal) (!) unique timer for all processes in the group SIG_DFL (default behaviour) #include <unistd.h> #include <signal.h> unsigned int alarm ( typedef void Sigfunc(int); unsigned int seconds Sigfunc *signal (int signum, Sigfunc *handler); ); © A.Lioy - Politecnico di Torino (2009) B-5 Distributed programming I (socket - nov'09) signal() function Notes on signals signal() is a pre-POSIX function SIGKILL and SIGSTOP cannot be intercepted nor its behaviour varies across Unix versions, and also ignored varied historically across different releases SIGCHLD and SIGURG are ignored by default the only portable use is setting a signal’s disposition to in Unix, use kill –l to list all signals SIG_DFL or SIG_IGN in Unix, signals can be generated also manually via POSIX has defined the new sigaction() function kill -signal pid most implementations of signal() now call sigaction() for example, to send SIGHUP to process 1234: see UNPv3 chap. 5.8 kill -HUP 1234 Standard signals (POSIX.1) Standard signals (POSIX.1) – cont.