imsd, engine: the Messaging status and a received text carry the SIM's operator

A consumer that keeps a number in the form it was written in — a
national number, an operator's short code — has to know where it was
written before it dials it again: a national number's country, a short
code's operator. The own number's country code is not enough for either:
countries share a code, and the operators of one country share theirs.
The daemon reads the SIM's operator id at bring-up (ModemManager's: MCC
then MNC), which names the subscription's country and operator. The
Messaging GetStatus gives it as `operator`, from the snapshot `ppi`
comes from, and a received text's record and its Received and List
dictionaries carry the one the daemon held at receipt, beside `to` —
additive keys, as `to` and `ppi` are, and the operator id only, never
the IMSI.

Tested: tests/Engine, the record round trip with `operator` and a
record without it.
This commit is contained in:
Jorijn van der Graaf 2026-11-12 00:21:41 +01:00
commit e39f7e39d6
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 30 additions and 18 deletions

View file

@ -822,6 +822,7 @@ struct StatusSnapshot {
std::int64_t expiry = 0;
std::int64_t cseq = 0;
std::string smsc; // the service centre a submission goes to (empty: none known)
std::string simOperator; // the SIM's operator id: MCC then MNC, as ModemManager gives it (empty: no SIM read when the status was taken)
};
// engine -> main-loop event
@ -834,6 +835,7 @@ struct Event {
imsd::engine::SmsReceived sms; // SmsReceived
std::int64_t at = 0; // SmsReceived: when it arrived (epoch seconds)
std::string to; // SmsReceived: the subscriber it arrived for (the own number the daemon held then)
std::string simOperator; // SmsReceived: the operator id of the SIM it arrived under
};
GDBusConnection* DbusConn = nullptr;
@ -1079,7 +1081,7 @@ std::vector<imsd::engine::StoredSms> LoadStoredSms(bool log = true) {
std::size_t StoredSmsCount() { return LoadStoredSms(false).size(); }
// a received short message as the Messaging interface's a{sv} (§8.4)
GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt, const std::string& to) {
GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt, const std::string& to, const std::string& simOperator) {
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&b, "{sv}", "from", g_variant_new_string(m.from.c_str()));
@ -1089,6 +1091,7 @@ GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt
g_variant_builder_add(&b, "{sv}", "sc", g_variant_new_string(m.sc.c_str()));
// from a header: made valid UTF-8 like a failed submission's reason
if (!to.empty()) g_variant_builder_add(&b, "{sv}", "to", g_variant_new_take_string(g_utf8_make_valid(to.c_str(), -1)));
if (!simOperator.empty()) g_variant_builder_add(&b, "{sv}", "operator", g_variant_new_string(simOperator.c_str()));
g_variant_builder_add(&b, "{sv}", "class", g_variant_new_int32(m.msgClass.value_or(-1)));
g_variant_builder_add(&b, "{sv}", "pid", g_variant_new_byte(m.pid));
g_variant_builder_add(&b, "{sv}", "dcs", g_variant_new_byte(m.dcs));
@ -1143,7 +1146,7 @@ gboolean OnIdleEvent(gpointer data) {
break;
case Event::Type::SmsReceived:
for (const std::string& to : Subscribers)
EmitSignalTo(to, "Received", g_variant_new("(s@a{sv})", ev->uni.c_str(), SmsVariant(ev->sms, ev->at, ev->to)));
EmitSignalTo(to, "Received", g_variant_new("(s@a{sv})", ev->uni.c_str(), SmsVariant(ev->sms, ev->at, ev->to, ev->simOperator)));
break;
case Event::Type::SmsState: {
std::set<std::string> to = Subscribers;
@ -1475,6 +1478,7 @@ private:
imsd::msg::Context ctx_;
RegState reg_;
std::string route_, ppi_, ss_;
std::string simOperator_; // the SIM's operator id as read at bring-up: MCC then MNC
bool registered_ = false;
SipFlow sip_;
ServerPorts server_;
@ -1586,6 +1590,7 @@ private:
auto info = imsd::aka::ParseSimInfo(siminfo.out);
if (!info) throw std::runtime_error("SIM IMSI/operator not found");
ctx_.id = imsd::aka::MakeIdentity(info->imsi, info->mcc, info->mnc);
simOperator_ = info->mcc + info->mnc;
ctx_.local = local_;
ctx_.ealgOffer = ealgOffer_;
SetTransport(transport_);
@ -2903,11 +2908,12 @@ private:
ev->at = NowEpoch();
ev->sms = m;
ev->to = ppi_; // the subscriber it arrived for: the own number held now, not a later SIM's
ev->simOperator = simOperator_;
std::string why;
StoreResult stored = StoreResult::Failed;
for (int tries = 0; tries < 1000; tries++) {
ev->uni = imsd::engine::ReceivedSmsId(smsRun_, ev->at, ++smsInSeq_);
stored = StoreReceivedSms({ev->uni, ev->at, m, ppi_}, why);
stored = StoreReceivedSms({ev->uni, ev->at, m, ppi_, simOperator_}, why);
if (stored != StoreResult::Exists) break;
Log(std::format("sms store: {} exists already; taking the next id", ev->uni));
}
@ -3156,7 +3162,7 @@ private:
void EmitStatus(bool registrationSignal) {
auto ev = std::make_unique<Event>();
ev->type = registrationSignal ? Event::Type::Registration : Event::Type::Status;
ev->status = StatusSnapshot{registered_, local_, pcscf_, ppi_, reg_.expiry, reg_.cseq, smsc_};
ev->status = StatusSnapshot{registered_, local_, pcscf_, ppi_, reg_.expiry, reg_.cseq, smsc_, simOperator_};
PostEvent(std::move(ev));
}
void PostAdded(const CallInfo& info) {
@ -3305,6 +3311,7 @@ void HandleMessagingMethod(GDBusConnection* conn, const gchar*, const gchar*, co
g_variant_builder_add(&b, "{sv}", "enabled", g_variant_new_boolean(TheEngine->SmsEnabled() ? TRUE : FALSE));
g_variant_builder_add(&b, "{sv}", "smsc", g_variant_new_string(StatusSnap.smsc.c_str()));
g_variant_builder_add(&b, "{sv}", "ppi", g_variant_new_take_string(g_utf8_make_valid(StatusSnap.ppi.c_str(), -1))); // from a header, as `to`
g_variant_builder_add(&b, "{sv}", "operator", g_variant_new_string(StatusSnap.simOperator.c_str()));
g_variant_builder_add(&b, "{sv}", "registered", g_variant_new_boolean(StatusSnap.registered ? TRUE : FALSE));
g_variant_builder_add(&b, "{sv}", "subscribers", g_variant_new_uint32(static_cast<guint32>(Subscribers.size())));
g_dbus_method_invocation_return_value(inv, g_variant_new("(a{sv})", &b));
@ -3313,7 +3320,7 @@ void HandleMessagingMethod(GDBusConnection* conn, const gchar*, const gchar*, co
if (m == "List") {
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a(sa{sv})"));
for (const auto& r : LoadStoredSms()) g_variant_builder_add(&b, "(s@a{sv})", r.id.c_str(), SmsVariant(r.sms, r.receivedAt, r.to));
for (const auto& r : LoadStoredSms()) g_variant_builder_add(&b, "(s@a{sv})", r.id.c_str(), SmsVariant(r.sms, r.receivedAt, r.to, r.simOperator));
g_dbus_method_invocation_return_value(inv, g_variant_new("(a(sa{sv}))", &b));
return;
}