/* * Creates a socket suitable for raw socket operations. The socket is * bound to the interface specified by the supplied name. The socket * value is placed into the supplied FileDescriptor instance. * * TODO(chesnutt): consider breaking this into pieces: create a * variety of constructors for different socket types, then a generic * setBlocking() method followed by polymorphic bind(). */ static void RawSocket_create(JNIEnv* env, jclass, jobject fileDescriptor, jshort protocolType, jstring interfaceName) { ScopedUtfChars ifname(env, interfaceName); if (ifname.c_str() == NULL) { return; } memset(&su, 0, sizeof(su)); su.sll.sll_family = PF_PACKET; su.sll.sll_protocol = htons(protocolType); su.sll.sll_ifindex = if_nametoindex(ifname.c_str()); int sock = socket(PF_PACKET, SOCK_DGRAM, htons(protocolType)); if (sock == -1) { ALOGE("Can't create socket %s", strerror(errno)); jniThrowSocketException(env, errno); return; } jniSetFileDescriptorOfFD(env, fileDescriptor, sock); if (!setBlocking(sock, false)) { ALOGE("Can't set non-blocking mode on socket %s", strerror(errno)); jniThrowSocketException(env, errno); return; } int err = bind(sock, &su.sa, sizeof(su)); if (err != 0) { ALOGE("Socket bind error %s", strerror(errno)); jniThrowSocketException(env, errno); return; } }
static bool doCommand(JNIEnv* env, jstring jIface, jstring javaCommand, char* reply, size_t reply_len) { ScopedUtfChars ifname(env, jIface); ScopedUtfChars command(env, javaCommand); if (command.c_str() == NULL) { return false; // ScopedUtfChars already threw on error. } if (DBG) { ALOGD("doCommand: %s:%s",ifname.c_str(), command.c_str()); } --reply_len; // Ensure we have room to add NUL termination. if (::wifi_command(ifname.c_str(), command.c_str(), reply, &reply_len) != 0) { return false; } // Strip off trailing newline. if (reply_len > 0 && reply[reply_len-1] == '\n') { reply[reply_len-1] = '\0'; } else { reply[reply_len] = '\0'; } return true; }
static jstring android_net_wifi_waitForEvent(JNIEnv* env, jobject, jstring jIface) { char buf[EVENT_BUF_SIZE]; ScopedUtfChars ifname(env, jIface); int nread = ::wifi_wait_for_event(ifname.c_str(), buf, sizeof buf); if (nread > 0) { return env->NewStringUTF(buf); } else { return NULL; } }
static jstring android_net_wifi_doStringCommand(JNIEnv* env, jobject, jstring jIface, jstring jCommand) { ScopedUtfChars ifname(env, jIface); ScopedUtfChars command(env, jCommand); if (command.c_str() == NULL) { return NULL; } if (DBG) ALOGD("doString: %s", command.c_str()); return doStringCommand(env, ifname.c_str(), "%s", command.c_str()); }
static jboolean android_net_wifi_doBooleanCommand(JNIEnv* env, jobject, jstring jIface, jstring jCommand) { ScopedUtfChars ifname(env, jIface); ScopedUtfChars command(env, jCommand); if (command.c_str() == NULL) { return JNI_FALSE; } if (DBG) ALOGD("doBoolean: %s", command.c_str()); return doBooleanCommand(ifname.c_str(), "OK", "%s", command.c_str()); }
std::string getFTSEndpoint() { const char* path[2] = {"/etc/glite-sd2cache-cron.conf","/opt/glite/etc/glite-sd2cache-cron.conf"}; string line; vector<std::string>::iterator myVectorIterator; std::string fts = ""; std::string result = ""; for(int index=0; index<2; index++){ string ifname(path[index]); ifstream in(ifname.c_str()); if(!in){ continue; } string line; while (!in.eof()) { getline(in, line); line = strip_space(line); if (line.length() && line[0] != '#') { size_t pos = line.find("="); if (pos != string::npos) { string key = strip_space(line.substr(0, pos)); string value = strip_space(line.substr(pos + 1)); ftsendpoint.insert(make_pair(key, value)); } } } map <string, string> ::const_iterator iter = ftsendpoint.find("FTS_HOST"); if (iter != ftsendpoint.end()) { fts = ftsendpoint.find("FTS_HOST")->second; if (fts.length() == 0) fts = ""; } } if(fts.length() > 0) { fts.erase(0, 1); fts.erase(fts.length()-1, fts.length()); result = "https://"; result += fts; result += ":8443/glite-data-transfer-fts/services/FileTransfer"; } return result; }
/* * Writes the L3 (IP) packet to the raw socket supplied in the * FileDescriptor instance. * * Assumes that the caller has validated the offset & byteCount values. */ static int RawSocket_sendPacket(JNIEnv* env, jclass, jobject fileDescriptor, jstring interfaceName, jshort protocolType, jbyteArray destMac, jbyteArray packet, jint offset, jint byteCount) { NetFd fd(env, fileDescriptor); if (fd.isClosed()) { return 0; } ScopedUtfChars ifname(env, interfaceName); if (ifname.c_str() == NULL) { return 0; } ScopedByteArrayRO byteArray(env, packet); if (byteArray.get() == NULL) { return 0; } ScopedByteArrayRO mac(env, destMac); if (mac.get() == NULL) { return 0; } sockunion su; memset(&su, 0, sizeof(su)); su.sll.sll_hatype = htons(1); // ARPHRD_ETHER su.sll.sll_halen = mac.size(); memcpy(&su.sll.sll_addr, mac.get(), mac.size()); su.sll.sll_family = AF_PACKET; su.sll.sll_protocol = htons(protocolType); su.sll.sll_ifindex = if_nametoindex(ifname.c_str()); int err; { int intFd = fd.get(); AsynchronousSocketCloseMonitor monitor(intFd); err = NET_FAILURE_RETRY(fd, sendto(intFd, byteArray.get() + offset, byteCount, 0, &su.sa, sizeof(su))); } return err; }
int reportRoutinTable( int route_sock, FILE * fpRouting , struct timespec tv , char * ifNameVar) { int i,j; route_request *request = (route_request *)malloc(sizeof(route_request)); int retValue = -1,nbytes = 0,reply_len = 0; char reply_ptr[1024]; ssize_t counter = 1024; // int count = 0; struct rtmsg *rtp; struct rtattr *rtap; struct nlmsghdr *nlp; int rtl; struct RouteInfo route[24]; char* buf = reply_ptr; unsigned long bufsize ; char * outputRouting ; outputRouting = (char *)malloc(sizeof(char) * 1024); bzero(request,sizeof(route_request)); // Fill in the NETLINK header request->nlMsgHdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); request->nlMsgHdr.nlmsg_type = RTM_GETROUTE; request->nlMsgHdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; // set the routing message header request->rtMsg.rtm_family = AF_INET; request->rtMsg.rtm_table = 254; // Send routing request if ((retValue = send(route_sock, request, sizeof(route_request), 0)) < 0) { perror("send"); exit(1); } for(;;) { if( counter < sizeof( struct nlmsghdr)) { printf("Routing table is bigger than 1024\n"); exit(1); } nbytes = recv(route_sock, &reply_ptr[reply_len], counter, 0); if(nbytes < 0 ) { printf("Error in recv\n"); break; } if(nbytes == 0) printf("EOF in netlink\n"); nlp = (struct nlmsghdr*)(&reply_ptr[reply_len]); if (nlp->nlmsg_type == NLMSG_DONE) { // All data has been received. // Truncate the reply to exclude this message, // i.e. do not increase reply_len. break; } if (nlp->nlmsg_type == NLMSG_ERROR) { printf("Error in msg\n"); exit(1); } reply_len += nbytes; counter -= nbytes; } /*======================================================*/ bufsize = reply_len; // string to hold content of the route // table (i.e. one entry) // unsigned int flags; // outer loop: loops thru all the NETLINK // headers that also include the route entry // header nlp = (struct nlmsghdr *) buf; for(i= -1; NLMSG_OK(nlp, bufsize); nlp=NLMSG_NEXT(nlp, bufsize)) { // // get route entry header rtp = (struct rtmsg *) NLMSG_DATA(nlp); // we are only concerned about the // tableId route table if(rtp->rtm_table != 254) continue; i++; // init all the strings bzero(&route[i], sizeof(struct RouteInfo)); // flags = rtp->rtm_flags; // route[i].proto = rtp->rtm_protocol; // // inner loop: loop thru all the attributes of // // one route entry rtap = (struct rtattr *) RTM_RTA(rtp); rtl = RTM_PAYLOAD(nlp); for( ; RTA_OK(rtap, rtl); rtap = RTA_NEXT(rtap, rtl)) { switch(rtap->rta_type) { // destination IPv4 address case RTA_DST: // count = 32 - rtp->rtm_dst_len; route[i].dstAddr = *(unsigned long *) RTA_DATA(rtap); break; case RTA_GATEWAY: route[i].gateWay = *(unsigned long *) RTA_DATA(rtap); //printf("gw:%s\t",inet_ntoa(route[i].gateWay)); break; // // unique ID associated with the network // // interface case RTA_OIF: ifname(*((int *) RTA_DATA(rtap)),route[i].ifName); //printf( "ifname %s\n", route[i].ifName); break; default: break; } } } for( j = 0; j<= i; j++) { if(strcmp(route[j].ifName, ifNameVar)==0) { char ipbuf[INET_ADDRSTRLEN]; char ipbuf2[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &route[j].dstAddr, ipbuf, INET_ADDRSTRLEN); inet_ntop(AF_INET, &route[j].gateWay, ipbuf2, INET_ADDRSTRLEN); sprintf(outputRouting, "%lu.%lu:%s:%s", (unsigned long) tv.tv_sec,(unsigned long) tv.tv_nsec, ipbuf ,ipbuf2); fprintf(fpRouting, "%s\n" ,outputRouting); } } fflush(fpRouting); // close(route_sock); return 0; }
bool get_mon_cfg_file() { std::string boolTOPIC; std::string boolACTIVE; std::string boolENABLELOG; std::string boolENABLEMSGLOG; std::string boolUSE_BROKER_CREDENTIALS; std::string filename; try { filename = getMsgConfigFile(); if (filename.length() == 0) return false; string ifname(filename); ifstream in(ifname.c_str()); if(!in){ logger::writeLog("msg config file cannot be read, check location and permissions", true); return false; } cfg.clear(); string line; while (!in.eof()) { getline(in, line); line = strip_space(line); if (line.length() && line[0] != '#') { size_t pos = line.find("="); if (pos != string::npos) { string key = strip_space(line.substr(0, pos)); string value = strip_space(line.substr(pos + 1)); cfg.insert(make_pair(key, value)); } } } map <string, string> ::const_iterator iter1 = cfg.find("BROKER"); map <string, string> ::const_iterator iter2 = cfg.find("START"); map <string, string> ::const_iterator iter3 = cfg.find("COMPLETE"); map <string, string> ::const_iterator iter4 = cfg.find("CRON"); map <string, string> ::const_iterator iter6 = cfg.find("TTL"); map <string, string> ::const_iterator iter5 = cfg.find("TOPIC"); map <string, string> ::const_iterator iter7 = cfg.find("ACTIVE"); map <string, string> ::const_iterator iter8 = cfg.find("ENABLELOG"); map <string, string> ::const_iterator iter9 = cfg.find("LOGFILEDIR"); map <string, string> ::const_iterator iter10 = cfg.find("LOGFILENAME"); map <string, string> ::const_iterator iter11 = cfg.find("FQDN"); map <string, string> ::const_iterator iter12 = cfg.find("ENABLEMSGLOG"); map <string, string> ::const_iterator iter13 = cfg.find("USERNAME"); map <string, string> ::const_iterator iter14 = cfg.find("PASSWORD"); map <string, string> ::const_iterator iter15 = cfg.find("USE_BROKER_CREDENTIALS"); if (iter13 != cfg.end()) { USERNAME = cfg.find("USERNAME")->second; if (USERNAME.length() == 0) USERNAME = ""; } if (iter14 != cfg.end()) { PASSWORD = cfg.find("PASSWORD")->second; if (PASSWORD.length() == 0) PASSWORD = ""; } if (iter15 != cfg.end()) { boolUSE_BROKER_CREDENTIALS = cfg.find("USE_BROKER_CREDENTIALS")->second; if (boolUSE_BROKER_CREDENTIALS.compare("true") == 0) USE_BROKER_CREDENTIALS = true; else USE_BROKER_CREDENTIALS = false; } if (iter12 != cfg.end()) { boolENABLEMSGLOG = cfg.find("ENABLEMSGLOG")->second; if (boolENABLEMSGLOG.compare("true") == 0) ENABLEMSGLOG = true; else ENABLEMSGLOG = false; } if (iter11 != cfg.end()) { CRONFQDN = cfg.find("FQDN")->second; if (CRONFQDN.length() == 0) CRONFQDN = ""; } if (iter9 != cfg.end()){ LOGFILEDIR = cfg.find("LOGFILEDIR")->second; if( LOGFILEDIR[LOGFILEDIR.length()-1] != '/') LOGFILEDIR += "/"; } else{ LOGFILEDIR = "/var/log/glite/"; } if (iter10 != cfg.end()) LOGFILENAME = cfg.find("LOGFILENAME")->second; else LOGFILENAME = "msg.log"; if (iter1 != cfg.end()) BROKER = cfg.find("BROKER")->second; if (iter2 != cfg.end()) START = cfg.find("START")->second; if (iter3 != cfg.end()) COMPLETE = cfg.find("COMPLETE")->second; if (iter4 != cfg.end()) CRON = cfg.find("CRON")->second; if (iter6 != cfg.end()) TTL = cfg.find("TTL")->second; if (iter5 != cfg.end()) { boolTOPIC = cfg.find("TOPIC")->second; if (boolTOPIC.compare("true") == 0) TOPIC = true; else TOPIC = false; } if (iter7 != cfg.end()) { boolACTIVE = cfg.find("ACTIVE")->second; if (boolACTIVE.compare("true") == 0) ACTIVE = true; else ACTIVE = false; } if (iter8 != cfg.end()) { boolENABLELOG = cfg.find("ENABLELOG")->second; if (boolENABLELOG.compare("true") == 0) ENABLELOG = true; else ENABLELOG = false; } return true; } catch (...) { logger::writeLog("msg config file error", true); return false; } }
static void android_net_wifi_closeSupplicantConnection(JNIEnv* env, jobject, jstring jIface) { ScopedUtfChars ifname(env, jIface); ::wifi_close_supplicant_connection(ifname.c_str()); }
//gwl direct static jboolean android_net_wifi_connectToSupplicant(JNIEnv* env, jobject, jstring jIface) { ScopedUtfChars ifname(env, jIface); return (::wifi_connect_to_supplicant(ifname.c_str()) == 0); }
std::list< shared_ptr<NetworkInterfaceInfo> > listNetworkInterfaces() { typedef std::map< std::string, shared_ptr<NetworkInterfaceInfo> > InterfacesMap; InterfacesMap ifmap; ifaddrs* ifa_list; if (::getifaddrs(&ifa_list) < 0) throw std::runtime_error("getifaddrs() failed"); for (ifaddrs* ifa = ifa_list; ifa != 0; ifa = ifa->ifa_next) { shared_ptr<NetworkInterfaceInfo> netif; std::string ifname(ifa->ifa_name); InterfacesMap::iterator i = ifmap.find(ifname); if (i == ifmap.end()) { netif = make_shared<NetworkInterfaceInfo>(); netif->name = ifname; netif->flags = ifa->ifa_flags; ifmap[ifname] = netif; } else { netif = i->second; } if (!ifa->ifa_addr) continue; switch (ifa->ifa_addr->sa_family) { case AF_INET: { const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_addr); char address[INET_ADDRSTRLEN]; if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) netif->ipv4Addresses.push_back(boost::asio::ip::address_v4::from_string(address)); else NFD_LOG_WARN("inet_ntop() failed on " << ifname); } break; case AF_INET6: { const sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr); char address[INET6_ADDRSTRLEN]; if (::inet_ntop(AF_INET6, &sin6->sin6_addr, address, sizeof(address))) netif->ipv6Addresses.push_back(boost::asio::ip::address_v6::from_string(address)); else NFD_LOG_WARN("inet_ntop() failed on " << ifname); } break; #if defined(__linux__) case AF_PACKET: { const sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ifa->ifa_addr); netif->index = sll->sll_ifindex; if (sll->sll_hatype == ARPHRD_ETHER && sll->sll_halen == ethernet::ADDR_LEN) netif->etherAddress = ethernet::Address(sll->sll_addr); else if (sll->sll_hatype != ARPHRD_LOOPBACK) NFD_LOG_WARN("Unrecognized hardware address on " << ifname); } break; #elif defined(__APPLE__) || defined(__FreeBSD__) case AF_LINK: { const sockaddr_dl* sdl = reinterpret_cast<sockaddr_dl*>(ifa->ifa_addr); netif->index = sdl->sdl_index; netif->etherAddress = ethernet::Address(reinterpret_cast<uint8_t*>(LLADDR(sdl))); } break; #endif } if (netif->isBroadcastCapable() && ifa->ifa_broadaddr != 0) { const sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(ifa->ifa_broadaddr); char address[INET_ADDRSTRLEN]; if (::inet_ntop(AF_INET, &sin->sin_addr, address, sizeof(address))) netif->broadcastAddress = boost::asio::ip::address_v4::from_string(address); else NFD_LOG_WARN("inet_ntop() failed on " << ifname); } } ::freeifaddrs(ifa_list); std::list< shared_ptr<NetworkInterfaceInfo> > list; BOOST_FOREACH(InterfacesMap::value_type elem, ifmap) { list.push_back(elem.second); }