[Windows Mobile Development and Hacking General] jmprince80:
Using vb.net i have created an application which listens on TCP port 85 for incoming connections via telnet, reads a line of text (until ascii 13) and then parses that line (phone number, ascii 9, sms message, ascii 13) and sends it as an sms....

I have two small problems:

1: i am unable to work out if the sms was sent ok or not, i do not know how to get the return code from the send sms function into a useable format...

2: all this works fine (apart from the above) it parses the line, and sends the sms (i know it sends as it delivers to my other phone, but i need to have it print the result to the socket) but ONLY over the USB connection on 169.254.x.x, and not if i change the listening IP to 192.168.0.12 (this is a static ip the device is always assigned by my dhcp server) i cannot connect to the server. I would like to use this wirelessly as that way i can forward the port using my router and therefore access the sms on the phone from an external server without needing my laptop to be switched on!

I have included my code below... excuse the sloppiness, i only started using vb.net today, but any help in trying to acheive what i am attempting would be much appreciated!

Imports System.Runtime.InteropServices
Imports interopserv = System.Runtime.InteropServices
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic

Public Enum SMS_ADDRESS_TYPE
SMSAT_UNKNOWN = 0
SMSAT_INTERNATIONAL
SMSAT_NATIONAL
SMSAT_NETWORKSPECIFIC
SMSAT_SUBSCRIBER
SMSAT_ALPHANUMERIC
SMSAT_ABBREVIATED
End Enum 'SMS_ADDRESS_TYPE

Public Structure PhoneAddress
'/ <summary>The address type.</summary>
Public AddressType As SMS_ADDRESS_TYPE
'/ <summary>The phone number in string format.</summary>
Public Address() As Char
End Structure 'PhoneAddress

Public Class SMS
Private Shared SMS_MSGTYPE_TEXT As String = "Microsoft Text SMS Protocol"
Private Shared SMS_MODE_SEND As Long = &H2
Private Shared SMS_OPTION_DELIVERY_NONE As Long = &H0
Private Shared SMS_OPTION_DELIVERY_NO_RETRY As Long = &H1
Private Shared PS_MESSAGE_OPTION_NONE As Long = &H0

Public Shared Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.ASCIIEncoding()
Return encoding.GetBytes(str)
End Function 'StrToByteArray

Private Enum SMS_DATA_ENCODING
SMSDE_OPTIMAL = 0
SMSDE_GSM
SMSDE_UCS2
End Enum 'SMS_DATA_ENCODING

Public Enum PROVIDER_SPECIFIC_MESSAGE_CLASS
PS_MESSAGE_CLASS0 = 0
PS_MESSAGE_CLASS1
PS_MESSAGE_CLASS2
PS_MESSAGE_CLASS3
End Enum 'PROVIDER_SPECIFIC_MESSAGE_CLASS

Private Enum PROVIDER_SPECIFIC_REPLACE_OPTION
PSRO_NONE = 0
PSRO_REPLACE_TYPE1
PSRO_REPLACE_TYPE2
PSRO_REPLACE_TYPE3
PSRO_REPLACE_TYPE4
PSRO_REPLACE_TYPE5
PSRO_REPLACE_TYPE6
PSRO_REPLACE_TYPE7
PSRO_RETURN_CALL
PSRO_DEPERSONALIZATION
End Enum 'PROVIDER_SPECIFIC_REPLACE_OPTION

Private Structure TEXT_PROVIDER_SPECIFIC_DATA
Public dwMessageOptions As Long
Public psMessageClass As PROVIDER_SPECIFIC_MESSAGE_CLASS
Public psReplaceOption As PROVIDER_SPECIFIC_REPLACE_OPTION
End Structure 'TEXT_PROVIDER_SPECIFIC_DATA

<System.Runtime.InteropServices.DllImport("sms.dll")> _
Private Shared Function SmsOpen(ByVal ptsMessageProtocol As [String], _
ByVal dwMessageModes As Int32, _
ByRef psmshHandle As IntPtr, _
ByVal phMessageAvailableEvent As IntPtr) As IntPtr
End Function

<System.Runtime.InteropServices.DllImport("sms.dll")> _
Private Shared Function SmsSendMessage(ByVal smshHandle As IntPtr, _
ByVal psmsaSMSCAddress As Int32, _
ByVal psmsaDestinationAddress As IntPtr, _
ByVal pstValidityPeriod As Int32, _
ByVal pbData As IntPtr, _
ByVal dwDataSize As Int32, _
ByVal pbProviderSpecificData() As Byte, _
ByVal dwProviderSpecificDataSize As Int32, _
ByVal smsdeDataEncoding As Int32, _
ByVal dwOptions As Int32, _
ByVal psmsmidMessageID As Int32) As IntPtr
End Function

<System.Runtime.InteropServices.DllImport("sms.dll")> _
Private Shared Function SmsClose(ByVal smshHandle As IntPtr) As IntPtr
End Function

<StructLayout(LayoutKind.Sequential)> _
Public Structure MsgSize
Public MsgSz As Int32
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure ProviderDataSize
Public ProvDataSize As Int32
End Structure

Public Shared Function SendMessage(ByVal sPhoneNumber As String, ByVal sMessage As String) As Integer
Dim retVal As IntPtr = IntPtr.Zero
Dim smsHandle As IntPtr = IntPtr.Zero
Dim smsProviderData As IntPtr = IntPtr.Zero
Dim smsMessage As IntPtr = IntPtr.Zero
Dim msgresult As Int32
Dim ProvData(12) As Byte

Try

retVal = SmsOpen(SMS_MSGTYPE_TEXT, SMS_MODE_SEND, smsHandle, IntPtr.Zero)
If retVal.ToInt32 <> 0 Then
Throw New Exception("Could not open SMS.")
End If

'Set address structure
Dim smsatAddressType As Byte() = BitConverter.GetBytes(SMS_ADDRESS_TYPE.SMSAT_UNKNOWN)
Dim ptsAddress As Byte() = System.Text.Encoding.Unicode.GetBytes(sPhoneNumber)
Dim smsAddressTag(smsatAddressType.Length + ptsAddress.Length) As Byte
Array.Copy(smsatAddressType, 0, smsAddressTag, 0, smsatAddressType.Length)
Array.Copy(ptsAddress, 0, smsAddressTag, smsatAddressType.Length, ptsAddress.Length)
Dim smsAddress As IntPtr = Marshal.AllocHLocal(smsAddressTag.Length)
System.Runtime.InteropServices.Marshal.Copy(smsAddressTag, 0, smsAddress, smsAddressTag.Length)

'Set message
Dim smsMessageTag As Byte() = System.Text.Encoding.Unicode.GetBytes(sMessage)
smsMessage = Marshal.AllocHLocal(smsMessageTag.Length)
System.Runtime.InteropServices.Marshal.Copy(smsMessageTag, 0, smsMessage, smsMessageTag.Length)

retVal = SmsSendMessage(smsHandle, 0, smsAddress, 0, smsMessage, smsMessageTag.Length, _
ProvData, 12, SMS_DATA_ENCODING.SMSDE_OPTIMAL, SMS_OPTION_DELIVERY_NONE, 0)
msgresult = retVal.ToInt32
'Stream.Write(StrToByteArray(retVal), 0, retVal.Length)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Try
retVal = SmsClose(smsHandle)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return msgresult
End Function
End Class 'SMS

Public Class MyTcpListener
Public Shared Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.ASCIIEncoding()
Return encoding.GetBytes(str)
End Function 'StrToByteArray

Public Shared Sub Main()

Dim server As TcpListener
server = Nothing
Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 85
Dim localAddr As IPAddress = IPAddress.Parse("0.0.0.0")

server = New TcpListener(localAddr, port)

' Start listening for client requests.
server.Start()

' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing

' Enter the listening loop.
While True
Console.Write("Waiting for a connection... ")

' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("Connected!")

data = Nothing

' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()

Dim i As Integer
Dim c As Char
Dim smeg As Char
Dim values(4) As String

'Dim msg As String

Dim j As String

i = stream.ReadByte()

While (i <> 1)
c = Microsoft.VisualBasic.ChrW(i)
data = data & c
i = stream.ReadByte()

End While
Dim mSep As String
mSep = "---------"
smeg = Microsoft.VisualBasic.ChrW(9)
values = data.Split(smeg)
stream.Write(StrToByteArray(values(0)), 0, values(0).Length)
stream.Write(StrToByteArray(mSep), 0, mSep.Length)
stream.Write(StrToByteArray(values(1)), 0, values(1).Length)
Dim result As Int32

result = SMS.SendMessage(values(0), values(1))

stream.Write(StrToByteArray(Microsoft.VisualBasic.ChrW(result)), 0, 1)

' Shutdown and end connection
client.Close()
stream.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try

Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
'Console.Read()
End Sub 'Main

End Class 'MyTcpListener