Ejemplo n.º 1
0
void
PutDone(obex_object_t* object)
{
    obex_headerdata_t hv;
    uint8_t hi;
    int hlen;
    const uint8_t *body = NULL;
    int bodylen = 0;
    char* name = NULL;
    char* namebuf = NULL;
    int nooverwrite = 0;

    while (OBEX_ObjectGetNextHeader(handleRcv, object, &hi, &hv, (uint32_t*)&hlen)) {
        switch (hi) {
        case OBEX_HDR_BODY:
            body = hv.bs;
            bodylen = hlen;
            break;
        case OBEX_HDR_NAME:
            if ( (namebuf = malloc(hlen / 2))) {
                /* OBEX_UnicodeToChar((uint8_t*)namebuf, hv.bs, hlen);*/
                strcpy(namebuf, hv.bs);
                name = namebuf;
            }
            break;

        case OBEX_HDR_LENGTH:
            printf("HEADERLENGTH = %d\n", hv.bq4);
            break;

        case HEADER_CREATOR_ID:
            printf("CREATORID = %#x\n", hv.bq4);
            break;

        default:
            /*printf(FUNCTION "() Skipped header %02x\n", hi);*/
            printf(" Skipped header %02x\n", hi);
        }
    }

    nooverwrite = TargetExist(name);
    if (nooverwrite == 0) {
        printf("A file named %s already exists in %s - I will not overwrite it.", name, rcvDir);
    } else {
        if (!body) {
            printf("Got a PUT without a body\n");
            return;
        }
        if (!name) {
            printf("Got a PUT without a name. Setting name to %s\n", name);
            name = "OBEX PUT Unknown object";
        }
        SaveSafely(name, body, bodylen);
    }
    free(namebuf);
}
Ejemplo n.º 2
0
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);
		}
	}
}
Ejemplo n.º 3
0
static void obex_requestdone (struct obex_state *state, obex_t *hdl,
                              obex_object_t *obj, int cmd, int resp)
{
	uint8_t header_id;
	obex_headerdata_t hdata;
	uint32_t hlen;

	switch (cmd & ~OBEX_FINAL) {
		case OBEX_CMD_CONNECT:
			while (OBEX_ObjectGetNextHeader(hdl, obj, &header_id,
							&hdata, &hlen)) {
				if (header_id == OBEX_HDR_CONNECTION) {
					state->got_connid=1;
					state->connid = hdata.bq4;
					printf("Connection ID: %d\n",
					       state->connid);
				}
			}
			break;

		case OBEX_CMD_GET:
			while (OBEX_ObjectGetNextHeader(hdl, obj, &header_id,
							&hdata, &hlen)) {
				if (header_id == OBEX_HDR_BODY ||
				    header_id == OBEX_HDR_BODY_END) {
					if (state->body)
						free(state->body);
					state->body = malloc(hlen+1);
					state->body_len = hlen;
					memcpy(state->body, hdata.bs, hlen);
					break;
				}
			}
			break;
	}

	state->req_done++;
}
Ejemplo n.º 4
0
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);
	} 
}
Ejemplo n.º 5
0
static void obex_connect_done(obex_t *handle,
					obex_object_t *object, int response)
{
	obex_context_t *context = OBEX_GetUserData(handle);
        obex_headerdata_t hd;
        uint32_t hl;
        uint8_t hi, *ptr;

	if (response != OBEX_RSP_SUCCESS)
		return;

	context->state = OBEX_CONNECTED;

	if (OBEX_ObjectGetNonHdrData(object, &ptr) == sizeof(obex_connect_hdr_t)) {
		obex_connect_hdr_t *chdr = (obex_connect_hdr_t *) ptr;
		uint16_t mtu = ntohs(chdr->mtu);
		int new_size;
		
		debug("Connect success. Version: 0x%02x. Flags: 0x%02x OBEX packet length: %d",
				chdr->version, chdr->flags, mtu);

		/* Leave space for headers */
		new_size = mtu - 200;
		if (new_size < context->tx_max) {
			debug("Resizing stream chunks to %d", new_size);
			context->tx_max = new_size;
		}
	}

	while (OBEX_ObjectGetNextHeader(handle, object, &hi, &hd, &hl)) {
		switch (hi) {
		case OBEX_HDR_CONNECTION:
			context->cid = hd.bq4;
			break;
		case OBEX_HDR_WHO:
			break;
		}
	}

	if (context->callback && context->callback->connect_cfm)
		context->callback->connect_cfm(handle, context->user_data);
}
Ejemplo n.º 6
0
static void get_target_size_and_time(obex_t *handle, obex_object_t *object,
					obex_context_t *context) {
    obex_headerdata_t hv;
    uint8_t hi;
    unsigned int hlen;

    context->target_size = -1;
    context->modtime = -1;

    while (OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) {
        switch (hi) {
            case OBEX_HDR_LENGTH:
                context->target_size = hv.bq4;
                break;
            case OBEX_HDR_TIME:
                context->modtime = parse_iso8601((char *) hv.bs, hlen);
                break;
            default:
                break;
        }
    }

    OBEX_ObjectReParseHeaders(handle, object);
}
Ejemplo n.º 7
0
/*!
    \internal
    Extracts all the headers from \a obj and inserts them into \a header.

    Remember you have to call OBEX_ObjectReParseHeaders() on \a obj if you
    want to read the headers again after calling this function.

    Returns whether the headers were read successfully.
 */
bool QObexHeaderPrivate::readOpenObexHeaders(QObexHeader &header, obex_t* handle, obex_object_t *obj)
{
    if (!handle || !obj)
        return false;

    uchar hi;
    obex_headerdata_t hv;
    unsigned int hv_size;

    while (OBEX_ObjectGetNextHeader(handle, obj, &hi, &hv, &hv_size))   {
        /*
        For each case, check whether the value is empty (e.g. name might
        be empty) otherwise it'll blow up...
        */

        // Special treatment for reading null-terminated byte stream
        if (hi == OBEX_HDR_TYPE) {
            if (hv_size == 0) {
                header.setValue(hi, QLatin1String("")); // empty, not null
            } else {
                // Remove the null terminator from the received string; otherwise,
                // if the string is compared to one without a null terminator
                // it will fail.
                if (hv_size > 0 && (hv.bs[hv_size - 1] == '\0')) {
                    // Null terminated, crop the string before the null
                    header.setValue(hi, QString::fromAscii(
                            reinterpret_cast<const char *>(hv.bs), hv_size-1));
                } else {
                    // Not null terminated
                    header.setValue(hi, QString::fromAscii(
                            reinterpret_cast<const char *>(hv.bs), hv_size));
                }
            }
            // skip to next header!
            continue;
        }

        switch (hi & QObexHeaderPrivate::HeaderEncodingMask) {
            case QObexHeaderPrivate::HeaderUnicodeEncoding:
                if (hv_size == 0) {
                    header.setValue(hi, QString("")); // empty, not null
                } else {
                    QString value;
                    QObexHeaderPrivate::stringFromUnicodeBytes(value, hv.bs, hv_size);
                    header.setValue(hi, value);
                }
                break;

            case QObexHeaderPrivate::HeaderByteSequenceEncoding:
                if (hv_size == 0) {
                    header.setValue(hi, QByteArray("")); // empty, not null
                } else {
                    header.setValue(hi, QByteArray(
                            reinterpret_cast<const char *>(hv.bs), hv_size));
                }
                break;

            case QObexHeaderPrivate::HeaderByteEncoding:
                // Must use qVariantFromValue() or else the value may be
                // added as zero in the variant.
                header.setValue(hi,
                                qVariantFromValue(static_cast<quint8>(hv.bq1)));
                break;

            case QObexHeaderPrivate::HeaderIntEncoding:
                header.setValue(hi,
                                qVariantFromValue(static_cast<quint32>(hv.bq4)));
                break;

            default:
                return false;
        }
    }

    return true;
}
Ejemplo n.º 8
0
/**
	Save body from object or return application parameters.
 */
static void client_done(obex_t *handle, obex_object_t *object, int UNUSED(obex_cmd), int UNUSED(obex_rsp))
{
	obex_headerdata_t hv;
	uint8_t hi;
	uint32_t hlen;
	const apparam_t *app = NULL;
	uint8_t *p;

        const uint8_t *body_data = NULL;
	uint32_t body_len = -1;

	/*@temp@*/ obexftp_client_t *cli;

	cli = OBEX_GetUserData(handle);

	DEBUG(3, "%s()\n", __func__);

	if (cli->fd > 0)
		(void) close(cli->fd);
	if (cli->buf_data) {
		DEBUG(1, "%s: Warning: buffer still active?\n", __func__);
	}

	while(OBEX_ObjectGetNextHeader(handle, object, &hi, &hv, &hlen)) {
		if(hi == OBEX_HDR_BODY) {
			DEBUG(3, "%s() Found body (length: %d)\n", __func__, hlen);
			if (cli->target_fn == NULL) {
				if (cli->buf_data) {
					DEBUG(1, "%s: Warning: purging non-empty buffer.\n", __func__);
					/* ok to free since we must have malloc' it right here */
					free(cli->buf_data);
				}
				p = malloc(hlen + 1);
				if (p) {
					memcpy(p, hv.bs, hlen);
					p[hlen] = '\0';
					cli->buf_size = hlen;
					cli->buf_data = p;
				}
			}
			body_len = hlen;
			body_data = hv.bs;
			cli->infocb(OBEXFTP_EV_BODY, hv.bs, hlen, cli->infocb_data);
			DEBUG(3, "%s() Done body\n", __func__);
                        /* break; */
                }
                else if(hi == OBEX_HDR_CONNECTION) {
			DEBUG(3, "%s() Found connection number: %d\n", __func__, hv.bq4);
			cli->connection_id = hv.bq4;
		}
                else if(hi == OBEX_HDR_WHO) {
			DEBUG(3, "%s() Sender identified\n", __func__);
		}
                else if(hi == OBEX_HDR_NAME) {
			DEBUG(3, "%s() Sender name\n", __func__);
			DEBUGBUFFER(hv.bs, hlen);
		}
                else if(hi == OBEX_HDR_APPARAM) {
			DEBUG(3, "%s() Found application parameters\n", __func__);
                        if(hlen == sizeof(apparam_t)) {
				app = (const apparam_t *)hv.bs;
				/* order is network byte order (big-endian) */
				cli->apparam_info = (app->info[0] << (3*8)) + (app->info[1] << (2*8)) +
				                    (app->info[2] << (1*8)) + (app->info[3] << (0*8));
				cli->infocb(OBEXFTP_EV_INFO, (char*)&cli->apparam_info, 0, cli->infocb_data);
			}
			else
				DEBUG(3, "%s() Application parameters don't fit %d vs. %d.\n", __func__, hlen, sizeof(apparam_t));
                        break;
                }
                else    {
                        DEBUG(3, "%s() Skipped header %02x\n", __func__, hi);
                }
        }

        if(body_data) {
		if (body_len > 0) {
			if (cli->target_fn != NULL) {
				/* simple body writer */
				int fd;
				//fd = open_safe("", cli-> target_fn);
				fd = open(cli-> target_fn, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, CREATE_MODE_FILE);
				if(fd > 0) {
					(void) write(fd, body_data, body_len);
					(void) close(fd);
				} else {
					DEBUG(3, "%s() Error writing body\n", __func__);
				}
				free (cli->target_fn);
				cli->target_fn = NULL;
			} else {
				DEBUG(3, "%s() Body not written\n", __func__);
			}
		} else {
			DEBUG(3, "%s() Skipping empty body\n", __func__);
		}
        }
        if(app) {
		DEBUG(3, "%s() Appcode %d, data (%d) %d\n", __func__,
			app->code, app->info_len, cli->apparam_info);

        }
}
Ejemplo n.º 9
0
static void obex_request_done(obex_t *handle, obex_object_t *object, int command, int response)
{
	struct obex_context *context;

	obex_headerdata_t hd;
	uint32_t hl;
	uint8_t hi;
	int secs;

	if (!(context = OBEX_GetUserData(handle)))
		return;

	switch (command) {
	case OBEX_CMD_CONNECT:
		//printf("Cmd: Connect\n");

		if (response != OBEX_RSP_SUCCESS)
			break;

		while (OBEX_ObjectGetNextHeader(handle, object, &hi, &hd, &hl)) {
			switch (hi) {
			case OBEX_HDR_CONNECTION:
				context->cid = hd.bq4;
				break;
			case OBEX_HDR_WHO:
				break;
			}
		}

		context->state = OBEX_CONNECTED;
		break;

	case OBEX_CMD_DISCONNECT:
		//printf("Cmd: Disconnect\n");

		context->state = OBEX_CLOSED;
		break;

	case OBEX_CMD_PUT:
		//printf("Cmd: Put\n");
		//printf("Rsp: %02x\n", response);

		context->response = response;

		if (response != OBEX_RSP_SUCCESS) {
			context->mode = OBEX_ERROR;
			break;
		}

		context->mode = OBEX_DONE;
		break;

	case OBEX_CMD_GET:
		//printf("Cmd: Get\n");
		//printf("Rsp: %02x\n", response);

		context->response = response;

		if (response != OBEX_RSP_SUCCESS) {
			context->mode = OBEX_ERROR;
			break;
		}

		while (OBEX_ObjectGetNextHeader(handle, object, &hi, &hd, &hl)) {
			switch (hi) {
			case OBEX_HDR_NAME:
				//printf("OBEX Header Name: %s\n", hd.bs);
				break;
			case OBEX_HDR_CONNECTION:
				break;
			case OBEX_HDR_LENGTH:
				context->hdr_length = hd.bq4;
				break;
			case OBEX_HDR_TIME:
				atotm((char *) hd.bs, &context->hdr_time);
				break;
			case OBEX_HDR_BODY:
				secs = time(0) - context->time;
				context->data_buf = malloc(hl + 1);
				context->data_len = hl;
				memset(context->data_buf, 0, hl + 1);
				memcpy(context->data_buf, hd.bs, hl);
				break;
			case OBEX_HDR_TYPE:
				/* FIXME: interpret mime type */
				break;
			default:
				printf("Hdr: Unknown header %02x\n", hi);
				break;
			}
		}

		context->mode = OBEX_DONE;
		break;

	case OBEX_CMD_SETPATH:
		//printf("Cmd: Setpath\n");

		if (response != OBEX_RSP_SUCCESS) {
			context->mode = OBEX_ERROR;
			break;
		}

		while (OBEX_ObjectGetNextHeader(handle, object, &hi, &hd, &hl)) {
			switch (hi) {
			case OBEX_HDR_CONNECTION:
				break;
			}
		}

		context->mode = OBEX_DONE;
		break;

	default:
		break;
	}
}
Ejemplo n.º 10
0
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);
}
Ejemplo n.º 11
0
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;
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
0
PyObject *lightblueobex_readheaders(obex_t *obex, obex_object_t *obj)
{
    PyObject *headers;
    uint8_t hi;
    obex_headerdata_t hv;
    uint32_t hv_size;
    int r;
    PyObject *value = NULL;

    DEBUG("%s()\n", __func__);

    headers = PyDict_New();
    if (headers == NULL)
        return NULL;

    if (obex == NULL || obj == NULL || headers == NULL) {
        DEBUG("\treadheaders() got null argument\n");
        return NULL;
    }

    if (!PyDict_Check(headers)) {
        DEBUG("\treadheaders() arg must be dict\n");
        return NULL;
    }

    while (OBEX_ObjectGetNextHeader(obex, obj, &hi, &hv, &hv_size)) {
        DEBUG("\tread header: 0x%02x\n", hi);
        switch (hi & OBEX_HI_MASK) {
        case OBEX_UNICODE:
        {
            if (hv_size < 2) {
                value = PyUnicode_FromUnicode(NULL, 0);
            } else {
                /* hv_size-2 for 2-byte null terminator */
                int byteorder = OBEX_BIG_ENDIAN;
                value = PyUnicode_DecodeUTF16((const char*)hv.bs, hv_size-2,
                        NULL, &byteorder);
                if (value == NULL) {
                    DEBUG("\terror reading unicode header 0x%02x\n", hi);
                    if (PyErr_Occurred()) {
                        PyErr_Print();
                        PyErr_Clear();  /* let caller set exception */
                    }
                    return NULL;
                }
            }
            break;
        }
        case OBEX_BYTE_STREAM:
        {
            value = PyBuffer_New(hv_size);
            if (value != NULL) {
                void *buf;
                Py_ssize_t buflen;
                if (PyObject_AsWriteBuffer(value, &buf, &buflen) < 0) {
                    Py_DECREF(value);
                    DEBUG("\terror writing to buffer for header 0x%02x\n", hi);
                    return NULL;
                }
                memcpy(buf, hv.bs, buflen);
            }
            break;
        }
        case OBEX_BYTE:
        {
            value = PyInt_FromLong(hv.bq1);
            break;
        }
        case OBEX_INT:
        {
            value = PyLong_FromUnsignedLong(hv.bq4);
            break;
        }
        default:
            DEBUG("\tunknown header id encoding %d\n", (hi & OBEX_HI_MASK));
            return NULL;
        }

        if (value == NULL) {
            if (PyErr_Occurred() == NULL)
                DEBUG("\terror reading headers\n");
            return NULL;
        }
        r = PyDict_SetItem(headers, PyInt_FromLong((long)hi), value);
        Py_DECREF(value);
        if (r < 0) {
            DEBUG("\tPyDict_SetItem() error\n");
            if (PyErr_Occurred()) {
                PyErr_Print();
                PyErr_Clear();  /* let caller set exception */
            }
            return NULL;
        }
    }
    return headers;
}
Ejemplo n.º 14
0
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;
}
Ejemplo n.º 15
0
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;
	}
}