genericopenlibs/openenvcore/libc/test/testifioctls/src/tifioctlsblocks.cpp
branchRCL_3
changeset 75 254b651f304e
parent 64 c44f36bb61a3
equal deleted inserted replaced
64:c44f36bb61a3 75:254b651f304e
  1186 		{
  1186 		{
  1187 		INFO_PRINTF1(_L("ioctl operation failed on STDIN"));
  1187 		INFO_PRINTF1(_L("ioctl operation failed on STDIN"));
  1188 		res=KErrGeneral;
  1188 		res=KErrGeneral;
  1189 		}
  1189 		}
  1190 
  1190 
  1191     return res;
  1191 	return res;
  1192     }
  1192 	}
  1193 
       
  1194 TInt CTestIfioctls::GetIapName(char * aIapName, int len)
       
  1195     {
       
  1196     _LIT(KIAPSectionName, "SectionIAPDetails");
       
  1197     _LIT(KIAPName,"IAPName");
       
  1198     TPtrC iapName;
       
  1199     TBool res = GetStringFromConfig(KIAPSectionName, KIAPName, iapName);
       
  1200     if (!res)
       
  1201         {
       
  1202         INFO_PRINTF1(_L("Unable to read the iapname fro the ini file.")) ;    
       
  1203         return KErrGeneral;
       
  1204         }
       
  1205     TPtr8 iap((unsigned char *)aIapName, len);
       
  1206     iap.Copy(iapName);
       
  1207     iap.ZeroTerminate();
       
  1208     return KErrNone;
       
  1209     }
       
  1210 
       
  1211 TInt CTestIfioctls::StartIap(const char *aIapName, int &fd)
       
  1212     {
       
  1213     TPtr8 iapName8((TUint8 * )aIapName,strlen(aIapName),strlen(aIapName)+1);
       
  1214     TBuf<IFNAMSIZ> iapName16;
       
  1215     iapName16.Copy(iapName8);
       
  1216     INFO_PRINTF2(_L("starting iap = %S"), &iapName16);
       
  1217 
       
  1218     if((fd = socket(AF_INET, SOCK_DGRAM , 0)) < 0)
       
  1219         {
       
  1220         INFO_PRINTF2(_L("Error creating scocket errno = %d\n "),errno);
       
  1221         return -1;
       
  1222         }
       
  1223 
       
  1224     if(ioctl(fd,SIOCSIFNAME,aIapName) == -1)
       
  1225         {
       
  1226         INFO_PRINTF2(_L("unable to set the interface name, reason = %d\n"), errno);
       
  1227         close(fd);
       
  1228         return -1;
       
  1229         }
       
  1230 
       
  1231     if(ioctl(fd,SIOCIFSTART,aIapName) == -1)
       
  1232         {
       
  1233         INFO_PRINTF2(_L("unable to start the interface, reason = %d\n"),errno);
       
  1234         close(fd);
       
  1235         return -1;
       
  1236         }
       
  1237     return KErrNone;
       
  1238     }
       
  1239 
       
  1240 TInt CTestIfioctls::TestGetIfIPAddr()
       
  1241     {
       
  1242     char iapName[IFNAMSIZ];
       
  1243     GetIapName(iapName, sizeof(iapName));
       
  1244     int fd;
       
  1245     ifreq ifr;
       
  1246 
       
  1247 
       
  1248     if(StartIap(iapName, fd) != KErrNone)
       
  1249         {
       
  1250         INFO_PRINTF2(_L("unable to start the iap, reason = %d\n"),errno);
       
  1251         return -1;
       
  1252         }
       
  1253     strcpy(ifr.ifr_name,iapName);
       
  1254     if(ioctl(fd,SIOCGIFADDR,&ifr) == -1) //getting the interface ip
       
  1255         {
       
  1256         INFO_PRINTF2(_L("unable to get ip of the interface, reason = %d\n"),errno);
       
  1257         close(fd);
       
  1258         return -1;
       
  1259         }
       
  1260     char address[50];
       
  1261 
       
  1262     if(inet_ntop(ifr.ifr_ifru.ifru_addr.sa_family,&(ifr.ifr_ifru.ifru_addr.sa_data),address,sizeof(address)) == NULL)
       
  1263         {
       
  1264         INFO_PRINTF2(_L("cannot convert to address to string, reason = %d \n"), errno);
       
  1265         close(fd);
       
  1266         return -1;
       
  1267         }
       
  1268     TPtr8 ipAddr8((TUint8*)address,strlen(address),strlen(address)+1);
       
  1269     TBuf<50> ipAddr16;
       
  1270     ipAddr16.Copy(ipAddr8);
       
  1271     INFO_PRINTF3(_L("family = %d, ipaddr = %S\n"),ifr.ifr_ifru.ifru_addr.sa_family,&ipAddr16);
       
  1272 
       
  1273     sockaddr_in addr;
       
  1274     memcpy(&addr,&ifr.ifr_ifru.ifru_addr,sizeof(sockaddr_in));
       
  1275     addr.sin_port = 12346;
       
  1276 
       
  1277     if (bind(fd,(sockaddr*)&addr,sizeof(addr)) < 0)
       
  1278         {
       
  1279         INFO_PRINTF2(_L("Error binding to socket, errno = %d\n"),errno);
       
  1280         close(fd);
       
  1281         return -1;
       
  1282         }
       
  1283     close(fd);
       
  1284     return KErrNone;
       
  1285 
       
  1286     }
       
  1287 
       
  1288 void CTestIfioctls::printFlags(TInt flags)
       
  1289     {
       
  1290     if(flags & IFF_UP)
       
  1291         {
       
  1292         INFO_PRINTF1(_L("\t\tThe inteface is up\n"));
       
  1293         }
       
  1294     else
       
  1295         {
       
  1296         INFO_PRINTF1(_L("\t\tThe inteface is down\n"));
       
  1297         }
       
  1298     if(flags & IFF_LOOPBACK)
       
  1299         {
       
  1300         INFO_PRINTF1(_L("\t\tThe inteface a loopback\n"));
       
  1301         }
       
  1302     if(flags & IFF_POINTOPOINT)
       
  1303         {
       
  1304         INFO_PRINTF1(_L("\t\tThe inteface a P2P\n"));
       
  1305         }
       
  1306     if(flags & IFF_BROADCAST)
       
  1307         {
       
  1308         INFO_PRINTF1(_L("\t\tThe inteface a Broadcast\n"));
       
  1309         }
       
  1310     }
       
  1311 
       
  1312 TInt CTestIfioctls::TestGetIfAttributes()
       
  1313     {
       
  1314     char iapName[IFNAMSIZ];
       
  1315     GetIapName(iapName, sizeof(iapName));
       
  1316     int fd;
       
  1317     ifreq ifr;
       
  1318 
       
  1319 
       
  1320     if(StartIap(iapName, fd) != KErrNone)
       
  1321         {
       
  1322         INFO_PRINTF2(_L("unable to start the iap, reason = %d\n"),errno);
       
  1323         return -1;
       
  1324         }
       
  1325     strcpy(ifr.ifr_name,iapName);
       
  1326 
       
  1327     if(ioctl(fd,SIOCGIFFLAGS,&ifr) == -1)
       
  1328         {
       
  1329         INFO_PRINTF2(_L("unable to get the flags for iap , reason = %d\n"),errno);
       
  1330         close(fd);
       
  1331         return -1;
       
  1332         }
       
  1333 
       
  1334     printFlags(*((int *)ifr.ifr_ifru.ifru_flags));
       
  1335 
       
  1336     if(ioctl(fd,SIOCGIFMTU,&ifr) == -1)
       
  1337         {
       
  1338         INFO_PRINTF2(_L("unable to get the mtu for the iap, reason = %d"),errno);
       
  1339         close(fd);
       
  1340         return -1;
       
  1341         }
       
  1342     INFO_PRINTF2(_L("The current MTU  = %d\n"),ifr.ifr_ifru.ifru_mtu);
       
  1343 
       
  1344     if(ioctl(fd,SIOCGIFNETMASK,&ifr) == -1)
       
  1345         {
       
  1346         INFO_PRINTF2(_L("unable to get the netmask for the iap reason = %d\n"), errno);
       
  1347         close(fd);
       
  1348         return -1;
       
  1349         }
       
  1350     char addr[50];
       
  1351     addr[0] = '\0';
       
  1352 
       
  1353     if(inet_ntop(ifr.ifr_ifru.ifru_addr.sa_family,&(ifr.ifr_ifru.ifru_addr.sa_data),addr,sizeof(addr)) == NULL)
       
  1354         {
       
  1355         INFO_PRINTF2(_L("cannot convert to address to string, error no = %d\n"),errno);
       
  1356         close(fd);
       
  1357         return -1;
       
  1358         }
       
  1359     TPtr8 addr8((TUint8 *)addr, strlen(addr), strlen(addr)+1);
       
  1360     TBuf<50> addr16;
       
  1361     addr16.Copy(addr8);
       
  1362     INFO_PRINTF2(_L("netmask  for the iap = %S \n"),&addr16);
       
  1363 
       
  1364     if(ioctl(fd,SIOCGIFDEFGATEWAY,&ifr) == -1)
       
  1365         {
       
  1366         INFO_PRINTF2(_L("unable to get the gateway for the iap, reason = %d\n"),errno);
       
  1367         close(fd);
       
  1368         return -1;
       
  1369         }
       
  1370     addr[0] = '\0';
       
  1371     if(inet_ntop(ifr.ifr_ifru.ifru_addr.sa_family,&(ifr.ifr_ifru.ifru_defgatewayaddr),addr,sizeof(addr)) == NULL)
       
  1372         {
       
  1373         INFO_PRINTF2(_L("cannot convert to address to string, error no = %d\n"),errno);
       
  1374         close(fd);
       
  1375         return -1;
       
  1376         }
       
  1377     addr8.Set((TUint8 *)addr, strlen(addr), strlen(addr)+1);
       
  1378     addr16.Copy(addr8);
       
  1379     INFO_PRINTF2(_L("gateway  for the iap = %S\n"),&addr16);
       
  1380     close(fd);
       
  1381     return KErrNone;
       
  1382     }
       
  1383 
       
  1384 TInt CTestIfioctls::TestSetIfAttributes()
       
  1385     {
       
  1386     char iapName[IFNAMSIZ];
       
  1387     GetIapName(iapName, sizeof(iapName));
       
  1388     int fd;
       
  1389     ifreq ifr;
       
  1390 
       
  1391 
       
  1392     if(StartIap(iapName, fd) != KErrNone)
       
  1393         {
       
  1394         INFO_PRINTF2(_L("unable to start the iap, reason = %d\n"),errno);
       
  1395         return -1;
       
  1396         }
       
  1397     strcpy(ifr.ifr_name,iapName);
       
  1398 
       
  1399 
       
  1400     ifreq ifr1;
       
  1401     strcpy(ifr1.ifr_name,iapName);
       
  1402 
       
  1403     if(ioctl(fd,SIOCGIFMTU,&ifr) == -1)
       
  1404         {
       
  1405         INFO_PRINTF2(_L("unable to get the mtu for the iap, reason = %d"),errno);
       
  1406         close(fd);
       
  1407         return -1;
       
  1408         }
       
  1409     ifr.ifr_ifru.ifru_mtu++;
       
  1410 
       
  1411     if(ioctl(fd,SIOCSIFMTU,&ifr) == -1)
       
  1412         {
       
  1413         INFO_PRINTF2(_L("unable to set the mtu for the iap, reason = %d"),errno);
       
  1414         close(fd);
       
  1415         return -1;
       
  1416         }
       
  1417 
       
  1418     if(ioctl(fd,SIOCGIFMTU,&ifr1) == -1)
       
  1419         {
       
  1420         INFO_PRINTF2(_L("unable to get the mtu for the iap , reason = %d"),errno);
       
  1421         close(fd);
       
  1422         return -1;
       
  1423         }
       
  1424 
       
  1425     if(ifr.ifr_ifru.ifru_mtu != ifr1.ifr_ifru.ifru_mtu)
       
  1426         {
       
  1427         INFO_PRINTF2(_L("unable to set the mtu for the iap = %s"),ifr.ifr_name);
       
  1428         close(fd);
       
  1429         return -1;
       
  1430         }
       
  1431     close(fd);
       
  1432     return KErrNone;
       
  1433     }
       
  1434 
       
  1435 void CTestIfioctls::FreeDnsSuffixes(char ** suffixes)
       
  1436     {
       
  1437     for(int i = 0; suffixes[i]; i++)
       
  1438         {
       
  1439         delete [] suffixes[i];
       
  1440         }
       
  1441     delete []suffixes;
       
  1442     }
       
  1443 
       
  1444 #define MAX_SUFFIXES  3
       
  1445 #define SUFFIXLEN   50
       
  1446 TInt CTestIfioctls::TestSetIfDNSSuffix()
       
  1447     {
       
  1448     char iapName[IFNAMSIZ];
       
  1449     //    ifreq ifr1;
       
  1450     GetIapName(iapName, sizeof(iapName));
       
  1451     int fd;
       
  1452 
       
  1453     struct if_dns_suffixes dns_suffixes;
       
  1454     strcpy(dns_suffixes.if_name,iapName);
       
  1455 
       
  1456     if(StartIap(iapName, fd) != KErrNone)
       
  1457         {
       
  1458         INFO_PRINTF2(_L("unable to start the iap, reason = %d\n"),errno);
       
  1459         return -1;
       
  1460         }
       
  1461 
       
  1462     char **&suffixes = dns_suffixes.suffixes;
       
  1463 
       
  1464     suffixes = new char*[MAX_SUFFIXES+1];
       
  1465     suffixes[MAX_SUFFIXES] = NULL;
       
  1466 
       
  1467     _LIT(KDNSSuffix, "DNSSuffix");
       
  1468 
       
  1469     for(int i = 0; i < MAX_SUFFIXES; i++)
       
  1470         {
       
  1471         TPtrC suffix16;
       
  1472         TBuf<20> suffixTag(KDNSSuffix);
       
  1473         suffixTag.AppendNum(i);
       
  1474         suffixes[i] = NULL;
       
  1475         TBool res = GetStringFromConfig(ConfigSection(), suffixTag, suffix16);
       
  1476         if (!res)
       
  1477             {
       
  1478             INFO_PRINTF1(_L("Unable to read the iapname from the ini file.")) ;    
       
  1479             FreeDnsSuffixes(suffixes);
       
  1480             close(fd);
       
  1481             return KErrGeneral;
       
  1482             }
       
  1483         suffixes[i] = new char[suffix16.Length() + 1];
       
  1484         TPtr8 suffix8((TUint8*)suffixes[i],suffix16.Length() + 1);
       
  1485         suffix8.Copy(suffix16);
       
  1486         suffix8.ZeroTerminate();
       
  1487         }
       
  1488 
       
  1489     if(ioctl(fd,SIOCSETDNSSUFFIX,&dns_suffixes) == -1)
       
  1490         {
       
  1491         INFO_PRINTF2(_L("unable to set the dbs suffixes , reason = %d\n"),errno);
       
  1492         close(fd);
       
  1493         FreeDnsSuffixes(suffixes);
       
  1494         return -1;
       
  1495         }
       
  1496     _LIT(KHostname1, "Hostname1");
       
  1497     TPtrC hostname16;
       
  1498     char hostname[50];
       
  1499     TPtr8 hostname8((TUint8*)hostname, sizeof(hostname));
       
  1500 
       
  1501     TBool res = GetStringFromConfig(ConfigSection(), KHostname1, hostname16);
       
  1502     if (!res)
       
  1503         {
       
  1504         INFO_PRINTF1(_L("Unable to read the hostname from the ini file.")) ;    
       
  1505         FreeDnsSuffixes(suffixes);
       
  1506         close(fd);
       
  1507         return KErrGeneral;
       
  1508         }
       
  1509     hostname8.Copy(hostname16);
       
  1510     hostname8.ZeroTerminate();
       
  1511     hostent * hentry;
       
  1512     ifreq defaultif;
       
  1513     strcpy(defaultif.ifr_name, iapName);
       
  1514     if(setdefaultif(&defaultif) != KErrNone)
       
  1515         {
       
  1516         INFO_PRINTF2(_L("setdefaultif failed, reason = %d "),errno) ;    
       
  1517         FreeDnsSuffixes(suffixes);
       
  1518         close(fd);
       
  1519         return KErrGeneral;
       
  1520         }
       
  1521 
       
  1522     if((hentry = gethostbyname(hostname)) == NULL)
       
  1523         {
       
  1524     INFO_PRINTF3(_L("getbyhostname failed for %S with errno = %d"),&hostname16,errno) ;    
       
  1525        // FreeDnsSuffixes(suffixes);
       
  1526        // close(fd);
       
  1527      //   return KErrGeneral;
       
  1528 
       
  1529         } 
       
  1530     _LIT(KHostname2, "Hostname2");
       
  1531 
       
  1532  res = GetStringFromConfig(ConfigSection(), KHostname2, hostname16);
       
  1533     if (!res)
       
  1534         {
       
  1535         INFO_PRINTF1(_L("Unable to read the hostname from the ini file.")) ;    
       
  1536         FreeDnsSuffixes(suffixes);
       
  1537         close(fd);
       
  1538         return KErrGeneral;
       
  1539         }
       
  1540     hostname8.Copy(hostname16);
       
  1541     hostname8.ZeroTerminate();
       
  1542     if((hentry = gethostbyname(hostname)) == NULL)
       
  1543         {
       
  1544     INFO_PRINTF3(_L("getbyhostname failed  for %S  with errno = %d "),&hostname16,errno) ;    
       
  1545        // FreeDnsSuffixes(suffixes);
       
  1546        // close(fd);
       
  1547        // return KErrGeneral;
       
  1548 
       
  1549         } 
       
  1550 
       
  1551     hostname8.Copy(_L8("2IND04966"));
       
  1552       hostname8.ZeroTerminate();
       
  1553       if((hentry = gethostbyname(hostname)) == NULL)
       
  1554           {
       
  1555           INFO_PRINTF3(_L("getbyhostname failed for %s, errno  =  %d "),&hostname8, errno) ;    
       
  1556           } 
       
  1557     
       
  1558     
       
  1559     FreeDnsSuffixes(suffixes);
       
  1560     close(fd);
       
  1561     return KErrNone;
       
  1562 
       
  1563     }
       
  1564 
       
  1565 
       
  1566 TInt CTestIfioctls::TestGetIfDNSSuffix()
       
  1567     {
       
  1568     char iapName[IFNAMSIZ];
       
  1569     GetIapName(iapName, sizeof(iapName));
       
  1570     int fd;
       
  1571 
       
  1572     if(StartIap(iapName, fd) != KErrNone)
       
  1573         {
       
  1574         INFO_PRINTF2(_L("unable to start the iap, reason = %d\n"),errno);
       
  1575         return -1;
       
  1576         }
       
  1577 
       
  1578     struct if_dns_suffixes dns_suffixes;
       
  1579     strcpy(dns_suffixes.if_name,iapName);
       
  1580 
       
  1581     if(ioctl(fd,SIOCGETDNSSUFFIX,&dns_suffixes) == -1)
       
  1582         {
       
  1583         INFO_PRINTF2(_L("unable to get the dns suffixes , reason = %d\n"),errno);
       
  1584         close(fd);
       
  1585         return -1;
       
  1586         }
       
  1587 
       
  1588     INFO_PRINTF1(_L("printing dns suffixes"));
       
  1589     for(int i = 0; dns_suffixes.suffixes[i]; i++)
       
  1590         {
       
  1591         TPtr8 suffix8((TUint8 *)dns_suffixes.suffixes[i],strlen(dns_suffixes.suffixes[i]),strlen(dns_suffixes.suffixes[i])+1);
       
  1592         TBuf<SUFFIXLEN> suffix16;
       
  1593         suffix16.Copy(suffix8);
       
  1594         INFO_PRINTF3(_L("suffixes %d =  %S"),i,&suffix16);
       
  1595         }
       
  1596     freednssuffixes(&dns_suffixes);
       
  1597     close(fd);
       
  1598     return KErrNone;
       
  1599     }
       
  1600 
       
  1601 TInt CTestIfioctls::TestGetNameServers()
       
  1602     {
       
  1603     if_name_servers dns_server;
       
  1604     char iapName[IFNAMSIZ];
       
  1605     GetIapName(iapName, sizeof(iapName));
       
  1606     int sockfd;
       
  1607     strcpy(dns_server.if_name, iapName);
       
  1608 
       
  1609     if(StartIap(iapName, sockfd) != KErrNone)
       
  1610         {
       
  1611         INFO_PRINTF2(_L("unable to start the iap, reason = %d\n"),errno);
       
  1612         return -1;
       
  1613         }
       
  1614 
       
  1615     TInt ret = ioctl(sockfd, SIOCGNAMESERVER, &dns_server);
       
  1616     if(ret == -1)
       
  1617         {
       
  1618         INFO_PRINTF2(_L("IOCTL option failed with errno =%d"), errno);
       
  1619         close(sockfd);
       
  1620         return errno;
       
  1621         }
       
  1622     char addr[50];
       
  1623     addr[0] = '\0';
       
  1624 
       
  1625     if(inet_ntop(dns_server.nameserver1.sa_family,&(dns_server.nameserver1.sa_data),addr,sizeof(addr)) == NULL)
       
  1626         {
       
  1627         INFO_PRINTF2(_L("cannot convert to address to string, error no = %d\n"),errno);
       
  1628         close(sockfd);
       
  1629         return -1;
       
  1630         }
       
  1631     TPtr8 addr8((TUint8 *)addr, strlen(addr), strlen(addr)+1);
       
  1632     TBuf<50> addr16;
       
  1633     addr16.Copy(addr8);
       
  1634     INFO_PRINTF2(_L("nameserver2  for the iap = %S \n"),&addr16);
       
  1635     if(inet_ntop(dns_server.nameserver2.sa_family,&(dns_server.nameserver2.sa_data),addr,sizeof(addr)) == NULL)
       
  1636         {   
       
  1637         INFO_PRINTF2(_L("cannot convert to address to string, error no = %d\n"),errno);
       
  1638         close(sockfd);
       
  1639         return -1;
       
  1640         }
       
  1641     addr8.Set((TUint8 *)addr, strlen(addr), strlen(addr)+1 );
       
  1642     addr16.Copy(addr8);
       
  1643     INFO_PRINTF2(_L("nameserver2  for the iap = %S \n"),&addr16);
       
  1644     close(sockfd);
       
  1645 
       
  1646         return KErrNone;
       
  1647     }
       
  1648 
       
  1649 TInt CTestIfioctls::TestSetNameServers()
       
  1650     {
       
  1651     if_name_servers dns_server;
       
  1652 
       
  1653     char iapName[IFNAMSIZ];
       
  1654     GetIapName(iapName, sizeof(iapName));
       
  1655 
       
  1656     memset(&dns_server,0, sizeof(dns_server));
       
  1657     strcpy(dns_server.if_name, iapName);
       
  1658     TPtrC nameServer16;
       
  1659     _LIT(KNameSrv1, "NameServer1");
       
  1660     _LIT(KNameSrv2, "NameServer2");
       
  1661 
       
  1662     TBool res = GetStringFromConfig(ConfigSection(), KNameSrv1, nameServer16);
       
  1663     if (!res)
       
  1664         {
       
  1665         INFO_PRINTF1(_L("Unable to read the nameserver1 from the ini file.")) ;    
       
  1666         return KErrGeneral;
       
  1667         }
       
  1668     char nameServer1[50];
       
  1669     TPtr8 nameServer8((TUint8 *)nameServer1, sizeof(nameServer1));
       
  1670     nameServer8.Copy(nameServer16);
       
  1671     nameServer8.ZeroTerminate();
       
  1672     res = GetStringFromConfig(ConfigSection(), KNameSrv2, nameServer16);
       
  1673     if (!res)
       
  1674         {
       
  1675         INFO_PRINTF1(_L("Unable to read nameserver2 from the ini file.")) ;    
       
  1676         return KErrGeneral;
       
  1677         }
       
  1678 
       
  1679     char nameServer2[50];
       
  1680     nameServer8.Set((TUint8 *)nameServer2,  0, sizeof(nameServer2));
       
  1681     nameServer8.Copy(nameServer16);
       
  1682     nameServer8.ZeroTerminate();
       
  1683     
       
  1684     dns_server.nameserver1.sa_family = AF_INET;
       
  1685     dns_server.nameserver2.sa_family = AF_INET;
       
  1686     TInt addr1 = inet_addr(nameServer1);
       
  1687     TInt addr2 = inet_addr(nameServer2);
       
  1688     unsigned char * ptr = (unsigned char *)&addr1;
       
  1689     dns_server.nameserver1.sa_data[0] = ptr[0];
       
  1690     dns_server.nameserver1.sa_data[1] = ptr[1];
       
  1691     dns_server.nameserver1.sa_data[2] = ptr[2];
       
  1692     dns_server.nameserver1.sa_data[3] = ptr[3];
       
  1693    
       
  1694     ptr = (unsigned char *)&addr2;
       
  1695     dns_server.nameserver2.sa_data[0] = ptr[0];
       
  1696     dns_server.nameserver2.sa_data[1] = ptr[1];
       
  1697     dns_server.nameserver2.sa_data[2] = ptr[2];
       
  1698     dns_server.nameserver2.sa_data[3] = ptr[3];
       
  1699     
       
  1700     TInt sockfd;
       
  1701     if(StartIap(iapName, sockfd) != KErrNone)
       
  1702         {
       
  1703         INFO_PRINTF2(_L("unable to start the iap, reason = %d\n"),errno);
       
  1704         return -1;
       
  1705         }
       
  1706 
       
  1707 
       
  1708     TInt ret = ioctl(sockfd, SIOCSNAMESERVER, &dns_server);
       
  1709     if(ret == -1)
       
  1710         {
       
  1711         INFO_PRINTF2(_L("IOCTL option failed with errno =%d"), errno);
       
  1712         close(sockfd);
       
  1713         return errno;
       
  1714         }
       
  1715 
       
  1716     return KErrNone;
       
  1717 
       
  1718     }
       
  1719 
       
  1720 //Testcase added to enumerate routes configured on the device.
       
  1721 //Use Ioctl option SIOCENUMROUTES to get the route details.
       
  1722 //PreCondition: User needs to allocate space for the buffer.
       
  1723 TInt CTestIfioctls::TestRouteIoctl()
       
  1724     {
       
  1725     rtconf rtc;
       
  1726     TInt ret = 0;
       
  1727     rtc.rt_len = 0;
       
  1728     TInt sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
       
  1729     if(sockfd < 0)
       
  1730         {
       
  1731         INFO_PRINTF2(_L("Unable to create the socket errno =%d"), errno);
       
  1732         return -1;
       
  1733         }
       
  1734     if((ret = ioctl (sockfd, SIOCENUMROUTES,&rtc)) == -1)
       
  1735         {
       
  1736         INFO_PRINTF2(_L("Unable to get the routes configured, errno = %d"), errno);
       
  1737         close(sockfd);
       
  1738         return -1;
       
  1739         }
       
  1740 
       
  1741     if (rtc.rt_len == 0)
       
  1742         {
       
  1743         INFO_PRINTF1(_L("No routes were found,"));
       
  1744         close(sockfd);
       
  1745         return KErrNone;
       
  1746         }
       
  1747     if((rtc.rtc_buf = (caddr_t)malloc( rtc.rt_len )) == NULL)
       
  1748         {
       
  1749         INFO_PRINTF1(_L("Unable to allocate memory "));
       
  1750         close(sockfd);
       
  1751         return KErrNoMemory;
       
  1752         }
       
  1753     
       
  1754     INFO_PRINTF1(_L("Now will fetch the routes!\n"));
       
  1755     if((ret = ioctl (sockfd, SIOCENUMROUTES,&rtc)) == -1)
       
  1756         {
       
  1757         INFO_PRINTF2(_L("Unable to get the routs configured, errno = %d"), errno);
       
  1758         close(sockfd);
       
  1759         free(rtc.rtc_buf);
       
  1760         return -1;
       
  1761         }
       
  1762     close(sockfd);
       
  1763     free(rtc.rtc_buf);
       
  1764     return KErrNone;
       
  1765     }
       
  1766 
       
  1767 
       
  1768 //With the route_entry buffer size zero, it should return the number of routes present on the system
       
  1769 //To use this ioctl effectively, we can find the number of routes by passing length as Zero and then
       
  1770 //pass the count*sizeof(route_entry) as the len the next time to fecth the route information.
       
  1771 TInt CTestIfioctls::TestNumberOfRoutesIoctl()
       
  1772     {
       
  1773     rtconf rtc;
       
  1774     rtc.rt_len = 0;
       
  1775     TInt ret = 0;
       
  1776 
       
  1777     TInt sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
       
  1778     if(sockfd < 0)
       
  1779         {
       
  1780         INFO_PRINTF2(_L("Unable to create the socket errno =%d"), errno);
       
  1781         return -1;
       
  1782         }
       
  1783     if((ret = ioctl (sockfd, SIOCENUMROUTES,&rtc)) == -1)
       
  1784         {
       
  1785         INFO_PRINTF2(_L("Unable to get the routs configures, errno = %d"), errno);
       
  1786         return -1;
       
  1787         }
       
  1788 
       
  1789     INFO_PRINTF2(_L("The number of routes available is %d\n"), rtc.rt_len/sizeof(route_entry));
       
  1790     close(sockfd);
       
  1791     return KErrNone;
       
  1792 
       
  1793     }
       
  1794 
       
  1795