File: tcp.c

package info (click to toggle)
ganglia-monitor-core 2.5.7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,300 kB
  • ctags: 3,900
  • sloc: ansic: 27,889; sh: 8,492; makefile: 148
file content (251 lines) | stat: -rw-r--r-- 5,472 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * $Id: tcp.c,v 1.3 2002/08/16 20:40:11 massie Exp $
 * This code was originally written by the authors below but 
 * I rewrote it and took out all the glib dependencies. 
 *
 * GNet - Networking library
 * Copyright (C) 2000  David Helder
 * Copyright (C) 2000  Andrew Lanoix
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */
#include <ganglia/net.h>

g_tcp_socket*
g_tcp_socket_connect (const char* hostname, int port)
{
  g_inet_addr* ia;
  g_tcp_socket* socket;

  ia = g_inetaddr_new(hostname, port);
  if (ia == NULL)
    return NULL;

  socket = g_tcp_socket_new(ia);
  g_inetaddr_delete(ia);

  return socket;
}

g_tcp_socket*
g_tcp_socket_new (const g_inet_addr* addr)
{
  int          sockfd;
  g_tcp_socket*     s;
  struct sockaddr_in*   sa_in;
  int       rv;

  if( addr == NULL )
     return NULL;

  /* Create socket */
  sockfd = socket (AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    return NULL;

  s = malloc( sizeof( g_tcp_socket ) );
  memset( s, 0, sizeof( g_tcp_socket ));
  s->sockfd = sockfd;
  s->ref_count = 1;

  /* Set up address and port for connection */
  memcpy(&s->sa, &addr->sa, sizeof(s->sa));
  sa_in = (struct sockaddr_in*) &s->sa;
  sa_in->sin_family = AF_INET;

  /* Connect */
  rv = connect(sockfd, &s->sa, sizeof(s->sa));
  if (rv != 0)
    {
      close (sockfd);
      free (s);
      return NULL;
    }

  return s;
}

static void
g_tcp_socket_ref(g_tcp_socket* s)
{
  if( s==NULL)
     return;

  ++s->ref_count;
}

static void
g_tcp_socket_unref(g_tcp_socket* s)
{
  if( s == NULL )
     return;

  --s->ref_count;

  if (s->ref_count == 0)
    {
       close(s->sockfd);
       free(s);
    }
}

void
g_tcp_socket_delete(g_tcp_socket* s)
{
  if (s != NULL)
    g_tcp_socket_unref(s);
}

g_tcp_socket*
g_tcp_socket_server_new (int port)
{
  g_inet_addr iface;
  struct sockaddr_in* sa_in;

  /* Set up address and port (any address, any port) */
  memset (&iface, 0, sizeof(iface));
  sa_in = (struct sockaddr_in*) &iface.sa;
  sa_in->sin_family = AF_INET;
  sa_in->sin_addr.s_addr = htonl(INADDR_ANY);
  sa_in->sin_port = htons(port);

  return g_tcp_socket_server_new_interface (&iface);
}


g_tcp_socket* 
g_tcp_socket_server_new_interface (const g_inet_addr* iface)
{
  g_tcp_socket* s;
  struct sockaddr_in* sa_in;
  socklen_t socklen;
  int flags, rval;
  const int on = 1;

  /* Create socket */
  s = malloc( sizeof(g_tcp_socket));
  memset(s, 0, sizeof(g_tcp_socket));

  s->ref_count = 1;
  s->sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (s->sockfd < 0)
    goto error;

  /* Set up address and port for connection */
  sa_in = (struct sockaddr_in*) &s->sa;
  sa_in->sin_family = AF_INET;
  if (iface)
    {
      sa_in->sin_addr.s_addr = G_SOCKADDR_IN(iface->sa).sin_addr.s_addr;
      sa_in->sin_port = G_SOCKADDR_IN(iface->sa).sin_port;
    }
  else
    {
      sa_in->sin_addr.s_addr = htonl(INADDR_ANY);
      sa_in->sin_port = 0;
    }

  /* Set REUSEADDR so we can reuse the port */
  rval = setsockopt(s->sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &on, sizeof(on));
  if ( rval <0)
     {
        err_ret("tcp_listen() setsockopt() SO_REUSEADDR error");
        goto error;
     }

  rval = setsockopt(s->sockfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
  if ( rval <0)
     {
        err_ret("tcp_listen() setsockopt() SO_KEEPALIVE error");
        goto error;
     }
  rval = setsockopt(s->sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
  if ( rval <0)
     {
        err_ret("tcp_listen() setsockopt() TCP_NODELAY error");
        goto error;
     }

  /* Bind */
  if (bind(s->sockfd, &s->sa, sizeof(s->sa)) != 0)
    goto error;
  
  /* Get the socket name */
  socklen = sizeof(s->sa);
  if (getsockname(s->sockfd, &s->sa, &socklen) != 0)
    goto error;
  
  /* Listen */
  if (listen(s->sockfd, 10) != 0)
    goto error;
  
  return s;
  
 error:
  if (s)    		free(s);
  return NULL;
}

g_tcp_socket *
g_tcp_socket_server_accept (g_tcp_socket* socket)
{
  int sockfd;
  struct sockaddr sa;
  socklen_t n;
  fd_set fdset;
  g_tcp_socket* s;

  if( socket == NULL )
     return NULL;

 try_again:

  FD_ZERO(&fdset);
  FD_SET(socket->sockfd, &fdset);

  if (select(socket->sockfd + 1, &fdset, NULL, NULL, NULL) == -1)
    {
      if (errno == EINTR)
   goto try_again;

      return NULL;
    }

  n = sizeof(s->sa);

  if ((sockfd = accept(socket->sockfd, &sa, &n)) == -1)
    {
      if (errno == EWOULDBLOCK ||
     errno == ECONNABORTED ||
#ifdef EPROTO     /* OpenBSD does not have EPROTO */
     errno == EPROTO ||
#endif
     errno == EINTR)
   goto try_again;

      return NULL;
    }

  s = malloc( sizeof(g_tcp_socket) );
  memset( s, 0, sizeof(g_tcp_socket));
  s->ref_count = 1;
  s->sockfd = sockfd;
  memcpy(&s->sa, &sa, sizeof(s->sa));

  return s;
}