amxmodx/modules/sockets/sockets.cpp

346 lines
7.4 KiB
C++
Raw Normal View History

// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
//
// Sockets Module
//
2004-06-26 17:31:20 +00:00
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
#include "amxxmodule.h"
#include <amtl/am-string.h>
2004-06-26 17:31:20 +00:00
#ifdef _WIN32
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
#include <winsock2.h>
#include <ws2tcpip.h>
#undef errno
#undef close
#define errno WSAGetLastError()
#define close(sockfd) closesocket(sockfd)
#define EINPROGRESS WSAEINPROGRESS
#define EWOULDBLOCK WSAEWOULDBLOCK
2004-06-26 17:31:20 +00:00
#else
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
2004-06-26 17:31:20 +00:00
#endif
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
#ifdef _WIN32
bool g_winsock_initialized = false;
#endif
static char *g_send2_buffer = nullptr;
static int g_send2_buffer_length = 0;
bool setnonblocking(int sockfd)
{
#ifdef _WIN32
unsigned long flags = 1;
2004-06-26 17:31:20 +00:00
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
return (ioctlsocket(sockfd, FIONBIO, &flags) == 0);
#else
int flags = -1;
if((flags = fcntl(sockfd, F_GETFL, 0)) == -1)
return false;
if(fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)
return false;
return true;
#endif
2004-06-26 17:31:20 +00:00
}
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
// native socket_open(_hostname[], _port, _protocol = SOCKET_TCP, &_error, _flags = 0);
static cell AMX_NATIVE_CALL socket_open(AMX *amx, cell *params)
2004-06-26 17:31:20 +00:00
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
// Socket flags and backwards compatibility
enum
{
SOCK_NON_BLOCKING = (1 << 0),
SOCK_LIBC_ERRORS = (1 << 1)
};
#define SOCK_ERROR_OK 0 // No error
#define SOCK_ERROR_CREATE_SOCKET 1 // Couldn't create a socket
#define SOCK_ERROR_SERVER_UNKNOWN 2 // Server unknown
#define SOCK_ERROR_WHILE_CONNECTING 3 // Error while connecting
#define ERROR_EHOSTUNREACH 113 // libc error code: No route to host
int hostname_length = 0;
char *hostname = MF_GetAmxString(amx, params[1], 0, &hostname_length);
cell *error = MF_GetAmxAddr(amx, params[4]);
*error = 0;
unsigned int flags = 0;
bool libc_errors = false, nonblocking_socket = false;
if((*params / sizeof(cell)) == 5)
{
flags = params[5];
nonblocking_socket = (flags & SOCK_NON_BLOCKING) != 0;
libc_errors = (flags & SOCK_LIBC_ERRORS) != 0;
}
if(hostname_length == 0)
{
*error = libc_errors ? ERROR_EHOSTUNREACH : SOCK_ERROR_SERVER_UNKNOWN;
return -1;
}
char port_number[6];
ke::SafeSprintf(port_number, sizeof(port_number), "%d", params[2]);
int sockfd = -1, getaddrinfo_status = -1, connect_status = -1;
bool setnonblocking_status = false, connect_inprogress = false;
struct addrinfo hints, *server_info, *server;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = params[3];
if((getaddrinfo_status = getaddrinfo(hostname, port_number, &hints, &server_info)) != 0)
{
*error = libc_errors ? getaddrinfo_status : SOCK_ERROR_SERVER_UNKNOWN;
return -1;
}
server = server_info;
do
{
if((sockfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol)) != -1)
{
if(nonblocking_socket)
setnonblocking_status = setnonblocking(sockfd);
if(nonblocking_socket == false || (nonblocking_socket && setnonblocking_status == true))
{
if((connect_status = connect(sockfd, server->ai_addr, server->ai_addrlen)) == -1)
{
*error = libc_errors ? errno : SOCK_ERROR_WHILE_CONNECTING;
if(nonblocking_socket && (errno == EINPROGRESS || errno == EWOULDBLOCK))
connect_inprogress = true;
else
close(sockfd);
}
else
{
*error = 0;
}
}
else
{
if(*error == 0)
*error = libc_errors ? errno : SOCK_ERROR_CREATE_SOCKET;
}
}
else
{
if(*error == 0)
*error = errno;
}
} while((nonblocking_socket && connect_inprogress == false) && connect_status != 0 && (server = server->ai_next) != nullptr);
freeaddrinfo(server_info);
if(sockfd == -1 || server == nullptr)
return -1;
return sockfd;
2004-06-26 17:31:20 +00:00
}
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
// native socket_close(_socket);
static cell AMX_NATIVE_CALL socket_close(AMX *amx, cell *params)
2004-06-26 17:31:20 +00:00
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
return (close(params[1]) == -1) ? 0 : 1;
2004-06-26 17:31:20 +00:00
}
// native socket_recv(_socket, _data[], _length);
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
static cell AMX_NATIVE_CALL socket_recv(AMX *amx, cell *params)
2004-06-26 17:31:20 +00:00
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
int sockfd = params[1];
int length = params[3];
char *recv_buffer = new char[length];
if(recv_buffer == nullptr)
return -1;
memset(recv_buffer, 0, length);
int bytes_received = -1;
bytes_received = recv(sockfd, recv_buffer, length - 1, 0);
if(bytes_received == -1)
2005-08-02 08:39:17 +00:00
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
delete[] recv_buffer;
2005-08-02 10:18:09 +00:00
return -1;
2005-08-02 08:39:17 +00:00
}
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
recv_buffer[bytes_received] = '\0';
cell* destination = MF_GetAmxAddr(amx, params[2]);
int current_length = 0;
int max_length = length - 1;
2017-03-04 13:14:08 +00:00
const char *buffer = recv_buffer;
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
while(max_length-- && current_length < bytes_received)
{
2017-03-04 13:14:08 +00:00
*destination++ = (cell)*buffer++;
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
current_length++;
}
*destination = 0;
delete[] recv_buffer;
return bytes_received;
2004-06-26 17:31:20 +00:00
}
// native socket_send(_socket, _data[], _length);
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
static cell AMX_NATIVE_CALL socket_send(AMX *amx, cell *params)
2004-06-26 17:31:20 +00:00
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
int sockfd = params[1];
int length = 0;
char *data = MF_GetAmxString(amx, params[2], 0, &length);
2005-12-05 02:07:22 +00:00
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
return send(sockfd, data, length, 0);
}
2006-02-05 02:20:45 +00:00
2005-12-05 02:07:22 +00:00
// native socket_send2(_socket, _data[], _length);
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
static cell AMX_NATIVE_CALL socket_send2(AMX *amx, cell *params)
2005-12-05 02:07:22 +00:00
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
int sockfd = params[1];
int length = params[3];
if(length > g_send2_buffer_length)
2006-02-05 02:20:45 +00:00
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
delete[] g_send2_buffer;
g_send2_buffer = new char[length + 1];
if(g_send2_buffer == nullptr)
{
g_send2_buffer_length = 0;
return -1;
}
g_send2_buffer_length = length;
2006-02-05 02:20:45 +00:00
}
2005-12-05 02:07:22 +00:00
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
cell *data = MF_GetAmxAddr(amx, params[2]);
char *buffer = g_send2_buffer;
while(length--)
*buffer++ = (char)*data++;
return send(sockfd, g_send2_buffer, params[3], 0);
}
// native socket_change(_socket, _timeout = 100000);
static cell AMX_NATIVE_CALL socket_change(AMX *amx, cell *params)
{
int sockfd = params[1];
unsigned int timeout = params[2];
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = timeout;
2005-12-05 02:07:22 +00:00
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
2005-12-05 02:07:22 +00:00
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
return (select(sockfd + 1, &readfds, nullptr, nullptr, &tv) > 0) ? 1 : 0;
2004-06-26 17:31:20 +00:00
}
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
// native socket_is_readable(_socket, _timeout = 100000);
static cell AMX_NATIVE_CALL socket_is_readable(AMX *amx, cell *params)
{
return socket_change(amx, params);
}
// native socket_is_writable(_socket, _timeout = 100000);
static cell AMX_NATIVE_CALL socket_is_writable(AMX *amx, cell *params)
{
int sockfd = params[1];
unsigned int timeout = params[2];
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = timeout;
fd_set writefds;
FD_ZERO(&writefds);
FD_SET(sockfd, &writefds);
return (select(sockfd + 1, nullptr, &writefds, nullptr, &tv) > 0) ? 1 : 0;
}
AMX_NATIVE_INFO sockets_natives[] =
{
2005-12-05 02:07:22 +00:00
{"socket_open", socket_open},
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
2005-12-05 02:07:22 +00:00
{"socket_close", socket_close},
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
2005-12-05 02:07:22 +00:00
{"socket_recv", socket_recv},
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
2005-12-05 02:07:22 +00:00
{"socket_send", socket_send},
{"socket_send2", socket_send2},
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
{"socket_change", socket_change},
{"socket_is_readable", socket_is_readable},
{"socket_is_writable", socket_is_writable},
2005-12-05 02:07:22 +00:00
{NULL, NULL}
2004-06-26 17:31:20 +00:00
};
void OnAmxxAttach()
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
#ifdef _WIN32
WSADATA WSAData;
int errorcode = WSAStartup(MAKEWORD(2, 2), &WSAData);
if(errorcode != 0)
{
MF_Log("[%s]: WSAStartup failed with error code %d. Natives will not be available.", MODULE_LOGTAG, errorcode);
return;
}
g_winsock_initialized = true;
#endif
2004-06-26 17:31:20 +00:00
MF_AddNatives(sockets_natives);
}
void OnAmxxDetach()
{
Update sockets module (#301) * Module rewrite - WinSock version changed from 1.1 to 2.2. - Properly check for WinSock initialization on OnAmxxAttach/Detach. - Now natives will not be added if we can't start up WinSock. - socket_open() is now IP version agnostic (both IPv4 and IPv6 are supported). - Error reporting has been changed on socket_open(), a new parameter called _libc_errors has been added, and, if enabled, libc errors will be returned instead of the previous made-up errors. - socket_close() now returns a value on success/failure. - Added non-blocking sockets at socket_open_nb(). - Added socket_is_writable() to check if a socket is ready for write. - Added socket_is_readable() as an alias to socket_change(). - Code rewritten to be more readable, it should be self-explaining now. * Update docs and fix AMBuild Updated documentation following the guidelines * Fixs for the module - Fixed the backwards compatibility with the return codes - Merged socket_connect and socket_connect_nb - Added a 5th parameter to socket_open that takes bit flags to enable the new features (libc errors & nonblocking sockets) - Fixed an error on socket_send2 that caused the buffet not to start from the beginning if multiple calls were made - Updated docs - [docs] Prefixed error codes with SOCK_ - [docs] Added the new flags SOCK_NON_BLOCKING and SOCK_LIBC_ERRORS - [docs] Added a new stock called SOCK_ERROR_EINPROGRESS(error) to be used when checking if a newly created nonblocking socket is connecting * Fixes for the docs Fix some typos, shorten lines, document SOCK_ERROR_EINPROGRESS * Document magic numbers No more magic * Revert "Document magic numbers" This reverts commit 0f233292063400ea7fdbcd5e5d5cd6e54f8cd71c. * More docs fixes * Fix broken socket_send2() * Add error checking in send2() * Remove stock It should not be needed because nb sockets should always be checked or writability * Fix some identations * Fix return codes doc * Fix socket_recv() regression strncopy will stop on a null byte, that makes the function unusable to receive binary data * More docs typo fixes
2017-03-03 18:56:22 +00:00
#ifdef _WIN32
if(g_winsock_initialized)
WSACleanup();
#endif
delete[] g_send2_buffer;
2004-09-12 00:44:15 +00:00
}