OSDN Git Service

*** empty log message ***
[drdeamon64/drdeamon64.git] / libdrd64 / drd64_libdrd64_socket.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #include"drd64_libdrd64.h"
38
39
40 typedef struct  {
41         struct  cmsghdr         t_hdr;
42         struct  cmsgcred        t_cred;
43 } Drd64_Server_SendCMsg;
44
45 Drd64_Server_SendCMsg   gt_sendmsg_cmsg;
46 struct  msghdr                  gt_sendmsg_msghdr;
47 struct  iovec                   gt_sendmsg_iovec;
48
49
50 void
51         Drd64_LibDrd64_Socket_InitSendBuffer(
52                 void )
53 {
54         struct                          cmsghdr         *p_cmsghdr;
55
56         memset( &gt_sendmsg_cmsg, 0x00,
57                                 sizeof( struct cmsghdr ) + sizeof( struct cmsgcred ) );
58         memset( &gt_sendmsg_msghdr, 0x00, sizeof( struct msghdr ) );
59
60         p_cmsghdr   = (struct cmsghdr *)&gt_sendmsg_cmsg;
61         p_cmsghdr->cmsg_len   = sizeof( struct cmsghdr )
62                                                                 + sizeof( struct cmsgcred );
63         p_cmsghdr->cmsg_level = SOL_SOCKET;
64         p_cmsghdr->cmsg_type  = SCM_CREDS;
65
66         gt_sendmsg_msghdr.msg_name              = NULL;
67         gt_sendmsg_msghdr.msg_namelen   = 0;
68         gt_sendmsg_msghdr.msg_iov               = &gt_sendmsg_iovec;
69         gt_sendmsg_msghdr.msg_iovlen    = 1;
70         gt_sendmsg_msghdr.msg_control   = (caddr_t)&gt_sendmsg_cmsg;
71         gt_sendmsg_msghdr.msg_controllen
72                         = sizeof( struct cmsghdr ) + sizeof( struct cmsgcred );
73
74         return;
75 }
76
77
78 int
79         Drd64_LibDrd64_Socket_Send_Cert(
80                 int             i_socket, 
81                 void    *pv_data,
82                 int             i_wsize )
83 {
84         gt_sendmsg_iovec.iov_base    = pv_data;
85         gt_sendmsg_iovec.iov_len     = i_wsize;
86
87         return sendmsg( i_socket, &gt_sendmsg_msghdr, 0x00 );
88 }
89
90
91 int
92         Drd64_LibDrd64_Socket_InitSocket_Local(
93                 const char *pstr_socket )
94 {
95         int             i_socket;
96         int             i_err;
97         struct  sockaddr_un             t_sun;
98
99         i_socket        = socket( PF_LOCAL, SOCK_STREAM, 0 );
100         if( -1 == i_socket )    { return -1; }
101
102         t_sun.sun_family        = AF_UNIX;
103         strcpy( t_sun.sun_path, pstr_socket );
104
105         i_err   = connect( i_socket, &t_sun, SUN_LEN( &t_sun ));
106         if( -1 == i_err )       { return -2; }
107
108         return i_socket;
109 }
110
111
112 int
113     Drd64_LibDrd64_Socket_ReadSocket(
114         int     i_socket,
115         Byte    *pb_data,
116         int     i_size,
117                 int             i_wait )
118 {
119     int     i_bytes;
120     int     i_reads;
121     int     i_err;
122     int     i_ret;
123     Byte    *pb_now;
124     fd_set  t_fds_socket;
125     fd_set  t_fds_temp;
126     struct  timeval t_wait;
127
128     i_err   = 0;
129     i_ret   = 0;
130
131     FD_ZERO( &t_fds_socket );
132     FD_SET( i_socket, &t_fds_socket );
133
134     t_wait.tv_usec  = 0;
135     t_wait.tv_sec   = i_wait;
136     pb_now  = pb_data;
137    
138     i_bytes = i_size;
139     memcpy( &t_fds_temp, &t_fds_socket, sizeof( fd_set ) );
140
141     do  {
142         i_err   = select( i_socket+1, &t_fds_temp, NULL, NULL, &t_wait );
143         if( -1 == i_err )   {
144             i_ret   = -1;
145             break;
146         } else if ( 0 == i_err )    {
147             i_ret   = -2;
148             break;
149         }
150
151         if( FD_ISSET( i_socket, &t_fds_temp ) )     {
152             i_reads = read( i_socket, pb_now, i_bytes );
153
154             if( -1 == i_reads )     {
155                 i_ret   = -3;
156                 break;
157             }
158
159             pb_now  += i_reads;
160             i_bytes -= i_reads;
161         }
162         memcpy( &t_fds_temp, &t_fds_socket, sizeof( fd_set ) );
163     } while( i_bytes > 0 );
164
165     if( 0 == i_ret )        {
166         i_ret   = i_size - i_bytes;
167     }
168
169     return i_ret;
170 }
171
172
173 /* EOF of drd64_.c ----------------------------------- */