source: box/trunk/lib/server/Socket.cpp @ 3049

Revision 3049, 4.1 KB checked in by chris, 5 months ago (diff)

Add remote host and port to post-login login message, requested by Pete Jalajas.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    Socket.cpp
5//              Purpose: Socket related stuff
6//              Created: 2003/07/31
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11
12#ifdef HAVE_UNISTD_H
13        #include <unistd.h>
14#endif
15
16#include <sys/types.h>
17#ifndef WIN32
18#include <sys/socket.h>
19#include <netdb.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#endif
23
24#include <string.h>
25#include <stdio.h>
26
27#include "Socket.h"
28#include "ServerException.h"
29
30#include "MemLeakFindOn.h"
31
32// --------------------------------------------------------------------------
33//
34// Function
35//              Name:    Socket::NameLookupToSockAddr(SocketAllAddr &, int,
36//                       char *, int)
37//              Purpose: Sets up a sockaddr structure given a name and type
38//              Created: 2003/07/31
39//
40// --------------------------------------------------------------------------
41void Socket::NameLookupToSockAddr(SocketAllAddr &addr, int &sockDomain,
42        enum Type Type, const std::string& rName, int Port,
43        int &rSockAddrLenOut)
44{
45        int sockAddrLen = 0;
46
47        switch(Type)
48        {
49        case TypeINET:
50                sockDomain = AF_INET;
51                {
52                        // Lookup hostname
53                        struct hostent *phost = ::gethostbyname(rName.c_str());
54                        if(phost != NULL)
55                        {
56                                if(phost->h_addr_list[0] != 0)
57                                {
58                                        sockAddrLen = sizeof(addr.sa_inet);
59#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
60                                        addr.sa_inet.sin_len = sizeof(addr.sa_inet);
61#endif
62                                        addr.sa_inet.sin_family = PF_INET;
63                                        addr.sa_inet.sin_port = htons(Port);
64                                        addr.sa_inet.sin_addr = *((in_addr*)phost->h_addr_list[0]);
65                                        for(unsigned int l = 0; l < sizeof(addr.sa_inet.sin_zero); ++l)
66                                        {
67                                                addr.sa_inet.sin_zero[l] = 0;
68                                        }
69                                }
70                                else
71                                {
72                                        THROW_EXCEPTION(ConnectionException, Conn_SocketNameLookupError);
73                                }
74                        }
75                        else
76                        {
77                                THROW_EXCEPTION(ConnectionException, Conn_SocketNameLookupError);
78                        }
79                }
80                break;
81       
82#ifndef WIN32
83        case TypeUNIX:
84                sockDomain = AF_UNIX;
85                {
86                        // Check length of name is OK
87                        unsigned int nameLen = rName.length();
88                        if(nameLen >= (sizeof(addr.sa_unix.sun_path) - 1))
89                        {
90                                THROW_EXCEPTION(ServerException, SocketNameUNIXPathTooLong);
91                        }
92                        sockAddrLen = nameLen + (((char*)(&(addr.sa_unix.sun_path[0]))) - ((char*)(&addr.sa_unix)));
93#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
94                        addr.sa_unix.sun_len = sockAddrLen;
95#endif
96                        addr.sa_unix.sun_family = PF_UNIX;
97                        ::strncpy(addr.sa_unix.sun_path, rName.c_str(),
98                                sizeof(addr.sa_unix.sun_path) - 1);
99                        addr.sa_unix.sun_path[sizeof(addr.sa_unix.sun_path)-1] = 0;
100                }
101                break;
102#endif
103       
104        default:
105                THROW_EXCEPTION(CommonException, BadArguments)
106                break;
107        }
108       
109        // Return size of structure to caller
110        rSockAddrLenOut = sockAddrLen;
111}
112
113
114
115
116// --------------------------------------------------------------------------
117//
118// Function
119//              Name:    Socket::LogIncomingConnection(const struct sockaddr *, socklen_t)
120//              Purpose: Writes a message logging the connection to syslog
121//              Created: 2003/08/01
122//
123// --------------------------------------------------------------------------
124void Socket::LogIncomingConnection(const struct sockaddr *addr, socklen_t addrlen)
125{
126        BOX_INFO("Incoming connection from " <<
127                IncomingConnectionLogMessage(addr, addrlen));
128}
129
130// --------------------------------------------------------------------------
131//
132// Function
133//              Name:    Socket::IncomingConnectionLogMessage(const struct sockaddr *, socklen_t)
134//              Purpose: Returns a string for use in log messages
135//              Created: 2003/08/01
136//
137// --------------------------------------------------------------------------
138std::string Socket::IncomingConnectionLogMessage(const struct sockaddr *addr, socklen_t addrlen)
139{
140        if(addr == NULL) {THROW_EXCEPTION(CommonException, BadArguments)}
141
142        switch(addr->sa_family)
143        {
144        case AF_UNIX:
145                return std::string("local (UNIX socket)");
146                break;         
147       
148        case AF_INET:
149                {
150                        sockaddr_in *a = (sockaddr_in*)addr;
151                        std::ostringstream oss;
152                        oss << inet_ntoa(a->sin_addr) << " port " <<
153                                ntohs(a->sin_port);
154                        return oss.str();
155                }
156                break;         
157       
158        default:
159                {
160                        std::ostringstream oss;
161                        oss << "unknown socket type " << addr->sa_family;
162                        return oss.str();
163                }
164                break;
165        }
166       
167        // Dummy.
168        return std::string();
169}
170
Note: See TracBrowser for help on using the repository browser.