1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- udp.c
- Abstract:
- This module implements the User Datagram Protocol (UDP).
- Author:
- Evan Green 5-Apr-2013
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- //
- // Protocol drivers are supposed to be able to stand on their own (ie be able to
- // be implemented outside the core net library). For the builtin ones, avoid
- // including netcore.h, but still redefine those functions that would otherwise
- // generate imports.
- //
- #define NET_API __DLLEXPORT
- #include <minoca/kernel/driver.h>
- #include <minoca/net/netdrv.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Define the allocation tag used by the UDP socket protocol.
- //
- #define UDP_PROTOCOL_ALLOCATION_TAG 0x21706455 // '!pdU'
- //
- // Define the maximum supported packet size of the UDP protocol, including the
- // UDP headers.
- //
- #define UDP_MAX_PACKET_SIZE 0xFFFF
- //
- // Define the default size of UDP's receive data buffer, in bytes.
- //
- #define UDP_DEFAULT_RECEIVE_BUFFER_SIZE (256 * _1KB)
- //
- // Define the minimum receive buffer size.
- //
- #define UDP_MIN_RECEIVE_BUFFER_SIZE _2KB
- //
- // Define the default minimum number of bytes necessary for the UDP socket to
- // become readable.
- //
- #define UDP_DEFAULT_RECEIVE_MINIMUM 1
- //
- // Define the minmum number of bytes necessary for UDP sockets to become
- // writable. There is no minimum and bytes are immediately sent on the wire.
- //
- #define UDP_SEND_MINIMUM 1
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure defines a UDP data socket.
- Members:
- NetSocket - Stores the common core networking parameters.
- ReceivedPacketList - Stores the list of packets ready to be read by the
- user.
- ReceiveLock - Stores the lock that protects the received packets list,
- dropped packet count, and various receive buffer parameters. This lock
- must always be acquired at low level.
- ReceiveBufferTotalSize - Stores the total size of the receive buffer, in
- bytes. Packets that are received but will not fit in the buffer are
- discarded.
- ReceiveBufferFreeSize - Stores the receive buffer's free space, in bytes.
- Packets that are received but do not fit in the free space are
- discarded.
- ReceiveTimeout - Stores the maximum amount of time, in milliseconds, that
- the socket will wait when receiving data.
- ReceiveMinimum - Stores the minimum amount of bytes that must be available
- before the socket is made readable. This is ignored.
- DroppedPacketCount - Stores the number of packets that have been dropped
- because the receive queue was full.
- ShutdownTypes - Stores the mask of shutdowns that have occurred on this
- socket.
- MaxPacketSize - Stores the maximum size of UDP datagrams.
- --*/
- typedef struct _UDP_SOCKET {
- NET_SOCKET NetSocket;
- LIST_ENTRY ReceivedPacketList;
- PQUEUED_LOCK ReceiveLock;
- ULONG ReceiveBufferTotalSize;
- ULONG ReceiveBufferFreeSize;
- ULONG ReceiveTimeout;
- ULONG ReceiveMinimum;
- ULONG DroppedPacketCount;
- ULONG ShutdownTypes;
- USHORT MaxPacketSize;
- } UDP_SOCKET, *PUDP_SOCKET;
- /*++
- Structure Description:
- This structure defines a UDP protocol header, which is pretty darn simple.
- Members:
- SourcePort - Stores the optional source port number (use 0 if not supplied).
- DestinationPort - Stores the destination port number.
- Length - Stores the length of the header and data.
- Checksum - Stores the optional checksum. Set to 0 if not supplied. The
- checksum is the one's complement of the one's complement sum of the
- entire header plus data, padded with zeros if needed to be a multiple of
- two octets. A pseudo IP header is used for the calculation.
- --*/
- typedef struct _UDP_HEADER {
- USHORT SourcePort;
- USHORT DestinationPort;
- USHORT Length;
- USHORT Checksum;
- } PACKED UDP_HEADER, *PUDP_HEADER;
- /*++
- Structure Description:
- This structure defines a UDP received message.
- Members:
- ListEntry - Stores pointers to the next and previous packets.
- Address - Stores the network address where this data came from.
- DataBuffer - Stores a pointer to the buffer containing the actual data.
- Size - Stores the number of bytes in the data buffer.
- --*/
- typedef struct _UDP_RECEIVED_PACKET {
- LIST_ENTRY ListEntry;
- NETWORK_ADDRESS Address;
- PVOID DataBuffer;
- ULONG Size;
- } UDP_RECEIVED_PACKET, *PUDP_RECEIVED_PACKET;
- /*++
- Structure Description:
- This structure defines a UDP socket option.
- Members:
- InformationType - Stores the information type for the socket option.
- Option - Stores the type-specific option identifier.
- Size - Stores the size of the option value, in bytes.
- SetAllowed - Stores a boolean indicating whether or not the option is
- allowed to be set.
- --*/
- typedef struct _UDP_SOCKET_OPTION {
- SOCKET_INFORMATION_TYPE InformationType;
- UINTN Option;
- UINTN Size;
- BOOL SetAllowed;
- } UDP_SOCKET_OPTION, *PUDP_SOCKET_OPTION;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- KSTATUS
- NetpUdpCreateSocket (
- PNET_PROTOCOL_ENTRY ProtocolEntry,
- PNET_NETWORK_ENTRY NetworkEntry,
- ULONG NetworkProtocol,
- PNET_SOCKET *NewSocket
- );
- VOID
- NetpUdpDestroySocket (
- PNET_SOCKET Socket
- );
- KSTATUS
- NetpUdpBindToAddress (
- PNET_SOCKET Socket,
- PNET_LINK Link,
- PNETWORK_ADDRESS Address
- );
- KSTATUS
- NetpUdpListen (
- PNET_SOCKET Socket
- );
- KSTATUS
- NetpUdpAccept (
- PNET_SOCKET Socket,
- PIO_HANDLE *NewConnectionSocket,
- PNETWORK_ADDRESS RemoteAddress
- );
- KSTATUS
- NetpUdpConnect (
- PNET_SOCKET Socket,
- PNETWORK_ADDRESS Address
- );
- KSTATUS
- NetpUdpClose (
- PNET_SOCKET Socket
- );
- KSTATUS
- NetpUdpShutdown (
- PNET_SOCKET Socket,
- ULONG ShutdownType
- );
- KSTATUS
- NetpUdpSend (
- BOOL FromKernelMode,
- PNET_SOCKET Socket,
- PSOCKET_IO_PARAMETERS Parameters,
- PIO_BUFFER IoBuffer
- );
- VOID
- NetpUdpProcessReceivedData (
- PNET_LINK Link,
- PNET_PACKET_BUFFER Packet,
- PNETWORK_ADDRESS SourceAddress,
- PNETWORK_ADDRESS DestinationAddress,
- PNET_PROTOCOL_ENTRY ProtocolEntry
- );
- KSTATUS
- NetpUdpProcessReceivedSocketData (
- PNET_LINK Link,
- PNET_SOCKET Socket,
- PNET_PACKET_BUFFER Packet,
- PNETWORK_ADDRESS SourceAddress,
- PNETWORK_ADDRESS DestinationAddress
- );
- KSTATUS
- NetpUdpReceive (
- BOOL FromKernelMode,
- PNET_SOCKET Socket,
- PSOCKET_IO_PARAMETERS Parameters,
- PIO_BUFFER IoBuffer
- );
- KSTATUS
- NetpUdpGetSetInformation (
- PNET_SOCKET Socket,
- SOCKET_INFORMATION_TYPE InformationType,
- UINTN SocketOption,
- PVOID Data,
- PUINTN DataSize,
- BOOL Set
- );
- KSTATUS
- NetpUdpUserControl (
- PNET_SOCKET Socket,
- ULONG CodeNumber,
- BOOL FromKernelMode,
- PVOID ContextBuffer,
- UINTN ContextBufferSize
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- NET_PROTOCOL_ENTRY NetUdpProtocol = {
- {NULL, NULL},
- NetSocketDatagram,
- SOCKET_INTERNET_PROTOCOL_UDP,
- NULL,
- NULL,
- {{0}, {0}, {0}},
- {
- NetpUdpCreateSocket,
- NetpUdpDestroySocket,
- NetpUdpBindToAddress,
- NetpUdpListen,
- NetpUdpAccept,
- NetpUdpConnect,
- NetpUdpClose,
- NetpUdpShutdown,
- NetpUdpSend,
- NetpUdpProcessReceivedData,
- NetpUdpProcessReceivedSocketData,
- NetpUdpReceive,
- NetpUdpGetSetInformation,
- NetpUdpUserControl
- }
- };
- UDP_SOCKET_OPTION NetUdpSocketOptions[] = {
- {
- SocketInformationBasic,
- SocketBasicOptionSendBufferSize,
- sizeof(ULONG),
- TRUE
- },
- {
- SocketInformationBasic,
- SocketBasicOptionSendMinimum,
- sizeof(ULONG),
- FALSE
- },
- {
- SocketInformationBasic,
- SocketBasicOptionReceiveBufferSize,
- sizeof(ULONG),
- TRUE
- },
- {
- SocketInformationBasic,
- SocketBasicOptionReceiveMinimum,
- sizeof(ULONG),
- TRUE
- },
- {
- SocketInformationBasic,
- SocketBasicOptionReceiveTimeout,
- sizeof(SOCKET_TIME),
- TRUE
- },
- };
- //
- // ------------------------------------------------------------------ Functions
- //
- VOID
- NetpUdpInitialize (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes support for UDP sockets.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- KSTATUS Status;
- //
- // Register the UDP socket handlers with the core networking library.
- //
- Status = NetRegisterProtocol(&NetUdpProtocol, NULL);
- if (!KSUCCESS(Status)) {
- ASSERT(FALSE);
- }
- return;
- }
- KSTATUS
- NetpUdpCreateSocket (
- PNET_PROTOCOL_ENTRY ProtocolEntry,
- PNET_NETWORK_ENTRY NetworkEntry,
- ULONG NetworkProtocol,
- PNET_SOCKET *NewSocket
- )
- /*++
- Routine Description:
- This routine allocates resources associated with a new socket. The protocol
- driver is responsible for allocating the structure (with additional length
- for any of its context). The core networking library will fill in the
- common header when this routine returns.
- Arguments:
- ProtocolEntry - Supplies a pointer to the protocol information.
- NetworkEntry - Supplies a pointer to the network information.
- NetworkProtocol - Supplies the raw protocol value for this socket used on
- the network. This value is network specific.
- NewSocket - Supplies a pointer where a pointer to a newly allocated
- socket structure will be returned. The caller is responsible for
- allocating the socket (and potentially a larger structure for its own
- context). The core network library will fill in the standard socket
- structure after this routine returns.
- Return Value:
- Status code.
- --*/
- {
- ULONG MaxPacketSize;
- PNET_SOCKET NetSocket;
- PNET_PACKET_SIZE_INFORMATION PacketSizeInformation;
- KSTATUS Status;
- PUDP_SOCKET UdpSocket;
- ASSERT(ProtocolEntry->Type == NetSocketDatagram);
- ASSERT((ProtocolEntry->ParentProtocolNumber ==
- SOCKET_INTERNET_PROTOCOL_UDP) &&
- (NetworkProtocol == ProtocolEntry->ParentProtocolNumber));
- NetSocket = NULL;
- UdpSocket = MmAllocatePagedPool(sizeof(UDP_SOCKET),
- UDP_PROTOCOL_ALLOCATION_TAG);
- if (UdpSocket == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto UdpCreateSocketEnd;
- }
- RtlZeroMemory(UdpSocket, sizeof(UDP_SOCKET));
- NetSocket = &(UdpSocket->NetSocket);
- NetSocket->KernelSocket.Protocol = NetworkProtocol;
- NetSocket->KernelSocket.ReferenceCount = 1;
- INITIALIZE_LIST_HEAD(&(UdpSocket->ReceivedPacketList));
- UdpSocket->ReceiveTimeout = WAIT_TIME_INDEFINITE;
- UdpSocket->ReceiveBufferTotalSize = UDP_DEFAULT_RECEIVE_BUFFER_SIZE;
- UdpSocket->ReceiveBufferFreeSize = UdpSocket->ReceiveBufferTotalSize;
- UdpSocket->ReceiveMinimum = UDP_DEFAULT_RECEIVE_MINIMUM;
- UdpSocket->MaxPacketSize = UDP_MAX_PACKET_SIZE;
- UdpSocket->ReceiveLock = KeCreateQueuedLock();
- if (UdpSocket->ReceiveLock == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto UdpCreateSocketEnd;
- }
- //
- // Give the lower layers a chance to initialize. Start the maximum packet
- // size at the largest possible value.
- //
- PacketSizeInformation = &(NetSocket->PacketSizeInformation);
- PacketSizeInformation->MaxPacketSize = MAX_ULONG;
- Status = NetworkEntry->Interface.InitializeSocket(ProtocolEntry,
- NetworkEntry,
- NetworkProtocol,
- NetSocket);
- if (!KSUCCESS(Status)) {
- goto UdpCreateSocketEnd;
- }
- //
- // If the max packet size is greater than what is allowed for a UDP packet
- // plus all the previous headers and footers, then truncate the max packet
- // size. Note that the UDP max packet size includes the UDP header.
- //
- MaxPacketSize = PacketSizeInformation->HeaderSize +
- UDP_MAX_PACKET_SIZE +
- PacketSizeInformation->FooterSize;
- if (PacketSizeInformation->MaxPacketSize > MaxPacketSize) {
- PacketSizeInformation->MaxPacketSize = MaxPacketSize;
- }
- //
- // Add the UDP header size to the protocol header size.
- //
- PacketSizeInformation->HeaderSize += sizeof(UDP_HEADER);
- Status = STATUS_SUCCESS;
- UdpCreateSocketEnd:
- if (!KSUCCESS(Status)) {
- if (UdpSocket != NULL) {
- if (UdpSocket->ReceiveLock != NULL) {
- KeDestroyQueuedLock(UdpSocket->ReceiveLock);
- }
- MmFreePagedPool(UdpSocket);
- UdpSocket = NULL;
- NetSocket = NULL;
- }
- }
- *NewSocket = NetSocket;
- return Status;
- }
- VOID
- NetpUdpDestroySocket (
- PNET_SOCKET Socket
- )
- /*++
- Routine Description:
- This routine destroys resources associated with an open socket, officially
- marking the end of the kernel and core networking library's knowledge of
- this structure.
- Arguments:
- Socket - Supplies a pointer to the socket to destroy. The core networking
- library will have already destroyed any resources inside the common
- header, the protocol should not reach through any pointers inside the
- socket header except the protocol and network entries.
- Return Value:
- None. This routine is responsible for freeing the memory associated with
- the socket structure itself.
- --*/
- {
- PUDP_RECEIVED_PACKET Packet;
- PUDP_SOCKET UdpSocket;
- UdpSocket = (PUDP_SOCKET)Socket;
- //
- // Loop through and free any leftover packets.
- //
- KeAcquireQueuedLock(UdpSocket->ReceiveLock);
- while (!LIST_EMPTY(&(UdpSocket->ReceivedPacketList))) {
- Packet = LIST_VALUE(UdpSocket->ReceivedPacketList.Next,
- UDP_RECEIVED_PACKET,
- ListEntry);
- LIST_REMOVE(&(Packet->ListEntry));
- UdpSocket->ReceiveBufferFreeSize += Packet->Size;
- MmFreePagedPool(Packet);
- }
- ASSERT(UdpSocket->ReceiveBufferFreeSize ==
- UdpSocket->ReceiveBufferTotalSize);
- KeReleaseQueuedLock(UdpSocket->ReceiveLock);
- KeDestroyQueuedLock(UdpSocket->ReceiveLock);
- MmFreePagedPool(UdpSocket);
- return;
- }
- KSTATUS
- NetpUdpBindToAddress (
- PNET_SOCKET Socket,
- PNET_LINK Link,
- PNETWORK_ADDRESS Address
- )
- /*++
- Routine Description:
- This routine binds the given socket to the specified network address.
- Usually this is a no-op for the protocol, it's simply responsible for
- passing the request down to the network layer.
- Arguments:
- Socket - Supplies a pointer to the socket to bind.
- Link - Supplies an optional pointer to a link to bind to.
- Address - Supplies a pointer to the address to bind the socket to.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- if (Socket->LocalAddress.Domain != NetDomainInvalid) {
- Status = STATUS_INVALID_PARAMETER;
- goto UdpBindToAddressEnd;
- }
- //
- // Currently only IPv4 addresses are supported.
- //
- if (Address->Domain != NetDomainIp4) {
- Status = STATUS_NOT_SUPPORTED;
- goto UdpBindToAddressEnd;
- }
- //
- // Pass the request down to the network layer.
- //
- Status = Socket->Network->Interface.BindToAddress(Socket, Link, Address);
- if (!KSUCCESS(Status)) {
- goto UdpBindToAddressEnd;
- }
- //
- // Begin listening immediately, as there is no explicit listen step for UDP.
- //
- Status = Socket->Network->Interface.Listen(Socket);
- if (!KSUCCESS(Status)) {
- goto UdpBindToAddressEnd;
- }
- IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_OUT, TRUE);
- UdpBindToAddressEnd:
- return Status;
- }
- KSTATUS
- NetpUdpListen (
- PNET_SOCKET Socket
- )
- /*++
- Routine Description:
- This routine adds a bound socket to the list of listening sockets,
- officially allowing clients to attempt to connect to it.
- Arguments:
- Socket - Supplies a pointer to the socket to mark as listning.
- Return Value:
- Status code.
- --*/
- {
- return STATUS_NOT_SUPPORTED;
- }
- KSTATUS
- NetpUdpAccept (
- PNET_SOCKET Socket,
- PIO_HANDLE *NewConnectionSocket,
- PNETWORK_ADDRESS RemoteAddress
- )
- /*++
- Routine Description:
- This routine accepts an incoming connection on a listening connection-based
- socket.
- Arguments:
- Socket - Supplies a pointer to the socket to accept a connection from.
- NewConnectionSocket - Supplies a pointer where a new socket will be
- returned that represents the accepted connection with the remote
- host.
- RemoteAddress - Supplies a pointer where the address of the connected
- remote host will be returned.
- Return Value:
- Status code.
- --*/
- {
- return STATUS_NOT_SUPPORTED;
- }
- KSTATUS
- NetpUdpConnect (
- PNET_SOCKET Socket,
- PNETWORK_ADDRESS Address
- )
- /*++
- Routine Description:
- This routine attempts to make an outgoing connection to a server.
- Arguments:
- Socket - Supplies a pointer to the socket to use for the connection.
- Address - Supplies a pointer to the address to connect to.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- //
- // Pass the request down to the network layer.
- //
- Status = Socket->Network->Interface.Connect(Socket, Address);
- if (!KSUCCESS(Status)) {
- goto UdpConnectEnd;
- }
- IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_OUT, TRUE);
- UdpConnectEnd:
- return Status;
- }
- KSTATUS
- NetpUdpClose (
- PNET_SOCKET Socket
- )
- /*++
- Routine Description:
- This routine closes a socket connection.
- Arguments:
- Socket - Supplies a pointer to the socket to shut down.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- //
- // Close it at the lower level and then release the reference taken on
- // create if the close was successful.
- //
- Status = Socket->Network->Interface.Close(Socket);
- if (!KSUCCESS(Status)) {
- goto UdpCloseEnd;
- }
- IoSocketReleaseReference(&(Socket->KernelSocket));
- UdpCloseEnd:
- return Status;
- }
- KSTATUS
- NetpUdpShutdown (
- PNET_SOCKET Socket,
- ULONG ShutdownType
- )
- /*++
- Routine Description:
- This routine shuts down communication with a given socket.
- Arguments:
- Socket - Supplies a pointer to the socket.
- ShutdownType - Supplies the shutdown type to perform. See the
- SOCKET_SHUTDOWN_* definitions.
- Return Value:
- Status code.
- --*/
- {
- PUDP_SOCKET UdpSocket;
- //
- // Shutdown is not supported unless the socket is connected.
- //
- if (Socket->RemoteAddress.Domain == NetDomainInvalid) {
- return STATUS_NOT_CONNECTED;
- }
- UdpSocket = (PUDP_SOCKET)Socket;
- RtlAtomicOr32(&(UdpSocket->ShutdownTypes), ShutdownType);
- //
- // Signal the read event if the read end was shut down.
- //
- if ((ShutdownType & SOCKET_SHUTDOWN_READ) != 0) {
- KeAcquireQueuedLock(UdpSocket->ReceiveLock);
- IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_IN, TRUE);
- KeReleaseQueuedLock(UdpSocket->ReceiveLock);
- }
- if ((ShutdownType & SOCKET_SHUTDOWN_WRITE) != 0) {
- IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_OUT, TRUE);
- }
- return STATUS_SUCCESS;
- }
- KSTATUS
- NetpUdpSend (
- BOOL FromKernelMode,
- PNET_SOCKET Socket,
- PSOCKET_IO_PARAMETERS Parameters,
- PIO_BUFFER IoBuffer
- )
- /*++
- Routine Description:
- This routine sends the given data buffer through the network using a
- specific protocol.
- Arguments:
- FromKernelMode - Supplies a boolean indicating whether the request is
- coming from kernel mode (TRUE) or user mode (FALSE).
- Socket - Supplies a pointer to the socket to send the data to.
- Parameters - Supplies a pointer to the socket I/O parameters. This will
- always be a kernel mode pointer.
- IoBuffer - Supplies a pointer to the I/O buffer containing the data to
- send.
- Return Value:
- Status code.
- --*/
- {
- UINTN BytesComplete;
- PNETWORK_ADDRESS Destination;
- NETWORK_ADDRESS DestinationLocal;
- ULONG Flags;
- ULONG FooterSize;
- ULONG HeaderSize;
- PNET_LINK Link;
- NET_LINK_LOCAL_ADDRESS LinkInformation;
- PNET_SOCKET_LINK_OVERRIDE LinkOverride;
- NET_SOCKET_LINK_OVERRIDE LinkOverrideBuffer;
- NETWORK_ADDRESS LocalAddress;
- USHORT NetworkLocalPort;
- USHORT NetworkRemotePort;
- PNET_PACKET_BUFFER Packet;
- NET_PACKET_LIST PacketList;
- UINTN Size;
- USHORT SourcePort;
- KSTATUS Status;
- PUDP_HEADER UdpHeader;
- PUDP_SOCKET UdpSocket;
- ASSERT(Socket->PacketSizeInformation.MaxPacketSize > sizeof(UDP_HEADER));
- BytesComplete = 0;
- Link = NULL;
- LinkInformation.Link = NULL;
- LinkOverride = NULL;
- NET_INITIALIZE_PACKET_LIST(&PacketList);
- Size = Parameters->Size;
- UdpSocket = (PUDP_SOCKET)Socket;
- Flags = Parameters->SocketIoFlags;
- Parameters->SocketIoFlags = 0;
- Destination = Parameters->NetworkAddress;
- if ((Destination != NULL) && (FromKernelMode == FALSE)) {
- Status = MmCopyFromUserMode(&DestinationLocal,
- Destination,
- sizeof(NETWORK_ADDRESS));
- Destination = &DestinationLocal;
- if (!KSUCCESS(Status)) {
- goto UdpSendEnd;
- }
- }
- if ((Destination == NULL) ||
- (Destination->Domain == NetDomainInvalid)) {
- if (Socket->RemoteAddress.Port == 0) {
- Status = STATUS_NOT_CONFIGURED;
- goto UdpSendEnd;
- }
- Destination = &(Socket->RemoteAddress);
- }
- //
- // Fail if the socket has already been closed for writing.
- //
- if ((UdpSocket->ShutdownTypes & SOCKET_SHUTDOWN_WRITE) != 0) {
- if ((Flags & SOCKET_IO_NO_SIGNAL) != 0) {
- Status = STATUS_BROKEN_PIPE_SILENT;
- } else {
- Status = STATUS_BROKEN_PIPE;
- }
- goto UdpSendEnd;
- }
- //
- // Fail if the socket's link went down.
- //
- if ((Socket->KernelSocket.IoState->Events & POLL_EVENT_DISCONNECTED) != 0) {
- Status = STATUS_NO_NETWORK_CONNECTION;
- goto UdpSendEnd;
- }
- //
- // Fail if there's ancillary data.
- //
- if (Parameters->ControlDataSize != 0) {
- Status = STATUS_NOT_SUPPORTED;
- goto UdpSendEnd;
- }
- //
- // If the size, including the header, is greater than the UDP socket's
- // maximum packet size, fail.
- //
- if ((Size + sizeof(UDP_HEADER)) > UdpSocket->MaxPacketSize) {
- Status = STATUS_MESSAGE_TOO_LONG;
- goto UdpSendEnd;
- }
- //
- // If the socket is not yet bound, then at least try to bind it to a local
- // port. This bind attempt may race with another bind attempt, but leave it
- // to the socket owner to synchronize bind and send.
- //
- if (Socket->BindingType == SocketBindingInvalid) {
- RtlZeroMemory(&LocalAddress, sizeof(NETWORK_ADDRESS));
- LocalAddress.Domain = Socket->Network->Domain;
- Status = NetpUdpBindToAddress(Socket, NULL, &LocalAddress);
- if (!KSUCCESS(Status)) {
- goto UdpSendEnd;
- }
- }
- //
- // The socket needs to at least be bound to a local port.
- //
- ASSERT(Socket->LocalAddress.Port != 0);
- //
- // If the socket has no link, then try to find a link that can service the
- // destination address.
- //
- if (Socket->Link == NULL) {
- Status = NetFindLinkForRemoteAddress(Destination, &LinkInformation);
- if (KSUCCESS(Status)) {
- //
- // The link override should use the socket's port.
- //
- LinkInformation.LocalAddress.Port = Socket->LocalAddress.Port;
- //
- // Synchronously get the correct header, footer, and max packet
- // sizes.
- //
- Status = NetInitializeSocketLinkOverride(Socket,
- &LinkInformation,
- &LinkOverrideBuffer);
- if (KSUCCESS(Status)) {
- LinkOverride = &LinkOverrideBuffer;
- }
- }
- if (!KSUCCESS(Status) && (Status != STATUS_CONNECTION_EXISTS)) {
- goto UdpSendEnd;
- }
- }
- //
- // Set the necessary local variables based on whether the socket's link or
- // an override link will be used to send the data.
- //
- if (LinkOverride != NULL) {
- ASSERT(LinkOverride == &LinkOverrideBuffer);
- Link = LinkOverrideBuffer.LinkInformation.Link;
- HeaderSize = LinkOverrideBuffer.PacketSizeInformation.HeaderSize;
- FooterSize = LinkOverrideBuffer.PacketSizeInformation.FooterSize;
- SourcePort = LinkOverrideBuffer.LinkInformation.LocalAddress.Port;
- } else {
- ASSERT(Socket->Link != NULL);
- Link = Socket->Link;
- HeaderSize = Socket->PacketSizeInformation.HeaderSize;
- FooterSize = Socket->PacketSizeInformation.FooterSize;
- SourcePort = Socket->LocalAddress.Port;
- }
- NetworkLocalPort = CPU_TO_NETWORK16(SourcePort);
- NetworkRemotePort = CPU_TO_NETWORK16(Destination->Port);
- //
- // Allocate a buffer for the packet.
- //
- Status = NetAllocateBuffer(HeaderSize,
- Size,
- FooterSize,
- Link,
- 0,
- &Packet);
- if (!KSUCCESS(Status)) {
- goto UdpSendEnd;
- }
- NET_ADD_PACKET_TO_LIST(Packet, &PacketList);
- //
- // Copy the packet data.
- //
- Status = MmCopyIoBufferData(IoBuffer,
- Packet->Buffer + Packet->DataOffset,
- BytesComplete,
- Size - BytesComplete,
- FALSE);
- if (!KSUCCESS(Status)) {
- goto UdpSendEnd;
- }
- //
- // Add the UDP header.
- //
- ASSERT(Packet->DataOffset >= sizeof(UDP_HEADER));
- Packet->DataOffset -= sizeof(UDP_HEADER);
- UdpHeader = (PUDP_HEADER)(Packet->Buffer + Packet->DataOffset);
- UdpHeader->SourcePort = NetworkLocalPort;
- UdpHeader->DestinationPort = NetworkRemotePort;
- UdpHeader->Length = CPU_TO_NETWORK16(Size + sizeof(UDP_HEADER));
- UdpHeader->Checksum = 0;
- if ((Link->Properties.ChecksumFlags &
- NET_LINK_CHECKSUM_FLAG_TRANSMIT_UDP_OFFLOAD) != 0) {
- Packet->Flags |= NET_PACKET_FLAG_UDP_CHECKSUM_OFFLOAD;
- }
- //
- // Send the datagram down to the network layer, which may have to send it
- // in fragments.
- //
- Status = Socket->Network->Interface.Send(Socket,
- Destination,
- LinkOverride,
- &PacketList);
- if (!KSUCCESS(Status)) {
- goto UdpSendEnd;
- }
- Packet = NULL;
- BytesComplete = Size;
- UdpSendEnd:
- Parameters->Size = BytesComplete;
- if (!KSUCCESS(Status)) {
- NetDestroyBufferList(&PacketList);
- }
- if (LinkInformation.Link != NULL) {
- NetLinkReleaseReference(LinkInformation.Link);
- }
- if (LinkOverride == &LinkOverrideBuffer) {
- ASSERT(LinkOverrideBuffer.LinkInformation.Link != NULL);
- NetLinkReleaseReference(LinkOverrideBuffer.LinkInformation.Link);
- }
- return Status;
- }
- VOID
- NetpUdpProcessReceivedData (
- PNET_LINK Link,
- PNET_PACKET_BUFFER Packet,
- PNETWORK_ADDRESS SourceAddress,
- PNETWORK_ADDRESS DestinationAddress,
- PNET_PROTOCOL_ENTRY ProtocolEntry
- )
- /*++
- Routine Description:
- This routine is called to process a received packet.
- Arguments:
- Link - Supplies a pointer to the link that received the packet.
- Packet - Supplies a pointer to a structure describing the incoming packet.
- This structure may be used as a scratch space while this routine
- executes and the packet travels up the stack, but will not be accessed
- after this routine returns.
- SourceAddress - Supplies a pointer to the source (remote) address that the
- packet originated from. This memory will not be referenced once the
- function returns, it can be stack allocated.
- DestinationAddress - Supplies a pointer to the destination (local) address
- that the packet is heading to. This memory will not be referenced once
- the function returns, it can be stack allocated.
- ProtocolEntry - Supplies a pointer to this protocol's protocol entry.
- Return Value:
- None. When the function returns, the memory associated with the packet may
- be reclaimed and reused.
- --*/
- {
- PUDP_HEADER Header;
- USHORT Length;
- PNET_SOCKET Socket;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- Header = (PUDP_HEADER)(Packet->Buffer + Packet->DataOffset);
- Length = NETWORK_TO_CPU16(Header->Length);
- if (Length > (Packet->FooterOffset - Packet->DataOffset)) {
- RtlDebugPrint("Invalid UDP length %d is bigger than packet data, "
- "which is only %d bytes large.\n",
- Length,
- (Packet->FooterOffset - Packet->DataOffset));
- return;
- }
- SourceAddress->Port = NETWORK_TO_CPU16(Header->SourcePort);
- DestinationAddress->Port = NETWORK_TO_CPU16(Header->DestinationPort);
- //
- // Find a socket willing to take this packet.
- //
- Socket = NetFindSocket(ProtocolEntry, DestinationAddress, SourceAddress);
- if (Socket == NULL) {
- return;
- }
- //
- // Pass the packet onto the socket for copying and safe keeping until the
- // data is read.
- //
- NetpUdpProcessReceivedSocketData(Link,
- Socket,
- Packet,
- SourceAddress,
- DestinationAddress);
- //
- // Release the reference on the socket added by the find socket call.
- //
- IoSocketReleaseReference(&(Socket->KernelSocket));
- return;
- }
- KSTATUS
- NetpUdpProcessReceivedSocketData (
- PNET_LINK Link,
- PNET_SOCKET Socket,
- PNET_PACKET_BUFFER Packet,
- PNETWORK_ADDRESS SourceAddress,
- PNETWORK_ADDRESS DestinationAddress
- )
- /*++
- Routine Description:
- This routine is called for a particular socket to process a received packet
- that was sent to it.
- Arguments:
- Link - Supplies a pointer to the network link that received the packet.
- Socket - Supplies a pointer to the socket that received the packet.
- Packet - Supplies a pointer to a structure describing the incoming packet.
- Use of this structure depends on its flags. If it is a multicast
- packet, then it cannot be modified by this routine. Otherwise it can
- be used as scratch space and modified.
- SourceAddress - Supplies a pointer to the source (remote) address that the
- packet originated from. This memory will not be referenced once the
- function returns, it can be stack allocated.
- DestinationAddress - Supplies a pointer to the destination (local) address
- that the packet is heading to. This memory will not be referenced once
- the function returns, it can be stack allocated.
- Return Value:
- Status code.
- --*/
- {
- ULONG AllocationSize;
- PUDP_HEADER Header;
- USHORT Length;
- USHORT PayloadLength;
- KSTATUS Status;
- PUDP_RECEIVED_PACKET UdpPacket;
- PUDP_SOCKET UdpSocket;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- UdpSocket = (PUDP_SOCKET)Socket;
- Header = (PUDP_HEADER)(Packet->Buffer + Packet->DataOffset);
- Length = NETWORK_TO_CPU16(Header->Length);
- if (Length > (Packet->FooterOffset - Packet->DataOffset)) {
- RtlDebugPrint("Invalid UDP length %d is bigger than packet data, "
- "which is only %d bytes large.\n",
- Length,
- (Packet->FooterOffset - Packet->DataOffset));
- Status = STATUS_BUFFER_TOO_SMALL;
- goto ProcessReceivedSocketDataEnd;
- }
- //
- // Since the socket has already been matched, the source and destination
- // addresses better be completely filled in.
- //
- ASSERT(SourceAddress->Port == NETWORK_TO_CPU16(Header->SourcePort));
- ASSERT(DestinationAddress->Port ==
- NETWORK_TO_CPU16(Header->DestinationPort));
- //
- // Create a received packet entry for this data.
- //
- PayloadLength = Length - sizeof(UDP_HEADER);
- AllocationSize = sizeof(UDP_RECEIVED_PACKET) + PayloadLength;
- UdpPacket = MmAllocatePagedPool(AllocationSize,
- UDP_PROTOCOL_ALLOCATION_TAG);
- if (UdpPacket == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto ProcessReceivedSocketDataEnd;
- }
- RtlCopyMemory(&(UdpPacket->Address),
- SourceAddress,
- sizeof(NETWORK_ADDRESS));
- UdpPacket->DataBuffer = (PVOID)(UdpPacket + 1);
- UdpPacket->Size = PayloadLength;
- //
- // Copy the packet contents into the receive packet buffer.
- //
- RtlCopyMemory(UdpPacket->DataBuffer, Header + 1, PayloadLength);
- //
- // Work to insert the packet on the list of received packets.
- //
- KeAcquireQueuedLock(UdpSocket->ReceiveLock);
- if (UdpPacket->Size <= UdpSocket->ReceiveBufferFreeSize) {
- INSERT_BEFORE(&(UdpPacket->ListEntry),
- &(UdpSocket->ReceivedPacketList));
- UdpSocket->ReceiveBufferFreeSize -= UdpPacket->Size;
- ASSERT(UdpSocket->ReceiveBufferFreeSize <
- UdpSocket->ReceiveBufferTotalSize);
- //
- // One packet is always enough to notify a waiting receiver.
- //
- IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_IN, TRUE);
- UdpPacket = NULL;
- } else {
- UdpSocket->DroppedPacketCount += 1;
- }
- KeReleaseQueuedLock(UdpSocket->ReceiveLock);
- //
- // If the packet wasn't nulled out, that's an indication it wasn't added to
- // the list, so free it up.
- //
- if (UdpPacket != NULL) {
- MmFreePagedPool(UdpPacket);
- }
- Status = STATUS_SUCCESS;
- ProcessReceivedSocketDataEnd:
- return Status;
- }
- KSTATUS
- NetpUdpReceive (
- BOOL FromKernelMode,
- PNET_SOCKET Socket,
- PSOCKET_IO_PARAMETERS Parameters,
- PIO_BUFFER IoBuffer
- )
- /*++
- Routine Description:
- This routine is called by the user to receive data from the socket on a
- particular protocol.
- Arguments:
- FromKernelMode - Supplies a boolean indicating whether the request is
- coming from kernel mode (TRUE) or user mode (FALSE).
- Socket - Supplies a pointer to the socket to receive data from.
- Parameters - Supplies a pointer to the socket I/O parameters.
- IoBuffer - Supplies a pointer to the I/O buffer where the received data
- will be returned.
- Return Value:
- STATUS_SUCCESS if any bytes were read.
- STATUS_TIMEOUT if the request timed out.
- STATUS_BUFFER_TOO_SMALL if the incoming datagram was too large for the
- buffer. The remainder of the datagram is discarded in this case.
- Other error codes on other failures.
- --*/
- {
- UINTN BytesComplete;
- ULONG CopySize;
- ULONGLONG CurrentTime;
- ULONGLONG EndTime;
- ULONG Flags;
- BOOL LockHeld;
- PUDP_RECEIVED_PACKET Packet;
- PLIST_ENTRY PacketEntry;
- ULONG ReturnedEvents;
- ULONG ReturnSize;
- UINTN Size;
- KSTATUS Status;
- ULONGLONG TimeCounterFrequency;
- ULONG Timeout;
- PUDP_SOCKET UdpSocket;
- ULONG WaitTime;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- BytesComplete = 0;
- EndTime = 0;
- LockHeld = FALSE;
- Flags = Parameters->SocketIoFlags;
- Parameters->SocketIoFlags = 0;
- if ((Flags & SOCKET_IO_OUT_OF_BAND) != 0) {
- Status = STATUS_NOT_SUPPORTED;
- goto UdpReceiveEnd;
- }
- //
- // Fail if there's ancillary data.
- //
- if (Parameters->ControlDataSize != 0) {
- Status = STATUS_NOT_SUPPORTED;
- goto UdpReceiveEnd;
- }
- Packet = NULL;
- Size = Parameters->Size;
- TimeCounterFrequency = 0;
- Timeout = Parameters->TimeoutInMilliseconds;
- UdpSocket = (PUDP_SOCKET)Socket;
- //
- // Set a timeout timer to give up on. The socket stores the maximum timeout.
- //
- if (Timeout > UdpSocket->ReceiveTimeout) {
- Timeout = UdpSocket->ReceiveTimeout;
- }
- if ((Timeout != 0) && (Timeout != WAIT_TIME_INDEFINITE)) {
- EndTime = KeGetRecentTimeCounter();
- EndTime += KeConvertMicrosecondsToTimeTicks(
- Timeout * MICROSECONDS_PER_MILLISECOND);
- TimeCounterFrequency = HlQueryTimeCounterFrequency();
- }
- //
- // Loop trying to get some data. This loop exits once one packet is read.
- //
- while (TRUE) {
- //
- // Wait for a packet to become available. Start by computing the wait
- // time.
- //
- if (Timeout == 0) {
- WaitTime = 0;
- } else if (Timeout != WAIT_TIME_INDEFINITE) {
- CurrentTime = KeGetRecentTimeCounter();
- WaitTime = (EndTime - CurrentTime) * MILLISECONDS_PER_SECOND /
- TimeCounterFrequency;
- } else {
- WaitTime = WAIT_TIME_INDEFINITE;
- }
- //
- // Wait for something to maybe become available. If the wait fails due
- // to a timeout, interruption, or something else, then fail out.
- // Otherwise when the read event is signalled, there is at least one
- // packet to receive.
- //
- Status = IoWaitForIoObjectState(Socket->KernelSocket.IoState,
- POLL_EVENT_IN,
- TRUE,
- WaitTime,
- &ReturnedEvents);
- if (!KSUCCESS(Status)) {
- goto UdpReceiveEnd;
- }
- if ((ReturnedEvents & POLL_ERROR_EVENTS) != 0) {
- if ((ReturnedEvents & POLL_EVENT_DISCONNECTED) != 0) {
- Status = STATUS_NO_NETWORK_CONNECTION;
- } else {
- Status = NET_SOCKET_GET_LAST_ERROR(Socket);
- if (KSUCCESS(Status)) {
- Status = STATUS_DEVICE_IO_ERROR;
- }
- }
- goto UdpReceiveEnd;
- }
- KeAcquireQueuedLock(UdpSocket->ReceiveLock);
- LockHeld = TRUE;
- //
- // Fail with EOF if the socket has already been closed for reading.
- //
- if ((UdpSocket->ShutdownTypes & SOCKET_SHUTDOWN_READ) != 0) {
- Status = STATUS_END_OF_FILE;
- goto UdpReceiveEnd;
- }
- //
- // If another thread beat this one to the punch, try again.
- //
- if (LIST_EMPTY(&(UdpSocket->ReceivedPacketList)) != FALSE) {
- KeReleaseQueuedLock(UdpSocket->ReceiveLock);
- LockHeld = FALSE;
- continue;
- }
- //
- // This should be the first packet being read.
- //
- ASSERT(BytesComplete == 0);
- PacketEntry = UdpSocket->ReceivedPacketList.Next;
- Packet = LIST_VALUE(PacketEntry, UDP_RECEIVED_PACKET, ListEntry);
- ReturnSize = Packet->Size;
- CopySize = ReturnSize;
- if (CopySize > Size) {
- Parameters->SocketIoFlags |= SOCKET_IO_DATA_TRUNCATED;
- CopySize = Size;
- //
- // The real packet size is only returned to the user on truncation
- // if the truncated flag was supplied to this routine. Default to
- // returning the truncated size.
- //
- if ((Flags & SOCKET_IO_DATA_TRUNCATED) == 0) {
- ReturnSize = CopySize;
- }
- }
- Status = MmCopyIoBufferData(IoBuffer,
- Packet->DataBuffer,
- 0,
- CopySize,
- TRUE);
- if (!KSUCCESS(Status)) {
- goto UdpReceiveEnd;
- }
- //
- // Copy the packet address out to the caller if requested.
- //
- if (Parameters->NetworkAddress != NULL) {
- if (FromKernelMode != FALSE) {
- RtlCopyMemory(Parameters->NetworkAddress,
- &(Packet->Address),
- sizeof(NETWORK_ADDRESS));
- } else {
- Status = MmCopyToUserMode(Parameters->NetworkAddress,
- &(Packet->Address),
- sizeof(NETWORK_ADDRESS));
- if (!KSUCCESS(Status)) {
- goto UdpReceiveEnd;
- }
- }
- }
- BytesComplete = ReturnSize;
- //
- // Remove the packet if not peeking.
- //
- if ((Flags & SOCKET_IO_PEEK) == 0) {
- LIST_REMOVE(&(Packet->ListEntry));
- UdpSocket->ReceiveBufferFreeSize += Packet->Size;
- //
- // The total receive buffer size may have been decreased. Don't
- // increment the free size above the total.
- //
- if (UdpSocket->ReceiveBufferFreeSize >
- UdpSocket->ReceiveBufferTotalSize) {
- UdpSocket->ReceiveBufferFreeSize =
- UdpSocket->ReceiveBufferTotalSize;
- }
- MmFreePagedPool(Packet);
- //
- // Unsignal the IN event if there are no more packets.
- //
- if (LIST_EMPTY(&(UdpSocket->ReceivedPacketList)) != FALSE) {
- IoSetIoObjectState(Socket->KernelSocket.IoState,
- POLL_EVENT_IN,
- FALSE);
- }
- }
- //
- // Wait-all does not apply to UDP sockets. Break out.
- //
- Status = STATUS_SUCCESS;
- break;
- }
- UdpReceiveEnd:
- if (LockHeld != FALSE) {
- KeReleaseQueuedLock(UdpSocket->ReceiveLock);
- }
- Parameters->Size = BytesComplete;
- return Status;
- }
- KSTATUS
- NetpUdpGetSetInformation (
- PNET_SOCKET Socket,
- SOCKET_INFORMATION_TYPE InformationType,
- UINTN Option,
- PVOID Data,
- PUINTN DataSize,
- BOOL Set
- )
- /*++
- Routine Description:
- This routine gets or sets properties of the given socket.
- Arguments:
- Socket - Supplies a pointer to the socket to get or set information for.
- InformationType - Supplies the socket information type category to which
- specified option belongs.
- Option - Supplies the option to get or set, which is specific to the
- information type. The type of this value is generally
- SOCKET_<information_type>_OPTION.
- Data - Supplies a pointer to the data buffer where the data is either
- returned for a get operation or given for a set operation.
- DataSize - Supplies a pointer that on input constains the size of the data
- buffer. On output, this contains the required size of the data buffer.
- Set - Supplies a boolean indicating if this is a get operation (FALSE) or
- a set operation (TRUE).
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_INVALID_PARAMETER if the information type is incorrect.
- STATUS_BUFFER_TOO_SMALL if the data buffer is too small to receive the
- requested option.
- STATUS_NOT_SUPPORTED_BY_PROTOCOL if the socket option is not supported by
- the socket.
- STATUS_NOT_HANDLED if the protocol does not override the default behavior
- for a basic socket option.
- --*/
- {
- ULONG Count;
- ULONG Index;
- LONGLONG Milliseconds;
- PNET_PACKET_SIZE_INFORMATION SizeInformation;
- ULONG SizeOption;
- PSOCKET_TIME SocketTime;
- SOCKET_TIME SocketTimeBuffer;
- PVOID Source;
- KSTATUS Status;
- PUDP_SOCKET_OPTION UdpOption;
- PUDP_SOCKET UdpSocket;
- UdpSocket = (PUDP_SOCKET)Socket;
- if ((InformationType != SocketInformationBasic) &&
- (InformationType != SocketInformationUdp)) {
- Status = STATUS_NOT_SUPPORTED;
- goto UdpGetSetInformationEnd;
- }
- //
- // Search to see if the socket option is supported by UDP.
- //
- Count = sizeof(NetUdpSocketOptions) / sizeof(NetUdpSocketOptions[0]);
- for (Index = 0; Index < Count; Index += 1) {
- UdpOption = &(NetUdpSocketOptions[Index]);
- if ((UdpOption->InformationType == InformationType) &&
- (UdpOption->Option == Option)) {
- break;
- }
- }
- if (Index == Count) {
- if (InformationType == SocketInformationBasic) {
- Status = STATUS_NOT_HANDLED;
- } else {
- Status = STATUS_NOT_SUPPORTED_BY_PROTOCOL;
- }
- goto UdpGetSetInformationEnd;
- }
- //
- // Handle failure cases common to all options.
- //
- if (Set != FALSE) {
- if (UdpOption->SetAllowed == FALSE) {
- Status = STATUS_NOT_SUPPORTED_BY_PROTOCOL;
- goto UdpGetSetInformationEnd;
- }
- if (*DataSize < UdpOption->Size) {
- *DataSize = UdpOption->Size;
- Status = STATUS_BUFFER_TOO_SMALL;
- goto UdpGetSetInformationEnd;
- }
- }
- //
- // There are currently no UDP options.
- //
- ASSERT(InformationType != SocketInformationUdp);
- //
- // Parse the basic socket option, getting the information from the UDP
- // socket or setting the new state in the UDP socket.
- //
- Source = NULL;
- Status = STATUS_SUCCESS;
- switch ((SOCKET_BASIC_OPTION)Option) {
- case SocketBasicOptionSendBufferSize:
- if (Set != FALSE) {
- SizeOption = *((PULONG)Data);
- ASSERT(UDP_MAX_PACKET_SIZE <= SOCKET_OPTION_MAX_ULONG);
- SizeInformation = &(Socket->PacketSizeInformation);
- if (SizeOption > UDP_MAX_PACKET_SIZE) {
- SizeOption = UDP_MAX_PACKET_SIZE;
- } else if (SizeOption < SizeInformation->MaxPacketSize) {
- SizeOption = SizeInformation->MaxPacketSize;
- }
- UdpSocket->MaxPacketSize = SizeOption;
- } else {
- SizeOption = UdpSocket->MaxPacketSize;
- Source = &SizeOption;
- }
- break;
- case SocketBasicOptionSendMinimum:
- ASSERT(Set == FALSE);
- SizeOption = UDP_SEND_MINIMUM;
- Source = &SizeOption;
- break;
- case SocketBasicOptionReceiveBufferSize:
- if (Set != FALSE) {
- SizeOption = *((PULONG)Data);
- if (SizeOption > SOCKET_OPTION_MAX_ULONG) {
- SizeOption = SOCKET_OPTION_MAX_ULONG;
- }
- if (SizeOption < UDP_MIN_RECEIVE_BUFFER_SIZE) {
- SizeOption = UDP_MIN_RECEIVE_BUFFER_SIZE;
- }
- //
- // Set the receive buffer size and truncate the available free
- // space if necessary. Do not remove any packets that have already
- // been received. This is not meant to be a truncate call.
- //
- KeAcquireQueuedLock(UdpSocket->ReceiveLock);
- UdpSocket->ReceiveBufferTotalSize = SizeOption;
- if (UdpSocket->ReceiveBufferFreeSize > SizeOption) {
- UdpSocket->ReceiveBufferFreeSize = SizeOption;
- }
- KeReleaseQueuedLock(UdpSocket->ReceiveLock);
- } else {
- SizeOption = UdpSocket->ReceiveBufferTotalSize;
- Source = &SizeOption;
- }
- break;
- case SocketBasicOptionReceiveMinimum:
- if (Set != FALSE) {
- SizeOption = *((PULONG)Data);
- if (SizeOption > SOCKET_OPTION_MAX_ULONG) {
- SizeOption = SOCKET_OPTION_MAX_ULONG;
- }
- UdpSocket->ReceiveMinimum = SizeOption;
- } else {
- Source = &SizeOption;
- SizeOption = UdpSocket->ReceiveMinimum;
- }
- break;
- case SocketBasicOptionReceiveTimeout:
- if (Set != FALSE) {
- SocketTime = (PSOCKET_TIME)Data;
- if (SocketTime->Seconds < 0) {
- Status = STATUS_DOMAIN_ERROR;
- break;
- }
- Milliseconds = SocketTime->Seconds * MILLISECONDS_PER_SECOND;
- if (Milliseconds < SocketTime->Seconds) {
- Status = STATUS_DOMAIN_ERROR;
- break;
- }
- Milliseconds += SocketTime->Microseconds /
- MICROSECONDS_PER_MILLISECOND;
- if ((Milliseconds < 0) || (Milliseconds > MAX_LONG)) {
- Status = STATUS_DOMAIN_ERROR;
- break;
- }
- UdpSocket->ReceiveTimeout = (ULONG)(LONG)Milliseconds;
- } else {
- Source = &SocketTimeBuffer;
- if (UdpSocket->ReceiveTimeout == WAIT_TIME_INDEFINITE) {
- SocketTimeBuffer.Seconds = 0;
- SocketTimeBuffer.Microseconds = 0;
- } else {
- SocketTimeBuffer.Seconds = UdpSocket->ReceiveTimeout /
- MILLISECONDS_PER_SECOND;
- SocketTimeBuffer.Microseconds = (UdpSocket->ReceiveTimeout %
- MILLISECONDS_PER_SECOND) *
- MICROSECONDS_PER_MILLISECOND;
- }
- }
- break;
- default:
- ASSERT(FALSE);
- Status = STATUS_NOT_HANDLED;
- break;
- }
- if (!KSUCCESS(Status)) {
- goto UdpGetSetInformationEnd;
- }
- //
- // Truncate all copies for get requests down to the required size and only
- // return the required size on set requests.
- //
- if (*DataSize > UdpOption->Size) {
- *DataSize = UdpOption->Size;
- }
- //
- // For get requests, copy the gathered information to the supplied data
- // buffer.
- //
- if (Set == FALSE) {
- ASSERT(Source != NULL);
- RtlCopyMemory(Data, Source, *DataSize);
- //
- // If the copy truncated the data, report that the given buffer was too
- // small. The caller can choose to ignore this if the truncated data is
- // enough.
- //
- if (*DataSize < UdpOption->Size) {
- *DataSize = UdpOption->Size;
- Status = STATUS_BUFFER_TOO_SMALL;
- goto UdpGetSetInformationEnd;
- }
- }
- UdpGetSetInformationEnd:
- return Status;
- }
- KSTATUS
- NetpUdpUserControl (
- PNET_SOCKET Socket,
- ULONG CodeNumber,
- BOOL FromKernelMode,
- PVOID ContextBuffer,
- UINTN ContextBufferSize
- )
- /*++
- Routine Description:
- This routine handles user control requests destined for a socket.
- Arguments:
- Socket - Supplies a pointer to the socket.
- CodeNumber - Supplies the minor code of the request.
- FromKernelMode - Supplies a boolean indicating whether or not this request
- (and the buffer associated with it) originates from user mode (FALSE)
- or kernel mode (TRUE).
- ContextBuffer - Supplies a pointer to the context buffer allocated by the
- caller for the request.
- ContextBufferSize - Supplies the size of the supplied context buffer.
- Return Value:
- Status code.
- --*/
- {
- return STATUS_NOT_SUPPORTED;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|