/* cblsrv is covered by the LGPL: This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Copyright (c) 2006 Zoltan Csizmadia All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "cblsrv.h" #include "utils.h" u8 yes8 = 1; u8 no8 = 0; // // Trace // void tracef(int level, const char* format, ...) { va_list args; if (level > g.trace_level) return; va_start(args, format); vprintf(format, args); va_end(args); } // // User mode I/O access // #if defined(WIN32) u8 inb(u16 port) { u8 rc; __asm mov dx,port __asm in al, dx __asm mov rc, al return rc; } void outb(u8 data, u16 port) { __asm mov dx,port __asm mov al, data __asm out dx, al } #endif int enable_user_mode_io() { #ifdef WIN32 HANDLE h; h = CreateFile( "\\\\.\\giveio", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (h != INVALID_HANDLE_VALUE) { // Giveio activated successfully CloseHandle(h); return 0; } #else if (iopl(3) == 0) // Success return 0; #endif tracef(1, "Failed to enable user mode I/O access!\n"); return -1; } // // Sockets // int sock_init() { #ifdef WIN32 WSADATA wsa; if(WSAStartup(MAKEWORD(2,2), &wsa) != NO_ERROR){ return -1; } #endif return 0; } int sock_deinit() { #ifdef WIN32 WSACleanup(); #endif return 0; } int sock_send(int sock, void* buff, int len) { int left = len; const char* p = (const char*)buff; int rc; if (g.trace_level >= 4) { printf("send -> "); for (int i = 0; i < len; i++) printf("%02X ", ((u8*)buff)[i]); printf("\n"); } while (left) { rc = send(sock, p, left, 0); if (rc < 0) return rc; left -= rc; p += rc; } return len; } int sock_recv(int sock, void* buff, int len) { int rc; int left = len; char* p = (char*)buff; while (left) { rc = recv(sock, p, left, 0); if (rc < 0) return rc; left -= rc; p += rc; } if (g.trace_level >= 4) { printf("recv -> "); for (int i = 0; i < len; i++) printf("%02X ", ((u8*)buff)[i]); printf("\n"); } return len; } int sock_set_timeout(int rx_ms, int tx_ms) { #ifdef WIN32 if (setsockopt(g.client, SOL_SOCKET, SO_RCVTIMEO, (const char*)&rx_ms, sizeof(rx_ms)) < 0) return -1; if (setsockopt(g.client, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tx_ms, sizeof(tx_ms)) < 0) return -1; #else struct timeval tv; tv.tv_sec = rx_ms / 1000; tv.tv_usec = (rx_ms % 1000) * 1000; if (setsockopt(g.client, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) return -1; tv.tv_sec = tx_ms / 1000; tv.tv_usec = (tx_ms % 1000) * 1000; if (setsockopt(g.client, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) return -1; #endif return 0; } int sock_create_tcp_server(u16 port) { int rc, sock, opt; struct sockaddr_in addr; sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { tracef(1, "socket() failed!\n"); return -1; } opt = 1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt)); memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); // Bind to the local address rc = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); if (rc < 0) { tracef(1, "bind() failed!\n"); closesocket(sock); return -1; } return sock; } // // Sleep // void sleep_us(int us) { #ifdef WIN32 Sleep((us + 999)/1000); #else usleep(us); #endif }