EKT434 Network Programming

Similar documents
Porting applications & DNS issues. socket interface extensions for IPv6. Eva M. Castro. ecastro@dit.upm.es. dit. Porting applications & DNS issues UPM

Tutorial on Socket Programming

Socket Programming. Srinidhi Varadarajan

INTRODUCTION UNIX NETWORK PROGRAMMING Vol 1, Third Edition by Richard Stevens

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Socket Programming. Request. Reply. Figure 1. Client-Server paradigm

Unix Network Programming

Socket Programming. Kameswari Chebrolu Dept. of Electrical Engineering, IIT Kanpur

Introduction to Socket programming using C

NS3 Lab 1 TCP/IP Network Programming in C

CSE 333 SECTION 6. Networking and sockets

TCP/IP - Socket Programming

Implementing Network Software

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Lab 4: Socket Programming: netcat part

Socket Programming in C/C++

Application Architecture

Review of Previous Lecture

Network Programming with Sockets. Anatomy of an Internet Connection

Communication Networks. Introduction & Socket Programming Yuval Rochman

IBM i Version 7.2. Programming Socket programming IBM

UNIX. Sockets. mgr inż. Marcin Borkowski

The POSIX Socket API

Windows Socket Programming & IPv6 Translation Middleware

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

Client / Server Programming with TCP/IP Sockets

VMCI Sockets Programming Guide VMware ESX/ESXi 4.x VMware Workstation 7.x VMware Server 2.0

Networks class CS144 Introduction to Computer Networking Goal: Teach the concepts underlying networks Prerequisites:

An Overview of IPv6 CHAPTER

Programming guidelines on transition to IPv6

IPv6 Enabling CIFS/SMB Applications

Network Programming with Sockets. Process Management in UNIX

Programmation Systèmes Cours 9 UNIX Domain Sockets

DESIGN AND IMPLEMENT AND ONLINE EXERCISE FOR TEACHING AND DEVELOPMENT OF A SERVER USING SOCKET PROGRAMMING IN C

Lecture 7: Introduction to Sockets

Hostnames. HOSTS.TXT was a bottleneck. Once there was HOSTS.TXT. CSCE515 Computer Network Programming. Hierarchical Organization of DNS

Packet Sniffing and Spoofing Lab

Elementary Name and Address. Conversions

KATRAGADDA INNOVATIVE TRUST FOR EDUCATION NETWORK PROGRAMMING. Notes prepared by D. Teja Santosh, Assistant Professor, KPES, Shabad, R.R. District.

A Client Server Transaction. Introduction to Computer Systems /18 243, fall th Lecture, Nov. 3 rd

Overview. Socket Programming. Using Ports to Identify Services. UNIX Socket API. Knowing What Port Number To Use. Socket: End Point of Communication

sys socketcall: Network systems calls on Linux

This tutorial has been designed for everyone interested in learning the data exchange features of Unix Sockets.

UNIX Sockets. COS 461 Precept 1

Computer Networks Network architecture

System calls. Problem: How to access resources other than CPU

Goal: learn how to build client/server application that communicate using sockets. An interface between application and network

TCP/IP Sockets in C. Practical Guide for Programmers

BSD Sockets Interface Programmer s Guide

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

Beej's Guide to Network Programming

Implementing and testing tftp

Writing Client/Server Programs in C Using Sockets (A Tutorial) Part I. Session Greg Granger grgran@sas. sas.com. SAS/C & C++ Support

Operating Systems Design 16. Networking: Sockets

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Giving credit where credit is due

The programming language C. sws1 1

Best practices in IPv6 enabled networking software development.

Porting and Deploying VoIP to IPv6: Lessons Learned

Carnegie Mellon. Internetworking : Introduc0on to Computer Systems 19 th Lecture, Oct. 28, Instructors: Randy Bryant and Dave O Hallaron

A Client-Server Transaction. Systemprogrammering 2006 Föreläsning 8 Network Programming. Hardware Org of a Network Host.

Example 1/24. Example

Network Programming. Chapter The Client-Server Programming Model

Networks. Inter-process Communication. Pipes. Inter-process Communication

Generalised Socket Addresses for Unix Squeak

Concurrent Server Design Alternatives

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Internetworking / : Introduc2on to Computer Systems 20 th Lecture, Nov. 6, 2012

An Introductory 4.4BSD Interprocess Communication Tutorial

Elementary Name and Address Conversions

Warning for programmers. Network programming: sockets. ISO/OSI, TCP/IP, network programming. Exercise copying data. Error functions.

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

Name and Address Conversions

RIOT-Lab. How to use RIOT in the IoT-Lab. Oliver "Oleg" Hahm. November 7, 2014 INRIA. O. Hahm (INRIA) RIOT-Lab November 7, / 29

How To Write Portable Programs In C

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

IPv4/IPv6 Transition for SIP VoIP Applications

Software changes for Website and Application IPv6 Readiness

Objectives of Lecture. Network Architecture. Protocols. Contents

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Domain Name System (1)! gethostbyname (2)! gethostbyaddr (2)!

MatrixSSL Porting Guide

RPG Does TCP/IP (Socket Progamming in RPG IV)

Writing a C-based Client/Server

Lecture 21: Networking & Protocol Stacks

Internet Addressing. Name: identifies what the object is Address: identifies where it is Route: identifies how to get there

Network Programming using sockets

The exam has 110 possible points, 10 of which are extra credit. There is a Word Bank on Page 8. Pages 7-8 can be removed from the exam.

Data Communication Networks. Lecture 1

陳 懷 恩 博 士 助 理 教 授 兼 計 算 機 中 心 資 訊 網 路 組 組 長 國 立 宜 蘭 大 學 資 工 所 TEL: # 340

transmission media and network topologies client/server architecture layers, protocols, and sockets

TCP/IP. Connect, Inc Quincy Avenue, Suites 5 & 6, Naperville, IL Ph: (630) Fax: (630)

Category: Standards Track August 1995

University of Amsterdam

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

Hardware Prerequisites Atmel SAM W25 Xplained Pro Evaluation Kit Atmel IO1 extension Micro-USB Cable (Micro-A / Micro-B)

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

Network Programming TDC 561

Transcription:

EKT434 Network Programming Socket Introduction

abstract Socket Address Structure Value-Result Argument Byte Ordering Function Byte Manipulation Function

How to Write Network Applications? TCP/IP is implemented inside the OS OS exports the TCP/IP functionalities through SOCKET APIs API: Application Program Interface A set of functions Programmers develop network applications by using SOCKET APIs To learn SOCKET programming Learn some data structures Learn the socket APIs Learn multi-threading

Socket Abstraction Socket is an interface between applications and the network services provided by OS An application sends and receives data through a socket Application SOCKETAPI TCP/IPv4 TCP/IPv6 UNIX

Socket Abstraction (Cont.) Process A data data Process B data socket socket

Socket Abstraction (Cont.) Browser A Web Server Browser B socket socket socket Browser C

Socket Descriptor Data Structure 0 1 2 3 4 Descriptor Table Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726

How to Create a Socket? The socket API contains a function socket() that can be called to create a socket. E.g.: #include <winsock2.h> SOCKET s; s = socket(af_inet, SOCK_DGRAM, 0) ; The socket is not usable yet. We need to specify more information before using it for data transmission.

Types of Sockets Under protocol family AF_INET Stream socket Uses TCP for connection-oriented reliable communication Identified by SOCK_STREAM s = socket(af_inet, SOCK_STREAM, 0) ; Datagram socket Uses UDP for connectionless communication Identified by SOCK_DGRAM s = socket(af_inet, SOCK_DGRAM, 0) ; RAW socket Uses IP directly Identified by SOCK_RAW Advanced topic.

Review: Structure C structures are collections of related variables under one name. struct employee { char firstname[20]; char lastname[20]; int age; float salary; }; typedef struct employee Employee; Employee em; Employee *pem; /* pointer to structure */ em.salary = 23456; /* dot operator */ pem = &em; /* assign address of em to pem */ pem->salary += 1000; /* struct pointer operator */

IPv4 Socket Address Structure Struct in_addr{ in_addr_t s_addr; /*32bit IPv4 address*/ }; /*network byte ordered*/ struct sockaddr_in { uint8_t sin_len; /* length of structure(16) */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* 16bit TCP or UDP port number */ /*network byte ordered*/ struct in_addr sin_addr; /* 32bit IPv4 address */ /*network byte ordered*/ char sin_zero[8]; /* unused */ }; /* included in <netinet/in.h> */

Generic Socket address structure Socket address structure protocol structure handling ANSI C : void* <sys/socket.h> : Generic Socket address structure struct sockaddr { uint8_t sa_len; sa_family_t sa_family; /*address family: AF_xxx value*/ char sa_data[14]; /*protocol specific address*/ }

Generic SA structure int bind(int, struct sockaddr *, socklen_t); (example) struct sockaddr_in serv; /*IPv4 socket address structure*/ /* fill in serv{} */ bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));

sockaddr vs. sockaddr_in struct sockaddr struct sockaddr_in 2 bytes sa_family sin_family 2 bytes sin_port 2 bytes 14 bytes sa_data sin_addr 4 bytes sin_zero 8 bytes A pointer to a struct sockaddr_in can be cast to a pointer to a struct sockaddr and vice-versa.

IPv6 Socket Address Structure Struct in6_addr{ uint8_t s6_addr[16]; /*128bit IPv6 address*/ }; /*network byte ordered*/ #define SIN6_LEN /* required for compile-time tests */ struct sockaddr_in6 { uint8_t sin6_len; /* length of structure(24) */ sa_family_t sin6_family; /* AF_INET6*/ in_port_t sin6_port; /* Transport layer port# */ /*network byte ordered*/ uint32_t sin6_flowinfo; /* priority & flow label */ /*network byte ordered*/ struct in6_addr sin6_addr; /* IPv6 address */ /*network byte ordered*/ }; /* included in <netinet/in.h> */

Comparison of socket address structure

Value-Result Argument

Socket address structure pass. Bind, connect, sendto Accept, recvfrom, getsockname, getpeername

Byte Ordering Byte Ordering: Memory is organized in bytes. E.g, a 16-bit integer takes 2 consecutive bytes; a 32-bit integer takes 4 consecutive bytes. Different machines use different host bye orderings: little-endian: least significant byte located at lower memory E.g., Intel 80x86 big-endian: least significant byte located at higher memory E.g., Motorola 68000 These machines may communicate with one another over the network, and they may have different understandings on the same data. Not a problem for data type char

Chaos of Byte Order Memory Memory Address 00001010 A 00010111 A+1 00001110 A+2 00000110 A+3 Big-Endian Computer Send out an integer 169283078 Big-Endian Little-Endian 00001010 00010111 00001110 00000110 00000110 00001110 00010111 00001010 Little-Endian Computer A A+1 A+2 A+3 A+3 A+2 A+1 169283078 68032266 A Receive an integer 68032266!

Solution: Network Byte Order Let s have some regulation! Network byte order is defined as big-endian byte order Integers should be sent out in network byte order The members sin_port and sin_addr in struct sockaddr_in should be in network byte order sin_port: TCP or UDP port number, put into the TCP/UDP header sin_addr: IP address, put into the IP header The member sin_family will not be sent out to the network. So it is not necessary to be in network byte order. If your application needs to transfer integers between two machines, you should transfer them into network byte order. You don t know the type of CPU that executes your program!

Network Byte Order When sending out an integer Transfer it into network byte order by: u_short htons (u_short host_short); u_long htonl (u_long host_long); After receiving an integer Transfer it into (local) host byte order by: u_short ntohs (u_short network_short); u_long ntohl (u_long network_long); Remark: u_short: unsigned 16-bit integer u_long: unsigned 32-bit integer h: host n: network s: short l: long h-to-n-s h-to-n-l n-to-h-s n-to-h-l

Convert the natives! Byte order h : host byte order n : network byte order s : short (16bit) l : long (32bit) uint16_t htons(uint16_t); uint16_t ntohs(uint_16_t); uint32_t htonl(uint32_t); uint32_t ntohl(uint32_t); In_addr values should be stored in network byte order (common mistake)

Determine host byte order #include "unp.h" int main(int argc, char **argv) { } union {short s; un.s = 0x0102; char c[sizeof(short)]; } un; printf("%s: ", CPU_VENDOR_OS); 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("littleendian\n"); else printf("unknown\n"); } else printf("sizeof(short) = %d\n", sizeof(short)); exit(0);

Byte Manipulation Functions: Operating on multibyte fields From 4.2BSD: #include <strings.h> void bzero (void *dest, size_t nbytes); void bcopy (const void *src, void *dest, size_t nbytes); int bcmp (const void *ptr1, const void *ptr2, size_t nbytes); returns: 0 if equal, nonzero if unequal From ANSI C: #include <string.h> void *memset (void *dest, int c, size_t len); void *memcpy (void *dest, const void *src, size_t nbytes); int memcmp (const void *ptr1, const void *ptr2, size_t nbytes); returns: 0 if equal, nonzero if unequal

Address Conversion Functions between ASCII strings and network byte ordered binary values For IPv4 only: ascii and numeric #include <arpa/inet.h> int inet_aton (const char *strptr, struct in_addr *addrptr); returns: 1 if string is valid, 0 on error int_addr_t inet_addr (const char *strptr); returns: 32-bit binary IPv4 addr, INADDR_NONE if error char * inet_ntoa (struct in_addr inaddr); returns: pointer to dotted-decimal string For IPv4 (AF_INET) and IPv6 (AF_INET6): presentation and numeric #include <arpa/inet.h> int inet_pton (int family, const char *strptr, void *addrptr); returns: 1 if OK, 0 if invalid presentation, -1 on error const char *inet_ntop (int family, const void *addrptr, char *strptr, size_t len); returns: pointer to result if OK, NULL on error