OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / libbrownie / drd64_libbrownie_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_libbrownie.h"
38
39
40 /* Drd64_Server_Socket_InitSocket_Local( */
41 int
42         Drd64_LibBrownie_Socket_InitSocketServer_Local(
43                 const char *pstr_addr )
44 {
45         int                                             i_socket_local;
46         int                                             i_err;
47         struct  sockaddr_un             t_sun;
48
49         i_socket_local  = socket( PF_LOCAL, SOCK_STREAM, 0 );
50         if( -1 == i_socket_local )
51                 { return -1; }
52
53         unlink( pstr_addr );
54
55         memset( &t_sun, 0x00, sizeof( struct sockaddr_un ) );
56
57         t_sun.sun_family        = AF_UNIX;
58         strcpy( t_sun.sun_path, pstr_addr );
59
60         i_err = bind( i_socket_local,
61                                         (struct sockaddr *)&t_sun,
62                                         SUN_LEN( &t_sun ) );
63         if( i_err == -1 )       { return -3; }
64
65         i_err = listen( i_socket_local, SOMAXCONN );
66         if( i_err == -1 )       { return -4; }
67         
68         return i_socket_local;
69 }
70
71
72 /* Drd64_Server_Socket_InitSocket_INet */
73 int
74         Drd64_LibBrownie_Socket_InitSocketServer_INet(
75                 const char *pstr_addr, 
76                 u_short us_port )
77 {
78         int                                             i_socket_inet;
79         int                                             i_err;
80         int                                             i_flag;
81         struct  sockaddr_in             t_sin;
82
83         i_socket_inet   = socket( PF_INET, SOCK_STREAM, 0 );
84         if( -1 == i_socket_inet )
85                 { return -1; }
86
87         memset( &t_sin, 0x00, sizeof( struct sockaddr_in ) );
88
89         t_sin.sin_family        = AF_INET;
90         t_sin.sin_port          = htons( us_port );
91         if( !strcmp( pstr_addr, "INADDR_ANY" ) )        {
92                 t_sin.sin_addr.s_addr   = htonl( INADDR_ANY );
93         }
94         else    {
95                 t_sin.sin_addr.s_addr   = inet_addr( pstr_addr );
96                 if( INADDR_NONE == t_sin.sin_addr.s_addr )      { return -2; }
97         }
98
99         i_flag  = 1;
100         i_err   = setsockopt(i_socket_inet, IPPROTO_TCP, TCP_NODELAY,
101                                                                                 (char *)&i_flag, sizeof(int));
102         if( 0 > i_err )         { return -3;} 
103
104         i_err = bind( i_socket_inet,
105                                         (struct sockaddr *)&t_sin,
106                                         sizeof( struct sockaddr_in ) );
107         if( i_err == -1 )       { return -4; }
108
109         i_err = listen( i_socket_inet, SOMAXCONN );
110         if( i_err == -1 )       { return -5; }
111         
112         return i_socket_inet;
113 }
114
115
116 /* Drd64_Server_Socket_InitSocket_Local_Client */
117 int
118         Drd64_LibBrownie_Socket_InitSocketClient_Local(
119                 const char *pstr_socket )
120 {
121         int                                             i_err;
122         int                                             i_socket_local;
123         struct  sockaddr_un             t_sun;
124
125         i_socket_local  = socket( PF_LOCAL, SOCK_STREAM, 0x00 );
126         if( -1 == i_socket_local )      { return -1; }
127
128         t_sun.sun_family        = AF_UNIX;
129         strncpy( t_sun.sun_path, pstr_socket, DRD64_SUNPATH_LEN - 1 );
130
131         i_err   = connect( i_socket_local,
132                                                 (struct sockaddr *)&t_sun, SUN_LEN( &t_sun ) );
133         if( -1 == i_err )       { return -2; }
134
135         return i_socket_local;
136 }
137
138
139 /* Drd64_Server_Socket_InitSocketClient_INet */
140 int
141         Drd64_LibBrownie_Socket_InitSocketClient_INet(
142                 struct  in_addr         *p_addr, 
143                 u_short us_port )
144 {
145         int             i_err;
146         int                                             i_socket_inet;
147         struct  sockaddr_in             t_sin;
148         int             i_flag;
149
150
151         i_socket_inet   = socket( PF_INET, SOCK_STREAM, 0x00 );
152         if( -1 == i_socket_inet )       { return -1; }
153
154         memset( &t_sin, 0x00, sizeof( struct sockaddr_in ) );
155
156         t_sin.sin_family        = AF_INET;
157         Drd64_LibBrownie_INetAddr_CopyInAddr( &t_sin.sin_addr, p_addr );
158         t_sin.sin_port          = htons( us_port );
159         
160         i_flag  = 1;
161         i_err   = setsockopt(i_socket_inet, IPPROTO_TCP, TCP_NODELAY,
162                                                                                 (char *)&i_flag, sizeof(int));
163         if( 0 > i_err )         { return -2;} 
164
165         i_err   = connect( i_socket_inet,
166                                         (struct sockaddr *)&t_sin,
167                                         sizeof( struct sockaddr_in ) );
168         if( -1 == i_err )       { return -3; }
169
170         return i_socket_inet;
171 }
172
173
174 /* EOF of drd64_.c ----------------------------------- */