24
Chapter18 broadcasting

Chapter18 broadcasting

  • Upload
    bryony

  • View
    68

  • Download
    1

Embed Size (px)

DESCRIPTION

Chapter18 broadcasting. contents. Introduction broadcast address unicast versus broadcast dg_cli function using broadcasting Race conditions. Introduction. Multicasting support is optional in IPv4 but mandatory in IPv6 broadcasting support is not provided in IPv6 - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter18 broadcasting

Chapter18broadcasting

Page 2: Chapter18 broadcasting

contents

• Introduction• broadcast address• unicast versus broadcast• dg_cli function using broadcasting• Race conditions

Page 3: Chapter18 broadcasting

Introduction

• Multicasting support is optional in IPv4 but mandatory in IPv6

• broadcasting support is not provided in IPv6• broadcasting and multicasting require UDP, they

do not work with TCP• internet application using broadcasting

– ARP, BOOTP, NTP(network time protocol),routing daemon

Page 4: Chapter18 broadcasting

Figure18.1

Page 5: Chapter18 broadcasting

broadcast address

• Subnet-directed broadcast address:{netid, subnetid,-1}– this address all the interface on the specified subnet– most common today

• All-subnet-directed brocast address:{netid, -1, -1}– this address all subnet on the specified network

• network-directed broadcast address:{netid, -1}– this is used on network that does not use subnetting

• limited broadcast address:{-1, -1, -1}or 255.255.255.255– datagram destined to this address must never be forwarded by a ro

uter

Page 6: Chapter18 broadcasting

Figure 18.2

Page 7: Chapter18 broadcasting

unicast versus broadcast

Page 8: Chapter18 broadcasting

unicast versus broadcast

Page 9: Chapter18 broadcasting

dg_cli function using broadcasting

• Modify dg_cli function=> this allow to broadcast to the standard UDP daytime server, and printing all the replies.– Servaddr.sin_port = htons(13);

Page 10: Chapter18 broadcasting

Figure 18.5#include "unp.h"static void recvfrom_alarm(int);void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen){

int n;const int on = 1;char sendline[MAXLINE], recvline[MAXLINE + 1];socklen_t len;struct sockaddr *preply_addr;

preply_addr = Malloc(servlen);

Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

Signal(SIGALRM, recvfrom_alarm);

while (Fgets(sendline, MAXLINE, fp) != NULL) {

Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

alarm(5);

Page 11: Chapter18 broadcasting

for ( ; ; ) {len = servlen;n = recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);if (n < 0) {

if (errno == EINTR)break; /* waited long enough for replies */

elseerr_sys("recvfrom error");

} else {recvline[n] = 0; /* null terminate */printf("from %s: %s", Sock_ntop_host(preply_addr, servlen), recvline);

}} } }

static void recvfrom_alarm(int signo){

return; /* just interrupt the recvfrom() */}

Figure 18.5

Page 12: Chapter18 broadcasting

The subnet-directed broadcast address of 206.62.226.63>udpcli 206.62.226.63hifrom ……………from ……………from …………….from ……………..from …………….from …………….Hellofrom …………….from ……………from …………….from ……………..from …………….from …………….

Page 13: Chapter18 broadcasting

Race conditions• When multiple processes are accessing data that is

shared among them but the correct outcome depends on the execution order of the processes

• race conditions of a different type often exist when dealing with signals. Problem: a signal can normally be delivered at anytime while a program is executing

• A Race Condition (See Figure 18.5)– alarm(1)– sleep(1)

Page 14: Chapter18 broadcasting

#include "unp.h"static void recvfrom_alarm(int);void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen){

int n;const int on = 1;char sendline[MAXLINE], recvline[MAXLINE + 1];sigset_t sigset_alrm;socklen_t len;struct sockaddr *preply_addr;

preply_addr = Malloc(servlen);

Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

Sigemptyset(&sigset_alrm);Sigaddset(&sigset_alrm, SIGALRM);

Signal(SIGALRM, recvfrom_alarm);

while (Fgets(sendline, MAXLINE, fp) != NULL) {

Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

Blocking and Unblocking the Signal

Page 15: Chapter18 broadcasting

alarm(5);for ( ; ; ) {

len = servlen;Sigprocmask(SIG_UNBLOCK, &sigset_alrm, NULL);n = recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);Sigprocmask(SIG_BLOCK, &sigset_alrm, NULL);if (n < 0) {

if (errno == EINTR)break; /* waited long enough for replies */

elseerr_sys("recvfrom error");

} else {recvline[n] = 0; /* null terminate */printf("from %s: %s", Sock_ntop_host(preply_addr, servlen), recvline);

}} } }

static void recvfrom_alarm(int signo){

return; /* just interrupt the recvfrom() */}

Page 16: Chapter18 broadcasting

The unblocking of the signal, the call to recvfrom, and the blockingof the signal are all independent system calls.Assume recvfrom returns with the final datagram reply and the signal is delivered between the recvfrom and the blocking of the signal

=> the next call to recvfrom will blcok forever

static void recvfrom_alarm(int signl) { had_alarm = 1; return;}

Sigprocmask(SIG_UNBLOCK, &sigset_alrm, NULL);

if (had_alarm == 1)

break;

n = recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);

Sigprocmask(SIG_BLOCK, &sigset_alrm, NULL);

Page 17: Chapter18 broadcasting

#include "unp.h"static void recvfrom_alarm(int);void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen){

int n;const int on = 1;char sendline[MAXLINE], recvline[MAXLINE + 1];fd_set rset;sigset_t sigset_alrm, sigset_empty;socklen_t len;struct sockaddr *preply_addr;

preply_addr = Malloc(servlen);Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));FD_ZERO(&rset);Sigemptyset(&sigset_empty);Sigemptyset(&sigset_alrm);Sigaddset(&sigset_alrm, SIGALRM);

Signal(SIGALRM, recvfrom_alarm);

while (Fgets(sendline, MAXLINE, fp) != NULL) {Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

Blocking and Unblocking the signal with pselect

Page 18: Chapter18 broadcasting

Sigprocmask(SIG_BLOCK, &sigset_alrm, NULL);alarm(5);for ( ; ; ) {FD_SET(sockfd, &rset);n = pselect(sockfd+1, &rset, NULL, NULL, NULL, &sigset_empty);if (n < 0) {if (errno == EINTR)break;elseerr_sys("pselect error");} else if (n != 1)err_sys("pselect error: returned %d", n);

len = servlen;n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);recvline[n] = 0; /* null terminate */printf("from %s: %s",Sock_ntop_host(preply_addr, servlen), recvline);} } }

static void recvfrom_alarm(int signo){

return; /* just interrupt the recvfrom() */}

Page 19: Chapter18 broadcasting

Figure 18.8#include "unp.h"

int pselect(int nfds, fd_set *rset, fd_set *wset, fd_set *xset,const struct timespec *ts, const sigset_t *sigmask)

{int n;struct timeval tv;sigset_t savemask;

if (ts != NULL) {tv.tv_sec = ts->tv_sec;tv.tv_usec = ts->tv_nsec / 1000; /* nanosec -> microsec */}

sigprocmask(SIG_SETMASK, sigmask, &savemask); /* caller's mask */n = select(nfds, rset, wset, xset, (ts == NULL) ? NULL : &tv);sigprocmask(SIG_SETMASK, &savemask, NULL); /* restore mask */

return(n);}

Page 20: Chapter18 broadcasting

Using sigsetjump and siglongjump#include "unp.h"#include <setjmp.h>

static void recvfrom_alarm(int);static sigjmp_buf jmpbuf;

void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen){

int n;const int on = 1;char sendline[MAXLINE], recvline[MAXLINE + 1];socklen_t len;struct sockaddr *preply_addr;

preply_addr = Malloc(servlen);

Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

Signal(SIGALRM, recvfrom_alarm);

while (Fgets(sendline, MAXLINE, fp) != NULL) {

Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

Page 21: Chapter18 broadcasting

alarm(5);for ( ; ; ) {

if (sigsetjmp(jmpbuf, 1) != 0)break;

len = servlen;n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);recvline[n] = 0; /* null terminate */printf("from %s: %s",

Sock_ntop_host(preply_addr, servlen), recvline);}

}}

static voidrecvfrom_alarm(int signo){

siglongjmp(jmpbuf, 1);}

Figure 18.9

Page 22: Chapter18 broadcasting

Using IPC from signal handler to function#include "unp.h"static void recvfrom_alarm(int);static int pipefd[2];

void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen){

int n, maxfdp1;const int on = 1;char sendline[MAXLINE], recvline[MAXLINE + 1];fd_set rset;socklen_t len;struct sockaddr *preply_addr;

preply_addr = Malloc(servlen);Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));Pipe(pipefd);maxfdp1 = max(sockfd, pipefd[0]) + 1;FD_ZERO(&rset);

Signal(SIGALRM, recvfrom_alarm);

while (Fgets(sendline, MAXLINE, fp) != NULL) {Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

Page 23: Chapter18 broadcasting

alarm(5);for ( ; ; ) {FD_SET(sockfd, &rset);FD_SET(pipefd[0], &rset);if ( (n = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0) {if (errno == EINTR)continue;elseerr_sys("pselect error");}

if (FD_ISSET(sockfd, &rset)) { len = servlen; n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len); recvline[n] = 0; /* null terminate */ printf("from %s: %s", Sock_ntop_host(preply_addr, servlen), recvline);}

if (FD_ISSET(pipefd[0], &rset)) {Read(pipefd[0], &n, 1); /* timer expired */break;}} } }

Figure 18.10

Page 24: Chapter18 broadcasting

static voidrecvfrom_alarm(int signo){

Write(pipefd[1], "", 1); /* write 1 null byte to pipe */return;

}

Figure 18.10