source: roaraudio/libroar/socket.c @ 0:2a41d2f42394

Last change on this file since 0:2a41d2f42394 was 0:2a41d2f42394, checked in by phi, 16 years ago

Initial revision

File size: 2.8 KB
Line 
1//socket.c:
2
3#include "libroar.h"
4
5#define MODE_LISTEN  1
6#define MODE_CONNECT 2
7
8int roar_socket_new_tcp (void) {
9 int fh;
10// int extra_flags = TCP_NODELAY;
11
12 fh = socket(PF_INET, SOCK_STREAM, 0);
13
14 return fh;
15}
16
17int roar_socket_new_unix (void) {
18 int fh;
19
20 fh = socket(AF_UNIX, SOCK_STREAM, 0);
21
22 return fh;
23}
24
25int roar_socket_nonblock(int fh, int state) {
26 int flags;
27
28 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 ) {
29  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not read flags: %s", fh, state, strerror(errno));
30  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
31  return -1;
32 }
33
34 flags |= O_NONBLOCK;
35
36 if ( state == ROAR_SOCKET_BLOCK )
37  flags -= O_NONBLOCK;
38
39 if ( fcntl(fh, F_SETFL, flags) == -1 ) {
40  ROAR_ERR("roar_socket_nonblock(fh=%i, state=%i): Can not set flags: %s", fh, state, strerror(errno));
41  ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = -1", fh, state);
42  return -1;
43 }
44
45 ROAR_DBG("roar_socket_nonblock(fh=%i, state=%i) = 0", fh, state);
46 return 0;
47}
48
49int roar_socket_listen  (int type, char * host, int port) {
50 return roar_socket_open(MODE_LISTEN, type, host, port);
51}
52
53int roar_socket_connect (char * host, int port) {
54 return roar_socket_open(MODE_CONNECT, ROAR_SOCKET_TYPE_UNKNOWN, host, port);
55}
56
57int roar_socket_open (int mode, int type, char * host, int port) {
58// int type = ROAR_SOCKET_TYPE_INET;
59 int fh;
60 struct sockaddr_in   socket_addr;
61 struct hostent     * he;
62 //unsigned int host_div = 0;
63 int (*mode_func)(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) = connect; // default is to connect
64
65 if ( mode == MODE_LISTEN )
66  mode_func = bind;
67
68 if ( type == ROAR_SOCKET_TYPE_UNKNOWN ) {
69  type = ROAR_SOCKET_TYPE_INET;
70  if ( *host == '/' )
71   type = ROAR_SOCKET_TYPE_UNIX;
72 }
73
74
75 ROAR_DBG("roar_socket_open(*): type=%s, host='%s', port=%i",
76             type == ROAR_SOCKET_TYPE_UNIX ? "UNIX" : "INET", host, port);
77
78 memset(&socket_addr, 0, sizeof(socket_addr));
79 memset(&he,          0, sizeof(he));
80
81
82 if ( type == ROAR_SOCKET_TYPE_INET ) {
83
84  if ( (he = gethostbyname(host)) == NULL ) {
85   ROAR_ERR("roar_socket_open(*): Can\'t resolve host name '%s'",
86                     host);
87   return -1;
88  }
89
90  memcpy((struct in_addr *)&socket_addr.sin_addr, he->h_addr, sizeof(struct in_addr));
91
92  /* set the connect information */
93  socket_addr.sin_family = AF_INET;
94  socket_addr.sin_port = htons( port );
95
96  fh = roar_socket_new_tcp();
97
98  if ( mode_func(fh, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1 ) {
99   ROAR_DBG("roar_socket_open(*): Can not connect/bind: %s", strerror(errno));
100   close(fh);
101   return -1;
102  }
103  // hey! we have a socket...
104 } else {
105  fh = roar_socket_new_unix();
106  close(fh);
107  return -1;
108 }
109
110 if ( mode == MODE_LISTEN )
111  if ( listen(fh, ROAR_SOCKET_QUEUE_LEN) == -1 ) {
112   close(fh);
113   return -1;
114  }
115
116 return fh;
117}
118
119//ll
Note: See TracBrowser for help on using the repository browser.