Line data Source code
1 : // This file (TCP_listen_fd.cpp) was created by Ron Rechenmacher <ron@fnal.gov> on
2 : // Apr 20, 2010. "TERMS AND CONDITIONS" governing this file are in the README
3 : // or COPYING file. If you do not have such a file, one can be obtained by
4 : // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 : // $RCSfile: TCP_listen_fd.cpp,v $
6 : // rev="$Revision: 1.3 $$Date: 2010/06/04 14:00:32 $";
7 :
8 : #include "artdaq/DAQdata/TCP_listen_fd.hh"
9 :
10 : #include "TRACE/tracemf.h"
11 : #include "artdaq/DAQdata/Globals.hh"
12 :
13 : #define TRACE_NAME (app_name + "_TCP_listen_fd").c_str()
14 :
15 : #include <arpa/inet.h> /* inet_aton */
16 : #include <netdb.h> /* gethostbyname */
17 : #include <netinet/in.h> /* inet_aton, struct sockaddr_in */
18 : #include <strings.h> // bzero
19 : #include <sys/socket.h> /* inet_aton, socket, bind, listen, accept */
20 : #include <cerrno> // errno
21 : #include <cstdio> // printf
22 : #include <cstdlib> // exit
23 :
24 2 : int TCP_listen_fd(int port, int rcvbuf)
25 : {
26 : int sts;
27 : int listener_fd;
28 : struct sockaddr_in sin;
29 :
30 2 : listener_fd = socket(PF_INET, SOCK_STREAM, 0); /* man TCP(7P) */
31 2 : if (listener_fd == -1)
32 : {
33 0 : TLOG(TLVL_ERROR) << "Could not open listen socket! Exiting with code 1!";
34 0 : perror("socket error");
35 0 : exit(1);
36 : }
37 :
38 2 : int opt = 1; // SO_REUSEADDR - man socket(7)
39 2 : sts = setsockopt(listener_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
40 2 : if (sts == -1)
41 : {
42 0 : TLOG(TLVL_ERROR) << "Could not set SO_REUSEADDR! Exiting with code 2!";
43 0 : perror("setsockopt SO_REUSEADDR");
44 0 : return (2);
45 : }
46 :
47 2 : memset(static_cast<void *>(&sin), 0, sizeof(sin));
48 2 : sin.sin_family = AF_INET;
49 2 : sin.sin_port = htons(port);
50 2 : sin.sin_addr.s_addr = INADDR_ANY;
51 :
52 : // printf( "bind..." );fflush(stdout);
53 2 : sts = bind(listener_fd, reinterpret_cast<struct sockaddr *>(&sin), sizeof(sin)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
54 2 : if (sts == -1)
55 : {
56 0 : TLOG(TLVL_ERROR) << "Could not bind socket for port " << port << "! Exiting with code 3!";
57 0 : perror("bind error");
58 0 : exit(3);
59 : }
60 : // printf( " OK\n" );
61 :
62 2 : int len = 0;
63 2 : socklen_t arglen = sizeof(len);
64 2 : sts = getsockopt(listener_fd, SOL_SOCKET, SO_RCVBUF, &len, &arglen);
65 6 : TLOG(TLVL_INFO) << port << ": RCVBUF initial: " << len << " sts/errno=" << sts << "/" << errno << " arglen=" << arglen << " rcvbuf=" << rcvbuf << " listener_fd=" << listener_fd;
66 2 : if (rcvbuf > 0)
67 : {
68 0 : len = rcvbuf;
69 0 : sts = setsockopt(listener_fd, SOL_SOCKET, SO_RCVBUF, &len, arglen);
70 0 : if (sts == -1)
71 : {
72 0 : TLOG(TLVL_ERROR) << "Error with setsockopt SNDBUF " << errno;
73 : }
74 0 : len = 0;
75 0 : sts = getsockopt(listener_fd, SOL_SOCKET, SO_RCVBUF, &len, &arglen);
76 0 : if (len < (rcvbuf * 2))
77 : {
78 0 : TLOG(TLVL_WARNING) << "RCVBUF " << len << " not expected (" << rcvbuf << " sts/errno=" << sts << "/" << errno;
79 : }
80 : else
81 : {
82 0 : TLOG(TLVL_DEBUG + 32) << "RCVBUF " << len << " sts/errno=" << sts << "/" << errno;
83 : }
84 : }
85 :
86 : // printf( "listen..." );fflush(stdout);
87 2 : sts = listen(listener_fd, 5 /*QLEN*/);
88 2 : if (sts == -1)
89 : {
90 0 : TLOG(TLVL_ERROR) << "Error calling listen! errno=" << errno << " (" << strerror(errno) << ")";
91 0 : perror("listen error");
92 0 : exit(1);
93 : }
94 : // printf( " OK\n" );
95 :
96 2 : return (listener_fd);
97 : } // TCP_listen_fd
|