123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- /*++
- Copyright (c) 2014 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- ntsock.c
- Abstract:
- This module contains socket support for Win32 platforms.
- Author:
- Evan Green 27-Aug-2014
- Environment:
- Win32
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <stdio.h>
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include "sock.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- BOOL DbgrSocketsInitialized = FALSE;
- //
- // ------------------------------------------------------------------ Functions
- //
- int
- DbgrSocketInitializeLibrary (
- void
- )
- /*++
- Routine Description:
- This routine initializes socket support in the application.
- Arguments:
- None.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- int Result;
- WSADATA WsaData;
- if (DbgrSocketsInitialized != FALSE) {
- return 0;
- }
- Result = WSAStartup(MAKEWORD(2, 2), &WsaData);
- if (Result != 0) {
- return Result;
- }
- DbgrSocketsInitialized = TRUE;
- return 0;
- }
- void
- DbgrSocketDestroyLibrary (
- void
- )
- /*++
- Routine Description:
- This routine tears down socket support in the application.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- if (DbgrSocketsInitialized == FALSE) {
- return;
- }
- WSACleanup();
- DbgrSocketsInitialized = FALSE;
- return;
- }
- int
- DbgrSocketCreateStreamSocket (
- void
- )
- /*++
- Routine Description:
- This routine creates an IPv4 TCP socket.
- Arguments:
- None.
- Return Value:
- Returns the socket on success.
- Returns a value less than zero on failure.
- --*/
- {
- return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- }
- int
- DbgrSocketBind (
- int Socket,
- char *Host,
- int Port
- )
- /*++
- Routine Description:
- This routine binds the given socket to the given address and port.
- Arguments:
- Socket - Supplies the socket to bind.
- Host - Supplies a pointer to the host string. Supply NULL to use any
- address.
- Port - Supplies the port to bind on.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- struct sockaddr_in Address;
- int Result;
- Address.sin_family = AF_INET;
- Address.sin_port = htons(Port);
- Address.sin_addr.s_addr = INADDR_ANY;
- if ((Host != NULL) && (*Host != '\0')) {
- Address.sin_addr.s_addr = inet_addr(Host);
- if (Address.sin_addr.s_addr == INADDR_NONE) {
- return 1;
- }
- }
- Result = bind(Socket,
- (struct sockaddr *)&Address,
- sizeof(struct sockaddr_in));
- return Result;
- }
- int
- DbgrSocketConnect (
- int Socket,
- char *Host,
- int Port
- )
- /*++
- Routine Description:
- This routine connects to a remote server.
- Arguments:
- Socket - Supplies the socket to connect.
- Host - Supplies a pointer to the host string to connect to.
- Port - Supplies the port to bind on.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- struct sockaddr_in Address;
- int Result;
- Address.sin_family = AF_INET;
- Address.sin_port = htons(Port);
- Address.sin_addr.s_addr = inet_addr(Host);
- if (Address.sin_addr.s_addr == INADDR_NONE) {
- return 1;
- }
- Result = connect(Socket,
- (struct sockaddr *)&Address,
- sizeof(struct sockaddr_in));
- return Result;
- }
- int
- DbgrSocketListen (
- int Socket
- )
- /*++
- Routine Description:
- This routine starts a server socket listening for connections.
- Arguments:
- Socket - Supplies the socket to listen on.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- return listen(Socket, SOMAXCONN);
- }
- int
- DbgrSocketAccept (
- int Socket,
- char **Host,
- int *Port
- )
- /*++
- Routine Description:
- This routine accepts a new incoming connection from the given listening
- socket.
- Arguments:
- Socket - Supplies the socket to accept an incoming connection from.
- Host - Supplies an optional pointer where a string describing the host will
- be returned on success. The caller is responsible for freeing this
- string.
- Port - Supplies an optional pointer where the port number will be returned
- on success.
- Return Value:
- Returns the newly connected client socket on success.
- Returns a negative value on failure.
- --*/
- {
- struct sockaddr_in Address;
- int Result;
- socklen_t Size;
- if (Host != NULL) {
- *Host = NULL;
- }
- if (Port != NULL) {
- *Port = 0;
- }
- Size = sizeof(struct sockaddr_in);
- Result = accept(Socket, (struct sockaddr *)&Address, &Size);
- if (Result < 0) {
- return Result;
- }
- if (Host != NULL) {
- *Host = strdup(inet_ntoa(Address.sin_addr));
- }
- if (Port != NULL) {
- *Port = ntohs(Address.sin_port);
- }
- return Result;
- }
- int
- DbgrSocketGetName (
- int Socket,
- char **Host,
- int *Port
- )
- /*++
- Routine Description:
- This routine gets the current local host and port for the given socket.
- Arguments:
- Socket - Supplies the socket to query.
- Host - Supplies an optional pointer where a string describing the host will
- be returned on success. The caller is responsible for freeing this
- string.
- Port - Supplies an optional pointer where the port number will be returned
- on success.
- Return Value:
- 0 on success.
- Returns a non-zero value on failure.
- --*/
- {
- struct sockaddr_in Address;
- int Result;
- int Size;
- if (Host != NULL) {
- *Host = NULL;
- }
- if (Port != NULL) {
- *Port = 0;
- }
- Size = sizeof(Address);
- Result = getsockname(Socket, (struct sockaddr *)&Address, &Size);
- if (Result != 0) {
- return Result;
- }
- if (Host != NULL) {
- *Host = strdup(inet_ntoa(Address.sin_addr));
- }
- if (Port != NULL) {
- *Port = ntohs(Address.sin_port);
- }
- return 0;
- }
- int
- DbgrSocketShutdown (
- int Socket
- )
- /*++
- Routine Description:
- This routine shuts down a socket. It shuts down both the read and write
- sides of the connection.
- Arguments:
- Socket - Supplies the socket to shut down.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- //
- // Unfortunately, Windows doesn't unblock threads blocked in send or
- // recv when a socket is shut down. The only solution is to close it,
- // which is not ideal as the file descriptor might get reused.
- //
- return closesocket(Socket);
- }
- void
- DbgrSocketClose (
- int Socket
- )
- /*++
- Routine Description:
- This routine closes a socket.
- Arguments:
- Socket - Supplies the socket to destroy.
- Return Value:
- None.
- --*/
- {
- closesocket(Socket);
- return;
- }
- int
- DbgrSocketSend (
- int Socket,
- const void *Data,
- int Length
- )
- /*++
- Routine Description:
- This routine sends data out of a connected socket.
- Arguments:
- Socket - Supplies the file descriptor of the socket to send data out of.
- Data - Supplies the buffer of data to send.
- Length - Supplies the length of the data buffer, in bytes.
- Return Value:
- Returns the number of bytes sent on success.
- -1 on error, and the errno variable will be set to contain more information.
- --*/
- {
- return send(Socket, Data, Length, 0);
- }
- int
- DbgrSocketReceive (
- int Socket,
- void *Buffer,
- int Length
- )
- /*++
- Routine Description:
- This routine receives data from a connected socket.
- Arguments:
- Socket - Supplies the file descriptor of the socket to receive data from.
- Buffer - Supplies a pointer to a buffer where the received data will be
- returned.
- Length - Supplies the length of the data buffer, in bytes.
- Return Value:
- Returns the number of bytes received on success.
- -1 on error, and the errno variable will be set to contain more information.
- --*/
- {
- return recv(Socket, Buffer, Length, 0);
- }
- int
- DbgrSocketPeek (
- int Socket,
- void *Buffer,
- int Length
- )
- /*++
- Routine Description:
- This routine peeks at data from a received socket, but does not remove it
- from the queue.
- Arguments:
- Socket - Supplies the file descriptor of the socket to receive data from.
- Buffer - Supplies a pointer to a buffer where the peeked data will be
- returned.
- Length - Supplies the length of the data buffer, in bytes.
- Return Value:
- Returns the number of bytes received on success.
- -1 on error, and the errno variable will be set to contain more information.
- --*/
- {
- return recvfrom(Socket, Buffer, Length, MSG_PEEK, NULL, NULL);
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|