Matakuliah Tahun Versi : T0483 / Bahasa rakitan : 2005 : 1.0 Pertemuan 2 Socket Introduction 1 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • menjelaskan konsep-konsep Sockets pada Unix, Linux dan Windows 2 Outline Materi • • • • • Port IPv4 Socket address structure Generic Socket address structure Byte Ordering function Byte manipulation function. 3 <<PORT>> • PORT Numbers – Each TCP/IP machine has multiple logical communication channels called ports – Setiap saat banyak proses bisa menggunakan UDP atau TCP. – TCP atau UDP menggunakan 16-bit positip integer Port Number untuk proses-proses yg berbeda tsb. – Nomor Ports mulai dari 0 – 65536. – Well known Port : 0 s/d 1023 dikontrol oleh IANA (Internet Assigned Numbers Authority) . Contoh: 4 <<Port>> • Contoh: – – – – – – Port 21 untuk FTP Port 69 untuk TFTP Port 7 untuk echo server Port 9 untuk discard server Port 13 untuk day time server Port 19 untuk chargen server 5 <<Port>> • A connection between two machines is uniquely defined by: – – – – – the protocol (TCP or UDP) the IP address of local machine the port number used on the local machine the IP address of remote machine the port number used on the remote machine 6 <<Network API >> • Network Application Programming Interface (Network API). – Network Application Programming Interface (Network API) : The services provided by the operating system that provide the interface between application and protocol software 7 << Network API >> • Network API – Generic Programming Interface. – Support for message oriented and connection oriented communication. – Work with existing I/O services – Operating System independence. • Generic Programming Interface – Support multiple communication protocol suites (families). – Address (endpoint) representation independence. – Provide special services for Client and Server ? 8 <<Network API>> • TCP/IP – TCP/IP does not include an API definition. – There are a variety of APIs for use with TCP/IP: • • • • Sockets TLI, XTI Winsock MacTCP Functions Needes – – – – – – Specify local and remote communication endpoints Initiate a connection Wait for incoming connection Send and receive data Terminate a connection Error handling 9 <<Sockets>> • SOCKETS – A socket is an abstract representation of a communication endpoint. – Sockets work with Unix I/O services just like files, pipes & FIFOs. – Sockets have special needs: • establishing a connection • specifying communication endpoint addresses 10 <<Sockets>> 11 <<Sockets>> • Creating a Socket – family specifies the protocol family (PF_INET for TCP/IP). – type specifies the type of service (SOCK_STREAM, SOCK_DGRAM). – protocol specifies the specific protocol (usually 0, which means the default). Protocol yang digunakan seperti tabel dibawah. – Menggunakan System call Socket(). Contoh : – fd = socket(AF_INET, SOCK_STREAM, 0); 12 <<Sockets>> • Protocol Family Description 13 <<Sockets>> 14 << Sockets>> • Socket() system call – The socket() system call returns a socket descriptor (small integer) or a -1 on error. – socket() allocates resources needed for a communication endpoint – TCP/IP requires an IP address and a port number for each endpoint address. 15 << Sockets>> POSIX Data Type 16 << Sockets>> Generic Socket Address Structure • Serupa dgn void * (generic pointer type pada ANSI C) <sys/socket.h> struct sockaddr { uint8_t sa_family_t char }; sa_len; sa_family; sa_data[14]; 17 << Sockets>> Dimana: • sa_family specifies the address type. • sa_data specifies the address value. • socket address structure selalu di-pass “by reference” jika sebagai argument pada socket functions. • Contoh: int bind(int, struct sockaddr*, socklen_t) struct sockaddr_in server; /* IP4 */ bind(sockfd, (struct sockaddr *) &server, sizeof(server)); 18 << Sockets>> • IP4 Socket Address Structure struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct in_addr { in_addr_t s_addr; }; – Alamat IP4 dan Nomor Port TCP atau UDP selalu disimpan dalam stuktur network byte order. 19 << Sockets>> Network Byte Order – Nilai yg disimpan pada “sockaddr_in” harus dalam “network byte order”, seperti : – sin_port : untuk menyimpan TCP atau UDP port number. – sin_addr : untuk menyimpan 32-bit IP4 address. 20 << Sockets>> Network Byte Order Function – Ada dua cara untuk menyimpan 2-byte data di memori: little-endian and big-endian. – Fungsi-fungsi untuk meng-konversi dari host byte order ke network byte order umumnya diawali dengan huruf: • • • • ‘h’ : host byte order ‘n’ : network byte order ‘s’ : short (16bit) ‘l’ : long (32bit) 21 << Sockets>> • Contoh Fungsi yg mengembalikan Network Byte Order : – uint16_t htons(uint16_t); – uint32_t htonl(uint32_t); • Contoh Fungsi yg mengembalikan Host Byte Order : - uint16_t ntohs(uint16_t); - uint32_t ntohl(uint32_t); 22 << Sockets>> Byte Manipulation Functions #include <string.h> void bzero(void *dst, size_t nbytes); void bcopy(const void *src, void *dest, size_t nbytes); int bcmp(const void *ptr1, const void *ptr2, size_t nbytes); Contoh: #include <string.h> #include <sys/errno.h> main(){ char x[10]; int i; for(i=0; i<10;i++) x[i]=65+i; bzero(x,4); for(i=0;i<10;i++) printf("%c\n",x[i]); } 23 << Sockets>> IPv4 Address Conversion int inet_aton( char *, struct in_addr *); • Fungsi inet_aton() berfungsi untuk mengkonversi “ASCII dotteddecimal IP address” menjadi nilai 32-bit dalam format “network byte order”. Fungsi ini mengembalikan nilai 1 jika sukses, dan mengembalikan nilai 0 jika ada error. char *inet_ntoa(struct in_addr); • Fungsi inet_ntoa() berfungsi untuk mengkonversi nilai “network byte ordered” menjadi string yg berisi “ASCII dotted-decimal IP address”. 24