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 • Frame structure 3
Address representation • Types: unicast, multicast and broadcast • Six hexadecimal values, between 00 and FF • Example: 00:60:1D:1E:31:18 4
Address representation, in C++ #define WLAN_ADDR_LEN 6 struct WLANAddr { unsigned char data[WLAN_ADDR_LEN]; char * wlan2asc(); Outcome str2wlan(char * s); }; 5
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
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
Channels sets 8
RF parameters 9
Range parameters 10
Signal strength, sensitivity and range -91 -87 -94 -83 weak strong Receive signal strength (dBm) 1/10 9.4 1/10 9.1 1/10 8.7 1/10 8.3 Receive signal strength (milli Watt) 1 2 5.5 11 Data rate (Mbps) BER 10 -5 11
Distance (m) vs rate (Mbps) 600 500 400 300 200 100 0 1 2 5.5 11 12
Reconfiguration in the ad hoc mode iwconfig eth0 essid mynet iwconfig eth0 mode Ad-hoc iwconfig eth0 channel 3 iwconfig eth0 rate auto 13
Format of WiFi/802.11 frames header� Destination� Source� Type� Body 0 - 2312 bytes� Address� Address� 14
Header in C++ struct WLANHeader { WLANAddr destAddr; WLANAddr srcAddr; unsigned short type; }; #define WLAN_HEADER_LEN 14 #define IP_TYPE x0800 15
Section 4.2 - Packet socket • Ifconfig structure • socket() system call • close() system call 16
Parameters of packet socket struct Ifconfig ifconfig struct sockaddr_ll sll ifindex sll_family 1 Extracted from interface table 1 using ioctl() sockid sll_ifindex 2 Created using socket() 2 Promiscuous mode set hwaddr sll_protocol 1 3 using setsockopt() mtu 1 Relationship created Process 4 using bind() level Interface table Kernel level . . . Data link Socket eth0 . . . True 3 (Packet) 4 . . . 17
Configuration parameters struct Ifconfig { int sockid; int ifindex; WLANAddr hwaddr; int mtu; }; Ifconfig ifconfig; 18
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
Section 4.3 - Parameters and control • ioctl() system call • ifreq structure • setsockopt() system call • packet mreq structure • bind() system call • sockaddr ll structure 20
Sender and receiver interaction Sender Data Receiver Ack Time 21
Persistence non persistent Data Data p-persistent 1-persistent Data Data 22
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
ioctl() fetches the hardware address and MTU ioctl(ifconfig.sockid, SIOCGIFHWADDR, &ifr); ... ioctl(ifconfig.sockid, SIOCGIFMTU, &ifr); ifconfig.mtu = ifr.ifr_mtu; 24
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
Section 4.4 - Receiving frames • recvfrom() system call • sockaddr ll structure 26
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
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
Section 4.5 - Sending frames • sendto() system call 29
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
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
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
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
Recommend
More recommend