3dd905c8d13c6d4bcfff159c40e9761709817b9a
[platform/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 <stddef.h>
23 #include <dpl/socket/unix_socket.h>
24 #include <dpl/log/log.h>
25 #include <dpl/exception.h>
26 #include <new>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/un.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <string.h>
34
35 namespace DPL
36 {
37 namespace Socket
38 {
39
40 UnixSocket::UnixSocket()
41 {
42 }
43
44 int UnixSocket::AllocSpecificDescriptor() const
45 {
46     LogPedantic("Creating UNIX socket...");
47
48     // Create new descriptor
49     int newSocket = socket(AF_UNIX, SOCK_STREAM, 0);
50
51     if (newSocket == -1)
52         Throw(Exception::CreateFailed);
53
54     LogPedantic("UNIX socket created");
55
56     // Return new descriptor
57     return newSocket;
58 }
59
60 std::pair<sockaddr *, socklen_t> UnixSocket::TranslateAddressGenericToSpecific(const Address &address) const
61 {
62     // Allocate new socket address structure
63     sockaddr_un *sockAddress = static_cast<sockaddr_un *>(malloc(sizeof(sockaddr_un)));
64     if (!sockAddress) throw std::bad_alloc();
65
66     memset(sockAddress, 0, sizeof(sockaddr_un));
67
68     // Copy address properties
69     sockAddress->sun_family = AF_UNIX;
70     strncpy(sockAddress->sun_path, address.GetAddress().c_str(), sizeof(sockAddress->sun_path) - 1);
71     sockAddress->sun_path[sizeof(sockAddress->sun_path) - 1] = '\0'; // Prevent buffer overflows
72
73     // Set proper address length
74     socklen_t sockAddressLength = SUN_LEN(sockAddress);
75
76     // Return new translated address
77     return std::make_pair(reinterpret_cast<sockaddr *>(sockAddress), sockAddressLength);
78 }
79
80 Address UnixSocket::TranslateAddressSpecificToGeneric(sockaddr *address, socklen_t) const
81 {
82     // FIXME: Constrain check ?
83     sockaddr_un *unixAddress = reinterpret_cast<sockaddr_un *>(address);
84     return Address(unixAddress->sun_path);
85 }
86
87 socklen_t UnixSocket::GetSpecificAddressSize() const
88 {
89     return static_cast<socklen_t>(sizeof(sockaddr_un));
90 }
91
92 UnixSocket *UnixSocket::AllocAcceptedSpecificSocket() const
93 {
94     return new UnixSocket();
95 }
96
97 void UnixSocket::Bind(const Address &address)
98 {
99     // Always remove socket file if any
100     unlink(address.GetAddress().c_str());
101
102     // Call base implementation
103     GenericSocket<UnixSocket>::Bind(address);
104
105     // Always set proper permissions to the socket file
106     if(chmod(address.GetAddress().c_str(), 0777)<0){
107         LogError("Error setting permissions to the socket file. Errno " << strerror(errno));
108     }
109 }
110 }
111 } // namespace DPL