3f5d670384
- applied remaining updates from Qemu slirp - add #if blocks to all slirp .cc files for conditional compilation - fixed some uninitialized variable warnings
123 lines
2.7 KiB
C++
123 lines
2.7 KiB
C++
/*
|
|
* QEMU compatibility functions
|
|
*
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "slirp.h"
|
|
|
|
#if BX_NETWORKING && BX_NETMOD_SLIRP_NEW
|
|
|
|
void pstrcpy(char *buf, int buf_size, const char *str)
|
|
{
|
|
int c;
|
|
char *q = buf;
|
|
|
|
if (buf_size <= 0)
|
|
return;
|
|
|
|
for(;;) {
|
|
c = *str++;
|
|
if (c == 0 || q >= buf + buf_size - 1)
|
|
break;
|
|
*q++ = c;
|
|
}
|
|
*q = '\0';
|
|
}
|
|
|
|
void qemu_set_nonblock(int fd)
|
|
{
|
|
#ifdef WIN32
|
|
unsigned long opt = 1;
|
|
ioctlsocket(fd, FIONBIO, &opt);
|
|
#else
|
|
int f;
|
|
f = fcntl(fd, F_GETFL);
|
|
fcntl(fd, F_SETFL, f | O_NONBLOCK);
|
|
#endif
|
|
}
|
|
|
|
#ifdef WIN32
|
|
int inet_aton(const char *cp, struct in_addr *ia)
|
|
{
|
|
uint32_t addr = inet_addr(cp);
|
|
if (addr == 0xffffffff) {
|
|
return 0;
|
|
}
|
|
ia->s_addr = addr;
|
|
return 1;
|
|
}
|
|
#endif
|
|
|
|
int socket_set_fast_reuse(int fd)
|
|
{
|
|
#ifndef WIN32
|
|
int val = 1, ret;
|
|
|
|
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
|
(const char *)&val, sizeof(val));
|
|
|
|
assert(ret == 0);
|
|
|
|
return ret;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
int socket_set_nodelay(int fd)
|
|
{
|
|
int v = 1;
|
|
return qemu_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
|
|
}
|
|
|
|
void qemu_set_cloexec(int fd)
|
|
{
|
|
#ifndef WIN32
|
|
int f;
|
|
f = fcntl(fd, F_GETFD);
|
|
fcntl(fd, F_SETFD, f | FD_CLOEXEC);
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
* Opens a socket with FD_CLOEXEC set
|
|
*/
|
|
int qemu_socket(int domain, int type, int protocol)
|
|
{
|
|
int ret;
|
|
|
|
#ifdef SOCK_CLOEXEC
|
|
ret = socket(domain, type | SOCK_CLOEXEC, protocol);
|
|
if (ret != -1 || errno != EINVAL) {
|
|
return ret;
|
|
}
|
|
#endif
|
|
ret = socket(domain, type, protocol);
|
|
if (ret >= 0) {
|
|
qemu_set_cloexec(ret);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
#endif
|