OSDN Git Service

*** empty log message ***
[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( DRD64_MARSHALD_CINFOTYPE_CLIENT
59                                                                 == p_cinfo->b_cinfo_type )      {
60                         if( 0x00 == Drd64_LibBrownie_INetAddr_CmpInAddr(
61                                                         p_addr, &(p_cinfo->t_dest_addr_inet)) )
62                                 { return p_cinfo; }
63                 }
64         }
65
66         return NULL;
67 }
68
69
70 EXTERN_MARSHALD_CINFO
71 Drd64_Marshald_ConnectInfo      *
72         Drd64_Marshald_CInfo_GetConnectInfo(
73                 int     i_cinfo_id )
74 {
75         if(( 0 > i_cinfo_id ) || ( gi_marshald_cinfo_max < i_cinfo_id ))
76                 { return NULL; }
77         return (gp_marshald_cinfo + i_cinfo_id);
78 }
79
80
81 // Alloc
82 EXTERN_MARSHALD_CINFO
83 Drd64_Marshald_ConnectInfo      *
84         Drd64_Marshald_CInfo_AllocConnectInfo(
85                 void )
86 {
87         int                                                     i_cnt;
88         int                                                     i_cinfo_newid;
89         Drd64_Marshald_ConnectInfo      *p_cinfo_temp;
90         Drd64_Marshald_ConnectInfo      *p_cinfo_now;
91
92         p_cinfo_now             = NULL;
93         i_cinfo_newid           = -1;
94
95         if( gi_marshald_cinfo_now == gi_marshald_cinfo_max )    {
96                 for( i_cnt = 0; i_cnt < gi_marshald_cinfo_max; i_cnt++ )        {
97                         p_cinfo_temp    = gp_marshald_cinfo + i_cnt;
98
99                         if( -1 == p_cinfo_temp->i_cinfo_id )    {
100                                 i_cinfo_newid   = i_cnt;
101                                 break;
102                         }
103                 }
104
105                 if( -1 == i_cinfo_newid )       {
106                         p_cinfo_temp
107                                 = (Drd64_Marshald_ConnectInfo *)
108                                         realloc( gp_marshald_cinfo,
109                                                         sizeof( Drd64_Marshald_ConnectInfo )
110                                                                 * ( gi_marshald_cinfo_max
111                                                                         + DRD64_MARSHALD_CINFO_UNITS) );
112                         if( NULL == p_cinfo_temp )      { return NULL; }
113
114                         gp_marshald_cinfo       = p_cinfo_temp;
115                         gi_marshald_cinfo_max   += DRD64_MARSHALD_CINFO_UNITS;
116                         i_cinfo_newid   = gi_marshald_cinfo_now++;
117                 }
118         }
119         else    {
120                 i_cinfo_newid   = gi_marshald_cinfo_now++;
121         }
122
123         if( -1 != i_cinfo_newid )       {
124                 p_cinfo_now     = gp_marshald_cinfo + i_cinfo_newid;
125
126                 memset( p_cinfo_now, 0x00, sizeof( Drd64_Marshald_ConnectInfo ) );
127                 p_cinfo_now->i_cinfo_id = i_cinfo_newid;
128         }
129
130         return  p_cinfo_now;
131 }
132
133
134 // Free
135 EXTERN_MARSHALD_CINFO
136 int
137         Drd64_Marshald_Cinfo_FreeConnectInfo(
138                 Drd64_Marshald_ConnectInfo *p_cinfo )
139 {
140         if( NULL == p_cinfo )   { return 0x01; }
141
142         if( p_cinfo->i_cinfo_id + 1 == gi_marshald_cinfo_now )
143                 { gi_marshald_cinfo_now--; }
144
145         p_cinfo->i_cinfo_id     = -1;
146
147         return 0x00;
148 }
149
150
151 EXTERN_MARSHALD_CINFO
152 int
153         Drd64_Marshald_CInfo_InitConnectInfo(
154                 void )
155 {
156         int             i_cnt;
157
158         gp_marshald_cinfo
159                 = (Drd64_Marshald_ConnectInfo *)malloc(
160                                 sizeof( Drd64_Marshald_ConnectInfo )
161                                         * DRD64_MARSHALD_CINFO_UNITS );
162
163         if( NULL == gp_marshald_cinfo )         {
164                 return 0x01;
165         }
166                                                 
167         memset( gp_marshald_cinfo, 0x00,
168                                 sizeof( Drd64_Marshald_ConnectInfo )
169                                         * DRD64_MARSHALD_CINFO_UNITS );
170
171         gi_marshald_cinfo_max   = DRD64_MARSHALD_CINFO_UNITS;
172         gi_marshald_cinfo_now   = 0;
173
174         return 0x00;
175 }
176
177
178 EXTERN_MARSHALD_CINFO
179 int
180         Drd64_Marshald_CInfo_TermConnectInfo(
181                 void )
182 {
183         if( NULL != gp_marshald_cinfo )         {
184                 free( gp_marshald_cinfo );
185                 gp_marshald_cinfo       = NULL;
186         }
187
188         gi_marshald_cinfo_max   = DRD64_MARSHALD_CINFO_UNITS;
189         gi_marshald_cinfo_now   = 0;
190
191         return 0x00;
192 }
193
194
195 /* EOF of drd64_.c ----------------------------------- */