VirtualBox

Changeset 96864 in vbox for trunk


Ignore:
Timestamp:
Sep 26, 2022 2:38:59 PM (2 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
153771
Message:

Runtime: Implement RTUdpCreateServerSocket(), bugref:9822

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/mangling.h

    r96813 r96864  
    27582758# define RTTraceSetDefaultBuf                           RT_MANGLER(RTTraceSetDefaultBuf)
    27592759# define RTUdpCreateClientSocket                        RT_MANGLER(RTUdpCreateClientSocket)
     2760# define RTUdpCreateServerSocket                        RT_MANGLER(RTUdpCreateServerSocket)
    27602761# define RTUdpRead                                      RT_MANGLER(RTUdpRead)
    27612762# define RTUdpServerCreate                              RT_MANGLER(RTUdpServerCreate)
  • trunk/include/iprt/udp.h

    r96407 r96864  
    175175RTR3DECL(int) RTUdpCreateClientSocket(const char *pszAddress, uint32_t uPort, PRTNETADDR pLocalAddr, PRTSOCKET pSock);
    176176
     177/**
     178 * Create a data socket acting as a server.
     179 *
     180 * @returns iprt status code.
     181 * @param   pszAddress          The address to connect to.
     182 * @param   uPort               The port to connect to.
     183 * @param   pSock               Where to store the handle to the established connection.
     184 */
     185RTR3DECL(int) RTUdpCreateServerSocket(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
     186
    177187/** @} */
    178188RT_C_DECLS_END
  • trunk/src/VBox/Runtime/r3/udp.cpp

    r96475 r96864  
    736736}
    737737
     738
     739RTR3DECL(int) RTUdpCreateServerSocket(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock)
     740{
     741    /*
     742     * Validate input.
     743     */
     744    AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
     745    AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
     746    AssertPtrReturn(pSock, VERR_INVALID_POINTER);
     747
     748    /*
     749     * Resolve the address.
     750     */
     751    RTNETADDR LocalAddr;
     752    int rc = RTSocketParseInetAddress(pszAddress, uPort, &LocalAddr);
     753    if (RT_FAILURE(rc))
     754        return rc;
     755
     756    /*
     757     * Setting up socket.
     758     */
     759    RTSOCKET Sock;
     760    rc = rtSocketCreate(&Sock, AF_INET, SOCK_DGRAM, IPPROTO_UDP, false /*fInheritable*/);
     761    if (RT_SUCCESS(rc))
     762    {
     763        /*
     764         * Set socket options.
     765         */
     766        int fFlag = 1;
     767        if (!rtSocketSetOpt(Sock, SOL_SOCKET, SO_REUSEADDR, &fFlag, sizeof(fFlag)))
     768        {
     769            /*
     770             * Bind a name to the socket.
     771             */
     772            rc = rtSocketBind(Sock, &LocalAddr);
     773            if (RT_SUCCESS(rc))
     774            {
     775                *pSock = Sock;
     776                return VINF_SUCCESS;
     777            }
     778        }
     779        RTSocketClose(Sock);
     780    }
     781    return rc;
     782}
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette