Principles of Ad Hoc Networking Michel Barbeau and Evangelos - - PowerPoint PPT Presentation

principles of ad hoc networking
SMART_READER_LITE
LIVE PREVIEW

Principles of Ad Hoc Networking Michel Barbeau and Evangelos - - PowerPoint PPT Presentation

Principles of Ad Hoc Networking Michel Barbeau and Evangelos Kranakis January 20, 2009 CHAPTER 4 - WIRELESS NETWORK PROGRAMMING 2 Section 4.1 - Structure of information Address representation ifconfig command iwconfig command


slide-1
SLIDE 1

Principles of Ad Hoc Networking

Michel Barbeau and Evangelos Kranakis January 20, 2009

slide-2
SLIDE 2

CHAPTER 4 - WIRELESS NETWORK PROGRAMMING

2

slide-3
SLIDE 3

Section 4.1 - Structure of information

  • Address representation
  • ifconfig command
  • iwconfig command
  • Frame structure

3

slide-4
SLIDE 4

Address representation

  • Types: unicast, multicast and broadcast
  • Six hexadecimal values, between 00 and FF
  • Example: 00:60:1D:1E:31:18

4

slide-5
SLIDE 5

Address representation, in C++ #define WLAN_ADDR_LEN 6 struct WLANAddr { unsigned char data[WLAN_ADDR_LEN]; char * wlan2asc(); Outcome str2wlan(char * s); };

5

slide-6
SLIDE 6

Configuration data: ifconfig

# ifconfig eth0 Link encap:Ethernet HWaddr 00:60:1D:1E:31:18 inet addr:134.117.5.12 Bcast:134.117.5.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5786 errors:0 dropped:0 overruns:0 frame:0 TX packets:352 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:870844 (850.4 Kb) TX bytes:52664 (51.4 Kb) Interrupt:10 Base address:0x100

6

slide-7
SLIDE 7

Configuration data: iwconfig

# iwconfig eth0 IEEE 802.11-DS ESSID:"ENTRUSTW" Nickname:"localhost" Mode:Managed Frequency:2.422GHz Access Point: 00:60:1D:F1:F0:27 Bit Rate:11Mb/s Tx-Power=15 dBm Sensitivity:1/3 Retry limit:4 RTS thr:off Fragment thr:off Encryption key:off Power Management:off Link Quality:22/92 Signal level:-70 dBm Noise level:-92 dBm Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:6 Tx excessive retries:0 Invalid misc:0 Missed beacon:0

7

slide-8
SLIDE 8

Channels sets

8

slide-9
SLIDE 9

RF parameters

9

slide-10
SLIDE 10

Range parameters

10

slide-11
SLIDE 11

Signal strength, sensitivity and range

Receive signal strength (dBm) Data rate (Mbps) BER 10-5 Receive signal strength (milli Watt)

  • 94
  • 87
  • 91
  • 83

1/109.4 1/109.1 1/108.7 1/108.3 1 2 5.5 11

weak strong 11

slide-12
SLIDE 12

400 500 600

Distance (m) vs rate (Mbps)

100 200 300 1 2 5.5 11

12

slide-13
SLIDE 13

Reconfiguration in the ad hoc mode iwconfig eth0 essid mynet iwconfig eth0 mode Ad-hoc iwconfig eth0 channel 3 iwconfig eth0 rate auto

13

slide-14
SLIDE 14

Format of WiFi/802.11 frames

Destination Address Source Address Body 0 - 2312 bytes header Type

14

slide-15
SLIDE 15

Header in C++ struct WLANHeader { WLANAddr destAddr; WLANAddr srcAddr; unsigned short type; }; #define WLAN_HEADER_LEN 14 #define IP_TYPE x0800

15

slide-16
SLIDE 16

Section 4.2 - Packet socket

  • Ifconfig structure
  • socket() system call
  • close() system call

16

slide-17
SLIDE 17

Parameters of packet socket

ifindex sockid hwaddr mtu struct Ifconfig ifconfig sll_family sll_ifindex sll_protocol struct sockaddr_ll sll Process level Kernel level Data link Socket (Packet) eth0 . . . . . . . . .

1 2 3 4

Interface table

1 1

True

1

Extracted from interface table using ioctl()

2

Created using socket()

3

Promiscuous mode set using setsockopt()

4

Relationship created using bind() 17

slide-18
SLIDE 18

Configuration parameters struct Ifconfig { int sockid; int ifindex; WLANAddr hwaddr; int mtu; }; Ifconfig ifconfig;

18

slide-19
SLIDE 19

Creation and release packet_socket = socket(PF_PACKET, int socket_type, int protocol); ifconfig.sockid = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)) close(ifconfig.sockid);

19

slide-20
SLIDE 20

Section 4.3 - Parameters and control

  • ioctl() system call
  • ifreq structure
  • setsockopt() system call
  • packet mreq structure
  • bind() system call
  • sockaddr ll structure

20

slide-21
SLIDE 21

Sender and receiver interaction

Data Ack Time Sender Receiver

21

slide-22
SLIDE 22

Persistence

Data Data Data Data 1-persistent p-persistent non persistent

22

slide-23
SLIDE 23

Wireless interface parameters: ioctl() system call // signature int ioctl(int d, int request, ...); ... // system call char device[] = "eth0"; // name of interface // input parameter struct ifreq ifr; strcpy(ifr.ifr_name, device); ioctl(ifconfig.sockid, SIOGIFINDEX, &ifr); // fetch index of wireless interface ifconfig.ifindex = ifr.ifr_ifindex;

23

slide-24
SLIDE 24

ioctl() fetches the hardware address and MTU ioctl(ifconfig.sockid, SIOCGIFHWADDR, &ifr); ... ioctl(ifconfig.sockid, SIOCGIFMTU, &ifr); ifconfig.mtu = ifr.ifr_mtu;

24

slide-25
SLIDE 25

Socket parameters: setsockopt() system call // set promiscuous mode struct packet_mreq mr; memset(&mr,0,sizeof(mr)); mr.mr_ifindex = ifconfig.ifindex; mr.mr_type = PACKET_MR_PROMISC; setsockopt(ifconfig.sockid, SOL_PACKET, PACKET_ADD_MEMBERSHIP, (char *)&mr, sizeof(mr)); ... // bind wireless interface to socket struct sockaddr_ll sll; memset(&sll, 0, sizeof(sll)); sll.sll_family = AF_PACKET; sll.sll_ifindex = ifconfig.ifindex; sll.sll_protocol = htons(ETH_P_ALL); bind(ifconfig.sockid, (struct sockaddr*)&sll, sizeof(sll));

25

slide-26
SLIDE 26

Section 4.4 - Receiving frames

  • recvfrom() system call
  • sockaddr ll structure

26

slide-27
SLIDE 27

recvfrom() system call // signature int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); ... // storage for incoming data and source address unsigned char * buff = new unsigned char[ifconfig.mtu]; unsigned int i; struct sockaddr_ll from; socklen_t fromlen = sizeof(struct sockaddr_ll);

27

slide-28
SLIDE 28

recvfrom() in a while loop while (true) { i = recvfrom(ifconfig.sockid, buff, ifconfig.mtu, 0, (struct sockaddr *) &from, &fromlen); if (i == -1) { printf("cannot receive data: %s\n", strerror(errno)); usleep(10000); } else { break; } }

28

slide-29
SLIDE 29

Section 4.5 - Sending frames

  • sendto() system call

29

slide-30
SLIDE 30

sendto() system call // signature int sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen); ... // send buffer variable # define BUFFSIZE 256 unsigned char buff[BUFFSIZE];

30

slide-31
SLIDE 31

Header construction // header variable WLANHeader hdr; // destination address in ASCII char * rp = "00:60:1D:F1:F0:27"; // address in binary form WLANAddr daddr; // construction daddr.str2wlan(rp); memmove(&hdr.destAddr, daddr.data, WLAN_ADDR_LEN); memmove(&hdr.srcAddr, ifconfig.hwaddr.data, WLAN_ADDR_LEN); hdr.type = htons(IP_TYPE); memmove(buff, &hdr, WLAN_HEADER_LEN);

31

slide-32
SLIDE 32

Body of frame // data char * dp = "This is a short message!"; // load data in frame buffer memmove(buff+WLAN_HEADER_LEN, dp, strlen(dp)); // construct address structure struct sockaddr_ll to; int tolen = sizeof(to); to.sll_family = AF_PACKET; to.sll_ifindex = ifconfig.ifindex; memmove(&(to.sll_addr), daddr.data, WLAN_ADDR_LEN); to.sll_halen = WLAN_ADDR_LEN;

32

slide-33
SLIDE 33

Send frame int sentlen = sendto( ifconfig.sockid, buff, WLAN_HEADER_LEN+strlen(dp), 0, (sockaddr *) &to, tolen); if (sentlen == -1 ) { printf("sendto failed\n"); return NOK; } return OK;

33