00001 /* ==================================================================== 00002 * The Kannel Software License, Version 1.0 00003 * 00004 * Copyright (c) 2001-2008 Kannel Group 00005 * Copyright (c) 1998-2001 WapIT Ltd. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * 3. The end-user documentation included with the redistribution, 00021 * if any, must include the following acknowledgment: 00022 * "This product includes software developed by the 00023 * Kannel Group (http://www.kannel.org/)." 00024 * Alternately, this acknowledgment may appear in the software itself, 00025 * if and wherever such third-party acknowledgments normally appear. 00026 * 00027 * 4. The names "Kannel" and "Kannel Group" must not be used to 00028 * endorse or promote products derived from this software without 00029 * prior written permission. For written permission, please 00030 * contact org@kannel.org. 00031 * 00032 * 5. Products derived from this software may not be called "Kannel", 00033 * nor may "Kannel" appear in their name, without prior written 00034 * permission of the Kannel Group. 00035 * 00036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00039 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 00040 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00041 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00042 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00043 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00044 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00045 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00046 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 * ==================================================================== 00048 * 00049 * This software consists of voluntary contributions made by many 00050 * individuals on behalf of the Kannel Group. For more information on 00051 * the Kannel Group, please see <http://www.kannel.org/>. 00052 * 00053 * Portions of this software are based upon software originally written at 00054 * WapIT Ltd., Helsinki, Finland for the Kannel project. 00055 */ 00056 00057 /* 00058 * General useful socket functions 00059 */ 00060 00061 #ifndef GW_SOCKET_H 00062 #define GW_SOCKET_H 00063 00064 #include <stddef.h> 00065 #include <stdio.h> 00066 #include <sys/types.h> 00067 #include <netinet/in.h> 00068 00069 #include "gw-config.h" 00070 00071 #ifndef HAVE_SOCKLEN_T 00072 typedef int socklen_t; 00073 #endif 00074 00075 #include "octstr.h" 00076 00077 /* Return the official and fully qualified domain name of the host. 00078 Caller should treat this as read-only. Caller MUST NOT destroy it. */ 00079 Octstr *get_official_name(void); 00080 00081 /* Return an official IP number for the host. Caller should treat this 00082 as read-only. Caller MUST NOT destroy it. Note that there can be 00083 multiple official IP numbers for the host. 00084 */ 00085 Octstr *get_official_ip(void); 00086 00087 /* Open a server socket. Return -1 for error, >= 0 socket number for OK.*/ 00088 int make_server_socket(int port, const char *interface_name); 00089 00090 /* Open a client socket. */ 00091 int tcpip_connect_to_server(char *hostname, int port, const char *interface_name); 00092 00093 /* As above, but binds our end to 'our_port' */ 00094 int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, 00095 const char *interface_name); 00096 00097 /* Open a client socket in nonblocking mode, done is 0 if socket 00098 connected immediatly, overwise done is 1 */ 00099 int tcpip_connect_nb_to_server(char *hostname, int port, const char *interface_name, 00100 int *done); 00101 00102 /* As above, but binds our end to 'our_port' */ 00103 int tcpip_connect_nb_to_server_with_port(char *hostname, int port, int our_port, 00104 const char *interface_name, int *done); 00105 00106 /* Write string to socket. */ 00107 int write_to_socket(int socket, char *str); 00108 00109 /* Set socket to blocking or non-blocking mode. Return -1 for error, 00110 * 0 for success. */ 00111 int socket_set_blocking(int socket, int blocking); 00112 00113 /* Check if there is something to be read in 'fd'. Return 1 if there 00114 * is data, 0 otherwise, -1 on error */ 00115 int read_available(int fd, long wait_usec); 00116 00117 00118 /* 00119 * Create a UDP socket for receiving from clients. Return -1 for failure, 00120 * a socket file descriptor >= 0 for OK. 00121 */ 00122 int udp_bind(int port, const char *interface_name); 00123 00124 00125 /* 00126 * Create the client end of a UDP socket (i.e., a UDP socket that can 00127 * be on any port). Return -1 for failure, a socket file descriptor >= 0 00128 * for OK. 00129 */ 00130 int udp_client_socket(void); 00131 00132 00133 /* 00134 * Encode a hostname or IP number and port number into a binary address, 00135 * and return that as an Octstr. Return NULL if the host doesn't exist 00136 * or the IP number is syntactically invalid, or the port is bad. 00137 */ 00138 Octstr *udp_create_address(Octstr *host_or_ip, int port); 00139 00140 00141 /* 00142 * Return the IP number of an encoded binary address, as a cleartext string. 00143 */ 00144 Octstr *udp_get_ip(Octstr *addr); 00145 00146 00147 /* 00148 * Return the port number of an encoded binary address, as a cleartext string. 00149 */ 00150 int udp_get_port(Octstr *addr); 00151 00152 00153 /* 00154 * Send a UDP message to a given server. 00155 */ 00156 int udp_sendto(int s, Octstr *datagram, Octstr *addr); 00157 00158 00159 /* 00160 * Receive a UDP message from a client. 00161 */ 00162 int udp_recvfrom(int s, Octstr **datagram, Octstr **addr); 00163 00164 00165 /* 00166 * Create an Octstr of character representation of an IP 00167 */ 00168 Octstr *host_ip(struct sockaddr_in addr); 00169 00170 00171 /* 00172 * Return the port number of an IP connection. 00173 */ 00174 int host_port(struct sockaddr_in addr); 00175 00176 00177 /* 00178 * This must be called before sockets are used. gwlib_init does that 00179 */ 00180 void socket_init(void); 00181 00182 00183 /* 00184 * Likewise, shutdown, called by gwlib_shutdown 00185 */ 00186 void socket_shutdown(void); 00187 00188 /* 00189 * Converts an address of various types to an Octstr representation. 00190 * Similar to host_ip, but works with more than IPv4 00191 */ 00192 Octstr *gw_netaddr_to_octstr(int af, void* src); 00193 00194 00195 /* 00196 * Do an accept() system call for the given file descriptor. Return -1 00197 * for error (from accept or gwthread_poll, or gwthread_poll was 00198 * interrupted by gwthread_wakeup) or the new file descriptor for success. 00199 * Return IP number (as formatted by host_ip) via *client_addr. 00200 */ 00201 int gw_accept(int fd, Octstr **client_addr); 00202 00203 00204 00205 #endif