1 /* 2 net_utilities.h 3 -------------- 4 A bunch of useful networking functions that I keep using all the time. 5 6 Copyright (C) 2000 Eu-Jin Goh 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 21 USA. 22 */ 23 24 /* necessary header files and defines */ 25 26 #ifndef NET_UTILITIES_H 27 #define NET_UTILITIES_H 28 29 #include <unistd.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 33 #include <netdb.h> 34 35 #ifndef INADDR_NONE 36 #define INADDR_NONE 0xffffffff /* should be in <netinet/in.h> */ 37 #endif 38 39 /* 40 NOTE: 41 all parameters should be passed in as host byte order. all functions 42 here convert the host byte order to network byte order 43 */ 44 45 /* ---------- Networking Functions ------------*/ 46 47 /* Creates a ipv4 stream socket. Exits on failure */ 48 int utlnet_CreateIPV4StreamSocket(); 49 50 /* Set socket to be reusable. Returns -1 on failure */ 51 int utlnet_SetSocketReusable(const int sock_fd); 52 53 /* 54 Converts a hostname into an IP and sets it in the sockaddr struct. 55 Returns -1 on failure 56 */ 57 int utlnet_SetIP(struct sockaddr_in *addr, const char *host_name); 58 59 /* Gets the hostname of the IP in the sockaddr_in struct */ 60 struct hostent *utlnet_GetHostName(struct sockaddr_in *addr); 61 62 /* 63 Set the port number in a socket addr struct 64 Set the protocol family in the socket addr struct 65 */ 66 void utlnet_SetPort(struct sockaddr_in *addr, const short port); 67 void utlnet_SetIPV4Protocol(struct sockaddr_in *addr); 68 69 /* 70 sets the IP, port and protocol for a IPV4 client connection. 71 returns -1 on failure 72 */ 73 int utlnet_InitIPV4ClientSockAddrStruct(struct sockaddr_in *addr, 74 const short port, 75 const char *host_name); 76 77 /* 78 sets the port, protocol and server ip. the serverIP is typically 79 set to INADDR_ANY to allow kernel selection. 80 */ 81 void utlnet_InitIPV4ServerSockAddrStruct(struct sockaddr_in *addr, 82 const short port, 83 const unsigned int server_ip); 84 85 /* 86 Sets up the listening socket for the given port and IP address 87 in servAddr. Also sets the queue backlog and the socket option 88 if the port number needs to be reusable. 89 */ 90 91 #define REUSABLE (char) 1 92 #define NOTRESUABLE (char) 0 93 94 int utlnet_InitIPV4ServerSocket(const int sock_fd, 95 struct sockaddr_in *addr, 96 int sock_queue_backlog, 97 char reusable); 98 99 /* 100 Connect to a remote host using an IPV4 sockaddr. 101 Returns -1 on failure 102 */ 103 int utlnet_IPV4Connect(const int sock_fd, struct sockaddr_in *to_addr); 104 105 /* 106 Waits on a passive socket till it receives a connection. 107 Returns the connecting socket number. 108 */ 109 int utlnet_Accept(const int sock_fd, struct sockaddr_in *from_addr); 110 111 /* 112 Write/Read to the socket. 113 Returns bytes sent/read or -1 on error 114 */ 115 int utlnet_WriteToSocket(int sock_fd, char *buffer, int length); 116 int utlnet_ReadFromSocket(int sock_fd, char *buffer, int length); 117 118 /* buffer should be at least n bytes long */ 119 int utlnet_WritenBytesToSocket(int sock_fd, char *buffer, int n_bytes); 120 int utlnet_ReadnBytesFromSocket(int sock_fd, char *buffer, int n_bytes); 121 122 int utlnet_PeekAtnBytesFromSocket(int sock_fd, char *buffer, int n_bytes); 123 124 #endif 125