Quantcast
Channel: Fortran
Viewing all articles
Browse latest Browse all 3108

WSAIoctl SIO_GET_BROADCAST_ADDRESS issue.

$
0
0

Intel Forum,

I am using UDP sockets in my application.

I would like to extract the Broadcast Address of my Windows 7 system so
as to pass the information to the <sendto> function.

Source Code:

      INTEGER(DWORD) disport, disend_fd, disrcv_fd
      INTEGER(DWORD) dwBytesRet
      INTEGER(DWORD) istat
c
      INTEGER(DWORD) :: sockfd = INVALID_SOCKET
      INTEGER(DWORD) :: status = 0
c
      logical(DWORD) :: boptval = .TRUE.
c
      INTEGER(DWORD), PARAMETER :: success = 0
c
c  -- Initialize Winsock v2.
c
      INTEGER(DWORD), PARAMETER ::  winsock_v2_2 = X'202'
c
      TYPE(T_WSADATA) wsaInfo
c
      TYPE(T_OVERLAPPED) lpOverlapped
c
      TYPE(T_SOCKADDR_IN) :: send_addr
      TYPE(T_SOCKADDR_IN) :: bcast_addr
c
      istat = 0
c
      status = WSAStartup(winsock_v2_2, wsaInfo)
c
      if (status .ne. success) then  
        print *, 'WSAStartup - Error Code = ', status
c
        istat = 1
        return
      endif
c
c     -------------------
c     -- Send segment.
c     -------------------
c
c   - Configure socket to support User Datagram Protocol (UDP) for Internet address family (AF_INET).
c
      sockfd = Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
      istat =  WSAGetLastError()
c
      if (sockfd .eq. INVALID_SOCKET) then
        print *, 'Socket - Error Code = ', istat
c
        status = WSACleanup()
        istat = 1
        return
      endif
c
      status = setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,%REF(boptval),
     &                    sizeof(boptval))
      istat =  WSAGetLastError()
c
      if (status .eq. SOCKET_ERROR) then  
        print *, 'setsockopt - Error Code = ', istat
c
        istat = 1
        return
      endif
c
      status = setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,%REF(boptval),
     &                    sizeof(boptval))
      istat =  WSAGetLastError()
c
      if (status .eq. SOCKET_ERROR) then  
        print *, 'setsockopt - Error Code = ', istat
c
        istat = 1
        return
      endif
c
c   - Connect to socket.
c
      call ZeroMemory(loc(send_addr), sizeof(send_addr))
c
      send_addr%sin_family      = INT2(AF_INET)
      send_addr%sin_port        = htons(INT2(disport))
      send_addr%sin_addr%s_addr = htonl(INADDR_ANY)
c
c   - WSAIoctl function populates the output buffer with a bcast_addr structure
c     containing a suitable broadcast address for use with sendto function.
c     [ Not supported for IPv6 sockets (returns WSAENOPROTOOPT). ]
c
      call ZeroMemory(loc(bcast_addr), sizeof(bcast_addr))
      call ZeroMemory(loc(lpOverlapped), sizeof(lpOverlapped))
c
      status = WSAIoctl(sockfd,
     &                  SIO_GET_BROADCAST_ADDRESS,
     &                  NULL,
     &                  0,
     &                  %REF(bcast_addr),
     &                  sizeof(bcast_addr),
     &                  dwBytesRet,
     &                  %REF(lpOverlapped),
     &                  NULL)
      istat =  WSAGetLastError()
c
      if (status .eq. SOCKET_ERROR) then
        print *, "WSAIoctl(SIO_GET_BROADCAST_ADDRESS) error code = ",
     &            istat
c
        istat = 1
        return
      else
        bcast_addr%sin_port = htons(INT2(disport))
c
        print *, "Broadcast address is: ",
     &            inet_ntoa(bcast_addr.sin_addr)
      endif
c
      disend_fd = sockfd

I have executed the code and everything seems to be working fine. I am send UDP packets from my Windows 7 system
over the network and am receiving UDP packets on another application running on my CentOS 6.5 system.

Question:

1). Every time I execute the code and print the Broadcast address, the Broadcast address changes and
    the value does not make sense in relation to my IP addressing scheme.

Am I implementing the WSAIoctl function correctly?

Thanks in advance.


Viewing all articles
Browse latest Browse all 3108

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>