OCStackResult OCSecureResource::unlinkDevices(const OCSecureResource &device2, ResultCallBack resultCallback) { if(!resultCallback) { oclog() << "Result calback can't be null"; return OC_STACK_INVALID_PARAM; } OCStackResult result; auto cLock = m_csdkLock.lock(); if(cLock) { ProvisionContext* context = new ProvisionContext(resultCallback); std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCUnlinkDevices(static_cast<void*>(context), devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper); } else { oclog() <<"Mutex not found"; result = OC_STACK_ERROR; } return result; }
OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred, const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2, ResultCallBack resultCallback) { if(!resultCallback) { oclog() << "Result calback can't be null"; return OC_STACK_INVALID_PARAM; } OCStackResult result; auto cLock = m_csdkLock.lock(); if(cLock) { ProvisionContext* context = new ProvisionContext(resultCallback); std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCProvisionPairwiseDevices(static_cast<void*>(context), cred.getCredentialType(), cred.getCredentialKeySize(), devPtr, const_cast<OicSecAcl_t*>(acl1), device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2), &OCSecureResource::callbackWrapper); } else { oclog() <<"Mutex not found"; result = OC_STACK_ERROR; } return result; }
OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery, ResultCallBack resultCallback) { if(!resultCallback) { oclog() << "Result calback can't be null"; return OC_STACK_INVALID_PARAM; } OCStackResult result; auto cLock = m_csdkLock.lock(); if(cLock) { ProvisionContext* context = new ProvisionContext(resultCallback); std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery, devPtr, &OCSecureResource::callbackWrapper); } else { oclog() <<"Mutex not found"; result = OC_STACK_ERROR; } return result; }
OCStackResult OCSecureResource::provisionACL( const OicSecAcl_t* acl, ResultCallBack resultCallback) { if(!resultCallback || !acl) { oclog() <<"Result callback or ACL can't be null"; return OC_STACK_INVALID_PARAM; } OCStackResult result; auto cLock = m_csdkLock.lock(); if(cLock) { ProvisionContext* context = new ProvisionContext(resultCallback); std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCProvisionACL(static_cast<void*>(context), devPtr, const_cast<OicSecAcl_t*>(acl), &OCSecureResource::callbackWrapper); } else { oclog() <<"Mutex not found"; result = OC_STACK_ERROR; } return result; }
/* returns the fd */ int connect_to_secmod(worker_st * ws) { int sd, ret, e; sd = socket(AF_UNIX, SOCK_STREAM, 0); if (sd == -1) { e = errno; oclog(ws, LOG_ERR, "error opening unix socket (for sec-mod) %s", strerror(e)); return -1; } ret = connect(sd, (struct sockaddr *)&ws->secmod_addr, ws->secmod_addr_len); if (ret < 0) { e = errno; close(sd); oclog(ws, LOG_ERR, "error connecting to sec-mod socket '%s': %s", ws->secmod_addr.sun_path, strerror(e)); return -1; } return sd; }
/* Set curl properties for link based on rc files */ static void ocsetcurlproperties(OCstate* state) { CURLcode cstat = CURLE_OK; /* process the triple store wrt to this state */ if(ocdodsrc_process(state) != OC_NOERR) { oclog(OCLOGERR,"Malformed .opendaprc configuration file"); goto fail; } if(state->creds.username == NULL && state->creds.password == NULL) { if(state->uri->user != NULL && state->uri->password != NULL) { /* this overrides .dodsrc */ if(state->creds.password) free(state->creds.password); state->creds.password = nulldup(state->uri->password); if(state->creds.username) free(state->creds.username); state->creds.username = nulldup(state->uri->user); } } if(state->curlflags.useragent == NULL) { size_t len = strlen(DFALTUSERAGENT) + strlen(VERSION) + 1; char* agent = (char*)malloc(len+1); if(occopycat(agent,len,2,DFALTUSERAGENT,VERSION)) state->curlflags.useragent = agent; else free(agent); } return; fail: if(cstat != CURLE_OK) oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat)); return; }
void ocsigaltstack(struct worker_st *ws) { #if defined(HAVE_SIGALTSTACK) && defined(HAVE_POSIX_MEMALIGN) stack_t ss; int e; /* setup the stack for signal handlers */ if (posix_memalign(&ss.ss_sp, getpagesize(), SIGSTKSZ) < 0) { oclog(ws, LOG_ERR, "could not allocate memory for signal stack"); exit(1); } if (mprotect(ss.ss_sp, SIGSTKSZ, PROT_READ|PROT_WRITE) == -1) { e = errno; oclog(ws, LOG_ERR, "mprotect: %s\n", strerror(e)); exit(1); } ss.ss_size = SIGSTKSZ; ss.ss_flags = 0; if (sigaltstack(&ss, NULL) == -1) { e = errno; oclog(ws, LOG_ERR, "sigaltstack: %s\n", strerror(e)); exit(1); } #endif }
/* grabs the username from the session certificate */ static int get_cert_info(worker_st * ws) { const gnutls_datum_t *cert; unsigned int ncerts; int ret; /* this is superflous. Verification has already been performed * during handshake. */ cert = gnutls_certificate_get_peers(ws->session, &ncerts); if (cert == NULL) { return -1; } ret = get_cert_names(ws, cert); if (ret < 0) { if (ws->config->cert_user_oid == NULL) { oclog(ws, LOG_ERR, "cannot read username from certificate; no cert-user-oid is set"); } else { oclog(ws, LOG_ERR, "cannot read username (%s) from certificate", ws->config->cert_user_oid); } return -1; } return 0; }
static int readfile(const char* path, const char* suffix, OCbytes* packet) { int stat = OC_NOERR; char buf[1024]; char filename[1024]; int fd = -1; int flags = 0; off_t filesize = 0; off_t totalread = 0; /* check for leading file:/// */ if(ocstrncmp(path,"file://",7)==0) path += 7; /* assume absolute path*/ if(!occopycat(filename,sizeof(filename),2,path,(suffix != NULL ? suffix : ""))) return OCTHROW(OC_EOVERRUN); flags = O_RDONLY; #ifdef O_BINARY flags |= O_BINARY; #endif fd = open(filename,flags); if(fd < 0) { oclog(OCLOGERR,"open failed:%s",filename); return OCTHROW(OC_EOPEN); } /* Get the file size */ filesize = lseek(fd,(off_t)0,SEEK_END); if(filesize < 0) { stat = OC_EIO; oclog(OCLOGERR,"lseek failed: %s",filename); goto done; } /* Move file pointer back to the beginning of the file */ (void)lseek(fd,(off_t)0,SEEK_SET); stat = OC_NOERR; for(totalread=0;;) { off_t count = (off_t)read(fd,buf,sizeof(buf)); if(count == 0) break; /*eof*/ else if(count < 0) { stat = OC_EIO; oclog(OCLOGERR,"read failed: %s",filename); goto done; } ocbytesappendn(packet,buf,(unsigned long)count); totalread += count; } if(totalread < filesize) { stat = OC_EIO; oclog(OCLOGERR,"short read: |%s|=%lu read=%lu\n", filename,(unsigned long)filesize,(unsigned long)totalread); goto done; } done: #ifdef OCDEBUG fprintf(stderr,"readfile: filesize=%lu totalread=%lu\n", (unsigned long)filesize,(unsigned long)totalread); #endif if(fd >= 0) close(fd); return OCTHROW(stat); }
static OCerror createtempfile(OCstate* state, OCtree* tree) { int stat = OC_NOERR; int fd = 0; char* name = NULL; stat = ocmktmp(TMPPATH1,&name, &fd); if(stat != OC_NOERR) stat = ocmktmp(TMPPATH2,&name,&fd); if(stat != OC_NOERR) goto fail; #ifdef OCDEBUG oclog(OCLOGNOTE,"oc_open: using tmp file: %s",name); #endif tree->data.filename = name; /* remember our tmp file name */ tree->data.file = fdopen(fd,"w+"); if(tree->data.file == NULL) return OC_EOPEN; /* unlink the temp file so it will automatically be reclaimed */ if(ocdebug == 0) unlink(tree->data.filename); return stat; fail: oclog(OCLOGERR,"oc_open: attempt to create tmp file failed: %s",name == NULL ? "[NULL]" : name); if(name != NULL) {free(name);name=NULL;} return stat; }
int get_config_handler(worker_st *ws, unsigned http_ver) { int ret; struct stat st; oclog(ws, LOG_HTTP_DEBUG, "requested config: %s", ws->req.url); if (ws->config->xml_config_file == NULL) { oclog(ws, LOG_INFO, "requested config but no config file is set"); cstp_printf(ws, "HTTP/1.%u 404 Not found\r\n", http_ver); return -1; } ret = stat( ws->config->xml_config_file, &st); if (ret == -1) { oclog(ws, LOG_INFO, "cannot load config file '%s'", ws->config->xml_config_file); cstp_printf(ws, "HTTP/1.%u 404 Not found\r\n", http_ver); return -1; } cstp_cork(ws); ret = cstp_printf(ws, "HTTP/1.%u 200 OK\r\n", http_ver); if (ret < 0) return -1; ret = cstp_puts(ws, "Connection: Keep-Alive\r\n"); if (ret < 0) return -1; ret = cstp_puts(ws, "Content-Type: text/xml\r\n"); if (ret < 0) return -1; ret = cstp_puts(ws, "X-Transcend-Version: 1\r\n"); if (ret < 0) return -1; ret = cstp_printf(ws, "Content-Length: %u\r\n", (unsigned)st.st_size); if (ret < 0) return -1; ret = cstp_puts(ws, "\r\n"); if (ret < 0) return -1; ret = cstp_uncork(ws); if (ret < 0) return -1; ret = cstp_send_file(ws, ws->config->xml_config_file); if (ret < 0) { oclog(ws, LOG_ERR, "error sending file '%s': %s", ws->config->xml_config_file, gnutls_strerror(ret)); return -1; } return 0; }
int ocfetchurl_file(CURL* curl, const char* url, FILE* stream, off_t* sizep, long* filetime) { int stat = OC_NOERR; CURLcode cstat = CURLE_OK; struct Fetchdata fetchdata; /* Set the URL */ cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url); if (cstat != CURLE_OK) goto fail; /* send all data to this function */ cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteFileCallback); if (cstat != CURLE_OK) goto fail; /* we pass our file to the callback function */ cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&fetchdata); if (cstat != CURLE_OK) goto fail; /* One last thing; always try to get the last modified time */ cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1); if (cstat != CURLE_OK) goto fail; fetchdata.stream = stream; fetchdata.size = 0; cstat = curl_easy_perform(curl); if (cstat != CURLE_OK) goto fail; if (stat == OC_NOERR) { /* return the file size*/ #ifdef OCDEBUG oclog(OCLOGNOTE,"filesize: %lu bytes",fetchdata.size); #endif if (sizep != NULL) *sizep = fetchdata.size; /* Get the last modified time */ if(filetime != NULL) cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime); if(cstat != CURLE_OK) goto fail; } return OCTHROW(stat); fail: oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat)); return OCTHROW(OC_ECURL); }
OCStackApplicationResult listenCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) { ClientCallbackContext::ListenContext* context = static_cast<ClientCallbackContext::ListenContext*>(ctx); if(clientResponse->result != OC_STACK_OK) { oclog() << "listenCallback(): failed to create resource. clientResponse: " << clientResponse->result << std::flush; return OC_STACK_KEEP_TRANSACTION; } auto clientWrapper = context->clientWrapper.lock(); if(!clientWrapper) { oclog() << "listenCallback(): failed to get a shared_ptr to the client wrapper" << std::flush; return OC_STACK_KEEP_TRANSACTION; } std::stringstream requestStream; requestStream << clientResponse->resJSONPayload; try { ListenOCContainer container(clientWrapper, *clientResponse->addr, requestStream); // loop to ensure valid construction of all resources for(auto resource : container.Resources()) { std::thread exec(context->callback, resource); exec.detach(); } } catch(const std::exception& e) { oclog() << "listenCallback failed to parse a malformed message: " << e.what() << std::endl <<std::endl << clientResponse->result << std::flush; return OC_STACK_KEEP_TRANSACTION; } return OC_STACK_KEEP_TRANSACTION; }
static void parse_ssl_tlvs(struct worker_st *ws, uint8_t *data, int data_size) { pp2_tlv tlv; while(data_size > 0) { AVAIL_HEADER_SIZE(data_size, sizeof(pp2_tlv)); memcpy(&tlv, data, sizeof(pp2_tlv)); /* that seems to be in little endian */ tlv.length = htons(tlv.length); data += sizeof(pp2_tlv); oclog(ws, LOG_INFO, "proxy-hdr: TLV type %x", (unsigned)tlv.type); if (tlv.type == PP2_TYPE_SSL) { pp2_tlv_ssl tssl; if (tlv.length < sizeof(pp2_tlv_ssl)) { oclog(ws, LOG_ERR, "proxy-hdr: TLV SSL header size is invalid"); continue; } tlv.length = sizeof(pp2_tlv_ssl); AVAIL_HEADER_SIZE(data_size, tlv.length); memcpy(&tssl, data, sizeof(pp2_tlv_ssl)); if ((tssl.client & PP2_CLIENT_SSL) && (tssl.client & PP2_CLIENT_CERT_SESS) && (tssl.verify == 0)) { oclog(ws, LOG_INFO, "proxy-hdr: user has presented valid certificate"); ws->cert_auth_ok = 1; } } else if (tlv.type == PP2_TYPE_SSL_CN && ws->cert_auth_ok) { if (tlv.length > sizeof(ws->cert_username)-1) { oclog(ws, LOG_ERR, "proxy-hdr: TLV SSL CN header size is too long"); continue; } AVAIL_HEADER_SIZE(data_size, tlv.length); memcpy(ws->cert_username, data, tlv.length); ws->cert_username[tlv.length] = 0; oclog(ws, LOG_INFO, "proxy-hdr: user's name is '%s'", ws->cert_username); } else { AVAIL_HEADER_SIZE(data_size, tlv.length); } data += tlv.length; } }
/* read and compile the rc file, if any */ OCerror ocrc_load(void) { OCerror stat = OC_NOERR; char* path = NULL; if(ocglobalstate.rc.ignore) { oclog(OCLOGDBG,"No runtime configuration file specified; continuing"); return OC_NOERR; } if(ocglobalstate.rc.loaded) return OC_NOERR; /* locate the configuration files in the following order: 1. specified by set_rcfile 2. set by DAPRCFILE env variable 3. '.' 4. $HOME */ if(ocglobalstate.rc.rcfile != NULL) { /* always use this */ path = strdup(ocglobalstate.rc.rcfile); } else if(getenv(OCRCFILEENV) != NULL && strlen(getenv(OCRCFILEENV)) > 0) { path = strdup(getenv(OCRCFILEENV)); } else { char** rcname; int found = 0; for(rcname=rcfilenames;!found && *rcname;rcname++) { stat = rc_search(".",*rcname,&path); if(stat == OC_NOERR && path == NULL) /* try $HOME */ stat = rc_search(ocglobalstate.home,*rcname,&path); if(stat != OC_NOERR) goto done; if(path != NULL) found = 1; } } if(path == NULL) { oclog(OCLOGDBG,"Cannot find runtime configuration file; continuing"); } else { if(ocdebug > 0) fprintf(stderr, "RC file: %s\n", path); if(ocrc_compile(path) == 0) { oclog(OCLOGERR, "Error parsing %s\n",path); stat = OC_ERCFILE; } } done: ocglobalstate.rc.loaded = 1; /* even if not exists */ if(path != NULL) free(path); return stat; }
OCStackApplicationResult listenCallback(void* ctx, OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::ListenContext* context = static_cast<ClientCallbackContext::ListenContext*>(ctx); if(clientResponse->result != OC_STACK_OK) { /* oclog() << "listenCallback(): failed to create resource. clientResponse: " << clientResponse->result << std::flush;*/ return OC_STACK_KEEP_TRANSACTION; } if(!clientResponse->payload) { oclog() << "listenCallback(): clientResponse payload was null" << std::flush; return OC_STACK_KEEP_TRANSACTION; } if(clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY) { oclog() << "listenCallback(): clientResponse payload was the wrong type" << std::flush; oclog() << "Type is: %i" << clientResponse->payload->type << std::flush; return OC_STACK_KEEP_TRANSACTION; } auto clientWrapper = context->clientWrapper.lock(); if(!clientWrapper) { oclog() << "listenCallback(): failed to get a shared_ptr to the client wrapper" << std::flush; return OC_STACK_KEEP_TRANSACTION; } ListenOCContainer container(clientWrapper, clientResponse->devAddr, reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload)); // loop to ensure valid construction of all resources for(auto resource : container.Resources()) { std::thread exec(context->callback, resource); exec.detach(); } return OC_STACK_KEEP_TRANSACTION; }
static int basic_auth_handler(worker_st * ws, unsigned http_ver, const char *msg) { int ret; oclog(ws, LOG_HTTP_DEBUG, "HTTP sending: 401 Unauthorized"); cstp_cork(ws); ret = cstp_printf(ws, "HTTP/1.%u 401 Unauthorized\r\n", http_ver); if (ret < 0) return -1; if (ws->perm_config->auth_methods > 1) { ret = cstp_puts(ws, "X-HTTP-Auth-Support: fallback\r\n"); if (ret < 0) return -1; } if (msg == NULL) { oclog(ws, LOG_HTTP_DEBUG, "HTTP sending: WWW-Authenticate: Negotiate"); ret = cstp_puts(ws, "WWW-Authenticate: Negotiate\r\n"); } else { oclog(ws, LOG_HTTP_DEBUG, "HTTP sending: WWW-Authenticate: Negotiate %s", msg); ret = cstp_printf(ws, "WWW-Authenticate: Negotiate %s\r\n", msg); } if (ret < 0) return -1; ret = cstp_puts(ws, "Content-Length: 0\r\n"); if (ret < 0) { ret = -1; goto cleanup; } ret = cstp_puts(ws, "\r\n"); if (ret < 0) { ret = -1; goto cleanup; } ret = cstp_uncork(ws); if (ret < 0) { ret = -1; goto cleanup; } ret = 0; cleanup: return ret; }
OCStackApplicationResult listenErrorCallback(void* ctx, OCDoHandle /*handle*/, OCClientResponse* clientResponse) { if (!ctx || !clientResponse) { return OC_STACK_KEEP_TRANSACTION; } ClientCallbackContext::ListenErrorContext* context = static_cast<ClientCallbackContext::ListenErrorContext*>(ctx); if (!context) { return OC_STACK_KEEP_TRANSACTION; } OCStackResult result = clientResponse->result; if (result == OC_STACK_OK) { if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY) { oclog() << "listenCallback(): clientResponse payload was null or the wrong type" << std::flush; return OC_STACK_KEEP_TRANSACTION; } auto clientWrapper = context->clientWrapper.lock(); if (!clientWrapper) { oclog() << "listenCallback(): failed to get a shared_ptr to the client wrapper" << std::flush; return OC_STACK_KEEP_TRANSACTION; } ListenOCContainer container(clientWrapper, clientResponse->devAddr, reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload)); // loop to ensure valid construction of all resources for (auto resource : container.Resources()) { std::thread exec(context->callback, resource); exec.detach(); } return OC_STACK_KEEP_TRANSACTION; } std::string resourceURI = clientResponse->resourceUri; std::thread exec(context->errorCallback, resourceURI, result); exec.detach(); return OC_STACK_DELETE_TRANSACTION; }
/* Determine if this version of curl supports "file://..." &/or "https://..." urls. */ void oc_curl_protocols(struct OCGLOBALSTATE* state) { const char* const* proto; /*weird*/ curl_version_info_data* curldata; curldata = curl_version_info(CURLVERSION_NOW); for(proto=curldata->protocols;*proto;proto++) { if(strcmp("file",*proto)==0) {state->curl.proto_file=1;} if(strcmp("http",*proto)==0) {state->curl.proto_https=1;} } if(ocdebug > 0) { oclog(OCLOGNOTE,"Curl file:// support = %d",state->curl.proto_file); oclog(OCLOGNOTE,"Curl https:// support = %d",state->curl.proto_https); } }
OCStackResult OCSecureResource::getLinkedDevices(UuidList_t &uuidList) { OCStackResult result; size_t numOfDevices = -1; auto devUuid = devPtr->doxm->deviceID; auto cLock = m_csdkLock.lock(); if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr; result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices); if (result == OC_STACK_OK) { for (tmp = linkedDevs; tmp; tmp = tmp->next) { uuidList.push_back(tmp->dev); } OCDeleteUuidList(linkedDevs); } } else { oclog() <<"Mutex not found"; result = OC_STACK_ERROR; } return result; }
OCStackApplicationResult subscribePresenceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) { char stringAddress[DEV_ADDR_SIZE_MAX]; ostringstream os; uint16_t port; if(OCDevAddrToString(clientResponse->addr, stringAddress) == 0 && OCDevAddrToPort(clientResponse->addr, &port) == 0) { os<<stringAddress<<":"<<port; ClientCallbackContext::SubscribePresenceContext* context = static_cast<ClientCallbackContext::SubscribePresenceContext*>(ctx); std::thread exec(context->callback, clientResponse->result, clientResponse->sequenceNumber, os.str()); exec.detach(); } else { oclog() << "subscribePresenceCallback(): OCDevAddrToString() or OCDevAddrToPort() " <<"failed"<< std::flush; } return OC_STACK_KEEP_TRANSACTION; }
OCStackResult InProcClientWrapper::DoDirectPairing(std::shared_ptr<OCDirectPairing> peer, const OCPrm_t& pmSel, const std::string& pinNumber, DirectPairingCallback& callback) { if (!peer || !callback) { oclog() << "Invalid parameters" << std::flush; return OC_STACK_INVALID_PARAM; } OCStackResult result = OC_STACK_ERROR; ClientCallbackContext::DirectPairingContext* context = new ClientCallbackContext::DirectPairingContext(callback); auto cLock = m_csdkLock.lock(); if (cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCDoDirectPairing(static_cast<void*>(context), peer->getDev(), pmSel, const_cast<char*>(pinNumber.c_str()), directPairingCallback); } else { delete context; result = OC_STACK_ERROR; } return result; }
void OCSecureResource::callbackWrapper(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError) { PMResultList_t *results = nullptr; ProvisionContext* context = static_cast<ProvisionContext*>(ctx); try { results = new PMResultList_t; } catch (std::bad_alloc& e) { oclog() <<"Bad alloc exception"; return; } for (int i = 0; i < nOfRes; i++) { results->push_back(arr[i]); } std::thread exec(context->callback, results, hasError); exec.detach(); delete context; }
/* Completes the VPN device information. * * Returns 0 on success. */ int complete_vpn_info(worker_st * ws, struct vpn_st *vinfo) { int ret, fd; struct ifreq ifr; if (vinfo->ipv4 == NULL && vinfo->ipv6 == NULL) { return -1; } if (ws->config->network.mtu != 0) { vinfo->mtu = ws->config->network.mtu; } else { fd = socket(AF_INET, SOCK_STREAM, 0); if (fd == -1) return -1; memset(&ifr, 0, sizeof(ifr)); ifr.ifr_addr.sa_family = AF_INET; snprintf(ifr.ifr_name, IFNAMSIZ, "%s", vinfo->name); ret = ioctl(fd, SIOCGIFMTU, (caddr_t) & ifr); if (ret < 0) { oclog(ws, LOG_INFO, "cannot obtain MTU for %s. Assuming 1500", vinfo->name); vinfo->mtu = 1500; } else { vinfo->mtu = ifr.ifr_mtu; } close(fd); } return 0; }
OCStackApplicationResult listenMQCallback(void* ctx, OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::ListenContext* context = static_cast<ClientCallbackContext::ListenContext*>(ctx); if (!clientResponse) { return OC_STACK_KEEP_TRANSACTION; } if (clientResponse->result != OC_STACK_OK) { oclog() << "listenMQCallback(): failed to create resource. clientResponse: " << clientResponse->result << std::flush; return OC_STACK_KEEP_TRANSACTION; } auto clientWrapper = context->clientWrapper.lock(); if (!clientWrapper) { oclog() << "listenMQCallback(): failed to get a shared_ptr to the client wrapper" << std::flush; return OC_STACK_KEEP_TRANSACTION; } try{ ListenOCContainer container(clientWrapper, clientResponse->devAddr, (OCRepPayload *) clientResponse->payload); // loop to ensure valid construction of all resources for (auto resource : container.Resources()) { std::thread exec(context->callback, resource); exec.detach(); } } catch (std::exception &e){ oclog() << "Exception in listCallback, ignoring response: " << e.what() << std::flush; } return OC_STACK_KEEP_TRANSACTION; }
/* read and compile the rc file, if any */ OCerror ocrc_load(void) { OCerror stat = OC_NOERR; char* path = NULL; if(ocglobalstate.rc.ignore) { oclog(OCLOGDBG,"No runtime configuration file specified; continuing"); return OC_NOERR; } if(ocglobalstate.rc.loaded) return OC_NOERR; /* locate the configuration files: first if specified, then '.', then $HOME */ if(ocglobalstate.rc.rcfile != NULL) { /* always use this */ path = ocglobalstate.rc.rcfile; } else { char** rcname; int found = 0; for(rcname=rcfilenames;!found && *rcname;rcname++) { stat = rc_search(".",*rcname,&path); if(stat == OC_NOERR && path == NULL) /* try $HOME */ stat = rc_search(ocglobalstate.home,*rcname,&path); if(stat != OC_NOERR) goto done; if(path != NULL) found = 1; } } if(path == NULL) { oclog(OCLOGDBG,"Cannot find runtime configuration file; continuing"); } else { if(ocdebug > 0) fprintf(stderr, "RC file: %s\n", path); if(ocrc_compile(path) == 0) { oclog(OCLOGERR, "Error parsing %s\n",path); stat = OC_ERCFILE; } } done: ocglobalstate.rc.loaded = 1; /* even if not exists */ if(path != NULL) free(path); return stat; }
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) { size_t realsize = size * nmemb; OCbytes* buf = (OCbytes*) data; if(realsize == 0) oclog(OCLOGWARN,"WriteMemoryCallback: zero sized chunk"); /* Optimize for reading potentially large dods datasets */ if(!ocbytesavail(buf,realsize)) { /* double the size of the packet */ ocbytessetalloc(buf,2*ocbytesalloc(buf)); } ocbytesappendn(buf, ptr, realsize); #ifdef OCPROGRESS oclog(OCLOGNOTE,"callback: %lu bytes",(off_t)realsize); #endif return realsize; }
InProcServerWrapper::~InProcServerWrapper() { try { stop(); } catch (InitializeException &e) { oclog() << "Exception in stop"<< e.what() << std::flush; } }
static size_t WriteFileCallback(void* ptr, size_t size, size_t nmemb, void* data) { size_t realsize = size * nmemb; size_t count; struct Fetchdata* fetchdata; fetchdata = (struct Fetchdata*) data; if(realsize == 0) oclog(OCLOGWARN,"WriteFileCallback: zero sized chunk"); count = fwrite(ptr, size, nmemb, fetchdata->stream); if (count > 0) { fetchdata->size += (count * size); } else { oclog(OCLOGWARN,"WriteFileCallback: zero sized write"); } #ifdef OCPROGRESS oclog(OCLOGNOTE,"callback: %lu bytes",(off_t)realsize); #endif return count; }
/* sends a cookie authentication request to main thread and waits for * a reply. * Returns 0 on success. */ int auth_cookie(worker_st * ws, void *cookie, size_t cookie_size) { int ret; AuthCookieRequestMsg msg = AUTH_COOKIE_REQUEST_MSG__INIT; if ((ws->selected_auth->type & AUTH_TYPE_CERTIFICATE) && ws->config->cisco_client_compat == 0) { if (ws->cert_auth_ok == 0) { oclog(ws, LOG_INFO, "no certificate provided for cookie authentication"); return -1; } else { ret = get_cert_info(ws); if (ret < 0) { oclog(ws, LOG_INFO, "cannot obtain certificate info"); return -1; } } } msg.cookie.data = cookie; msg.cookie.len = cookie_size; ret = send_msg_to_main(ws, AUTH_COOKIE_REQ, &msg, (pack_size_func) auth_cookie_request_msg__get_packed_size, (pack_func) auth_cookie_request_msg__pack); if (ret < 0) { oclog(ws, LOG_INFO, "error sending cookie authentication request"); return ret; } ret = recv_cookie_auth_reply(ws); if (ret < 0) { oclog(ws, LOG_INFO, "error receiving cookie authentication reply"); return ret; } return 0; }