Kannel: Open Source WAP and SMS gateway  svn-r5335
socket.h
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * General useful socket functions
59  */
60 
61 #ifndef GW_SOCKET_H
62 #define GW_SOCKET_H
63 
64 #include <stddef.h>
65 #include <stdio.h>
66 #include <sys/types.h>
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
69 
70 #include "gw-config.h"
71 
72 #ifndef HAVE_SOCKLEN_T
73 typedef int socklen_t;
74 #endif
75 
76 #include "octstr.h"
77 
78 /* Return the official and fully qualified domain name of the host.
79  Caller should treat this as read-only. Caller MUST NOT destroy it. */
81 
82 /* Return an official IP number for the host. Caller should treat this
83  as read-only. Caller MUST NOT destroy it. Note that there can be
84  multiple official IP numbers for the host.
85  */
86 Octstr *get_official_ip(void);
87 
88 /* Open a server socket. Return -1 for error, >= 0 socket number for OK.*/
89 int make_server_socket(int port, const char *source_addr);
90 
91 /* Open a client socket. */
92 int tcpip_connect_to_server(char *hostname, int port, const char *source_addr);
93 
94 /* As above, but binds our end to 'our_port' */
96  const char *source_addr);
97 
98 /* Open a client socket in nonblocking mode, done is 0 if socket
99  connected immediatly, overwise done is 1 */
100 int tcpip_connect_nb_to_server(char *hostname, int port, const char *source_addr,
101  int *done);
102 
103 /* As above, but binds our end to 'our_port' */
105  const char *source_addr, int *done);
106 
107 /* Write string to socket. */
108 int write_to_socket(int socket, char *str);
109 
110 /* Set socket to blocking or non-blocking mode. Return -1 for error,
111  * 0 for success. */
112 int socket_set_blocking(int socket, int blocking);
113 
114 /* Diable or enable TCP_NODELAY.
115  * Return -1 for error, 0 for success.
116  */
117 int socket_set_nodelay(int fd, int on);
118 
119 /* Check if there is something to be read in 'fd'. Return 1 if there
120  * is data, 0 otherwise, -1 on error */
121 int read_available(int fd, long wait_usec);
122 
123 
124 /*
125  * Create a UDP socket for receiving from clients. Return -1 for failure,
126  * a socket file descriptor >= 0 for OK.
127  */
128 int udp_bind(int port, const char *source_addr);
129 
130 
131 /*
132  * Create the client end of a UDP socket (i.e., a UDP socket that can
133  * be on any port). Return -1 for failure, a socket file descriptor >= 0
134  * for OK.
135  */
136 int udp_client_socket(void);
137 
138 
139 /*
140  * Encode a hostname or IP number and port number into a binary address,
141  * and return that as an Octstr. Return NULL if the host doesn't exist
142  * or the IP number is syntactically invalid, or the port is bad.
143  */
144 Octstr *udp_create_address(Octstr *host_or_ip, int port);
145 
146 
147 /*
148  * Return the IP number of an encoded binary address, as a cleartext string.
149  */
150 Octstr *udp_get_ip(Octstr *addr);
151 
152 
153 /*
154  * Return the port number of an encoded binary address, as a cleartext string.
155  */
156 int udp_get_port(Octstr *addr);
157 
158 
159 /*
160  * Send a UDP message to a given server.
161  */
162 int udp_sendto(int s, Octstr *datagram, Octstr *addr);
163 
164 
165 /*
166  * Receive a UDP message from a client.
167  */
168 int udp_recvfrom_flags(int s, Octstr **datagram, Octstr **addr, int sockrcvflags);
169 int udp_recvfrom(int s, Octstr **datagram, Octstr **addr);
170 
171 
172 /*
173  * Create an Octstr of character representation of an IP
174  */
175 Octstr *host_ip(struct sockaddr_in addr);
176 
177 
178 /*
179  * Return the port number of an IP connection.
180  */
181 int host_port(struct sockaddr_in addr);
182 
183 
184 /*
185  * This must be called before sockets are used. gwlib_init does that
186  */
187 void socket_init(void);
188 
189 
190 /*
191  * Likewise, shutdown, called by gwlib_shutdown
192  */
193 void socket_shutdown(void);
194 
195 /*
196  * Converts an address of various types to an Octstr representation.
197  * Similar to host_ip, but works with more than IPv4
198  */
199 Octstr *gw_netaddr_to_octstr(int af, void* src);
200 
201 
202 /*
203  * Do an accept() system call for the given file descriptor. Return -1
204  * for error (from accept or gwthread_poll, or gwthread_poll was
205  * interrupted by gwthread_wakeup) or the new file descriptor for success.
206  * Return IP number (as formatted by host_ip) via *client_addr.
207  */
208 int gw_accept(int fd, Octstr **client_addr);
209 
210 
211 
212 #endif
static long our_port
Definition: radius_acct.c:87
Definition: http.c:2014
int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr)
Definition: socket.c:156
int udp_bind(int port, const char *source_addr)
Definition: socket.c:478
int udp_get_port(Octstr *addr)
Definition: socket.c:547
int socket_set_nodelay(int fd, int on)
Definition: socket.c:394
int tcpip_connect_nb_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr, int *done)
Definition: socket.c:246
void socket_shutdown(void)
Definition: socket.c:668
Octstr * udp_create_address(Octstr *host_or_ip, int port)
Definition: socket.c:517
Octstr * get_official_name(void)
Definition: socket.c:627
int udp_client_socket(void)
Definition: socket.c:464
int make_server_socket(int port, const char *source_addr)
Definition: socket.c:93
int udp_sendto(int s, Octstr *datagram, Octstr *addr)
Definition: socket.c:567
int tcpip_connect_to_server(char *hostname, int port, const char *source_addr)
Definition: socket.c:149
int udp_recvfrom(int s, Octstr **datagram, Octstr **addr)
Definition: socket.c:582
Octstr * udp_get_ip(Octstr *addr)
Definition: socket.c:557
Octstr * hostname
Definition: fakewap.c:232
Octstr * gw_netaddr_to_octstr(int af, void *src)
Definition: socket.c:677
void socket_init(void)
Definition: socket.c:663
int gw_accept(int fd, Octstr **client_addr)
Definition: socket.c:700
int tcpip_connect_nb_to_server(char *hostname, int port, const char *source_addr, int *done)
Definition: socket.c:241
int socket_set_blocking(int socket, int blocking)
Definition: socket.c:368
int write_to_socket(int socket, char *str)
Definition: socket.c:345
int read_available(int fd, long wait_usec)
Definition: socket.c:406
Definition: octstr.c:118
int udp_recvfrom_flags(int s, Octstr **datagram, Octstr **addr, int sockrcvflags)
Definition: socket.c:588
Octstr * get_official_ip(void)
Definition: socket.c:634
int socklen_t
Definition: socket.h:73
int host_port(struct sockaddr_in addr)
Definition: socket.c:621
Octstr * host_ip(struct sockaddr_in addr)
Definition: socket.c:615
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.