File: net.c

package info (click to toggle)
bld 0.3.4.1-4
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 576 kB
  • ctags: 268
  • sloc: ansic: 2,333; sh: 175; makefile: 157; perl: 135; python: 36
file content (171 lines) | stat: -rw-r--r-- 4,824 bytes parent folder | download
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
/*-
 * Copyright (c) 2004 Free (Olivier Beyssac)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <syslog.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include "net.h"
#include "utils.h"
#include "options.h"


#define LISTENQ 5


struct options opt;


/* Set a socket non-blocking. Return 1 if successful, 0 if not */
extern int net_socket_set_nb(const int sd)
{
  int flags;
  
  if ((flags = fcntl(sd, F_GETFL, 0)) == -1) {
    syslog(LOG_ERR, "fcntl: %s", strerror(errno));
    return 0;
  }
      
  if ((fcntl(sd, F_SETFL, flags | O_NONBLOCK)) == -1) {
    syslog(LOG_ERR, "fcntl: %s", strerror(errno));
    return 0;
  }
  
  return 1;
}


/* Return a TCP socket listening on ip:port
   Return -1 on error */
extern int net_server_sock_create(const char *ip, const char *port)
{
  struct sockaddr_in servaddr;
  struct servent *sv;
  struct hostent *h;
  int listen_port;
  int optval = 1;
  int sd;

  if ((listen_port = atoi(port)) == 0) {
    if ((sv = getservbyname(port, "tcp")) == NULL) {
      syslog(LOG_ERR, "getservbyname() error on %s", port);
      return -1;
    } else {
      listen_port = ntohs(sv->s_port);
    }
  }

  if ((h = gethostbyname(ip)) == NULL) {
    syslog(LOG_ERR, "gethostbyname() error on %s\n", ip);
    return -1;
  }
  
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family      = h->h_addrtype;
  memcpy((char *)&servaddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
  servaddr.sin_port = htons(listen_port);

  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    syslog(LOG_ERR, "socket(): %s", strerror(errno));
    return -1;
  }

  setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

  if (bind(sd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
    syslog(LOG_ERR, "bind() to port %s: %s", port, strerror(errno));
    return -1;
  }

  if (listen(sd, LISTENQ) == -1) {
    syslog(LOG_ERR, "listen() on port %s: %s", port, strerror(errno));
    return -1;
  }

  return sd;
}

static void alrm_signal_handler(const int signum)
{
  if (opt.log_level >= 3)
    syslog(LOG_INFO, "caught signal %d", signum);
}


/* Return a TCP socket connected to server:port
   Return -1 on error
   NOTE: SIGALRM used to handle connect(2) timeout */
extern int net_client_sock_create(const char *server, const char *server_port)
{
  int sd, port;
  struct sockaddr_in local_addr, serv_addr;
  struct hostent *h;
  struct servent *sv;
  struct sigaction sa;

  if ((h = gethostbyname(server)) == NULL)
    return -1;
  
  if ((port = atoi(server_port)) == 0) {
    if ((sv = getservbyname(server_port, "tcp")) == NULL)
      return -1;
    else
      port = sv->s_port;
  }
  
  serv_addr.sin_family = h->h_addrtype;
  memcpy((char *)&serv_addr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
  serv_addr.sin_port = htons(port);

  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return -1;

  local_addr.sin_family = AF_INET;
  local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  local_addr.sin_port = htons(0);  

  if (bind(sd, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0)
    return -1;

  xsigaction(sa, SIGALRM, alrm_signal_handler);
  alarm(opt.connect_timeout);
  if (connect(sd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    return -1;
  alarm(0);
  xsigaction(sa, SIGALRM, SIG_IGN);
  
  return sd;
}