OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / deamon / drd64_marshald_send_connect.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 #define DRD64_SRCID_MARSHALD_SEND_CONNECT       0x0000
38 #include"drd64_marshald.h"
39
40
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 #define DRD64_FUNCID_Marshald_SendCmd_Connect   0x01
44 int     
45         Drd64_Marshald_SendCmd_Connect(
46                 Drd64_Marshald_ConnectInfo **pp_cinfo,
47                 struct  in_addr *p_addr_dest,
48                 int             i_recv_id )
49 {
50         int                                                     i_err;
51         int                                                     i_wlen;
52         int                                                     i_socket_inet;
53         DWord                                           dw_errcode;
54         DWord                                           dw_location;
55         Drd64_PacketHeader                      *p_phead;
56         Drd64_PacketData_Connect        *p_connect;
57         Drd64_Marshald_ConnectInfo      *p_cinfo;
58         Drd64_Server_RecvStatus         *p_recv;
59
60         DRD64_LOG_DEBUG("  [DEBUG]SEND: Connect Cmd (for INet-Marshald) Start");
61
62         dw_errcode  = DRD64_ERROR_CODE_CLEAR;
63         dw_location = DRD64_ERROR_SET_LOCATION(
64                                                 DRD64_ERROR_MODULE_MARSHALD,
65                                                 DRD64_ERROR_ARCH_NODEPEND,
66                                                 DRD64_SRCID_MARSHALD_SEND_CONNECT,
67                                                 DRD64_FUNCID_Marshald_SendCmd_Connect );
68         *pp_cinfo       = NULL;
69
70
71         /* Alloc CInfo ----*/
72         p_cinfo = Drd64_Marshald_CInfo_AllocConnectInfo();
73         if( NULL == p_cinfo )   {
74                 dw_errcode  = DRD64_ERROR_SET_CODE( DRD64_ERROR_TYPE_ERROR,
75                                                         DRD64_ERROR_PTN_NOMEMORY, 0x00, 0x01 );
76                 //Drd64_LibBrownie_Error_SetErrorInfo( dw_errcode, dw_location, errno );
77
78                 return -0x01;
79         }
80
81
82         /* Set Packet Header ---*/
83         p_phead = (Drd64_PacketHeader *)gp_sendbuffer;
84         memset( gp_sendbuffer, 0x00, ( sizeof( Drd64_PacketHeader )
85                                                                 + sizeof( Drd64_PacketData_Connect ) ));
86
87         p_phead->w_header               = DRD64_PACKET_HEADER;
88         p_phead->i_cid_dest             = DRD64_CONNECTID_NONE;
89         p_phead->i_cid_src              = DRD64_CONNECTID_NONE;
90         p_phead->dw_command             = DRD64_COMMAND_CONNECT;
91         p_phead->w_packettype   = DRD64_PACKETTYPE_LINK;
92         p_phead->i_datalen              = sizeof( Drd64_PacketData_Connect );
93         p_phead->i_rid_resume   = i_recv_id;
94         p_phead->dw_errcode     = DRD64_ERROR_CODE_CLEAR;
95         p_phead->dw_errlocation = DRD64_ERROR_LOCATION_CLEAR;
96
97
98         p_connect
99                 = (Drd64_PacketData_Connect *)DRD64_PacketData( gp_sendbuffer );
100
101         p_connect->i_client_pid                 = getpid();
102         p_connect->i_client_cinfo_id    = p_cinfo->i_cinfo_id;
103         p_connect->i_connect_id                 = -1;
104         i_err   = Drd64_LibBrownie_INetAddr_SetInAddr_LocalHost(
105                                                                 &(p_connect->t_client_addr_inet) );
106         if( 0x00 != i_err )             {
107                 dw_errcode  = DRD64_ERROR_SET_CODE( DRD64_ERROR_TYPE_ERROR,
108                                                         DRD64_ERROR_PTN_INETADDR, 0x00, 0x02 );
109                 //Drd64_LibBrownie_Error_SetErrorInfo( dw_errcode, dw_location, errno );
110
111                 Drd64_Marshald_Cinfo_FreeConnectInfo( p_cinfo );
112                 return -0x02;
113         }
114
115
116         /* Create INet-Marshald Socket ------*/
117         i_socket_inet   =
118                 Drd64_LibBrownie_Socket_InitSocketClient_INet(
119                                                 p_addr_dest, DRD64_SOCKET_NETWORK_PORT ); 
120         if( 0 > i_socket_inet )         {
121                 dw_errcode  = DRD64_ERROR_SET_CODE( DRD64_ERROR_TYPE_ERROR,
122                                                         DRD64_ERROR_PTN_SOCKET, 0x00, 0x03 );
123                 //Drd64_LibBrownie_Error_SetErrorInfo( dw_errcode, dw_location, errno );
124
125                 Drd64_Marshald_Cinfo_FreeConnectInfo( p_cinfo );
126
127                 return -0x03;
128         }
129
130
131         /* Init. RecvStatus for INet-Socket ---- */
132         p_recv = Drd64_Server_RecvStatus_AllocRecvStatus(
133                                         i_socket_inet, DRD64_MARSHALD_RECVSTATUS_UNITS );
134         if( NULL == p_recv )    {
135                 dw_errcode  = DRD64_ERROR_SET_CODE( DRD64_ERROR_TYPE_ERROR,
136                                                         DRD64_ERROR_PTN_NOMEMORY, 0x00, 0x04 );
137                 //Drd64_LibBrownie_Error_SetErrorInfo( dw_errcode, dw_location, errno );
138
139                 Drd64_Marshald_Cinfo_FreeConnectInfo( p_cinfo );
140                 return -0x04;
141         }
142
143         //Drd64_Marshald_Dispatch_SetFD( i_socket_inet );
144         Drd64_Server_RecvStatus_InitRecvStatus( p_recv, i_socket_inet );
145         Drd64_Server_RecvStatus_AddSocketsChain( p_recv );
146         /* Error Proc. is NONE. => Error Pattern is p_recv = NULL only */
147         Drd64_Server_RecvStatus_SetConnectionInfoPointer(
148                                                                 p_recv, (void *)p_cinfo );
149         Drd64_Marshald_Dispatch_SetFD( i_socket_inet );
150
151
152         /* Phase 4 : Send Packet to INet Marshald -----*/
153         i_wlen  = send( i_socket_inet, (void *)p_phead,
154                                                                 DRD64_PacketSize(p_phead), 0x00 );
155         DRD64_DEBUG_PRINT("send",i_wlen,"FD",i_socket_inet,"errno",errno);
156         if( i_wlen != DRD64_PacketSize(p_phead) )       {
157                 dw_errcode  = DRD64_ERROR_SET_CODE( DRD64_ERROR_TYPE_ERROR,
158                                                         DRD64_ERROR_PTN_SOCKET, 0x00, 0x05 );
159                 //Drd64_LibBrownie_Error_SetErrorInfo( dw_errcode, dw_location, errno );
160
161                 Drd64_Marshald_Dispatch_CloseSocket( p_recv );
162                 Drd64_Marshald_Cinfo_FreeConnectInfo( p_cinfo );
163
164                 return -0x05;
165         } 
166
167         /* Phase 5 : Set Cinfo Data ------------------*/
168         p_cinfo->b_cinfo_type                   = DRD64_MARSHALD_CINFOTYPE_CLIENT;
169         p_cinfo->i_cinfo_refcount               = 1;
170
171         Drd64_LibBrownie_INetAddr_CopyInAddr(
172                                                 &(p_cinfo->t_dest_addr_inet),
173                                                 p_addr_dest );
174         /* p_cinfo->i_dest_cinfo_id             = p_connect->i_connect_id; 
175                                         (Set i_dest_cinfo_id when Connect-recv.) */
176         p_cinfo->str_dest_addr_local[0] = '\0';
177         p_cinfo->i_dest_socket                  = i_socket_inet;
178
179         /* p_cinfo->t_src_addr_inet             = NONE */
180         p_cinfo->i_src_cinfo_id                 = -1;
181         p_cinfo->i_src_socket                   = -1;
182
183         /* t_prog_addr_inet                     = NONE */
184         p_cinfo->i_prog_pid                             = -1;
185
186         p_cinfo->i_debugd_pid                   = -1;
187         p_cinfo->i_debugd_socket                = -1;
188
189         i_err   = Drd64_LibBrownie_INetAddr_SetInAddr_LocalHost(
190                                                                 &(p_cinfo->t_client_addr_inet) );
191         if( 0x00 != i_err )             {
192                 dw_errcode  = DRD64_ERROR_SET_CODE( DRD64_ERROR_TYPE_ERROR,
193                                                         DRD64_ERROR_PTN_INETADDR, 0x00, 0x06 );
194                 //Drd64_LibBrownie_Error_SetErrorInfo( dw_errcode, dw_location, errno );
195
196                 Drd64_Marshald_Dispatch_CloseSocket( p_recv );
197                 Drd64_Marshald_Cinfo_FreeConnectInfo( p_cinfo );
198
199                 return -0x06;
200         }
201
202         p_cinfo->i_client_socket                = i_socket_inet;
203         p_cinfo->i_client_pid                   = getpid();
204
205         *pp_cinfo       = p_cinfo;
206
207         DRD64_LOG_DEBUG("  [DEBUG]SEND: Connect Cmd (for INet-Marshald) Ended");
208
209         return 0x00;
210 }
211
212
213
214 /* EOF of drd64_.c ----------------------------------- */