]>
git.ipfire.org Git - people/ms/rstp.git/blob - rstplib/uid_sock.c
1 /************************************************************************
2 * RSTP library - Rapid Spanning Tree (802.1t, 802.1w)
3 * Copyright (C) 2001-2003 Optical Access
6 * This file is part of RSTP library.
8 * RSTP library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by the
10 * Free Software Foundation; version 2.1
12 * RSTP library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
15 * General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with RSTP library; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 **********************************************************************/
32 static int Socket(int family
, int type
, int protocol
)
36 sock_fd
= socket(family
, type
, protocol
);
45 #define UID_SET_SOCKET_ID(SPTR, X) \
46 strncpy((SPTR)->socket_id, (X),SOCKET_NAME_LENGTH); \
47 (SPTR)->socket_id[SOCKET_NAME_LENGTH - 1] = '\0';
49 #define UID_SET_SOCKET_SERVER_ID(SPTR, X) \
50 (SPTR)->serverAddr.sun_path[0] = '\0'; \
51 strncpy((SPTR)->serverAddr.sun_path + 1, (X),SOCKET_NAME_LENGTH - 1); \
52 (SPTR)->serverAddr.sun_path[SOCKET_NAME_LENGTH - 1] = '\0';
54 #define UID_SET_SOCKET_CLIENT_ID(SPTR, X) \
55 (SPTR)->clientAddr.sun_path[0] = '\0'; \
56 strncpy((SPTR)->clientAddr.sun_path + 1, (X),SOCKET_NAME_LENGTH - 1); \
57 (SPTR)->clientAddr.sun_path[SOCKET_NAME_LENGTH - 1] = '\0';
60 UiD_SocketCompare (UID_SOCKET_T
* s
, UID_SOCKET_T
* t
)
65 ps
= s
->clientAddr
.sun_path
;
69 pt
= t
->clientAddr
.sun_path
;
80 int UiD_SocketInit(UID_SOCKET_T
* s
,
81 UID_SOCK_ID socket_id
,
82 TYPE_OF_BINDING binding
)
84 bzero(s
, sizeof (UID_SOCKET_T
));
86 s
->sock_fd
= Socket(AF_LOCAL
, SOCK_DGRAM
, 0);
88 s
->clientAddr
.sun_family
= AF_LOCAL
;
90 s
->serverAddr
.sun_family
= AF_LOCAL
;
94 case UID_BIND_AS_CLIENT
:
95 strncpy (s
->socket_id
, tmpnam(NULL
),SOCKET_NAME_LENGTH
);
96 UID_SET_SOCKET_CLIENT_ID(s
,s
->socket_id
);
97 if (bind(s
->sock_fd
, (SA
*)&(s
->clientAddr
), SIZE_OF_ADDRESS
) < 0) {
100 bzero(&s
->serverAddr
, SIZE_OF_ADDRESS
);
101 s
->serverAddr
.sun_family
= AF_LOCAL
;
102 UID_SET_SOCKET_SERVER_ID(s
, socket_id
);
106 case UID_BIND_AS_SERVER
:
108 strncpy (s
->socket_id
, socket_id
, SOCKET_NAME_LENGTH
);
109 UID_SET_SOCKET_SERVER_ID(s
,s
->socket_id
);
111 if (bind(s
->sock_fd
, (SA
*)&(s
->serverAddr
), SIZE_OF_ADDRESS
) < 0) {
123 s
->binding
= binding
;
128 int UiD_SocketRecvfrom (UID_SOCKET_T
* sock
,
129 void* msg
, int buffer_size
,
130 UID_SOCKET_T
* sock_4_reply
)
133 socklen_t len
= SIZE_OF_ADDRESS
;
136 size
= recvfrom(sock
->sock_fd
, msg
, buffer_size
, 0,
137 (struct sockaddr
*)(((UID_BIND_AS_CLIENT
== sock
->binding
) || !sock_4_reply
) ?
138 NULL
: &(sock_4_reply
->clientAddr
)),
139 (UID_BIND_AS_CLIENT
== (sock
->binding
)) ?
141 if (size
< 0 && errno
== EINTR
) continue;
145 if ((UID_BIND_AS_CLIENT
!= sock
->binding
) && sock_4_reply
) {
146 sock_4_reply
->sock_fd
= sock
->sock_fd
;
147 sock_4_reply
->binding
= UID_BIND_AS_SERVER
;
153 int UiD_SocketSendto (UID_SOCKET_T
* sock
, void* msg
, int msg_size
)
157 while (size
!= msg_size
) {
158 rc
= sendto (sock
->sock_fd
, (msg
+size
), (msg_size
-size
), 0,
159 (struct sockaddr
*)((UID_BIND_AS_CLIENT
== sock
->binding
) ? &sock
->serverAddr
: &sock
->clientAddr
),
163 if (errno
== EINTR
) {
175 int UiD_SocketClose(UID_SOCKET_T
* s
)
182 int UiD_SocketSetReadTimeout (UID_SOCKET_T
* s
, int timeout
)
188 wait
.tv_sec
= timeout
;
192 FD_SET(s
->sock_fd
, &read_mask
);
194 retval
= select (s
->sock_fd
+ 1, &read_mask
, NULL
, NULL
, &wait
);
196 if (retval
< 0) { // Error
197 fprintf (stderr
, "UiD_SocketSetReadTimeout failed: %s\n", strerror(errno
));
199 } else if (! retval
) { // Timeout expired