void ServerRequest(obex_object_t* object, int event, int cmd) { switch (cmd) { case OBEX_CMD_SETPATH: printf("Received SETPATH command\n"); OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); break; case OBEX_CMD_PUT: OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); PutDone(object); break; case OBEX_CMD_CONNECT: OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS); break; case OBEX_CMD_DISCONNECT: OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS); break; default: /* printf(FUNCTION "() Denied %02x request\n", cmd); */ printf(" Denied %02x request\n", cmd); OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, OBEX_RSP_NOT_IMPLEMENTED); break; } return; }
static int obexserver_setresponse(OBEXServer *self, obex_object_t *obj, int responsecode, PyObject *responseheaders) { DEBUG("%s()\n", __func__); if (responseheaders != NULL) { if (lightblueobex_addheaders(self->obex, responseheaders, obj) < 0) { obexserver_errorstr(self, PyExc_IOError, "error setting response headers"); OBEX_ObjectSetRsp(obj, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR); return -1; } } if (responsecode == OBEX_RSP_SUCCESS || responsecode == OBEX_RSP_CONTINUE) { DEBUG("\tAccepting request...\n"); OBEX_ObjectSetRsp(obj, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); } else { DEBUG("\tRefusing request (%s)...\n", OBEX_ResponseToString(responsecode)); OBEX_ObjectSetRsp(obj, responsecode, responsecode); } return 1; }
void obexsrv_req(obex_t *handle, obex_object_t *object, int cmd) { switch(cmd) { case OBEX_CMD_CONNECT: obexsrv_connect(handle, object); break; case OBEX_CMD_DISCONNECT: DBPRT("We got a disconnect-request"); OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS); break; case OBEX_CMD_GET: obexsrv_get(handle, object); break; case OBEX_CMD_PUT: obexsrv_put(handle, object); break; case OBEX_CMD_SETPATH: obexsrv_setpath(handle, object); break; default: BTERROR(" Denied %02x request", cmd); OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, OBEX_RSP_NOT_IMPLEMENTED); break; } return; }
void obexsrv_connect(obex_t *handle, obex_object_t *object) { obex_headerdata_t hv; uint8_t hi; int hlen; uint8_t *nonhdrdata; obex_target_t target = {0, NULL}; obexsrv_t *srv = OBEX_GetUserData(handle); int err; DBPRT(""); if(OBEX_ObjectGetNonHdrData(object, &nonhdrdata) == 4) { #ifdef CONFIG_AFFIX_DEBUG obex_connect_hdr_t *hdr = (obex_connect_hdr_t*)nonhdrdata; DBPRT("Version: 0x%02x. Flags: 0x%02x OBEX packet length:%d", hdr->version, hdr->flags, ntohs(hdr->mtu)); #endif } else { BTERROR("Invalid packet content."); } while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) { switch (hi) { case OBEX_HDR_TARGET: target.data = (void*)hv.bs; target.len = hlen; if (hlen == 16) DBPRT("got TARGET. uuid_t: %08X-%04X-%04X-%04X-%08X%04X", *(uint32_t *)&target.data[0], *(uint16_t *)&target.data[4], *(uint16_t *)&target.data[6], *(uint16_t *)&target.data[8], *(uint32_t *)&target.data[10], *(uint16_t *)&target.data[14]); else DBPRT("got TARGET. unknown fmt"); break; default: DBPRT(" Skipped header %02x", hi); break; } } // call handler err = srv->connect(srv, &target); if (err < 0) { /* error */ OBEX_ObjectSetRsp(object, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR); } else { OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS); if (target.data) { hv.bq4 = err; /* set connection id */ OBEX_ObjectAddHeader(handle, object, OBEX_HDR_CONNECTION, hv, 4, 0); hv.bs = target.data; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_WHO, hv, target.len, 0); } } }
void ObexEvent(obex_t* handle, obex_object_t *object, int mode, int event, int obexcmd, int obexrsp) { switch (event) { case OBEX_EV_PROGRESS: printf("."); break; case OBEX_EV_REQDONE: printf("\n"); if (mode == OBEX_MODE_CLIENT) SetClientDone(object, obexcmd, obexrsp); else SetSrvDone(object, obexcmd); break; case OBEX_EV_REQHINT: /* Comes BEFORE the lib parses anything. */ switch (obexcmd) { case OBEX_CMD_PUT: case OBEX_CMD_CONNECT: case OBEX_CMD_DISCONNECT: OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); break; default: OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, OBEX_RSP_NOT_IMPLEMENTED); break; } break; case OBEX_EV_REQ: ServerRequest(object, event, obexcmd); break; case OBEX_EV_LINKERR: printf("Link broken (this does not have to be an error)!\n"); if (mode == OBEX_MODE_CLIENT) sendingFinished = 1; else receivingFinished = 1; break; default: printf("Unknown event...\n"); break; } }
void obexsrv_reqhint(obex_t *handle, obex_object_t *object, int cmd) { DBPRT("reqhint: %#x\n", cmd); switch(cmd) { case OBEX_CMD_PUT: OBEX_ObjectReadStream(handle, object, NULL); break; default: break; } /* accept all */ OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); }
static void obexserver_streamempty(OBEXServer *self, obex_object_t *obj) { DEBUG("%s()\n", __func__); Py_XDECREF(self->tempbuf); self->tempbuf = lightblueobex_filetostream(self->obex, obj, self->fileobj, self->sendbufsize); if (self->tempbuf == NULL) { obexserver_errorstr(self, PyExc_IOError, "error reading file object"); OBEX_ObjectSetRsp(obj, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR); } }
static void obexserver_incomingrequest(OBEXServer *self, obex_object_t *obj, int obex_cmd) { self->notifiednewrequest = 0; self->hasbodydata = 0; Py_XDECREF(self->tempbuf); Py_XDECREF(self->fileobj); // signal we want to stream body data if (obex_cmd == OBEX_CMD_PUT) { if (OBEX_ObjectReadStream(self->obex, obj, NULL) < 0) { DEBUG("\tUnable to stream body data\n"); OBEX_ObjectSetRsp(obj, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR); return; } } OBEX_ObjectSetRsp(obj, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); #ifdef LIGHTBLUEOBEX_SERVER_TEST self->puttotal = 0; #endif }
static void connect_server(obex_t *handle, obex_object_t *object) { obex_headerdata_t hv; uint8_t hi; uint32_t hlen; uint8_t *target = NULL; int target_len = 0; //printf("%s()\n", __FUNCTION__); while (OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) { switch(hi) { case OBEX_HDR_TARGET: if (0 < hlen) { if( (target = malloc(hlen))) { target_len = hlen; memcpy(target,hv.bs,target_len); } } break; default: printf("%s() Skipped header %02x\n", __FUNCTION__, hi); break; } } OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS); hv.bq4 = connection_id; if(OBEX_ObjectAddHeader(handle, object, OBEX_HDR_CONNECTION, hv, sizeof(hv.bq4), OBEX_FL_FIT_ONE_PACKET) < 0 ) { fprintf(stderr, "Error adding header CONNECTION\n"); OBEX_ObjectDelete(handle, object); return; } if (target && target_len) { hv.bs = target; if(OBEX_ObjectAddHeader(handle,object,OBEX_HDR_WHO, hv,target_len,OBEX_FL_FIT_ONE_PACKET) < 0 ) { fprintf(stderr, "Error adding header WHO\n"); OBEX_ObjectDelete(handle, object); return; } free(target); } }
static void obex_event(obex_t *handle, obex_object_t *object, int mode, int event, int command, int response) { switch (event) { case OBEX_EV_PROGRESS: //printf("Evt: Progress\n"); break; case OBEX_EV_ABORT: printf("Evt: Abort\n"); break; case OBEX_EV_REQDONE: //printf("Evt: Request done\n"); obex_request_done(handle, object, command, response); break; case OBEX_EV_REQHINT: printf("Evt: Request hint\n"); /* Accept any command. Not really good :) */ OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); break; case OBEX_EV_REQ: printf("Evt: Request\n"); break; case OBEX_EV_LINKERR: printf("Evt: Link error\n"); OBEX_TransportDisconnect(handle); break; case OBEX_EV_PARSEERR: printf("Evt: Parse error\n"); OBEX_TransportDisconnect(handle); break; case OBEX_EV_STREAMEMPTY: printf("Evt: Stream empty\n"); break; default: printf("Evt: Unknown event %02x\n", event); break; } }
static void obexserver_streamavailable(OBEXServer *self, obex_object_t *obj) { DEBUG("%s()\n", __func__); /* if got OBEX_EV_STREAMAVAIL, it means the request contains body data (and therefore is not a Put-Delete) */ self->hasbodydata = 1; if (!self->notifiednewrequest) { PyObject *respheaders; int respcode; respheaders = obexserver_notifynewrequest(self, obj, OBEX_CMD_PUT, &respcode); if (respheaders == NULL) { obexserver_setresponse(self, obj, OBEX_RSP_INTERNAL_SERVER_ERROR, NULL); return; } obexserver_setresponse(self, obj, respcode, respheaders); Py_DECREF(respheaders); if (respcode != OBEX_RSP_CONTINUE && respcode != OBEX_RSP_SUCCESS) return; } if (self->fileobj == NULL) { obexserver_errorstr(self, PyExc_IOError, "file object is null"); return; } int result; result = lightblueobex_streamtofile(self->obex, obj, self->fileobj); if (result < 0) { obexserver_errorstr(self, PyExc_IOError, "error reading body data or writing to file object"); OBEX_ObjectSetRsp(obj, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR); } #ifdef LIGHTBLUEOBEX_SERVER_TEST if (result > 0) self->puttotal += result; #endif }
// // Called by the obex-layer when some event occurs. // void obex_event(obex_t *handle, obex_object_t *object, int mode, int event, int obex_cmd, int obex_rsp) { switch (event) { case OBEX_EV_PROGRESS: printf("Made some progress...\n"); break; case OBEX_EV_ABORT: printf("Request aborted!\n"); break; case OBEX_EV_REQDONE: if(mode == OBEX_MODE_CLIENT) { client_done(handle, object, obex_cmd, obex_rsp); } else { server_done(handle, object, obex_cmd, obex_rsp); } break; case OBEX_EV_REQHINT: /* Accept any command. Not rellay good, but this is a test-program :) */ OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); break; case OBEX_EV_REQ: server_request(handle, object, event, obex_cmd); break; case OBEX_EV_LINKERR: OBEX_TransportDisconnect(handle); printf("Link broken!\n"); break; case OBEX_EV_STREAMEMPTY: fillstream(handle, object); break; default: printf("Unknown event %02x!\n", event); break; } }
static void get_server(obex_t *handle, obex_object_t *object) { uint8_t *buf = NULL; obex_headerdata_t hv; uint8_t hi; uint32_t hlen; int file_size; char *name = NULL; char *type = NULL; printf("%s()\n", __FUNCTION__); while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) { switch(hi) { case OBEX_HDR_NAME: printf("%s() Found name\n", __FUNCTION__); if( (name = malloc(hlen / 2))) { OBEX_UnicodeToChar((uint8_t*)name, hv.bs, hlen); printf("name:%s\n", name); } break; case OBEX_HDR_TYPE: if( (type = malloc(hlen + 1))) { strcpy(type, (char *)hv.bs); } printf("%s() type:%s\n", __FUNCTION__, type); case 0xbe: // user-defined inverse push printf("%s() Found inverse push req\n", __FUNCTION__); printf("data:%02x\n", hv.bq1); break; case OBEX_HDR_APPARAM: printf("%s() Found apparam\n", __FUNCTION__); printf("name:%d (%02x %02x ...)\n", hlen, *hv.bs, *(hv.bs+1)); break; default: printf("%s() Skipped header %02x\n", __FUNCTION__, hi); } } //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); if (is_type_fl(type)) { struct dirent *dirp; DIR *dp; struct stat statdir; struct stat statbuf; char *filename; struct rawdata_stream *xmldata; xmldata = INIT_RAWDATA_STREAM(512); if (NULL == xmldata) goto out; FL_XML_VERSION(xmldata); FL_XML_TYPE(xmldata); FL_XML_BODY_BEGIN(xmldata); stat(CUR_DIR, &statdir); dp = opendir(CUR_DIR); while(NULL != dp && NULL != (dirp = readdir(dp))) { if (0 == strcmp(dirp->d_name, ".") || 0 == strcmp(dirp->d_name, "..")) continue; FL_XML_BODY_ITEM_BEGIN(xmldata); //Adding 1 bytes due to containing '\0' filename = malloc(strlen(CUR_DIR) + strlen(dirp->d_name) + 1); strcpy(filename, CUR_DIR); strcat(filename, dirp->d_name); lstat(filename, &statbuf); if (0 == S_ISDIR(statbuf.st_mode)) //it is a file FL_XML_BODY_FILENAME(xmldata, dirp->d_name); else //it is a directory FL_XML_BODY_FOLDERNAME(xmldata, dirp->d_name); FL_XML_BODY_SIZE(xmldata, statbuf.st_size); FL_XML_BODY_PERM(xmldata, statbuf.st_mode, statdir.st_mode); FL_XML_BODY_MTIME(xmldata, statbuf.st_mtime); FL_XML_BODY_CTIME(xmldata, statbuf.st_ctime); FL_XML_BODY_ATIME(xmldata, statbuf.st_atime); FL_XML_BODY_ITEM_END(xmldata); //printf("---filename:%s\n", filename); //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); free(filename); filename = NULL; //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); } //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); FL_XML_BODY_END(xmldata); closedir(dp); printf("xml doc:%s\n", xmldata->data); //composite the obex obejct OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); hv.bs = (uint8_t *)xmldata->data; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY, hv, xmldata->size, 0); hv.bq4 = xmldata->size; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hv, sizeof(uint32_t), 0); //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); FREE_RAWDATA_STREAM(xmldata); //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); } else if (name) { printf("%s() Got a request for %s\n", __FUNCTION__, name); buf = easy_readfile(name, &file_size); if(buf == NULL) { printf("Can't find file %s\n", name); OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_FOUND, OBEX_RSP_NOT_FOUND); return; } OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); hv.bs = buf; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY, hv, file_size, 0); hv.bq4 = file_size; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hv, sizeof(uint32_t), 0); } else { printf("%s() Got a GET without a name-header!\n", __FUNCTION__); OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_FOUND, OBEX_RSP_NOT_FOUND); return; } //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); if (NULL != buf) { free(buf); } //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); out: if (NULL != name) { free(name); } //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); if (NULL != type) { free(type); } //fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__); return; }
static void obex_event(obex_t *handle, obex_object_t *object, int mode, int event, int command, int response) { obex_context_t *context = OBEX_GetUserData(handle); switch (event) { case OBEX_EV_PROGRESS: obex_progress(handle, object); break; case OBEX_EV_REQHINT: OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, response); break; case OBEX_EV_REQ: OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_IMPLEMENTED, response); break; case OBEX_EV_REQDONE: debug("OBEX_EV_REQDONE"); context->obex_rsp = response; queue_event(context, OBEX_EV_REQDONE); if (context->pending && OBEX_Request(handle, context->pending) == 0) { if (OBEX_ObjectGetCommand(handle, context->pending) == OBEX_CMD_PUT) queue_event(context, OBEX_EV_STREAMEMPTY); context->pending = NULL; } switch (command) { case OBEX_CMD_CONNECT: obex_connect_done(handle, object, response); break; case OBEX_CMD_DISCONNECT: obex_disconnect_done(handle, object, response); break; case OBEX_CMD_PUT: case OBEX_CMD_GET: break; case OBEX_CMD_SETPATH: break; case OBEX_CMD_SESSION: break; case OBEX_CMD_ABORT: break; } break; case OBEX_EV_LINKERR: OBEX_TransportDisconnect(handle); break; case OBEX_EV_PARSEERR: OBEX_TransportDisconnect(handle); break; case OBEX_EV_ACCEPTHINT: break; case OBEX_EV_ABORT: queue_event(context, OBEX_EV_ABORT); break; case OBEX_EV_STREAMEMPTY: debug("OBEX_EV_STREAMEMPTY"); obex_writestream(handle, object); break; case OBEX_EV_STREAMAVAIL: debug("OBEX_EV_STREAMAVAIL"); obex_readstream(handle, object); break; case OBEX_EV_UNEXPECTED: break; case OBEX_EV_REQCHECK: break; } }
void obexsrv_put(obex_t *handle, obex_object_t *object) { obex_headerdata_t hv; uint8_t hi; int hlen; const uint8_t *body = NULL; int body_len = 0; char *name = NULL; int endofbody = 0; int len, con_id = -1; char *obj_type = NULL; obexsrv_t *srv = OBEX_GetUserData(handle); int flags = 0, err; DBPRT(""); /* body received. close it */ if (srv->sfd >= 0) { close(srv->sfd); srv->sfd = -1; } while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) { switch (hi) { case OBEX_HDR_CONNECTION: con_id = hv.bq4; DBPRT("Found connection id: %08x", con_id); break; case OBEX_HDR_LENGTH: len = hv.bq4; DBPRT("Found Length: %d, hlen: %d", len, hlen); break; case OBEX_HDR_NAME: if ((name = malloc(hlen/2))) { OBEX_UnicodeToChar(name, hv.bs, hlen); } DBPRT("Found name: %s", name); break; case OBEX_HDR_TYPE: obj_type = (char *)hv.bs; DBPRT("Found type:%*s, hlen: %d", hlen, obj_type, hlen); break; case OBEX_HDR_BODY: DBPRT("Found body"); body = hv.bs; body_len = hlen; break; case OBEX_HDR_BODY_END: DBPRT("Found end of body"); endofbody = 1; break; default: DBPRT("Skipped header %02x", hi); break; } } if (!body) { if (endofbody) { DBPRT("Got a PUT with only end of body header->create empty object."); flags |= 0x01; // empty object } else { DBPRT("Got a PUT without a body?????"); if (!srv->streamming) { DBPRT("Got a PUT without a body->delete"); flags |= 0x02; // delete } } } if (!name) { if ((flags & 0x03)) {// empty or delete BTERROR("name is missing."); OBEX_ObjectSetRsp(object, OBEX_RSP_BAD_REQUEST, OBEX_RSP_BAD_REQUEST); goto error; } } // call handler err = srv->put(srv, srv->name, name, obj_type, flags); if (err < 0) { if (errno == ENOENT) OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_FOUND, OBEX_RSP_NOT_FOUND); else OBEX_ObjectSetRsp(object, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR); if (srv->name) unlink(srv->name); goto error; } OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); error: if (srv->name) { free(srv->name); srv->name = NULL; } if (name) free(name); }
void obexsrv_get(obex_t *handle, obex_object_t *object) { obex_headerdata_t hv; uint8_t hi; int hlen; char *obj_type = NULL; char *name = NULL; int con_id = -1; obexsrv_t *srv = OBEX_GetUserData(handle); int err; DBPRT(""); while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) { switch(hi) { case OBEX_HDR_NAME: if ((name = malloc(hlen/2))) { OBEX_UnicodeToChar(name, hv.bs, hlen); } DBPRT(" Found name: %s", name); break; case OBEX_HDR_TYPE: obj_type = (char *)hv.bs; DBPRT(" Found type:%-30s", obj_type); break; case OBEX_HDR_CONNECTION: con_id = hv.bq4; DBPRT(" Found connection id: %08x", con_id); break; default: DBPRT(" Skipped header %02x", hi); break; } } /* call handler */ err = srv->get(srv, name, obj_type); if (err < 0 || !srv->name) { DBPRT("File not found: %s\n", name); OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_FOUND, OBEX_RSP_NOT_FOUND); return; } srv->sfd = open(srv->name, O_RDONLY); if (srv->sfd < 0) { OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_FOUND, OBEX_RSP_NOT_FOUND); return; } srv->buf = malloc(OBEX_STREAM_CHUNK); if (!srv->buf) { OBEX_ObjectSetRsp(object, OBEX_RSP_INTERNAL_SERVER_ERROR, OBEX_RSP_INTERNAL_SERVER_ERROR); return; } OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); hv.bq4 = get_fdsize(srv->sfd); OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hv, sizeof(uint32_t), 0); hv.bs = NULL; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY, hv, 0, OBEX_FL_STREAM_START); if (name) free(name); return; }
void obexsrv_setpath(obex_t *handle, obex_object_t *object) { obex_headerdata_t hv; uint8_t hi; int hlen; char *name = NULL; int con_id = -1; uint8_t *nonhdrdata; int flags; int len; obexsrv_t *srv = OBEX_GetUserData(handle); int err; DBPRT(""); if ((len = OBEX_ObjectGetNonHdrData(object, &nonhdrdata)) == 2) { obex_setpath_hdr_t *hdr = (obex_setpath_hdr_t*)nonhdrdata; flags = hdr->flags; DBPRT("Flags= 0x%02x Constants=%d", flags, hdr->constants); } else { BTERROR("Invalid packet content. len=%d", len); flags = 0; } while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) { switch(hi) { case OBEX_HDR_NAME: if (hlen) { if ((name = malloc(hlen/2))) OBEX_UnicodeToChar(name, hv.bs, hlen); } else flags |= 0x80; // name exits but zero -> set root DBPRT("Found name: %s", name); break; case OBEX_HDR_CONNECTION: con_id = hv.bq4; DBPRT("Found connection id: %08x", con_id); break; default: DBPRT("Skipped header %02x", hi); break; } } // call handler err = srv->setpath(srv, name, flags); if (err) goto error; OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); if (name) free(name); return; error: OBEX_ObjectSetRsp(object, OBEX_RSP_NOT_FOUND, OBEX_RSP_NOT_FOUND); if (name) free(name); return; }
static void set_server_path(obex_t *handle, obex_object_t *object) { char *name = NULL; char fullname[WORK_PATH_MAX]; // "Backup Level" and "Don't Create" flag in first byte uint8_t setpath_nohdr_dummy = 0; uint8_t *setpath_nohdr_data; obex_headerdata_t hv; uint8_t hi; uint32_t hlen; OBEX_ObjectGetNonHdrData(object, &setpath_nohdr_data); if (NULL == setpath_nohdr_data) { setpath_nohdr_data = &setpath_nohdr_dummy; printf("nohdr data not found\n"); } printf("nohdr data: %x\n", *setpath_nohdr_data); while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) { switch(hi) { case OBEX_HDR_NAME: printf("%s() Found name\n", __FUNCTION__); if (0 < hlen) { if( (name = malloc(hlen / 2))) { OBEX_UnicodeToChar((uint8_t*)name, hv.bs, hlen); printf("name:%s\n", name); } } else { if (verbose) printf("set path to root\n"); chdir(init_work_path); } break; default: printf("%s() Skipped header %02x\n", __FUNCTION__, hi); } } if (name) { if (strstr(name, "/../")) { OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_FORBIDDEN); } else { strcpy(fullname, CUR_DIR); strncat(fullname, name, sizeof(fullname)-1); if ((*setpath_nohdr_data & 2) == 0) { if (verbose) printf("mkdir %s\n", name); if (mkdir(fullname, 0755) < 0) { perror("requested mkdir failed"); } } if (verbose) printf("Set path to %s\n",fullname); if (chdir(fullname) < 0) { perror("requested chdir failed\n"); OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_FORBIDDEN); } } free(name); name = NULL; } }