2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/socket.h>
32 #include "download-provider.h"
33 #include "download-provider-log.h"
34 #include "download-provider-socket.h"
36 //////////////////////////////////////////////////////////////////////////
37 /// @brief write the error to socket
38 /// @return if success, return 0
39 int dp_ipc_send_errorcode(int fd, dp_error_type errorcode)
42 TRACE_ERROR("[ERROR] CHECK FD[%d]", fd);
46 if (fd >= 0 && write(fd, &errorcode, sizeof(dp_error_type)) <= 0) {
47 TRACE_STRERROR("[ERROR] write FD[%d]", fd);
53 //////////////////////////////////////////////////////////////////////////
54 /// @brief write the progressinfo to socket
55 /// @return if success, return 0
56 int dp_ipc_send_event(int fd, int id, dp_state_type state,
57 dp_error_type errorcode, unsigned long long received_size)
60 TRACE_ERROR("[ERROR][%d] CHECK FD[%d]", id, fd);
64 dp_event_info eventinfo;
66 eventinfo.state = state;
67 eventinfo.err = errorcode;
68 eventinfo.received_size = received_size;
71 if (fd >= 0 && write(fd, &eventinfo, sizeof(dp_event_info)) <= 0) {
72 TRACE_STRERROR("[ERROR][%d] write FD[%d]", id, fd);
78 // keep the order/ unsigned , str
79 char *dp_ipc_read_string(int fd)
85 TRACE_ERROR("[ERROR] CHECK FD[%d]", fd);
89 // read flexible URL from client.
90 if (read(fd, &length, sizeof(unsigned)) < 0) {
91 TRACE_STRERROR("[ERROR] read FD[%d] length[%d]", fd, length);
94 if (length < 1 || length > DP_MAX_URL_LEN) {
95 TRACE_ERROR("[STRING LEGNTH] [%d]", length);
98 str = (char *)calloc((length + 1), sizeof(char));
99 if (read(fd, str, length * sizeof(char)) < 0) {
100 TRACE_STRERROR("[ERROR] read FD[%d]", fd);
109 // keep the order/ unsigned , str
110 int dp_ipc_send_string(int fd, const char *str)
115 TRACE_ERROR("[ERROR] CHECK FD[%d]", fd);
119 TRACE_ERROR("[ERROR] CHECK STRING FD[%d]", fd);
123 length = strlen(str);
125 TRACE_ERROR("[ERROR] CHECK LENGTH FD[%d]", fd);
128 if (fd >= 0 && write(fd, &length, sizeof(unsigned)) <= 0) {
129 TRACE_STRERROR("[ERROR] read FD[%d] length[%d]", fd, length);
132 if (fd >= 0 && write(fd, str, length * sizeof(char)) <= 0) {
133 TRACE_STRERROR("[ERROR] write FD[%d]", fd);
139 int dp_ipc_send_custom_type(int fd, void *value, size_t type_size)
142 TRACE_ERROR("[ERROR] CHECK FD[%d]", fd);
146 TRACE_ERROR("[ERROR] CHECK VALUE FD[%d]", fd);
149 if (fd >= 0 && write(fd, value, type_size) <= 0) {
150 TRACE_STRERROR("[ERROR] write FD[%d]", fd);
156 int dp_ipc_read_custom_type(int fd, void *value, size_t type_size)
159 TRACE_ERROR("[ERROR] CHECK FD[%d]", fd);
163 if (read(fd, value, type_size) < 0) {
164 TRACE_STRERROR("[ERROR] read FD[%d]", fd);
170 int dp_accept_socket_new()
173 struct sockaddr_un listenaddr;
175 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
176 TRACE_STRERROR("failed to create socket");
180 bzero(&listenaddr, sizeof(listenaddr));
181 listenaddr.sun_family = AF_UNIX;
182 strcpy(listenaddr.sun_path, DP_IPC);
184 if (bind(sockfd, (struct sockaddr *)&listenaddr, sizeof listenaddr) !=
186 TRACE_STRERROR("[CRITICAL] bind");
191 if (chmod(listenaddr.sun_path, 0777) < 0) {
192 TRACE_STRERROR("[CRITICAL] chmod");
197 // need 3 socket per a group
198 if (listen(sockfd, DP_MAX_GROUP * 3) != 0) {
199 TRACE_STRERROR("[CRITICAL] listen");
206 int dp_socket_free(int sockfd)