fixes in mcrealloc
[tedtools.git] / udp.c
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36
37 #include "connection.h"
38 #include "tlog.h"
39 #include "tmalloc.h"
40
41 int 
42 TC_AcceptUdp(char *host, int port) {
43         struct sockaddr_in serv_addr;
44         int sockfd, flags;
45
46         if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
47                 tlog(TL_CRIT|TL_EXIT, "udp socket %s", strerror(errno));
48         if ((flags=fcntl(sockfd,F_GETFL,0)) == -1)
49                 tlog(TL_ALARM,"fcntl F_GETFL - %s",strerror(errno));
50
51         if (fcntl(sockfd,F_SETFL,flags|O_NDELAY) < 0 )
52                 tlog(TL_ALARM,"fcntl O_NDELAY - %s",strerror(errno));
53
54         memset(&serv_addr, 0, sizeof(serv_addr));
55         serv_addr.sin_family = AF_INET;
56         serv_addr.sin_addr.s_addr = inet_addr(host);
57         serv_addr.sin_port = htons(port);
58
59         if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
60                 tlog(TL_CRIT|TL_EXIT, "cannot bind to %s address: %s",
61                         inet_ntoa(serv_addr.sin_addr), strerror(errno));
62
63         return sockfd;
64 }
65
66 #define MSGMAXSIZE      8192
67 char pc_msg_buf[MSGMAXSIZE];
68
69 u_int32_t 
70 TC_getMsg( int sockfd, Msg *msg ) {
71         struct sockaddr_in cli_addr;
72         int n;
73         socklen_t clilen = sizeof(cli_addr);
74         TCMsg *pmsg = (TCMsg*)pc_msg_buf;
75         
76         n = recvfrom(sockfd, pc_msg_buf, MSGMAXSIZE, 0, (struct sockaddr *)&cli_addr, &clilen);
77
78         if ( n<0 ) {
79                 if ( errno == EAGAIN || errno == EWOULDBLOCK)
80                         return CS_AGAIN;
81                 tlog(TL_ALARM, "recvfrom error: %s", strerror(errno));
82                 return CS_ERROR;
83         }
84
85         if ( n<TCMSGHDRSZ  ) {
86                 tlog(TL_ALARM, "Got message %d bytes (should be al least %d)", n, TCMSGHDRSZ );
87                 return CS_AGAIN;
88         }
89
90         if ( pmsg->len > MSGMAXSIZE  ) {
91                 tlog(TL_ALARM, "Messages (%d bytes) is too big", pmsg->len);
92                 return CS_AGAIN;
93         }
94
95         if ( pmsg->len != n ) {
96                 tlog(TL_ALARM, "Wrong size of messages (got %d bytes, should be %d bytes)", n, pmsg->len);
97                 return CS_AGAIN;
98         }
99
100         memcpy( &(msg->host_addr), &cli_addr, clilen );
101         msg->msg = pmsg;
102         
103         return CS_OK; 
104 }
105
106 /* send */
107 u_int32_t 
108 TC_sendMsg( Msg *msg ) {
109         if ( msg->msg == NULL || msg->msg->len <=0  )
110                 return CS_OK;
111
112         if ( msg->msg->len > MSGMAXSIZE )
113                 return CS_ERROR; 
114
115         if ( msg->sockfd <=0 ) {
116                 struct sockaddr_in cli_addr;
117                 if ( (msg->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
118                         tlog(TL_CRIT,"udp socket %s %d: %s", msg->host, msg->port, strerror(errno));
119                         return CS_ERROR;
120                 }
121         
122                 memset(&cli_addr, 0, sizeof(cli_addr));
123                 cli_addr.sin_family = AF_INET;
124                 cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
125                 cli_addr.sin_port = htons(0);
126         
127                 if (bind(msg->sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
128                         tlog(TL_CRIT, "cannot bind to address: %s", strerror(errno));   
129                         close(msg->sockfd);
130                         msg->sockfd=-1;
131                         return CS_ERROR;        
132                 }
133                 memset(&(msg->host_addr), 0, sizeof(msg->host_addr));
134                 msg->host_addr.sin_family = AF_INET;
135                 msg->host_addr.sin_addr.s_addr = inet_addr(msg->host);
136                 msg->host_addr.sin_port = htons(msg->port);
137         }
138
139         if (sendto(msg->sockfd, (void*)msg->msg, msg->msg->len, 0, (struct sockaddr *) &(msg->host_addr), sizeof(msg->host_addr)) != msg->msg->len) {
140                 tlog(TL_CRIT,"Can't send message to %s:%d : %s", msg->host, msg->port, strerror(errno));
141                 close(msg->sockfd);
142                 msg->sockfd=-1;
143                 return CS_ERROR;        
144         }
145
146         return CS_OK;
147 }
148
149 void      
150 TC_closefd( Msg *msg ) {
151         if ( msg->sockfd > 0 )
152                 close(msg->sockfd);
153         msg->sockfd=-1;
154 }
155
156