Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

socket.c File Reference

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/utsname.h>
#include "gwlib.h"

Include dependency graph for socket.c:

Include dependency graph

Go to the source code of this file.

Defines

#define UDP_PACKET_MAX_SIZE   (64*1024)

Functions

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

Variables

Octstrofficial_name = NULL
Octstrofficial_ip = NULL
const struct sockaddr_in empty_sockaddr_in


Define Documentation

#define UDP_PACKET_MAX_SIZE   (64*1024)
 

Definition at line 89 of file socket.c.

Referenced by udp_recvfrom().


Function Documentation

Octstr* get_official_ip void   ) 
 

Definition at line 596 of file socket.c.

References gw_assert, and official_ip.

Referenced by version_report_string().

00597 {
00598     gw_assert(official_ip != NULL);
00599     return official_ip;
00600 }

Octstr* get_official_name void   ) 
 

Definition at line 589 of file socket.c.

References gw_assert, and official_name.

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

00590 {
00591     gw_assert(official_name != NULL);
00592     return official_name;
00593 }

int gw_accept int  fd,
Octstr **  client_addr
 

Definition at line 677 of file socket.c.

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

Referenced by smpp_emu().

00678 {
00679     struct sockaddr_in addr;
00680     int addrlen;
00681     int new_fd;
00682 
00683     if (gwthread_pollfd(fd, POLLIN, -1.0) != POLLIN) {
00684     debug("gwlib.socket", 0, "gwthread_pollfd interrupted or failed");
00685     return -1;
00686     }
00687     addrlen = sizeof(addr);
00688     new_fd = accept(fd, (struct sockaddr *) &addr, &addrlen);
00689     if (new_fd == -1) {
00690     error(errno, "accept system call failed.");
00691     return -1;
00692     }
00693     *client_addr = host_ip(addr);
00694     debug("test_smsc", 0, "accept() succeeded, client from %s",
00695       octstr_get_cstr(*client_addr));
00696     return new_fd;
00697 }

Here is the call graph for this function:

Octstr* gw_netaddr_to_octstr int  af,
void *  src
 

Definition at line 660 of file socket.c.

References gw_netaddr_to_octstr4().

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

00661 {
00662     switch (af) {
00663     case AF_INET:
00664     return gw_netaddr_to_octstr4(src);
00665 
00666 #ifdef AF_INET6
00667     case AF_INET6:
00668     return gw_netaddr_to_octstr6(src);
00669 #endif
00670 
00671     default:
00672     return NULL;
00673     } 
00674 }

Here is the call graph for this function:

Octstr* gw_netaddr_to_octstr4 unsigned char *  src  )  [static]
 

Definition at line 639 of file socket.c.

References octstr_format().

Referenced by gw_netaddr_to_octstr().

00640 {
00641     return octstr_format("%d.%d.%d.%d", src[0], src[1], src[2], src[3]);
00642 }

Here is the call graph for this function:

Octstr* host_ip struct sockaddr_in  addr  ) 
 

Definition at line 577 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().

00578 {
00579     return gw_netaddr_to_octstr(AF_INET, &addr.sin_addr);
00580 }

Here is the call graph for this function:

int host_port struct sockaddr_in  addr  ) 
 

Definition at line 583 of file socket.c.

00584 {
00585     return ntohs(addr.sin_port);
00586 }

int make_server_socket int  port,
const char *  interface_name
 

Definition at line 93 of file socket.c.

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

Referenced by accept_thread(), 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().

00094 {
00095     struct sockaddr_in addr;
00096     int s;
00097     int reuse;
00098     struct hostent hostinfo;
00099     char *buff = NULL;
00100 
00101     s = socket(PF_INET, SOCK_STREAM, 0);
00102     if (s == -1) {
00103         error(errno, "socket failed");
00104         goto error;
00105     }
00106 
00107     addr = empty_sockaddr_in;
00108     addr.sin_family = AF_INET;
00109     addr.sin_port = htons(port);
00110     if (interface_name == NULL || strcmp(interface_name, "*") == 0)
00111         addr.sin_addr.s_addr = htonl(INADDR_ANY);
00112     else {
00113         if (gw_gethostbyname(&hostinfo, interface_name, &buff) == -1) {
00114             error(errno, "gethostbyname failed");
00115             goto error;
00116         }
00117         addr.sin_addr = *(struct in_addr *) hostinfo.h_addr;
00118     }
00119 
00120     reuse = 1;
00121     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,
00122                    sizeof(reuse)) == -1) {
00123         error(errno, "setsockopt failed for server address");
00124         goto error;
00125     }
00126 
00127     if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
00128         error(errno, "bind failed");
00129         goto error;
00130     }
00131 
00132     if (listen(s, 10) == -1) {
00133         error(errno, "listen failed");
00134         goto error;
00135     }
00136 
00137     gw_free(buff);
00138 
00139     return s;
00140 
00141 error:
00142     if (s >= 0)
00143         (void) close(s);
00144     gw_free(buff);
00145     return -1;
00146 }

Here is the call graph for this function:

int read_available int  fd,
long  wait_usec
 

Definition at line 373 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().

00374 {
00375     fd_set rf;
00376     struct timeval to;
00377     int ret;
00378     div_t waits;
00379 
00380     gw_assert(fd >= 0);
00381 
00382     FD_ZERO(&rf);
00383     FD_SET(fd, &rf);
00384     waits = div(wait_usec, 1000000);
00385     to.tv_sec = waits.quot;
00386     to.tv_usec = waits.rem;
00387 retry:
00388     ret = select(fd + 1, &rf, NULL, NULL, &to);
00389     if (ret > 0 && FD_ISSET(fd, &rf))
00390         return 1;
00391     if (ret < 0) {
00392         /* In most select() implementations, to will now contain the
00393          * remaining time rather than the original time.  That is exactly
00394          * what we want when retrying after an interrupt. */
00395         switch (errno) {
00396             /*The first two entries here are OK*/
00397         case EINTR:
00398             goto retry;
00399         case EAGAIN:
00400             return 1;
00401             /* We are now sucking mud, figure things out here
00402              * as much as possible before it gets lost under
00403              * layers of abstraction.  */
00404         case EBADF:
00405             if (!FD_ISSET(fd, &rf)) {
00406                 warning(0, "Tried to select on fd %d, not in the set!\n", fd);
00407             } else {
00408                 warning(0, "Tried to select on invalid fd %d!\n", fd);
00409             }
00410             break;
00411         case EINVAL:
00412             /* Solaris catchall "It didn't work" error, lets apply
00413              * some tests and see if we can catch it. */
00414 
00415             /* First up, try invalid timeout*/
00416             if (to.tv_sec > 10000000)
00417                 warning(0, "Wait more than three years for a select?\n");
00418             if (to.tv_usec > 1000000)
00419                 warning(0, "There are only 1000000 usec in a second...\n");
00420             break;
00421 
00422 
00423         }
00424         return -1;  /* some error */
00425     }
00426     return 0;
00427 }

Here is the call graph for this function:

void setup_official_name void   )  [static]
 

Definition at line 603 of file socket.c.

References error(), gw_assert, gw_gethostbyname(), GW_NAME, gw_netaddr_to_octstr(), octstr_create, official_ip, official_name, and panic.

Referenced by socket_init().

00604 {
00605     struct utsname u;
00606     struct hostent h;
00607     char *buff = NULL;
00608 
00609     gw_assert(official_name == NULL);
00610     if (uname(&u) == -1)
00611         panic(0, "uname failed - can't happen, unless " GW_NAME " is buggy.");
00612     if (gw_gethostbyname(&h, u.nodename, &buff) == -1) {
00613         error(0, "Can't find out official hostname for this host, "
00614               "using `%s' instead.", u.nodename);
00615         official_name = octstr_create(u.nodename);
00616         official_ip = octstr_create("127.0.0.1");
00617     } else {
00618         official_name = octstr_create(h.h_name);
00619         official_ip = gw_netaddr_to_octstr(AF_INET, h.h_addr);
00620     }
00621     gw_free(buff);
00622 }

Here is the call graph for this function:

void socket_init void   ) 
 

Definition at line 625 of file socket.c.

References setup_official_name().

Referenced by gwlib_init().

00626 {
00627     setup_official_name();
00628 }

Here is the call graph for this function:

int socket_set_blocking int  fd,
int  blocking
 

Definition at line 347 of file socket.c.

References error().

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

00348 {
00349     int flags, newflags;
00350 
00351     flags = fcntl(fd, F_GETFL);
00352     if (flags < 0) {
00353         error(errno, "cannot get flags for fd %d", fd);
00354         return -1;
00355     }
00356 
00357     if (blocking)
00358         newflags = flags & ~O_NONBLOCK;
00359     else
00360         newflags = flags | O_NONBLOCK;
00361 
00362     if (newflags != flags) {
00363         if (fcntl(fd, F_SETFL, newflags) < 0) {
00364             error(errno, "cannot set flags for fd %d", fd);
00365             return -1;
00366         }
00367     }
00368 
00369     return 0;
00370 }

Here is the call graph for this function:

void socket_shutdown void   ) 
 

Definition at line 630 of file socket.c.

References octstr_destroy(), official_ip, and official_name.

Referenced by gwlib_shutdown().

00631 {
00632     octstr_destroy(official_name);
00633     official_name = NULL;
00634     octstr_destroy(official_ip);
00635     official_ip = NULL;
00636 }

Here is the call graph for this function:

int tcpip_connect_nb_to_server char *  hostname,
int  port,
const char *  interface_name,
int *  done
 

Definition at line 230 of file socket.c.

References hostname, port, and tcpip_connect_nb_to_server_with_port().

00231 {
00232   return tcpip_connect_nb_to_server_with_port(hostname, port, 0, interface_name, done);
00233 }

Here is the call graph for this function:

int tcpip_connect_nb_to_server_with_port char *  hostname,
int  port,
int  our_port,
const char *  interface_name,
int *  done
 

Definition at line 235 of file socket.c.

References error(), gw_gethostbyname(), hostname, and port.

Referenced by conn_open_tcp_nb_with_port(), and tcpip_connect_nb_to_server().

00236 {
00237   struct sockaddr_in addr;
00238   struct sockaddr_in o_addr;
00239   struct hostent hostinfo;
00240   struct hostent o_hostinfo;
00241   int s;
00242   int flags,rc;
00243   char *buff, *buff1;
00244 
00245   *done = 1;
00246   buff = buff1 = NULL;
00247 
00248   s = socket(PF_INET, SOCK_STREAM, 0);
00249   if (s == -1) {
00250     error(errno, "Couldn't create new socket.");
00251     goto error;
00252   }
00253 
00254   if (gw_gethostbyname(&hostinfo, hostname, &buff) == -1) {
00255     error(errno, "gethostbyname failed");
00256     goto error;
00257   }
00258 
00259   addr = empty_sockaddr_in;
00260   addr.sin_family = AF_INET;
00261   addr.sin_port = htons(port);
00262   addr.sin_addr = *(struct in_addr *) hostinfo.h_addr;
00263 
00264   if (our_port > 0 || (interface_name != NULL && strcmp(interface_name, "*") != 0)) {
00265     int reuse;
00266 
00267     o_addr = empty_sockaddr_in;
00268     o_addr.sin_family = AF_INET;
00269     o_addr.sin_port = htons(our_port);
00270     if (interface_name == NULL || strcmp(interface_name, "*") == 0)
00271       o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
00272     else {
00273       if (gw_gethostbyname(&o_hostinfo, interface_name, &buff1) == -1) {
00274     error(errno, "gethostbyname failed");
00275     goto error;
00276       }
00277       o_addr.sin_addr = *(struct in_addr *) o_hostinfo.h_addr;
00278     }
00279 
00280     reuse = 1;
00281     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,
00282            sizeof(reuse)) == -1) {
00283       error(errno, "setsockopt failed before bind");
00284       goto error;
00285     }
00286     if (bind(s, (struct sockaddr *) &o_addr, sizeof(o_addr)) == -1) {
00287       error(errno, "bind to local port %d failed", our_port);
00288       goto error;
00289     }
00290   }
00291 
00292   flags = fcntl(s, F_GETFL, 0);
00293   fcntl(s, F_SETFL, flags | O_NONBLOCK);
00294 
00295   if ((rc = connect(s, (struct sockaddr *) &addr, sizeof(addr))) < 0) {
00296     if (errno != EINPROGRESS) {
00297       error(errno, "nonblocking connect failed");
00298       goto error;
00299     }
00300   }
00301   
00302   /* May be connected immediatly
00303    * (if we connecting to localhost for example) */
00304   if (rc == 0) { 
00305     *done = 0;
00306   }
00307 
00308   gw_free(buff);
00309   gw_free(buff1);
00310 
00311   return s;
00312 
00313  error:
00314   error(0, "error connecting to server `%s' at port `%d'",
00315     hostname, port);
00316   if (s >= 0)
00317     close(s);
00318   gw_free(buff);
00319   gw_free(buff1);
00320   return -1;
00321 }

Here is the call graph for this function:

int tcpip_connect_to_server char *  hostname,
int  port,
const char *  interface_name
 

Definition at line 149 of file socket.c.

References hostname, port, and tcpip_connect_to_server_with_port().

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

00150 {
00151 
00152     return tcpip_connect_to_server_with_port(hostname, port, 0, interface_name);
00153 }

Here is the call graph for this function:

int tcpip_connect_to_server_with_port char *  hostname,
int  port,
int  our_port,
const char *  interface_name
 

Definition at line 156 of file socket.c.

References error(), gw_gethostbyname(), hostname, and port.

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

00157 {
00158     struct sockaddr_in addr;
00159     struct sockaddr_in o_addr;
00160     struct hostent hostinfo;
00161     struct hostent o_hostinfo;
00162     int s;
00163     char *buff, *buff1;
00164 
00165     buff = buff1 = NULL;
00166 
00167     s = socket(PF_INET, SOCK_STREAM, 0);
00168     if (s == -1) {
00169         error(errno, "Couldn't create new socket.");
00170         goto error;
00171     }
00172 
00173     if (gw_gethostbyname(&hostinfo, hostname, &buff) == -1) {
00174         error(errno, "gethostbyname failed");
00175         goto error;
00176     }
00177 
00178     addr = empty_sockaddr_in;
00179     addr.sin_family = AF_INET;
00180     addr.sin_port = htons(port);
00181     addr.sin_addr = *(struct in_addr *) hostinfo.h_addr;
00182 
00183     if (our_port > 0 || (interface_name != NULL && strcmp(interface_name, "*") != 0))  {
00184         int reuse;
00185 
00186         o_addr = empty_sockaddr_in;
00187         o_addr.sin_family = AF_INET;
00188         o_addr.sin_port = htons(our_port);
00189     if (interface_name == NULL || strcmp(interface_name, "*") == 0)
00190         o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
00191     else {
00192         if (gw_gethostbyname(&o_hostinfo, interface_name, &buff1) == -1) {
00193         error(errno, "gethostbyname failed");
00194         goto error;
00195         }
00196         o_addr.sin_addr = *(struct in_addr *) o_hostinfo.h_addr;
00197     }
00198 
00199         reuse = 1;
00200         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,
00201                        sizeof(reuse)) == -1) {
00202             error(errno, "setsockopt failed before bind");
00203             goto error;
00204         }
00205         if (bind(s, (struct sockaddr *) &o_addr, sizeof(o_addr)) == -1) {
00206             error(errno, "bind to local port %d failed", our_port);
00207             goto error;
00208         }
00209     }
00210 
00211     if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
00212         error(errno, "connect failed");
00213         goto error;
00214     }
00215 
00216     gw_free(buff);
00217     gw_free(buff1);
00218     return s;
00219 
00220 error:
00221     error(0, "error connecting to server `%s' at port `%d'",
00222           hostname, port);
00223     if (s >= 0)
00224         close(s);
00225     gw_free(buff);
00226     gw_free(buff1);
00227     return -1;
00228 }

Here is the call graph for this function:

int udp_bind int  port,
const char *  interface_name
 

Definition at line 445 of file socket.c.

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

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

00446 {
00447     int s;
00448     struct sockaddr_in sa;
00449     struct hostent hostinfo;
00450     char *buff = NULL;
00451 
00452     s = socket(PF_INET, SOCK_DGRAM, 0);
00453     if (s == -1) {
00454         error(errno, "Couldn't create a UDP socket");
00455         return -1;
00456     }
00457 
00458     sa = empty_sockaddr_in;
00459     sa.sin_family = AF_INET;
00460     sa.sin_port = htons(port);
00461     if (strcmp(interface_name, "*") == 0)
00462         sa.sin_addr.s_addr = htonl(INADDR_ANY);
00463     else {
00464         if (gw_gethostbyname(&hostinfo, interface_name, &buff) == -1) {
00465             error(errno, "gethostbyname failed");
00466             gw_free(buff);
00467             return -1;
00468         }
00469         sa.sin_addr = *(struct in_addr *) hostinfo.h_addr;
00470     }
00471 
00472     if (bind(s, (struct sockaddr *) &sa, (int) sizeof(sa)) == -1) {
00473         error(errno, "Couldn't bind a UDP socket to port %d", port);
00474         (void) close(s);
00475         return -1;
00476     }
00477 
00478     gw_free(buff);
00479 
00480     return s;
00481 }

Here is the call graph for this function:

int udp_client_socket void   ) 
 

Definition at line 431 of file socket.c.

References error().

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

00432 {
00433     int s;
00434 
00435     s = socket(PF_INET, SOCK_DGRAM, 0);
00436     if (s == -1) {
00437         error(errno, "Couldn't create a UDP socket");
00438         return -1;
00439     }
00440 
00441     return s;
00442 }

Here is the call graph for this function:

Octstr* udp_create_address Octstr host_or_ip,
int  port
 

Definition at line 484 of file socket.c.

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

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

00485 {
00486     struct sockaddr_in sa;
00487     struct hostent h;
00488     char *buff = NULL;
00489     Octstr *ret;
00490 
00491     sa = empty_sockaddr_in;
00492     sa.sin_family = AF_INET;
00493     sa.sin_port = htons(port);
00494 
00495     if (strcmp(octstr_get_cstr(host_or_ip), "*") == 0) {
00496         sa.sin_addr.s_addr = INADDR_ANY;
00497     } else {
00498         if (gw_gethostbyname(&h, octstr_get_cstr(host_or_ip), &buff) == -1) {
00499             error(0, "Couldn't find the IP number of `%s'",
00500                   octstr_get_cstr(host_or_ip));
00501             gw_free(buff);
00502             return NULL;
00503         }
00504         sa.sin_addr = *(struct in_addr *) h.h_addr;
00505     }
00506 
00507     ret = octstr_create_from_data((char *) &sa, sizeof(sa));
00508     gw_free(buff);
00509 
00510     return ret;
00511 }

Here is the call graph for this function:

Octstr* udp_get_ip Octstr addr  ) 
 

Definition at line 524 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().

00525 {
00526     struct sockaddr_in sa;
00527 
00528     gw_assert(octstr_len(addr) == sizeof(sa));
00529     memcpy(&sa, octstr_get_cstr(addr), sizeof(sa));
00530     return gw_netaddr_to_octstr(AF_INET, &sa.sin_addr);
00531 }

Here is the call graph for this function:

int udp_get_port Octstr addr  ) 
 

Definition at line 514 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().

00515 {
00516     struct sockaddr_in sa;
00517 
00518     gw_assert(octstr_len(addr) == sizeof(sa));
00519     memcpy(&sa, octstr_get_cstr(addr), sizeof(sa));
00520     return ntohs(sa.sin_port);
00521 }

Here is the call graph for this function:

int udp_recvfrom int  s,
Octstr **  datagram,
Octstr **  addr
 

Definition at line 549 of file socket.c.

References error(), octstr_create_from_data, and UDP_PACKET_MAX_SIZE.

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

00550 {
00551     struct sockaddr_in sa;
00552     int salen;
00553     char *buf;
00554     int bytes;
00555 
00556     buf = gw_malloc(UDP_PACKET_MAX_SIZE);
00557 
00558     salen = sizeof(sa);
00559     bytes = recvfrom(s, buf, UDP_PACKET_MAX_SIZE, 0,
00560                      (struct sockaddr *) &sa, &salen);
00561     if (bytes == -1) {
00562         if (errno != EAGAIN)
00563             error(errno, "Couldn't receive UDP packet");
00564     gw_free(buf);
00565         return -1;
00566     }
00567 
00568     *datagram = octstr_create_from_data(buf, bytes);
00569     *addr = octstr_create_from_data((char *) &sa, salen);
00570     
00571     gw_free(buf);
00572 
00573     return 0;
00574 }

Here is the call graph for this function:

int udp_sendto int  s,
Octstr datagram,
Octstr addr
 

Definition at line 534 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().

00535 {
00536     struct sockaddr_in sa;
00537 
00538     gw_assert(octstr_len(addr) == sizeof(sa));
00539     memcpy(&sa, octstr_get_cstr(addr), sizeof(sa));
00540     if (sendto(s, octstr_get_cstr(datagram), octstr_len(datagram), 0,
00541                (struct sockaddr *) &sa, (int) sizeof(sa)) == -1) {
00542         error(errno, "Couldn't send UDP packet");
00543         return -1;
00544     }
00545     return 0;
00546 }

Here is the call graph for this function:

int write_to_socket int  socket,
char *  str
 

Definition at line 324 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().

00325 {
00326     size_t len;
00327     int ret;
00328 
00329     len = strlen(str);
00330     while (len > 0) {
00331         ret = write(socket, str, len);
00332         if (ret == -1) {
00333             if (errno == EAGAIN) continue;
00334             if (errno == EINTR) continue;
00335             error(errno, "Writing to socket failed");
00336             return -1;
00337         }
00338         /* ret may be less than len, if the writing was interrupted
00339            by a signal. */
00340         len -= ret;
00341         str += ret;
00342     }
00343     return 0;
00344 }

Here is the call graph for this function:


Variable Documentation

const struct sockaddr_in empty_sockaddr_in [static]
 

Definition at line 86 of file socket.c.

Octstr* official_ip = NULL [static]
 

Definition at line 79 of file socket.c.

Referenced by get_official_ip(), setup_official_name(), and socket_shutdown().

Octstr* official_name = NULL [static]
 

Definition at line 78 of file socket.c.

Referenced by get_official_name(), setup_official_name(), and socket_shutdown().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.