[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ICMP socket with Gambas


...try:

[code]

Library "libc:6"

Private Enum IPPROTO_IP, IPPROTO_ICMP, AF_INET, SOCK_DGRAM = 2, SOCK_RAW, IP_HDRINCL = 3, IPPROTO_TCP = 6

Public Struct SockAddr 
  SinFamily As Integer
  SinPort As Integer
  SinAddr As Pointer
End Struct

Private Extern socket(domain As Integer, type As Integer, protocol As Integer) As Integer
Private Extern close(sockfd As Integer) As Integer
Private Extern inet_pton(af As Integer, src As String, dst As Pointer) As Integer
Private Extern sendto(sockfd As Integer, buf As Byte[], size As Integer, flags As Integer, sockaddr As SockAddr, addrlen As Integer) As Integer

Private Sub CalcChecksum(aHeader As Byte[]) As Short
  
  Dim iSum As Long
  Dim iVal As Short
  
  For i As Integer = 0 To aHeader.Max Step 2
    iVal = (CShort(aHeader[i]) Lsl 8) + aHeader[i + 1]
    iSum += iVal
  Next
  
  While iSum > &HFFFF&
    iSum = (iSum Lsr 16) + (iSum And &HFFFF)
  Wend
  
  Return Not iSum
  
End

Public Sub Main()

  Dim sockfd, rc As Integer
  Dim aHeader As Short[] = [8, 0, 0, 0, 0, 0, 0, 0]
  Dim iChecksum As Short
  Dim DestAddr As New SockAddr
  
  sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP)
  If sockfd < 0 Then 'error
    Print "Socket call error: " & System.Error
    Quit 
  Endif

  DestAddr.SinFamily = AF_INET
  DestAddr.SinPort = 0
  Dim p As Pointer = DestAddr.SinAddr
  rc = inet_pton(AF_INET, "8.8.8.8", VarPtr(p))
  If rc < 1 Then 
    Print "Error converting address."
    Quit 
  Endif

  iChecksum = CalcChecksum(aHeader)
  aHeader[2] = (iChecksum Lsr 8) And &HFF
  aHeader[3] = iChecksum And &HFF

  rc = sendto(sockfd, aHeader, 8, 0, DestAddr, Object.SizeOf(DestAddr))
  If rc < 0 Then 
    Print "Send error:", System.Error
  Else 
    Print "Bytes sent:", rc
  Endif

  Finally 
    If sockfd Then 
      rc = close(sockfd)
    Endif
    If rc < 0 Then  'error
      Print "Close error: " & System.Error
    Endif
    
    Catch 
      Debug Error.Text;; "at";; Error.Where

End

[/code]





28 giu 2025, 00:05 da t.lee.davidson@xxxxxxxxx:

> On 6/27/25 2:03 PM, Linus wrote:
>
>> Hello Lee,
>>
>> Thank you for your comment and research and yes I knew about the need to be root to run the code.
>> A lot a network command on Linux require the ‘root’ access but I was not aware about the SOCK_DGRAM option.
>>
>> Unfortunately, in the end, for now, no way to do an ICMP request in Gambas…
>>
>> I will wait the answer from Benoit, about if is possible to do something on this way.
>>
>> Olivier
>>
>
> I created a test project using the libc:6 shared library for creating a socket and sending a ping echo request.
>
> Wireshark confirms that the 'ping_cmd' executable does indeed send the request, but the attached project does not even though I get no errors from the libc function calls.
>
> I figure I must be doing something wrong as I am not strong on external functions with structures and pointers.
>
>
> -- 
> Lee
>
> --- Gambas User List Netiquette [https://gambaswiki.org/wiki/doc/netiquette] ----
> --- Gambas User List Archive [https://lists.gambas-basic.org/archive/user] ----
>


References:
Re: ICMP socket with GambasLee <t.lee.davidson@xxxxxxxxx>
Re: ICMP socket with GambasLinus <olivier.cruilles@xxxxxxxx>
Re: ICMP socket with GambasLee <t.lee.davidson@xxxxxxxxx>