From 808db2173b907c6e030c157e098c586d59ea1166 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Fri, 12 Jun 2009 21:17:42 -0500 Subject: [PATCH] Add utility to extract application port address --- src/smsutil.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/smsutil.h | 1 + 2 files changed, 74 insertions(+) diff --git a/src/smsutil.c b/src/smsutil.c index 1bbc4a3..a244e58 100644 --- a/src/smsutil.c +++ b/src/smsutil.c @@ -1702,3 +1702,76 @@ const char *sms_address_to_string(const struct sms_address *addr) return buffer; } + +gboolean sms_extract_app_port(const struct sms *sms, int *dst, int *src) +{ + struct sms_udh_iter iter; + enum sms_iei iei; + guint8 addr_hdr[4]; + + int srcport = -1; + int dstport = -1; + + if (!sms_udh_iter_init(sms, &iter)) + return FALSE; + + /* According to the specification, we have to use the last + * useable header. Also, we have to ignore ports that are reserved: + * A receiving entity shall ignore (i.e. skip over and commence + * processing at the next information element) any information element + * where the value of the Information-Element-Data is Reserved or not + * supported. + */ + while ((iei = sms_udh_iter_get_ie_type(&iter)) != + SMS_IEI_INVALID) { + switch (iei) { + case SMS_IEI_APPLICATION_ADDRESS_8BIT: + if (sms_udh_iter_get_ie_length(&iter) != 2) + break; + + sms_udh_iter_get_ie_data(&iter, addr_hdr); + + if (addr_hdr[0] < 240) + break; + + if (addr_hdr[1] < 240) + break; + + dstport = addr_hdr[0]; + srcport = addr_hdr[1]; + break; + + case SMS_IEI_APPLICATION_ADDRESS_16BIT: + if (sms_udh_iter_get_ie_length(&iter) != 4) + break; + + sms_udh_iter_get_ie_data(&iter, addr_hdr); + + if (((addr_hdr[0] << 8) | addr_hdr[1]) > 49151) + break; + + if (((addr_hdr[2] << 8) | addr_hdr[3]) > 49151) + break; + + dstport = (addr_hdr[0] << 8) | addr_hdr[1]; + srcport = (addr_hdr[2] << 8) | addr_hdr[3]; + break; + + default: + break; + } + + sms_udh_iter_next(&iter); + } + + if (dstport == -1 || srcport == -1) + return FALSE; + + if (dst) + *dst = dstport; + + if (src) + *src = srcport; + + return TRUE; +} diff --git a/src/smsutil.h b/src/smsutil.h index f31994c..a2fc063 100644 --- a/src/smsutil.h +++ b/src/smsutil.h @@ -353,4 +353,5 @@ gboolean sms_mwi_dcs_decode(guint8 dcs, enum sms_mwi_type *type, enum sms_charset *charset, gboolean *active, gboolean *discard); +gboolean sms_extract_app_port(const struct sms *sms, int *dst, int *src); #endif -- 2.7.4