53
TELE402 Lecture 1 Protocol Layering 1 TELE402 Internetworking

lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 1

TELE402

Internetworking

Page 2: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 2

People •  Lecturer

•  Teaching Assistant

Dr. Zhiyi Huang Email: [email protected] Phone: 479-5680 Office: 1.26 Owheo Building

Kai-Cheung Leung Email: [email protected] Phone: 479-7852 Office: 2.55 Owheo Building

Page 3: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 3

Assessment

•  Labs –  20%

•  Assignments –  30%

•  Exam (Have to get 40 out of 100 to pass) –  50%

Page 4: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 4

Textbooks

Internetworking with TCP/IP, Vol. 1, Principles, Protocols, and Architectures (5th ed), D.E. Comer, Prentice Hall

Unix Network Programming, Vol. 1, The Sockets Networking API (3rd ed), W.R. Stevens, B. Fenner, A.M. Rudoff, Addison Wesley

Page 5: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 5

About the Course

•  Lectures & reading –  Internetworking with TCP/IP – Socket API

•  Labs – Programming network applications

•  Assignments

Page 6: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 6

Overview

•  This lecture – The C programming language – Source: A good C programming book – Protocol layering – Source: Chapter 10 for reading

•  Next lecture – TCP and UDP

Page 7: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 7

Functions - 1 •  Remember: no classes or methods in C.

C is case-sensitive. •  Functions are the main abstraction mechanism and

represent an operation to be performed more than once.

•  Functions have a name, arguments and a return type. If a function does not produce a value, it has a return type of void.

•  The actions of a function are expressed in the body that is enclosed in curly braces -- “{…}”

•  A function is invoked whenever the call operator (“()”) is applied to the function’s name.

Page 8: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 8

Functions - 2 •  Every C program must have a function

named, “main”. This function is called to run the program. The main function returns an int or void. #include <stdio.h>

int main() {

printf(“hello, world\n”); }

Page 9: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 9

Functions - 3

•  There are many standard functions already defined that are part of the C language. These functions are stored in libraries.

•  The first line of the previous program (#include <stdio.h>) tells the preprocessor to include the standard input/output library.

Page 10: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 10

Functions - 4

int area( int length, int width )

{ return length * width;

}

•  Functions must be declared before they are called. •  An expression including a call to the area function:

2 * area( 5, 7) + 25;

Page 11: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 11

Functions – 5 (scope)

•  Variables that are declared inside a function are called, ‘automatic’, and are local to that function.

•  It is possible to define ‘external’ variables that are external to all functions. The scope of an external variable lasts from the point at which it is declared to the end of the file. If an external variable is to be used and has been defined in a different source file, then an extern declaration is required.

Page 12: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 12

Functions – 6 (scope)

•  Automatic variables are so-named, because they appear and disappear automatically when a function is called.

•  The values of function parameters are also automatic. They are copies of the values of the variables that were passed to the function in a function call. This is known as ‘call-by-value’ parameter passing.

Page 13: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 13

Example #include <stdio.h> int sum( int a, int b); // function prototype at the start of the file

int main() { int x = 4, y = 5;

int total = sum( x, y ); // function call printf( “The sum of 4 and 5 is %d”, total); }

int sum( int a, int b ) // call-by-value { return( a + b ); }

Page 14: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 14

Memory and addresses

5 10 12.5 9.8 z

2100

int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘z’;

(the addresses are at 2100, 2104, etc.)

2104 2108 2112 2116

Page 15: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering

15

Pointers - 1 •  A variable can contain a value of a data type,

such as an integer or a float. •  It can also contain a memory address - it can

contain the address of another variable. •  float x; // data variable

float *xaddr // pointer variable

x xaddr

2100 2104

any float

some address

Page 16: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 16

Pointers - 2

??? x xaddr

2100 2104 2100

xaddr = &x; // & = address-of operator

*xaddr = 4.4; // * = dereference operator

4.4 x xaddr

2100 2104 2100

Page 17: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 17

Pointers - 3

x = 3.3; 3.3

x xaddr

2100 2104 2100

Now xaddr “points to” x. If x has its value changed, the dereferencing of xaddr will yield that new value.

Page 18: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 18

Pointers - 4

int x = 1, y = 8;

int *ip, *iq;

ip = &x; // ip now points to x y = *ip; // y is now 1

*ip = 6; // x is now 6;

iq = ip; // iq points to what ip points to

Page 19: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 19

Pointer arithmetic •  int i, j, k; int *ip;

ip = &i; *ip = *ip + 2; // add 2 to i ip = ip + 2; // add to the address of

// i, which is the // address ip contains.

The addition of 2 to a pointer increases the value of the address it contains by the size of two objects of its type.

Page 20: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 20

Why use pointers? #include <stdio.h>

void swap( int, int );

main() { int num1 = 5, num2 = 10; swap( num1, num2 ); printf(“num1 = %d and num2 = %d\n”, num1, num2); }

void swap(int n1, int n2) // passed by value { int temp;

temp = n1; n1 = n2; n2 = temp; }

There’s a problem here:

Page 21: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 21

Use of pointers helps #include <stdio.h>

void swap( int*, int* );

main() { int num1 = 5, num2 = 10; swap( &num1, &num2 ); printf(“num1 = %d and num2 = %d\n”, num1, num2); }

void swap( int *n1, int *n2 ) // passed by ‘reference’ { int temp;

temp = *n1; *n1 = *n2; *n2 = temp; }

Page 22: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 22

Basic input/output

•  If you want to use the standard input/output library, you must have #include <stdio.h> before the first usage.

•  The simplest function reads one character from the standard input (usually the keyboard): int getchar( void ); (It returns the next input character each time it is called.)

•  int putchar( int c ) puts the character c on the standard output, which is usually the screen.

Page 23: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 23

Formatted output •  printf is somewhat complicated, but enables you to

produce formatted output. int printf ( char *format, arg1, arg2, … )

•  The format string contains two types of objects: ordinary characters (which are just copied to the output stream) and special conversion specifications. Each conversion begins with a ‘%’. ‘\’ is used for special characters.

•  Recall:

printf(“num1 = %d and num2 = %d\n”, num1, num2);

Page 24: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 24

Formatted input

•  The function scanf is the input analog to printf and uses many of the same conversion facilities (in the opposite direction). int scanf( char *format, arg1, arg2, … )

•  Note that each of the input arguments must be a pointer indicating where the converted input is supposed to be stored.

Page 25: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 25

Type conversion

•  At the machine level, we just have a collection of bits.

•  Changing one predefined type to another will change some properties, but not the underlying bits.

•  Some conversions are ‘safe’ and some are ‘unsafe’.

•  The programmer can request a conversion by performing a ‘cast’.

Page 26: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 26

Casting

•  There are two ways to request a cast: –  type (expr) –  (type) expr

•  What happens when we do this? –  double (int (3.14159) );

•  Some conversions are safe on some machines, but not on others (it depends on the word size of the machine), because an int is usually the same size as either a short or a long, but not both.

Page 27: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 27

Implicit (automatic) conversions

•  Assigning a value to an object converts the value to the type of that object. void ff( int ); int val = 3.14159; // converts to int 3

ff( 3.14159 ); // converts to int 3 •  The widest data type in an arithmetic expression is the target

conversion type: val + 3.14159; // double

(but val is still an int)

Page 28: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 28

Pointer conversions int ival; int *pi = 0; char *pc = 0; void *pv; // can convert others to this, // but a void* pointer cannot // be dereferenced directly. pv = pi; // ok pc = pv; // ok *pc = *pv; // error

Page 29: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 29

Arrays - 1 •  An array is a collection of ordered data items, all

belonging to the same type.

•  For example, declare: int values[9];

values[0] = 107; values[5] = 33;

•  The first array index is always 0.

33

107

values[8]

values[7]

values[6]

values[5]

values[4]

values[3]

values[2]

values[1]

values[0]

Page 30: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 30

Arrays - 2 #include <stdio.h>

void main(void) { int myary[12]; // 12 cells int index, sum = 0;

// Initialize array before use for (index = 0; index < 12; index++) { myary[index] = index;

}

for (index = 0; index < 12; index++) { sum += myary[index]; // sum array elements

} printf( “The sum is %d”, sum );

}

Page 31: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 31

Arrays - 3

• We can have multidimensional arrays: int points[3][4];

points[1][2]; • What is points[1,2]? (wrong!) • Initialize arrays:

int counters[5] = { 0, 0, 0, 0, 4 }; char letters[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ };

Notice the second example. If the array is initialized, you don’t have to specify the array size. The compiler will figure it out.

Page 32: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 32

Example: convert number base int main () { char base_digits[16] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’ }; int converted_number[64]; long int num_to_convert; int next_digit, base, index = 0;

// get the number and base printf(“Number to be converted? “); scanf(“%ld”, &num_to_convert); printf(“Base? “); scanf(“%i”, &base);

Page 33: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 33

// convert to the indicated base do { converted_number[index] = num_to_convert % base; ++index; num_to_convert = num_to_convert / base; } while ( num_to_convert != 0 ); // display the results in reverse order printf(“Converted number = “); for ( --index; index >= 0; --index ) { next_digit = converted_number[index]; printf(“%c”, base_digits[next_digit]); } printf(“\n”); }

Page 34: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 34

More about arrays •  The name of the array by itself, is

like a pointer to the first element of the array

•  This means that we can write *(v+i) and that has exactly the same meaning as v[i]. (In fact when the C compiler sees v[i] , it converts it to *(v+i).)

•  But there is one difference -- an array name is not a variable. ‘!’

‘o’ ‘l’ ‘l’ ‘e’ ‘H’

v[5]

v[4]

v[3]

v[2]

v[1]

v[0]

v

Page 35: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 35

Character strings •  We can write

char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’ }; or char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’ };

•  The special character at the end is useful for searching down the end of character strings to find the end point.

•  The second declaration can be duplicated by char word[] = “Hello!”; Character string constants in C are automatically terminated by the null (‘\0’) character. What is the length of this string array?

Page 36: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 36

Character strings •  Function to concatenate two character strings:

void concat( char result[], char str1[], char str2[]) {

int i, j;

for( i=0; str1[i] != ‘\0; ++i ) result[i] = str1[i];

for( j=0; str2[j] != ‘\0; ++j )

result[i+j] = str2[j];

result[i+j] = ‘\0’;

}

Page 37: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 37

Useful string functions

•  char *strcat(s1, s2) – append s2 onto s1 •  char *strncat(s1,s2, n) – append only n characters •  char *strchr (s, c) – search s for character c •  int strcmp (s1, s2) – compare s1, s2 •  char *strcpy (s1, s2) – copy s2 to s1 •  char *strrchr (s, c) – search s for last occurrence of c •  int strstr (s1, s2) – search s1 for first occurrence of s2 •  int strlen (s) – count number of characters in s

Page 38: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 38

Arrays

•  When an array (or character string) is passed to a function, it is passed by reference. int main() { void concat( char result[], char str1[], char str2[]) char s1[] = { “Hoo “}; char sw[] = { “Ha!” }; char se[20]; concat(s3, s1, s2); printf(“%s\n”, s3); }

Page 39: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 39

Arrays and strings

•  Why does this work for copying strings? while (*p++ = *q++)

•  C performs no array boundary checks. This means that you can overrun the end of an array, without the compiler complaining. This is the cause of many runtime errors!!!

Page 40: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 40

Structs - 1 #include <stdio.h>

struct birthday

{

int month;

int day;

int year;

}; // note semicolon

int main() {

struct birthday bd;

bd.day=1; bd.month=1; bd.year=1977;

printf(“My birthday is %d/%d/%d”, bd.day, bd.month, bd.year);

}

Page 41: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 41

Structs - 2 struct person { char name[80]; // elements can be different types int age; float height; struct // embedded struct { int month; int day; int year; } birth; };

struct person me; struct person class[60];

me.birth.year=1977; class[0].name=“Jack”; class[0].birth.year = 1971; . . .

Page 42: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 42

Structs - 3

•  So structs are like records (in Pascal) or classes in Java (without methods, though).

•  When structs are passed to functions as arguments, they are passed by value, unlike arrays, which are passed by reference.

Page 43: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 43

Dynamic memory allocation

•  The C compiler automatically allocates memory for the variables that are declared.

•  But sometimes we need to allocate memory during runtime, as we need it.

•  malloc is a memory allocation function. It takes one argument: the number of bytes to allocate. It returns a pointer to void, so you have to cast it to what’s been allocated.

•  sizeof is an operator that is useful here. It tells you the size of a data type.

Page 44: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 44

Dynamic memory allocation

•  Suppose you want to allocate enough memory to store 1,000 integers. You can int *intptr intptr = (int *) malloc (1000 * sizeof (int));

The cast

Page 45: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 45

Memory management

•  After you are finished with the memory space allocated to a pointer q using malloc, you should free it up by calling: free(q); This will release the memory space allocated to q.

Page 46: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 46

Operations on Files

•  Open a file before you use it –  fp = fopen(“t.txt”, “r”);

•  Read data from an opened file –  fread(buffer, 1024, 2, fp);

•  Write data into an opened file –  fwrite(data, 512, 2, fp);

•  Close an opened file –  fclose(fp);

Page 47: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

TELE402 Lecture 1 Protocol Layering 47

Low-level file operations

•  open, close, read, write, ioctl –  open(char *name, int flags); –  close(int fd); –  read(int fd, void *buf, size_t count); – write(int fd, void *buf, size_t count); –  ioctl(int fd, int request, char *argp);

•  Use “man 2 func” to find out the details.

Page 48: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

Protocol Layering

•  Why layering? – Hardware failure – Network congestion – Packet delay or loss – Data corruption – Data duplication and reordering

TELE402 Lecture 1 Protocol Layering 48

Page 49: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

Layering principle •  Layering principle

– Layered protocols are designed so that layer n at the destination receives exactly the same object sent by layer n at the source

•  Pros and cons – Complexity, process time, memory usage – Modularity, simplicity, interoperability,

robustness, security, cost effective

TELE402 Lecture 1 Protocol Layering 49

Page 50: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

Layered models •  ISO OSI model

– Seven layers

•  TCP/IP model – Five layers

•  PDU and SDU – Protocol data unit: payload and meta-data

(headers) – Service data unit: PDU that has been passed

down to the lower layer – PDU of a layer is the SDU of the layer below.

TELE402 Lecture 1 Protocol Layering 50

Page 51: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

PDUs in TCP/IP •  Application layer

– Messages, request/response

•  Transport layer – UPD datagrams, TCP segments

•  Network layer –  IP packets

•  Data link layer – Data frames (Ethernet frames)

TELE402 Lecture 1 Protocol Layering 51

Page 52: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

Multiplexing/demultiplexing •  Multiplexing

–  All data streams are placed into the same network entity for transfer

•  Demultiplexing –  Separate the different streams at the receiver end

•  Data link layer –  IP module, ARP module, RARP module

•  IP layer –  TCP, UDP, ICMP, SCTP, DCCP

•  Transport (Socket) layer based on ports –  smtpd, sshd, httpd, …

TELE402 Lecture 1 Protocol Layering 52

Page 53: lecture 1 - telecom.otago.ac.nz 1.pdf · TELE402 Lecture 1 Protocol Layering 7 Functions - 1 • Remember: no classes or methods in C. C is case-sensitive. • Functions are the main

Kernel/user space

•  Application is in user space •  Socket, TCP, UDP, IP, data link (network

drivers) are in the kernel space. •  Applications access network protocols

through socket API (system calls)

TELE402 Lecture 1 Protocol Layering 53