6 #include <sys/socket.h>
11 #include "ipcsocket.h"
14 int opensocket(int *sockfd
, const char *name
, int connecttype
)
18 if (!name
|| strlen(name
) > MAX_SOCK_NAME_LEN
) {
19 fprintf(stderr
, "<%s>: Invalid socket name.\n", __func__
);
23 ret
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
25 fprintf(stderr
, "<%s>: Failed socket: <%s>\n",
26 __func__
, strerror(errno
));
31 if (setsockopt(*sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
32 (char *)&temp
, sizeof(int)) < 0) {
33 fprintf(stderr
, "<%s>: Failed setsockopt: <%s>\n",
34 __func__
, strerror(errno
));
38 sprintf(sock_name
, "/tmp/%s", name
);
40 if (connecttype
== 1) {
41 /* This is for Server connection */
42 struct sockaddr_un skaddr
;
47 memset(&skaddr
, 0, sizeof(skaddr
));
48 skaddr
.sun_family
= AF_LOCAL
;
49 strcpy(skaddr
.sun_path
, sock_name
);
51 ret
= bind(*sockfd
, (struct sockaddr
*)&skaddr
,
54 fprintf(stderr
, "<%s>: Failed bind: <%s>\n",
55 __func__
, strerror(errno
));
59 ret
= listen(*sockfd
, 5);
61 fprintf(stderr
, "<%s>: Failed listen: <%s>\n",
62 __func__
, strerror(errno
));
66 memset(&skaddr
, 0, sizeof(skaddr
));
67 sklen
= sizeof(skaddr
);
69 ret
= accept(*sockfd
, (struct sockaddr
*)&skaddr
,
72 fprintf(stderr
, "<%s>: Failed accept: <%s>\n",
73 __func__
, strerror(errno
));
80 /* This is for client connection */
81 struct sockaddr_un skaddr
;
83 memset(&skaddr
, 0, sizeof(skaddr
));
84 skaddr
.sun_family
= AF_LOCAL
;
85 strcpy(skaddr
.sun_path
, sock_name
);
87 ret
= connect(*sockfd
, (struct sockaddr
*)&skaddr
,
90 fprintf(stderr
, "<%s>: Failed connect: <%s>\n",
91 __func__
, strerror(errno
));
105 int sendtosocket(int sockfd
, struct socketdata
*skdata
)
109 char cmsg_b
[CMSG_SPACE(sizeof(int))];
110 struct cmsghdr
*cmsg
;
113 struct timeval timeout
;
117 fprintf(stderr
, "<%s>: socketdata is NULL\n", __func__
);
123 FD_SET(sockfd
, &selFDs
);
127 ret
= select(sockfd
+1, NULL
, &selFDs
, NULL
, &timeout
);
129 fprintf(stderr
, "<%s>: Failed select: <%s>\n",
130 __func__
, strerror(errno
));
134 if (FD_ISSET(sockfd
, &selFDs
)) {
135 buffd
= skdata
->data
;
137 memset(&msgh
, 0, sizeof(msgh
));
138 msgh
.msg_control
= &cmsg_b
;
139 msgh
.msg_controllen
= CMSG_LEN(len
);
144 cmsg
= CMSG_FIRSTHDR(&msgh
);
145 cmsg
->cmsg_level
= SOL_SOCKET
;
146 cmsg
->cmsg_type
= SCM_RIGHTS
;
147 cmsg
->cmsg_len
= CMSG_LEN(len
);
148 memcpy(CMSG_DATA(cmsg
), &buffd
, len
);
150 ret
= sendmsg(sockfd
, &msgh
, MSG_DONTWAIT
);
152 fprintf(stderr
, "<%s>: Failed sendmsg: <%s>\n",
153 __func__
, strerror(errno
));
161 int receivefromsocket(int sockfd
, struct socketdata
*skdata
)
164 unsigned int len
= 0;
165 char cmsg_b
[CMSG_SPACE(sizeof(int))];
166 struct cmsghdr
*cmsg
;
173 fprintf(stderr
, "<%s>: socketdata is NULL\n", __func__
);
179 FD_SET(sockfd
, &recvFDs
);
181 ret
= select(sockfd
+1, &recvFDs
, NULL
, NULL
, NULL
);
183 fprintf(stderr
, "<%s>: Failed select: <%s>\n",
184 __func__
, strerror(errno
));
188 if (FD_ISSET(sockfd
, &recvFDs
)) {
190 memset(&msgh
, 0, sizeof(msgh
));
191 msgh
.msg_control
= &cmsg_b
;
192 msgh
.msg_controllen
= CMSG_LEN(len
);
194 iov
.iov_len
= sizeof(data
)-1;
197 cmsg
= CMSG_FIRSTHDR(&msgh
);
198 cmsg
->cmsg_level
= SOL_SOCKET
;
199 cmsg
->cmsg_type
= SCM_RIGHTS
;
200 cmsg
->cmsg_len
= CMSG_LEN(len
);
202 ret
= recvmsg(sockfd
, &msgh
, MSG_DONTWAIT
);
204 fprintf(stderr
, "<%s>: Failed recvmsg: <%s>\n",
205 __func__
, strerror(errno
));
209 memcpy(&buffd
, CMSG_DATA(cmsg
), len
);
210 skdata
->data
= buffd
;
216 int closesocket(int sockfd
, char *name
)
218 char sockname
[MAX_SOCK_NAME_LEN
];
222 sprintf(sockname
, "/tmp/%s", name
);