2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "connection.h"
43 TC_AcceptUdp(char *host, int port) {
44 struct sockaddr_in serv_addr;
47 if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
48 tlog(TL_CRIT|TL_EXIT, "udp socket %s", strerror(errno));
49 if ((flags=fcntl(sockfd,F_GETFL,0)) == -1)
50 tlog(TL_ALARM,"fcntl F_GETFL - %s",strerror(errno));
52 if (fcntl(sockfd,F_SETFL,flags|O_NDELAY) < 0 )
53 tlog(TL_ALARM,"fcntl O_NDELAY - %s",strerror(errno));
55 memset(&serv_addr, 0, sizeof(serv_addr));
56 serv_addr.sin_family = AF_INET;
57 serv_addr.sin_addr.s_addr = inet_addr(host);
58 serv_addr.sin_port = htons(port);
60 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
61 tlog(TL_CRIT|TL_EXIT, "cannot bind to %s address: %s",
62 inet_ntoa(serv_addr.sin_addr), strerror(errno));
67 #define MSGMAXSIZE 8192
68 char pc_msg_buf[MSGMAXSIZE];
71 TC_getMsg( int sockfd, Msg *msg ) {
72 struct sockaddr_in cli_addr;
74 socklen_t clilen = sizeof(cli_addr);
75 TCMsg *pmsg = (TCMsg*)pc_msg_buf;
77 n = recvfrom(sockfd, pc_msg_buf, MSGMAXSIZE, 0, (struct sockaddr *)&cli_addr, &clilen);
80 if ( errno == EAGAIN || errno == EWOULDBLOCK)
82 tlog(TL_ALARM, "recvfrom error: %s", strerror(errno));
87 tlog(TL_ALARM, "Got message %d bytes (should be al least %d)", n, TCMSGHDRSZ );
91 if ( pmsg->len > MSGMAXSIZE ) {
92 tlog(TL_ALARM, "Messages (%d bytes) is too big", pmsg->len);
96 if ( pmsg->len != n ) {
97 tlog(TL_ALARM, "Wrong size of messages (got %d bytes, should be %d bytes)", n, pmsg->len);
101 memcpy( &(msg->host_addr), &cli_addr, clilen );
109 TC_sendMsg( Msg *msg ) {
110 if ( msg->msg == NULL || msg->msg->len <=0 )
113 if ( msg->msg->len > MSGMAXSIZE )
116 if ( msg->sockfd <=0 ) {
117 struct sockaddr_in cli_addr;
118 if ( (msg->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
119 tlog(TL_CRIT,"udp socket %s %d: %s", msg->host, msg->port, strerror(errno));
123 memset(&cli_addr, 0, sizeof(cli_addr));
124 cli_addr.sin_family = AF_INET;
125 cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
126 cli_addr.sin_port = htons(0);
128 if (bind(msg->sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
129 tlog(TL_CRIT, "cannot bind to address: %s", strerror(errno));
134 memset(&(msg->host_addr), 0, sizeof(msg->host_addr));
135 msg->host_addr.sin_family = AF_INET;
136 msg->host_addr.sin_addr.s_addr = inet_addr(msg->host);
137 msg->host_addr.sin_port = htons(msg->port);
140 if (sendto(msg->sockfd, (void*)msg->msg, msg->msg->len, 0, (struct sockaddr *) &(msg->host_addr), sizeof(msg->host_addr)) != msg->msg->len) {
141 tlog(TL_CRIT,"Can't send message to %s:%d : %s", msg->host, msg->port, strerror(errno));
151 TC_closefd( Msg *msg ) {
152 if ( msg->sockfd > 0 )