📄 target_generic_network.h
字号:
/* target_generic_network.h - Native methods for network operations. Copyright (C) 1998, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. *//*Description: generic target defintions of network functionsSystems : all*/#ifndef __TARGET_GENERIC_NETWORK__#define __TARGET_GENERIC_NETWORK__/* check if target_native_network.h included */#ifndef __TARGET_NATIVE_NETWORK__ #error Do NOT INCLUDE generic target files! Include the corresponding native target files instead!#endif/****************************** Includes *******************************//* do not move; needed here because of some macro definitions */#include "config.h"#include <stdlib.h>#include "target_native.h"/****************** Conditional compilation switches *******************//***************************** Constants *******************************//***************************** Datatypes *******************************//***************************** Variables *******************************//****************************** Macros *********************************//***********************************************************************\* Name : TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT* Purpose : convert IP adddress (4 parts) into integer (host-format* 32bit)* Input : n0,n1,n2,n3 - IP address parts* Output : i - integer with IP address in host-format* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT #define TARGET_NATIVE_NETWORK_IPADDRESS_BYTES_TO_INT(n0,n1,n2,n3,i) \ do { \ i=(((unsigned char)n0) << 24) | \ (((unsigned char)n1) << 16) | \ (((unsigned char)n2) << 8) | \ (((unsigned char)n3) << 0); \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES* Purpose : convert IP adddress (4 parts) into integer (host-format* 32bit)* Input : n0,n1,n2,n3 - IP address parts* Output : i - integer with IP address in host-format* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES #define TARGET_NATIVE_NETWORK_INT_TO_IPADDRESS_BYTES(i,n0,n1,n2,n3) \ do { \ n0=(i & 0xFF000000) >> 24; \ n1=(i & 0x00FF0000) >> 16; \ n2=(i & 0x0000FF00) >> 8; \ n3=(i & 0x000000FF) >> 0; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_GET_HOSTNAME* Purpose : get hostname* Input : maxNameLen - max. length of name* Output : name - name (NUL terminated)* result - TARGET_NATIVE_OK if no error occurred,* TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_GET_HOSTNAME #include <unistd.h> #define TARGET_NATIVE_NETWORK_GET_HOSTNAME(name,maxNameLen,result) \ do { \ result=(gethostname(name,maxNameLen-1)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ name[maxNameLen-1]='\0'; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS* Purpose : get hostname by address* Input : address - IP address (32bit, NOT network byte order!)* maxNameLen - max. length of name* Output : name - name (NUL terminated)* result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************//* XXX NYI??? reentrant? */#ifndef TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS #include <netdb.h> #define TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_ADDRESS(address,name,maxNameLen,result) \ do { \ int __networkAddress; \ struct hostent *__hostEntry; \ \ __networkAddress=htonl(address); \ __hostEntry = gethostbyaddr((char*)&__networkAddress,sizeof(__networkAddress),AF_INET); \ if (__hostEntry!=NULL) \ { \ strncpy(name,__hostEntry->h_name,maxNameLen-1); \ name[maxNameLen]='\0'; \ result=TARGET_NATIVE_OK; \ } \ else \ { \ result=TARGET_NATIVE_ERROR; \ } \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME* Purpose : get hostname by name* Input : name - hostname* maxAddressSize - max. size of address array* Output : addresses - host adddresses (array, NOT in network* byte order!)* addressCount - number of entries in address array* result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************//* XXX NYI??? reentrant? */#ifndef TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME #include <netdb.h> #define TARGET_NATIVE_NETWORK_GET_HOSTNAME_BY_NAME(name,addresses,maxAddressSize,addressCount,result) \ do { \ struct hostent *__hostEntry; \ \ addressCount=0; \ \ __hostEntry = gethostbyname(name); \ if (__hostEntry!=NULL) \ { \ while ((addressCount<maxAddressSize) && (__hostEntry->h_addr_list[addressCount]!=NULL)) \ { \ addresses[addressCount]=ntohl(*(int*)(__hostEntry->h_addr_list[addressCount])); \ addressCount++; \ } \ result=TARGET_NATIVE_OK; \ } \ else \ { \ result=TARGET_NATIVE_ERROR; \ } \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM* Purpose : open stream socket* Input : -* Output : socketDescriptor - socket descriptor* result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM #include <sys/types.h> #include <sys/socket.h> #include <fcntl.h> #define TARGET_NATIVE_NETWORK_SOCKET_OPEN_STREAM(socketDescriptor,result) \ do { \ socketDescriptor=socket(AF_INET,SOCK_STREAM,0); \ fcntl(socketDescriptor,F_SETFD,FD_CLOEXEC); \ result=(socketDescriptor!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM* Purpose : open datagram socket* Input : -* Output : socketDescriptor - socket descriptor* result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM #include <sys/types.h> #include <sys/socket.h> #include <fcntl.h> #define TARGET_NATIVE_NETWORK_SOCKET_OPEN_DATAGRAM(socketDescriptor,result) \ do { \ socketDescriptor=socket(AF_INET,SOCK_DGRAM,0); \ fcntl(socketDescriptor,F_SETFD,FD_CLOEXEC); \ result=(socketDescriptor!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_CLOSE* Purpose : close socket* Input : socketDescriptor - socket descriptor* Output : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_CLOSE #include <unistd.h> #define TARGET_NATIVE_NETWORK_SOCKET_CLOSE(socketDescriptor,result) \ do { \ result=(close(socketDescriptor)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_CONNECT* Purpose : connect socket* Input : socketDescriptor - socket descriptor* address - address (network format???)* port - port number (NOT in network byte order!)* Output : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_CONNECT #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_CONNECT(socketDescriptor,address,port,result) \ do { \ struct sockaddr_in __socketAddress; \ \ memset(&__socketAddress,0,sizeof(__socketAddress)); \ __socketAddress.sin_family = AF_INET; \ __socketAddress.sin_addr.s_addr = htonl(address); \ __socketAddress.sin_port = htons(((short)port)); \ \ result=(connect(socketDescriptor,(struct sockaddr*)&__socketAddress,sizeof(__socketAddress))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_BIND* Purpose : bind socket* Input : socketDescriptor - socket descriptor* address - address (NOT ??? in network byte order!)* port - port (NOT in network byte order!)* Output : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************//* XXX ??? address in network byte order? */#ifndef TARGET_NATIVE_NETWORK_SOCKET_BIND #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -