OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / deamon / drd64_marshald_cinfo.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_SRC_MARSHALD_CINFO
38 #include"drd64_marshald.h"
39
40 Drd64_Marshald_ConnectInfo      *gp_marshald_cinfo;
41 int                                                     gi_marshald_cinfo_max;
42 int                                                     gi_marshald_cinfo_now;
43
44
45 EXTERN_MARSHALD_CINFO
46 Drd64_Marshald_ConnectInfo      *
47         Drd64_Marshald_SearchINetConnection(
48                 struct  in_addr *p_addr )
49 {
50         int             i_cnt;
51         Drd64_Marshald_ConnectInfo      *p_cinfo;
52
53         p_cinfo = NULL;
54         
55         for( i_cnt = 0; i_cnt < gi_marshald_cinfo_now; i_cnt++ )        {
56                 p_cinfo = (gp_marshald_cinfo + i_cnt);
57
58                 if( -1 == p_cinfo->i_cinfo_id )         { continue; }
59
60                 /* host = client */
61                 if( DRD64_MARSHALD_CINFOTYPE_CLIENT
62                                                                 == p_cinfo->b_cinfo_type )      {
63                         if( 0x00 == Drd64_LibBrownie_INetAddr_CmpInAddr(
64                                                         p_addr, &(p_cinfo->t_dest_addr_inet)) )
65                                 { return p_cinfo; }
66                 }
67                 /* host = server */
68                 else if( DRD64_MARSHALD_CINFOTYPE_SERVER
69                                                                 == p_cinfo->b_cinfo_type )      {
70                         if( 0x00 == Drd64_LibBrownie_INetAddr_CmpInAddr(
71                                                         p_addr, &(p_cinfo->t_src_addr_inet)) )
72                                 { return p_cinfo; }
73                 }
74         }
75
76         return NULL;
77 }
78
79
80 EXTERN_MARSHALD_CINFO
81 Drd64_Marshald_ConnectInfo      *
82         Drd64_Marshald_Search_DebugdConnection(
83                 int     i_prog_pid )
84 {
85         int             i_cnt;
86         Drd64_Marshald_ConnectInfo      *p_cinfo;
87
88         p_cinfo = NULL;
89         
90         for( i_cnt = 0; i_cnt < gi_marshald_cinfo_now; i_cnt++ )        {
91                 p_cinfo = (gp_marshald_cinfo + i_cnt);
92
93                 if( -1 == p_cinfo->i_cinfo_id )         { continue; }
94
95                 if( DRD64_MARSHALD_CINFOTYPE_DEBUGD
96                                                                 == p_cinfo->b_cinfo_type )      {
97                         if( i_prog_pid == p_cinfo->i_prog_pid )
98                                 { return p_cinfo; }
99                 }
100         }
101
102         return NULL;
103 }
104
105
106 EXTERN_MARSHALD_CINFO
107 Drd64_Marshald_ConnectInfo      *
108         Drd64_Marshald_CInfo_GetConnectInfo(
109                 int     i_cinfo_id )
110 {
111         if(( 0 > i_cinfo_id ) || ( gi_marshald_cinfo_max < i_cinfo_id ))
112                 { return NULL; }
113         return (gp_marshald_cinfo + i_cinfo_id);
114 }
115
116
117 // Alloc
118 EXTERN_MARSHALD_CINFO
119 Drd64_Marshald_ConnectInfo      *
120         Drd64_Marshald_CInfo_AllocConnectInfo(
121                 void )
122 {
123         int                                                     i_cnt;
124         int                                                     i_cinfo_newid;
125         Drd64_Marshald_ConnectInfo      *p_cinfo_temp;
126         Drd64_Marshald_ConnectInfo      *p_cinfo_now;
127
128         p_cinfo_now             = NULL;
129         i_cinfo_newid           = -1;
130
131         if( gi_marshald_cinfo_now == gi_marshald_cinfo_max )    {
132                 for( i_cnt = 0; i_cnt < gi_marshald_cinfo_max; i_cnt++ )        {
133                         p_cinfo_temp    = gp_marshald_cinfo + i_cnt;
134
135                         if( -1 == p_cinfo_temp->i_cinfo_id )    {
136                                 i_cinfo_newid   = i_cnt;
137                                 break;
138                         }
139                 }
140
141                 if( -1 == i_cinfo_newid )       {
142                         p_cinfo_temp
143                                 = (Drd64_Marshald_ConnectInfo *)
144                                         realloc( gp_marshald_cinfo,
145                                                         sizeof( Drd64_Marshald_ConnectInfo )
146                                                                 * ( gi_marshald_cinfo_max
147                                                                         + DRD64_MARSHALD_CINFO_UNITS) );
148                         if( NULL == p_cinfo_temp )      { return NULL; }
149
150                         gp_marshald_cinfo       = p_cinfo_temp;
151                         gi_marshald_cinfo_max   += DRD64_MARSHALD_CINFO_UNITS;
152                         i_cinfo_newid   = gi_marshald_cinfo_now++;
153                 }
154         }
155         else    {
156                 i_cinfo_newid   = gi_marshald_cinfo_now++;
157         }
158
159         if( -1 != i_cinfo_newid )       {
160                 p_cinfo_now     = gp_marshald_cinfo + i_cinfo_newid;
161
162                 memset( p_cinfo_now, 0x00, sizeof( Drd64_Marshald_ConnectInfo ) );
163                 p_cinfo_now->i_cinfo_id = i_cinfo_newid;
164                 p_cinfo_now->i_dest_cinfo_id    = DRD64_CONNECTID_NONE;
165                 p_cinfo_now->i_dest_socket              = -1;
166                 p_cinfo_now->p_dest_cinfo_connection = NULL;
167                 p_cinfo_now->i_src_cinfo_id             = DRD64_CONNECTID_NONE;
168                 p_cinfo_now->i_src_socket               = -1;
169                 p_cinfo_now->p_src_cinfo_connection = NULL;
170                 p_cinfo_now->i_debugd_socket    = -1;
171                 p_cinfo_now->i_client_socket    = -1;
172         }
173
174         return  p_cinfo_now;
175 }
176
177
178 // Free
179 EXTERN_MARSHALD_CINFO
180 int
181         Drd64_Marshald_Cinfo_FreeConnectInfo(
182                 Drd64_Marshald_ConnectInfo *p_cinfo )
183 {
184         int             i_cnt;
185         Drd64_Marshald_ConnectInfo      *p_cinfo_temp;
186
187         if( NULL == p_cinfo )   { return 0x01; }
188
189         //if( p_cinfo->i_cinfo_id + 1 == gi_marshald_cinfo_now )
190                 //{ gi_marshald_cinfo_now--; }
191         for( i_cnt = gi_marshald_cinfo_now - 1; i_cnt >= 0 ; i_cnt-- )  {
192                 p_cinfo_temp    = (gp_marshald_cinfo + i_cnt);
193                 assert( NULL != p_cinfo_temp );
194
195                 if( -1 < p_cinfo_temp->i_cinfo_id )     { break; }
196         }
197
198         gi_marshald_cinfo_now   = i_cnt + 1;
199         p_cinfo->i_cinfo_id     = -1;
200
201         return 0x00;
202 }
203
204
205 EXTERN_MARSHALD_CINFO
206 int
207         Drd64_Marshald_CInfo_InitConnectInfo(
208                 void )
209 {
210
211         gp_marshald_cinfo
212                 = (Drd64_Marshald_ConnectInfo *)malloc(
213                                 sizeof( Drd64_Marshald_ConnectInfo )
214                                         * DRD64_MARSHALD_CINFO_UNITS );
215
216         if( NULL == gp_marshald_cinfo )         {
217                 return 0x01;
218         }
219                                                 
220         memset( gp_marshald_cinfo, 0x00,
221                                 sizeof( Drd64_Marshald_ConnectInfo )
222                                         * DRD64_MARSHALD_CINFO_UNITS );
223
224         gi_marshald_cinfo_max   = DRD64_MARSHALD_CINFO_UNITS;
225         gi_marshald_cinfo_now   = 0;
226
227         return 0x00;
228 }
229
230
231 EXTERN_MARSHALD_CINFO
232 int
233         Drd64_Marshald_CInfo_TermConnectInfo(
234                 void )
235 {
236         if( NULL != gp_marshald_cinfo )         {
237                 free( gp_marshald_cinfo );
238                 gp_marshald_cinfo       = NULL;
239         }
240
241         gi_marshald_cinfo_max   = DRD64_MARSHALD_CINFO_UNITS;
242         gi_marshald_cinfo_now   = 0;
243
244         return 0x00;
245 }
246
247
248 /* EOF of drd64_.c ----------------------------------- */