Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
socket.h File Reference
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "gw-config.h"
#include "octstr.h"

Go to the source code of this file.

Typedefs

typedef int socklen_t
 

Functions

Octstrget_official_name (void)
 
Octstrget_official_ip (void)
 
int make_server_socket (int port, const char *source_addr)
 
int tcpip_connect_to_server (char *hostname, int port, const char *source_addr)
 
int tcpip_connect_to_server_with_port (char *hostname, int port, int our_port, const char *source_addr)
 
int tcpip_connect_nb_to_server (char *hostname, int port, const char *source_addr, int *done)
 
int tcpip_connect_nb_to_server_with_port (char *hostname, int port, int our_port, const char *source_addr, int *done)
 
int write_to_socket (int socket, char *str)
 
int socket_set_blocking (int socket, int blocking)
 
int socket_set_nodelay (int fd, int on)
 
int read_available (int fd, long wait_usec)
 
int udp_bind (int port, const char *source_addr)
 
int udp_client_socket (void)
 
Octstrudp_create_address (Octstr *host_or_ip, int port)
 
Octstrudp_get_ip (Octstr *addr)
 
int udp_get_port (Octstr *addr)
 
int udp_sendto (int s, Octstr *datagram, Octstr *addr)
 
int udp_recvfrom_flags (int s, Octstr **datagram, Octstr **addr, int sockrcvflags)
 
int udp_recvfrom (int s, Octstr **datagram, Octstr **addr)
 
Octstrhost_ip (struct sockaddr_in addr)
 
int host_port (struct sockaddr_in addr)
 
void socket_init (void)
 
void socket_shutdown (void)
 
Octstrgw_netaddr_to_octstr (int af, void *src)
 
int gw_accept (int fd, Octstr **client_addr)
 

Typedef Documentation

typedef int socklen_t

Definition at line 73 of file socket.h.

Function Documentation

Octstr* get_official_ip ( void  )

Definition at line 634 of file socket.c.

References gw_assert(), and official_ip.

Referenced by name(), and version_report_string().

635 {
636  gw_assert(official_ip != NULL);
637  return official_ip;
638 }
gw_assert(wtls_machine->packet_to_send!=NULL)
static Octstr * official_ip
Definition: socket.c:79
Octstr* get_official_name ( void  )

Definition at line 627 of file socket.c.

References gw_assert(), and official_name.

Referenced by add_via(), challenge(), tell_ppg_name(), and version_report_string().

628 {
629  gw_assert(official_name != NULL);
630  return official_name;
631 }
static Octstr * official_name
Definition: socket.c:78
gw_assert(wtls_machine->packet_to_send!=NULL)
int gw_accept ( int  fd,
Octstr **  client_addr 
)

Definition at line 700 of file socket.c.

References debug(), error(), gwthread_pollfd(), host_ip(), octstr_get_cstr, and POLLIN.

Referenced by smpp_emu().

701 {
702  struct sockaddr_in addr;
703  socklen_t addrlen;
704  int new_fd;
705 
706  if (gwthread_pollfd(fd, POLLIN, -1.0) != POLLIN) {
707  debug("gwlib.socket", 0, "gwthread_pollfd interrupted or failed");
708  return -1;
709  }
710  addrlen = sizeof(addr);
711  new_fd = accept(fd, (struct sockaddr *) &addr, &addrlen);
712  if (new_fd == -1) {
713  error(errno, "accept system call failed.");
714  return -1;
715  }
716  *client_addr = host_ip(addr);
717  debug("test_smsc", 0, "accept() succeeded, client from %s",
718  octstr_get_cstr(*client_addr));
719  return new_fd;
720 }
void error(int err, const char *fmt,...)
Definition: log.c:612
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define POLLIN
Definition: gwpoll.h:91
int gwthread_pollfd(int fd, int events, double timeout)
Octstr * host_ip(struct sockaddr_in addr)
Definition: socket.c:615
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
int socklen_t
Definition: socket.h:73
Octstr* gw_netaddr_to_octstr ( int  af,
void *  src 
)

Definition at line 677 of file socket.c.

References octstr_create.

Referenced by host_ip(), ois_open_receiver(), setup_official_name(), tcpip_connect_nb_to_server_with_port(), tcpip_connect_to_server_with_port(), udp_get_ip(), and wait_for_client().

678 {
679  switch (af) {
680  case AF_INET: {
681  char straddr[INET_ADDRSTRLEN];
682  inet_ntop(AF_INET, src, straddr, sizeof(straddr));
683  return octstr_create(straddr);
684  }
685 
686 #ifdef AF_INET6
687  case AF_INET6: {
688  char straddr[INET6_ADDRSTRLEN];
689  inet_ntop(AF_INET6, src, straddr, sizeof(straddr));
690  return octstr_create(straddr);
691  }
692 #endif
693 
694  default:
695  return NULL;
696  }
697 }
#define octstr_create(cstr)
Definition: octstr.h:125
Octstr* host_ip ( struct sockaddr_in  addr)

Definition at line 615 of file socket.c.

References gw_netaddr_to_octstr().

Referenced by accept_boxc(), cgw_listener(), emi2_listener(), fake_listener(), get_data(), gw_accept(), and server_thread().

616 {
617  return gw_netaddr_to_octstr(AF_INET, &addr.sin_addr);
618 }
Octstr * gw_netaddr_to_octstr(int af, void *src)
Definition: socket.c:677
int host_port ( struct sockaddr_in  addr)

Definition at line 621 of file socket.c.

622 {
623  return ntohs(addr.sin_port);
624 }
int make_server_socket ( int  port,
const char *  source_addr 
)

Definition at line 93 of file socket.c.

References empty_sockaddr_in, error(), and gw_gethostbyname().

Referenced by accept_thread(), cgw_open_listening_socket(), emi2_open_listening_socket(), fake_open_connection(), http_open_port_if(), ois_open_listener(), smpp_emu(), smsboxc_run(), start_wapbox(), wait_for_client(), and wapboxc_run().

94 {
95  struct sockaddr_in addr;
96  int s;
97  int reuse;
98  struct hostent hostinfo;
99  char *buff = NULL;
100 
101  s = socket(PF_INET, SOCK_STREAM, 0);
102  if (s == -1) {
103  error(errno, "socket failed");
104  goto error;
105  }
106 
107  addr = empty_sockaddr_in;
108  addr.sin_family = AF_INET;
109  addr.sin_port = htons(port);
110  if (interface_name == NULL || strcmp(interface_name, "*") == 0)
111  addr.sin_addr.s_addr = htonl(INADDR_ANY);
112  else {
113  if (gw_gethostbyname(&hostinfo, interface_name, &buff) == -1) {
114  error(errno, "gethostbyname failed");
115  goto error;
116  }
117  addr.sin_addr = *(struct in_addr *) hostinfo.h_addr;
118  }
119 
120  reuse = 1;
121  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,
122  sizeof(reuse)) == -1) {
123  error(errno, "setsockopt failed for server address");
124  goto error;
125  }
126 
127  if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
128  error(errno, "bind failed");
129  goto error;
130  }
131 
132  if (listen(s, 10) == -1) {
133  error(errno, "listen failed");
134  goto error;
135  }
136 
137  gw_free(buff);
138 
139  return s;
140 
141 error:
142  if (s >= 0)
143  (void) close(s);
144  gw_free(buff);
145  return -1;
146 }
static const struct sockaddr_in empty_sockaddr_in
Definition: socket.c:86
void error(int err, const char *fmt,...)
Definition: log.c:612
Definition: http.c:1998
int gw_gethostbyname(struct hostent *ent, const char *name, char **buff)
Definition: protected.c:248
static Octstr * interface_name
Definition: wapproxy.c:111
int read_available ( int  fd,
long  wait_usec 
)

Definition at line 406 of file socket.c.

References gw_assert(), and warning().

Referenced by cimd2_get_packet(), cimd2_receive_msg(), ois_read_into_buffer(), oisd_get_packet(), oisd_receive_msg(), proxy_thread(), udp_receiver(), and wap_msg_recv().

407 {
408  fd_set rf;
409  struct timeval to;
410  int ret;
411  div_t waits;
412 
413  gw_assert(fd >= 0);
414 
415  FD_ZERO(&rf);
416  FD_SET(fd, &rf);
417  waits = div(wait_usec, 1000000);
418  to.tv_sec = waits.quot;
419  to.tv_usec = waits.rem;
420 retry:
421  ret = select(fd + 1, &rf, NULL, NULL, &to);
422  if (ret > 0 && FD_ISSET(fd, &rf))
423  return 1;
424  if (ret < 0) {
425  /* In most select() implementations, to will now contain the
426  * remaining time rather than the original time. That is exactly
427  * what we want when retrying after an interrupt. */
428  switch (errno) {
429  /*The first two entries here are OK*/
430  case EINTR:
431  goto retry;
432  case EAGAIN:
433  return 1;
434  /* We are now sucking mud, figure things out here
435  * as much as possible before it gets lost under
436  * layers of abstraction. */
437  case EBADF:
438  if (!FD_ISSET(fd, &rf)) {
439  warning(0, "Tried to select on fd %d, not in the set!\n", fd);
440  } else {
441  warning(0, "Tried to select on invalid fd %d!\n", fd);
442  }
443  break;
444  case EINVAL:
445  /* Solaris catchall "It didn't work" error, lets apply
446  * some tests and see if we can catch it. */
447 
448  /* First up, try invalid timeout*/
449  if (to.tv_sec > 10000000)
450  warning(0, "Wait more than three years for a select?\n");
451  if (to.tv_usec > 1000000)
452  warning(0, "There are only 1000000 usec in a second...\n");
453  break;
454 
455 
456  }
457  return -1; /* some error */
458  }
459  return 0;
460 }
void warning(int err, const char *fmt,...)
Definition: log.c:624
gw_assert(wtls_machine->packet_to_send!=NULL)
void socket_init ( void  )

Definition at line 663 of file socket.c.

References setup_official_name().

Referenced by gwlib_init().

664 {
666 }
static void setup_official_name(void)
Definition: socket.c:641
int socket_set_blocking ( int  socket,
int  blocking 
)

Definition at line 368 of file socket.c.

References error().

Referenced by cgw_open_listening_socket(), conn_wrap_fd(), emi2_open_listening_socket(), fake_open_connection(), fill_threadinfo(), ois_open_listener(), and wait_for_client().

369 {
370  int flags, newflags;
371 
372  flags = fcntl(fd, F_GETFL);
373  if (flags < 0) {
374  error(errno, "cannot get flags for fd %d", fd);
375  return -1;
376  }
377 
378  if (blocking)
379  newflags = flags & ~O_NONBLOCK;
380  else
381  newflags = flags | O_NONBLOCK;
382 
383  if (newflags != flags) {
384  if (fcntl(fd, F_SETFL, newflags) < 0) {
385  error(errno, "cannot set flags for fd %d", fd);
386  return -1;
387  }
388  }
389 
390  return 0;
391 }
void error(int err, const char *fmt,...)
Definition: log.c:612
int socket_set_nodelay ( int  fd,
int  on 
)

Definition at line 394 of file socket.c.

References error().

395 {
396  int rc;
397 
398  rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on));
399  if (rc == -1)
400  error(errno, "Unable set TCP_NODELAY(%d)", on);
401 
402  return rc;
403 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void socket_shutdown ( void  )

Definition at line 668 of file socket.c.

References octstr_destroy().

Referenced by gwlib_shutdown().

669 {
671  official_name = NULL;
673  official_ip = NULL;
674 }
static Octstr * official_name
Definition: socket.c:78
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
static Octstr * official_ip
Definition: socket.c:79
int tcpip_connect_nb_to_server ( char *  hostname,
int  port,
const char *  source_addr,
int *  done 
)

Definition at line 241 of file socket.c.

References tcpip_connect_nb_to_server_with_port().

242 {
243  return tcpip_connect_nb_to_server_with_port(hostname, port, 0, source_addr, done);
244 }
Definition: http.c:1998
Octstr * hostname
Definition: fakewap.c:232
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
int tcpip_connect_nb_to_server_with_port ( char *  hostname,
int  port,
int  our_port,
const char *  source_addr,
int *  done 
)

Definition at line 246 of file socket.c.

References debug(), empty_sockaddr_in, error(), gw_gethostbyname(), gw_netaddr_to_octstr(), octstr_destroy(), and octstr_get_cstr.

Referenced by conn_open_tcp_nb_with_port(), and tcpip_connect_nb_to_server().

247 {
248  struct sockaddr_in addr;
249  struct sockaddr_in o_addr;
250  struct hostent hostinfo;
251  struct hostent o_hostinfo;
252  int s, flags, rc = -1, i;
253  char *buff, *buff1;
254 
255  *done = 1;
256  buff = buff1 = NULL;
257 
258  s = socket(PF_INET, SOCK_STREAM, 0);
259  if (s == -1) {
260  error(errno, "Couldn't create new socket.");
261  goto error;
262  }
263 
264  if (gw_gethostbyname(&hostinfo, hostname, &buff) == -1) {
265  error(errno, "gethostbyname failed");
266  goto error;
267  }
268 
269  if (our_port > 0 || (source_addr != NULL && strcmp(source_addr, "*") != 0)) {
270  int reuse;
271 
272  o_addr = empty_sockaddr_in;
273  o_addr.sin_family = AF_INET;
274  o_addr.sin_port = htons(our_port);
275  if (source_addr == NULL || strcmp(source_addr, "*") == 0)
276  o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
277  else {
278  if (gw_gethostbyname(&o_hostinfo, source_addr, &buff1) == -1) {
279  error(errno, "gethostbyname failed");
280  goto error;
281  }
282  o_addr.sin_addr = *(struct in_addr *) o_hostinfo.h_addr;
283  }
284 
285  reuse = 1;
286  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse)) == -1) {
287  error(errno, "setsockopt failed before bind");
288  goto error;
289  }
290  if (bind(s, (struct sockaddr *) &o_addr, sizeof(o_addr)) == -1) {
291  error(errno, "bind to local port %d failed", our_port);
292  goto error;
293  }
294  }
295 
296  flags = fcntl(s, F_GETFL, 0);
297  fcntl(s, F_SETFL, flags | O_NONBLOCK);
298 
299  i = 0;
300  do {
301  Octstr *ip2;
302 
303  addr = empty_sockaddr_in;
304  addr.sin_family = AF_INET;
305  addr.sin_port = htons(port);
306  addr.sin_addr = *(struct in_addr *) hostinfo.h_addr_list[i];
307 
308  ip2 = gw_netaddr_to_octstr(AF_INET, &addr.sin_addr);
309 
310  debug("gwlib.socket", 0, "Connecting nonblocking to <%s>", octstr_get_cstr(ip2));
311 
312  if ((rc = connect(s, (struct sockaddr *) &addr, sizeof(addr))) < 0) {
313  if (errno != EINPROGRESS) {
314  error(errno, "nonblocking connect to <%s> failed", octstr_get_cstr(ip2));
315  }
316  }
317  octstr_destroy(ip2);
318  } while (rc == -1 && errno != EINPROGRESS && hostinfo.h_addr_list[++i] != NULL);
319 
320  if (rc == -1 && errno != EINPROGRESS)
321  goto error;
322 
323  /* May be connected immediatly
324  * (if we connecting to localhost for example)
325  */
326  if (rc == 0) {
327  *done = 0;
328  }
329 
330  gw_free(buff);
331  gw_free(buff1);
332 
333  return s;
334 
335 error:
336  error(0, "error connecting to server `%s' at port `%d'", hostname, port);
337  if (s >= 0)
338  close(s);
339  gw_free(buff);
340  gw_free(buff1);
341  return -1;
342 }
static const struct sockaddr_in empty_sockaddr_in
Definition: socket.c:86
void error(int err, const char *fmt,...)
Definition: log.c:612
static long our_port
Definition: radius_acct.c:87
Definition: http.c:1998
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * gw_netaddr_to_octstr(int af, void *src)
Definition: socket.c:677
Octstr * hostname
Definition: fakewap.c:232
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Definition: octstr.c:118
int gw_gethostbyname(struct hostent *ent, const char *name, char **buff)
Definition: protected.c:248
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
int tcpip_connect_to_server ( char *  hostname,
int  port,
const char *  source_addr 
)

Definition at line 149 of file socket.c.

References tcpip_connect_to_server_with_port().

Referenced by at2_open_device1(), cimd_open_connection(), connect_tcpip(), ois_open_sender(), and oisd_login().

150 {
151 
152  return tcpip_connect_to_server_with_port(hostname, port, 0, source_addr);
153 }
Definition: http.c:1998
Octstr * hostname
Definition: fakewap.c:232
int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr)
Definition: socket.c:156
int tcpip_connect_to_server_with_port ( char *  hostname,
int  port,
int  our_port,
const char *  source_addr 
)

Definition at line 156 of file socket.c.

References debug(), empty_sockaddr_in, error(), gw_gethostbyname(), gw_netaddr_to_octstr(), octstr_destroy(), and octstr_get_cstr.

Referenced by cimd2_login(), conn_open_tcp_with_port(), emi_open_connection_ip(), and tcpip_connect_to_server().

157 {
158  struct sockaddr_in addr;
159  struct sockaddr_in o_addr;
160  struct hostent hostinfo;
161  struct hostent o_hostinfo;
162  int s, rc = -1, i;
163  char *buff, *buff1;
164 
165  buff = buff1 = NULL;
166 
167  s = socket(PF_INET, SOCK_STREAM, 0);
168  if (s == -1) {
169  error(errno, "Couldn't create new socket.");
170  goto error;
171  }
172 
173  if (gw_gethostbyname(&hostinfo, hostname, &buff) == -1) {
174  error(errno, "gethostbyname failed");
175  goto error;
176  }
177 
178  if (our_port > 0 || (source_addr != NULL && strcmp(source_addr, "*") != 0)) {
179  int reuse;
180 
181  o_addr = empty_sockaddr_in;
182  o_addr.sin_family = AF_INET;
183  o_addr.sin_port = htons(our_port);
184  if (source_addr == NULL || strcmp(source_addr, "*") == 0)
185  o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
186  else {
187  if (gw_gethostbyname(&o_hostinfo, source_addr, &buff1) == -1) {
188  error(errno, "gethostbyname failed");
189  goto error;
190  }
191  o_addr.sin_addr = *(struct in_addr *) o_hostinfo.h_addr;
192  }
193 
194  reuse = 1;
195  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse)) == -1) {
196  error(errno, "setsockopt failed before bind");
197  goto error;
198  }
199  if (bind(s, (struct sockaddr *) &o_addr, sizeof(o_addr)) == -1) {
200  error(errno, "bind to local port %d failed", our_port);
201  goto error;
202  }
203  }
204 
205  i = 0;
206  do {
207  Octstr *ip2;
208 
209  addr = empty_sockaddr_in;
210  addr.sin_family = AF_INET;
211  addr.sin_port = htons(port);
212  addr.sin_addr = *(struct in_addr *) hostinfo.h_addr_list[i];
213 
214  ip2 = gw_netaddr_to_octstr(AF_INET, &addr.sin_addr);
215 
216  debug("gwlib.socket", 0, "Connecting to <%s>", octstr_get_cstr(ip2));
217 
218  rc = connect(s, (struct sockaddr *) &addr, sizeof(addr));
219  if (rc == -1) {
220  error(errno, "connect to <%s> failed", octstr_get_cstr(ip2));
221  }
222  octstr_destroy(ip2);
223  } while (rc == -1 && hostinfo.h_addr_list[++i] != NULL);
224 
225  if (rc == -1)
226  goto error;
227 
228  gw_free(buff);
229  gw_free(buff1);
230  return s;
231 
232 error:
233  error(0, "error connecting to server `%s' at port `%d'", hostname, port);
234  if (s >= 0)
235  close(s);
236  gw_free(buff);
237  gw_free(buff1);
238  return -1;
239 }
static const struct sockaddr_in empty_sockaddr_in
Definition: socket.c:86
void error(int err, const char *fmt,...)
Definition: log.c:612
static long our_port
Definition: radius_acct.c:87
Definition: http.c:1998
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * gw_netaddr_to_octstr(int af, void *src)
Definition: socket.c:677
Octstr * hostname
Definition: fakewap.c:232
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Definition: octstr.c:118
int gw_gethostbyname(struct hostent *ent, const char *name, char **buff)
Definition: protected.c:248
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
int udp_bind ( int  port,
const char *  source_addr 
)

Definition at line 478 of file socket.c.

References empty_sockaddr_in, error(), and gw_gethostbyname().

Referenced by proxy_thread(), server(), and udpc_create().

479 {
480  int s;
481  struct sockaddr_in sa;
482  struct hostent hostinfo;
483  char *buff = NULL;
484 
485  s = socket(PF_INET, SOCK_DGRAM, 0);
486  if (s == -1) {
487  error(errno, "Couldn't create a UDP socket");
488  return -1;
489  }
490 
491  sa = empty_sockaddr_in;
492  sa.sin_family = AF_INET;
493  sa.sin_port = htons(port);
494  if (strcmp(source_addr, "*") == 0)
495  sa.sin_addr.s_addr = htonl(INADDR_ANY);
496  else {
497  if (gw_gethostbyname(&hostinfo, source_addr, &buff) == -1) {
498  error(errno, "gethostbyname failed");
499  gw_free(buff);
500  return -1;
501  }
502  sa.sin_addr = *(struct in_addr *) hostinfo.h_addr;
503  }
504 
505  if (bind(s, (struct sockaddr *) &sa, (int) sizeof(sa)) == -1) {
506  error(errno, "Couldn't bind a UDP socket to port %d", port);
507  (void) close(s);
508  return -1;
509  }
510 
511  gw_free(buff);
512 
513  return s;
514 }
static const struct sockaddr_in empty_sockaddr_in
Definition: socket.c:86
void error(int err, const char *fmt,...)
Definition: log.c:612
Definition: http.c:1998
int gw_gethostbyname(struct hostent *ent, const char *name, char **buff)
Definition: protected.c:248
int udp_client_socket ( void  )

Definition at line 464 of file socket.c.

References error().

Referenced by client(), client_session(), main(), proxy_thread(), and server().

465 {
466  int s;
467 
468  s = socket(PF_INET, SOCK_DGRAM, 0);
469  if (s == -1) {
470  error(errno, "Couldn't create a UDP socket");
471  return -1;
472  }
473 
474  return s;
475 }
void error(int err, const char *fmt,...)
Definition: log.c:612
Octstr* udp_create_address ( Octstr host_or_ip,
int  port 
)

Definition at line 517 of file socket.c.

References empty_sockaddr_in, error(), gw_gethostbyname(), octstr_create_from_data, and octstr_get_cstr.

Referenced by client(), main(), proxy_thread(), send_udp(), server(), udp_addwdp_from_client(), udp_addwdp_from_server(), and udpc_create().

518 {
519  struct sockaddr_in sa;
520  struct hostent h;
521  char *buff = NULL;
522  Octstr *ret;
523 
524  sa = empty_sockaddr_in;
525  sa.sin_family = AF_INET;
526  sa.sin_port = htons(port);
527 
528  if (strcmp(octstr_get_cstr(host_or_ip), "*") == 0) {
529  sa.sin_addr.s_addr = INADDR_ANY;
530  } else {
531  if (gw_gethostbyname(&h, octstr_get_cstr(host_or_ip), &buff) == -1) {
532  error(0, "Couldn't find the IP number of `%s'",
533  octstr_get_cstr(host_or_ip));
534  gw_free(buff);
535  return NULL;
536  }
537  sa.sin_addr = *(struct in_addr *) h.h_addr;
538  }
539 
540  ret = octstr_create_from_data((char *) &sa, sizeof(sa));
541  gw_free(buff);
542 
543  return ret;
544 }
static const struct sockaddr_in empty_sockaddr_in
Definition: socket.c:86
void error(int err, const char *fmt,...)
Definition: log.c:612
Definition: http.c:1998
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Definition: octstr.c:118
int gw_gethostbyname(struct hostent *ent, const char *name, char **buff)
Definition: protected.c:248
#define octstr_create_from_data(data, len)
Definition: octstr.h:134
Octstr* udp_get_ip ( Octstr addr)

Definition at line 557 of file socket.c.

References gw_assert(), gw_netaddr_to_octstr(), octstr_get_cstr, and octstr_len().

Referenced by client(), proxy_thread(), server(), udp_addwdp(), udp_addwdp_from_client(), udp_addwdp_from_server(), udp_receiver(), udpc_create(), and udpc_find_mapping().

558 {
559  struct sockaddr_in sa;
560 
561  gw_assert(octstr_len(addr) == sizeof(sa));
562  memcpy(&sa, octstr_get_cstr(addr), sizeof(sa));
563  return gw_netaddr_to_octstr(AF_INET, &sa.sin_addr);
564 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * gw_netaddr_to_octstr(int af, void *src)
Definition: socket.c:677
gw_assert(wtls_machine->packet_to_send!=NULL)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
int udp_get_port ( Octstr addr)

Definition at line 547 of file socket.c.

References gw_assert(), octstr_get_cstr, and octstr_len().

Referenced by client(), proxy_thread(), server(), udp_addwdp(), udp_addwdp_from_client(), udp_addwdp_from_server(), udp_receiver(), udpc_create(), and udpc_find_mapping().

548 {
549  struct sockaddr_in sa;
550 
551  gw_assert(octstr_len(addr) == sizeof(sa));
552  memcpy(&sa, octstr_get_cstr(addr), sizeof(sa));
553  return ntohs(sa.sin_port);
554 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
gw_assert(wtls_machine->packet_to_send!=NULL)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
int udp_recvfrom ( int  s,
Octstr **  datagram,
Octstr **  addr 
)

Definition at line 582 of file socket.c.

References udp_recvfrom_flags().

Referenced by client(), proxy_thread(), server(), and udp_receiver().

583 {
584  return udp_recvfrom_flags(s, datagram, addr, 0);
585 }
int udp_recvfrom_flags(int s, Octstr **datagram, Octstr **addr, int sockrcvflags)
Definition: socket.c:588
int udp_recvfrom_flags ( int  s,
Octstr **  datagram,
Octstr **  addr,
int  sockrcvflags 
)

Definition at line 588 of file socket.c.

References error(), octstr_create_from_data, and UDP_PACKET_MAX_SIZE.

Referenced by udp_recvfrom(), and wap_msg_recv().

589 {
590  struct sockaddr_in sa;
591  socklen_t salen;
592  char *buf;
593  int bytes;
594 
595  buf = gw_malloc(UDP_PACKET_MAX_SIZE);
596 
597  salen = sizeof(sa);
598  bytes = recvfrom(s, buf, UDP_PACKET_MAX_SIZE, sockrcvflags, (struct sockaddr *) &sa, &salen);
599  if (bytes == -1) {
600  if (errno != EAGAIN)
601  error(errno, "Couldn't receive UDP packet");
602  gw_free(buf);
603  return -1;
604  }
605 
606  *datagram = octstr_create_from_data(buf, bytes);
607  *addr = octstr_create_from_data((char *) &sa, salen);
608 
609  gw_free(buf);
610 
611  return 0;
612 }
void error(int err, const char *fmt,...)
Definition: log.c:612
#define UDP_PACKET_MAX_SIZE
Definition: socket.c:89
int socklen_t
Definition: socket.h:73
#define octstr_create_from_data(data, len)
Definition: octstr.h:134
int udp_sendto ( int  s,
Octstr datagram,
Octstr addr 
)

Definition at line 567 of file socket.c.

References error(), gw_assert(), octstr_get_cstr, and octstr_len().

Referenced by client(), proxy_thread(), send_file(), send_udp(), server(), and wap_msg_send().

568 {
569  struct sockaddr_in sa;
570 
571  gw_assert(octstr_len(addr) == sizeof(sa));
572  memcpy(&sa, octstr_get_cstr(addr), sizeof(sa));
573  if (sendto(s, octstr_get_cstr(datagram), octstr_len(datagram), 0,
574  (struct sockaddr *) &sa, (int) sizeof(sa)) == -1) {
575  error(errno, "Couldn't send UDP packet");
576  return -1;
577  }
578  return 0;
579 }
void error(int err, const char *fmt,...)
Definition: log.c:612
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
gw_assert(wtls_machine->packet_to_send!=NULL)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
int write_to_socket ( int  socket,
char *  str 
)

Definition at line 345 of file socket.c.

References error().

Referenced by cimd_close(), cimd_open_connection(), cimd_pending_smsmessage(), cimd_submit_msg(), connect_tcpip(), ois_deliver_sm_result(), ois_submit_sm_invoke(), and send_acknowledge().

346 {
347  size_t len;
348  int ret;
349 
350  len = strlen(str);
351  while (len > 0) {
352  ret = write(socket, str, len);
353  if (ret == -1) {
354  if (errno == EAGAIN) continue;
355  if (errno == EINTR) continue;
356  error(errno, "Writing to socket failed");
357  return -1;
358  }
359  /* ret may be less than len, if the writing was interrupted
360  by a signal. */
361  len -= ret;
362  str += ret;
363  }
364  return 0;
365 }
void error(int err, const char *fmt,...)
Definition: log.c:612
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.