From patchwork Wed Mar 12 17:26:17 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Chemeris X-Patchwork-Id: 329590 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ganesha.gnumonks.org (ganesha.gnumonks.org [IPv6:2001:780:45:1d:225:90ff:fe52:c662]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D28012C00C2 for ; Thu, 13 Mar 2014 04:27:08 +1100 (EST) Received: from localhost ([127.0.0.1] helo=ganesha.gnumonks.org) by ganesha.gnumonks.org with esmtp (Exim 4.72) (envelope-from ) id 1WNmvf-00079x-MO; Wed, 12 Mar 2014 18:26:56 +0100 Received: from mail-pb0-x231.google.com ([2607:f8b0:400e:c01::231]) by ganesha.gnumonks.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.72) (envelope-from ) id 1WNmvS-00079q-7F for openbsc@lists.osmocom.org; Wed, 12 Mar 2014 18:26:44 +0100 Received: by mail-pb0-f49.google.com with SMTP id jt11so1374393pbb.8 for ; Wed, 12 Mar 2014 10:26:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=sy9FpoCJOsxagjIUrKrgvVUfvdPZO29dUuw0utGc68c=; b=ittGLa2zi8zhuzoJf3nC31kxZbeKVliu9kGtF9+mADfBD8QkMMB4Ub16TZufEqhL0G 91fyPEMWSzpHz9NjKfHokWlE+ReewsNk2URx2BNSia3h30k1WMvJjxVx0T0Wt17p2V1h 2gDsbp/BENB4xpwoRK7e4G1IH9Op1flUXGcnrfnwtoFfpSgeWgq1bazWtVwqhOPm3yLs F0Ox6Dxc3ZfsdwRGAixFqAG38xbJqP9sHU6IKrEd4/hVfnGWEvQJU4AaTlIig5+DhokA iKEDnOB2CgcnNdaR9iBos4cyB0T7rQhTBjetdPZ+N2rRsaRBCqHZxCSVc4zxEbvlQx0J yaUQ== X-Received: by 10.68.43.2 with SMTP id s2mr6530557pbl.60.1394645200379; Wed, 12 Mar 2014 10:26:40 -0700 (PDT) MIME-Version: 1.0 Received: by 10.70.88.239 with HTTP; Wed, 12 Mar 2014 10:26:17 -0700 (PDT) In-Reply-To: <20140311133347.GE17965@xiaoyu.lan> References: <20140308164911.GA4815@xiaoyu.lan> <20140311133347.GE17965@xiaoyu.lan> From: Alexander Chemeris Date: Wed, 12 Mar 2014 21:26:17 +0400 Message-ID: Subject: Re: [PATCH] libmsc,sms: Properly calculate and store validity and received timestamps for incoming SMS. To: Holger Hans Peter Freyther X-Spam-Score: -0.1 (/) Cc: OpenBSC Mailing List X-BeenThere: openbsc@lists.osmocom.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Development of the OpenBSC GSM base station controller List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: openbsc-bounces@lists.osmocom.org Errors-To: openbsc-bounces@lists.osmocom.org I've updated the patch due to gsm340_validity_time() function rename. On Tue, Mar 11, 2014 at 5:33 PM, Holger Hans Peter Freyther wrote: > On Sat, Mar 08, 2014 at 10:16:25PM +0100, Alexander Chemeris wrote: >> On Sat, Mar 8, 2014 at 5:49 PM, Holger Hans Peter Freyther >> wrote: >> > In case you hurry up we can get it into schema version 4. >> >> I've submitted one more patch, affecting the DB scheme. I hope we >> could include it into the same batch of DB changes. >> [PATCH 2/2] sms,db: Do not store delivered messages in the DB. > > Sure. We should do all the schema changes now. > From c70e1282d692e376542b3f789e5340d02af78f17 Mon Sep 17 00:00:00 2001 From: Alexander Chemeris Date: Tue, 26 Nov 2013 15:04:48 -0600 Subject: [PATCH] libmsc,sms: Properly calculate and store validity and received timestamps for incoming SMS. --- openbsc/include/openbsc/gsm_data.h | 3 ++- openbsc/src/libmsc/db.c | 24 +++++++++++++++--------- openbsc/src/libmsc/gsm_04_11.c | 7 ++++++- openbsc/tests/db/db_test.c | 5 +++++ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/openbsc/include/openbsc/gsm_data.h b/openbsc/include/openbsc/gsm_data.h index e6a94ad..2ce7727 100644 --- a/openbsc/include/openbsc/gsm_data.h +++ b/openbsc/include/openbsc/gsm_data.h @@ -315,7 +315,8 @@ struct gsm_sms { char msg_id[16]; } smpp; - unsigned long validity_minutes; + time_t received_time; + time_t valid_until; uint8_t reply_path_req; uint8_t status_rep_req; uint8_t ud_hdr_ind; diff --git a/openbsc/src/libmsc/db.c b/openbsc/src/libmsc/db.c index f7f7c56..c1207ed 100644 --- a/openbsc/src/libmsc/db.c +++ b/openbsc/src/libmsc/db.c @@ -2,6 +2,7 @@ /* (C) 2008 by Jan Luebbe * (C) 2009 by Holger Hans Peter Freyther * (C) 2009 by Harald Welte + * (C) 2014 by Alexander Chemeris * All Rights Reserved * * This program is free software; you can redistribute it and/or modify @@ -113,6 +114,7 @@ static const char *create_stmts[] = { /* metadata, not part of sms */ "id INTEGER PRIMARY KEY AUTOINCREMENT, " "created TIMESTAMP NOT NULL, " + "received TIMESTAMP, " "sent TIMESTAMP, " "deliver_attempts INTEGER NOT NULL DEFAULT 0, " /* data directly copied/derived from SMS */ @@ -245,6 +247,8 @@ static struct gsm_sms *sms_from_result_v3(dbi_result result) sms->protocol_id = dbi_result_get_uint(result, "protocol_id"); sms->data_coding_scheme = dbi_result_get_uint(result, "data_coding_scheme"); + sms->received_time = dbi_result_get_datetime(result, "created"); + sms->valid_until = dbi_result_get_datetime(result, "valid_until"); daddr = dbi_result_get_string(result, "dest_addr"); if (daddr) { @@ -1278,32 +1282,33 @@ int db_sms_store(struct gsm_sms *sms) dbi_result result; char *q_text, *q_daddr, *q_saddr; unsigned char *q_udata; - char *validity_timestamp = "2222-2-2"; - - /* FIXME: generate validity timestamp based on validity_minutes */ + char received_timestamp[22]; + char validity_timestamp[22]; dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); dbi_conn_quote_string_copy(conn, (char *)sms->dst.addr, &q_daddr); dbi_conn_quote_string_copy(conn, (char *)sms->src.addr, &q_saddr); dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len, &q_udata); - - /* FIXME: correct validity period */ + strftime(received_timestamp, sizeof(received_timestamp), + "'%F %T'", gmtime(&sms->received_time)); + strftime(validity_timestamp, sizeof(validity_timestamp), + "'%F %T'", gmtime(&sms->valid_until)); result = dbi_conn_queryf(conn, "INSERT INTO SMS " - "(created, valid_until, " + "(created, received, valid_until, " "reply_path_req, status_rep_req, protocol_id, " "data_coding_scheme, ud_hdr_ind, " "user_data, text, " "dest_addr, dest_ton, dest_npi, " "src_addr, src_ton, src_npi) VALUES " - "(datetime('now'), %u, " + "(datetime('now'), %s, %s, " "%u, %u, %u, " "%u, %u, " "%s, %s, " "%s, %u, %u, " "%s, %u, %u)", - validity_timestamp, + received_timestamp, validity_timestamp, sms->reply_path_req, sms->status_rep_req, sms->protocol_id, sms->data_coding_scheme, sms->ud_hdr_ind, q_udata, q_text, @@ -1332,7 +1337,6 @@ static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result resul sms->id = dbi_result_get_ulonglong(result, "id"); - /* FIXME: validity */ /* FIXME: those should all be get_uchar, but sqlite3 is braindead */ sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req"); sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req"); @@ -1340,6 +1344,8 @@ static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result resul sms->protocol_id = dbi_result_get_uint(result, "protocol_id"); sms->data_coding_scheme = dbi_result_get_uint(result, "data_coding_scheme"); + sms->received_time = dbi_result_get_datetime(result, "received"); + sms->valid_until = dbi_result_get_datetime(result, "valid_until"); /* sms->msg_ref is temporary and not stored in DB */ sms->dst.npi = dbi_result_get_uint(result, "dest_npi"); diff --git a/openbsc/src/libmsc/gsm_04_11.c b/openbsc/src/libmsc/gsm_04_11.c index 566feb9..15b17c3 100644 --- a/openbsc/src/libmsc/gsm_04_11.c +++ b/openbsc/src/libmsc/gsm_04_11.c @@ -102,6 +102,9 @@ struct gsm_sms *sms_from_text(struct gsm_subscriber *receiver, sms->protocol_id = 0; /* implicit */ sms->data_coding_scheme = dcs; strncpy(sms->dst.addr, receiver->extension, sizeof(sms->dst.addr)-1); + /* Timestamps */ + time(&sms->received_time); + sms->valid_until = sms->received_time + SMS_DEFAULT_VALIDITY_PERIOD; /* Generate user_data */ sms->user_data_len = gsm_7bit_encode_n(sms->user_data, sizeof(sms->user_data), sms->text, NULL); @@ -384,7 +387,9 @@ static int gsm340_rx_tpdu(struct gsm_subscriber_connection *conn, struct msgb *m sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text : osmo_hexdump(gsms->user_data, gsms->user_data_len)); - gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp); + gsms->received_time = time(NULL); + gsms->valid_until = gsm340_validity_time(gsms->received_time, + sms_vpf, sms_vp); /* FIXME: This looks very wrong */ send_signal(0, NULL, gsms, 0); diff --git a/openbsc/tests/db/db_test.c b/openbsc/tests/db/db_test.c index ef2e538..8c00254 100644 --- a/openbsc/tests/db/db_test.c +++ b/openbsc/tests/db/db_test.c @@ -73,6 +73,9 @@ static void test_sms(void) sms = sms_alloc(); sms->receiver = subscr_get(subscr); + sms->received_time = 12345; + sms->valid_until = 67890; + sms->src.ton = 0x23; sms->src.npi = 0x24; memcpy(sms->src.addr, "1234", strlen("1234") + 1); @@ -100,6 +103,8 @@ static void test_sms(void) sms = db_sms_get_unsent_for_subscr(subscr); OSMO_ASSERT(sms); OSMO_ASSERT(sms->receiver == subscr); + OSMO_ASSERT(sms->received_time == 12345); + OSMO_ASSERT(sms->valid_until == 67890); OSMO_ASSERT(sms->reply_path_req == 1); OSMO_ASSERT(sms->status_rep_req == 2); OSMO_ASSERT(sms->ud_hdr_ind == 3); -- 1.7.9.5