ntohs(cs->serv_addr.sin_port),strerror(errno));
if (setsockopt(cs->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
- tlog(TL_CRIT|TL_EXIT, "socketsockopt failed: %s (d%)", strerror(errno), errno);
+ tlog(TL_CRIT|TL_EXIT, "socketsockopt failed: %s (%d)", strerror(errno), errno);
return CS_ERROR;
}
sc = (TC_Connection *)tmalloc(sizeof(TC_Connection));
memset(sc, 0, sizeof(TC_Connection));
sc->serv_addr.sin_family = AF_INET;
- sc->serv_addr.sin_addr.s_addr = (name) ? inet_addr(name) : htonl(INADDR_ANY);
+ sc->serv_addr.sin_addr.s_addr = (name && *name != '*' ) ? inet_addr(name) : htonl(INADDR_ANY);
+ if ( sc->serv_addr.sin_addr.s_addr == INADDR_NONE ) {
+ struct hostent *host;
+
+ /*
+ * Can't parse address: it's a DNS Name
+ */
+ host = gethostbyname(name);
+ if ( host && host->h_addrtype == AF_INET ) {
+ memcpy(&sc->serv_addr.sin_addr.s_addr, host->h_addr_list[0],
+ sizeof(&sc->serv_addr.sin_addr.s_addr));
+ } else {
+#ifdef HAVE_HSTRERROR
+ tlog(TL_CRIT,"gethostbyname: %s - %s", name, hstrerror(h_errno));
+#else
+ tlog(TL_CRIT,"gethostbyname: %s - %s", name, strerror(errno));
+#endif
+ sc->state = CS_ERROR;
+ return sc;
+ }
+ }
+
sc->serv_addr.sin_port = htons(port);
sc->state = CS_NOTINITED;
return sc;
}
u_int32_t
-TC_Read( TC_Connection *cs ) {
+TC_Read( TC_Connection *cs, size_t maxsize ) {
int sz, totalread = -1, toread=0, alreadyread;
if ( cs->state == CS_ERROR )
resizeCS(cs, sizeof(u_int32_t));
} else {
totalread = *(u_int32_t*)(cs->buf);
+ if ( maxsize > 0 && totalread > maxsize )
+ {
+ tlog(TL_ALARM,"TC_Read: message size (%d b) is greater than max allowed (%d b)", totalread, maxsize);
+ cs->state = CS_ERROR;
+ return CS_ERROR;
+ }
toread = totalread - alreadyread;
if ( toread == 0 ) {
cs->state = CS_FINISHREAD;
}
u_int32_t
-TC_Talk( TC_Connection *cs ) {
+TC_Talk( TC_Connection *cs, size_t maxsize ) {
if ( cs->state==CS_NOTINITED )
TC_ServerInitConnect( cs );
cs->ptr = cs->buf;
while( cs->state != CS_FINISHREAD ) {
while( !TC_ReadyIO( &cs, 1, 100) );
- if ( TC_Read(cs) == CS_ERROR ) return CS_ERROR;
+ if ( TC_Read(cs, maxsize) == CS_ERROR ) return CS_ERROR;
}
return CS_OK;