f185da3771623e10a43a4be27240ab049f4c3bc4
[framework/web/wrt-commons.git] / modules / socket / src / unix_socket.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        unix_socket.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of unix socket
21  */
22 #include <dpl/socket/unix_socket.h>
23 #include <dpl/log/log.h>
24 #include <dpl/exception.h>
25 #include <new>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/un.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 namespace DPL
34 {
35 namespace Socket
36 {
37
38 UnixSocket::UnixSocket()
39 {
40 }
41
42 int UnixSocket::AllocSpecificDescriptor() const
43 {
44     LogPedantic("Creating UNIX socket...");
45
46     // Create new descriptor
47     int newSocket = socket(AF_UNIX, SOCK_STREAM, 0);
48
49     if (newSocket == -1)
50         Throw(Exception::CreateFailed);
51
52     LogPedantic("UNIX socket created");
53
54     // Return new descriptor
55     return newSocket;
56 }
57
58 std::pair<sockaddr *, socklen_t> UnixSocket::TranslateAddressGenericToSpecific(const Address &address) const
59 {
60     // Allocate new socket address structure
61     sockaddr_un *sockAddress = static_cast<sockaddr_un *>(malloc(sizeof(sockaddr_un)));
62     if (!sockAddress) throw std::bad_alloc();
63
64     memset(sockAddress, 0, sizeof(sockaddr_un));
65
66     // Copy address properties
67     sockAddress->sun_family = AF_UNIX;
68     strncpy(sockAddress->sun_path, address.GetAddress().c_str(), sizeof(sockAddress->sun_path) - 1);
69     sockAddress->sun_path[sizeof(sockAddress->sun_path) - 1] = '\0'; // Prevent buffer overflows
70
71     // Set proper address length
72     socklen_t sockAddressLength = SUN_LEN(sockAddress);
73
74     // Return new translated address
75     return std::make_pair(reinterpret_cast<sockaddr *>(sockAddress), sockAddressLength);
76 }
77
78 Address UnixSocket::TranslateAddressSpecificToGeneric(sockaddr *address, socklen_t) const
79 {
80     // FIXME: Constrain check ?
81     sockaddr_un *unixAddress = reinterpret_cast<sockaddr_un *>(address);
82     return Address(unixAddress->sun_path);
83 }
84
85 socklen_t UnixSocket::GetSpecificAddressSize() const
86 {
87     return static_cast<socklen_t>(sizeof(sockaddr_un));
88 }
89
90 UnixSocket *UnixSocket::AllocAcceptedSpecificSocket() const
91 {
92     return new UnixSocket();
93 }
94
95 void UnixSocket::Bind(const Address &address)
96 {
97     // Always remove socket file if any
98     unlink(address.GetAddress().c_str());
99
100     // Call base implementation
101     GenericSocket<UnixSocket>::Bind(address);
102
103     // Always set proper permissions to the socket file
104     chmod(address.GetAddress().c_str(), 0777);
105 }
106
107 }
108 } // namespace DPL