/*-------------------------------------------------------------------------*\ * Returns all information provided by the resolver given a host name * or ip address \*-------------------------------------------------------------------------*/ static int inet_gethost(const char *address, struct hostent **hp) { struct in_addr addr; if (inet_aton(address, &addr)) return sock_gethostbyaddr((char *) &addr, sizeof(addr), hp); else return sock_gethostbyname(address, hp); }
int sock_t_connect_and_send_http( vj_sock_t *s, char *host, int port, char *buf, int buf_len ) { s->he = sock_gethostbyname( host ); if(s->he==NULL) return 0; s->sock_fd = socket( AF_INET, SOCK_STREAM , 0); if(s->sock_fd < 0) { return 0; } s->port_num = port; s->addr.sin_family = AF_INET; s->addr.sin_port = htons( port ); s->addr.sin_addr = *( (struct in_addr*) s->he->h_addr ); if( connect( s->sock_fd, (struct sockaddr*) &s->addr, sizeof( struct sockaddr )) == -1 ) { return 0; } struct sockaddr_in sinfo; socklen_t sinfolen=0; char server_name[1024]; if( getsockname(s->sock_fd,(struct sockaddr*) &sinfo,&sinfolen)==0) { char *tmp = inet_ntoa( sinfo.sin_addr ); strncpy( server_name, tmp, 1024); } else { return 0; } int len = strlen(server_name) + 128 + buf_len; char *msg = (char*) malloc(sizeof(char) * len ); struct utsname name; if( uname(&name) == -1 ) { snprintf(msg,len,"%s%s/veejay-%s\n\n",buf,server_name,PACKAGE_VERSION); } else { snprintf(msg,len,"%s%s/veejay-%s/%s-%s\n\n",buf,server_name,PACKAGE_VERSION,name.sysname,name.release ); } int msg_len = strlen(msg); int n = send(s->sock_fd,msg,msg_len, 0 ); free(msg); if( n == -1 ) { return 0; } return n; }
int sock_t_connect( vj_sock_t *s, char *host, int port ) { s->he = sock_gethostbyname( host ); if(s->he==NULL) return 0; s->sock_fd = socket( AF_INET, SOCK_STREAM , 0); if(s->sock_fd < 0) { veejay_msg(VEEJAY_MSG_ERROR, "Socket error with Veejay host %s:%d %s ", host,port,strerror(errno)); return 0; } s->port_num = port; s->addr.sin_family = AF_INET; s->addr.sin_port = htons( port ); s->addr.sin_addr = *( (struct in_addr*) s->he->h_addr ); if( connect( s->sock_fd, (struct sockaddr*) &s->addr, sizeof( struct sockaddr )) == -1 ) { veejay_msg(VEEJAY_MSG_ERROR, "Connection error with Veejay host %s:%d %s", host, port, strerror(errno)); return 0; } unsigned int tmp = sizeof(int); if( getsockopt( s->sock_fd , SOL_SOCKET, SO_SNDBUF, (unsigned char*) &(s->send_size), &tmp) < 0 ) { veejay_msg(VEEJAY_MSG_ERROR, "Unable to get buffer size for output: %s", strerror(errno)); return 0; } if( getsockopt( s->sock_fd, SOL_SOCKET, SO_RCVBUF, (unsigned char*) &(s->recv_size), &tmp) < 0 ) { veejay_msg(VEEJAY_MSG_ERROR, "Unable to get buffer size for input: %s", strerror(errno)); return 0; } veejay_msg(VEEJAY_MSG_DEBUG, "Connected to host '%s' port %d, fd %d", host,port,s->sock_fd ); veejay_msg(VEEJAY_MSG_DEBUG, "Receive buffer size is %d bytes, send buffer size is %d bytes", s->recv_size, s->send_size ); return 1; }
/*-------------------------------------------------------------------------*\ * Tries to bind socket to (address, port) \*-------------------------------------------------------------------------*/ const char *inet_trybind(p_sock ps, const char *address, unsigned short port) { struct sockaddr_in local; int err; memset(&local, 0, sizeof(local)); /* address is either wildcard or a valid ip address */ local.sin_addr.s_addr = htonl(INADDR_ANY); local.sin_port = htons(port); local.sin_family = AF_INET; if (strcmp(address, "*") && !inet_aton(address, &local.sin_addr)) { struct hostent *hp = NULL; struct in_addr **addr; err = sock_gethostbyname(address, &hp); if (err != IO_DONE) return sock_hoststrerror(err); addr = (struct in_addr **) hp->h_addr_list; memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); } err = sock_bind(ps, (SA *) &local, sizeof(local)); if (err != IO_DONE) sock_destroy(ps); return sock_strerror(err); }
/*-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/ const char *inet_tryconnect(p_sock ps, const char *address, unsigned short port, p_tm tm) { struct sockaddr_in remote; int err; memset(&remote, 0, sizeof(remote)); remote.sin_family = AF_INET; remote.sin_port = htons(port); if (strcmp(address, "*")) { if (!inet_aton(address, &remote.sin_addr)) { struct hostent *hp = NULL; struct in_addr **addr; err = sock_gethostbyname(address, &hp); if (err != IO_DONE) return sock_hoststrerror(err); addr = (struct in_addr **) hp->h_addr_list; memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); } } else remote.sin_family = AF_UNSPEC; err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm); if (err != IO_DONE) sock_destroy(ps); return sock_strerror(err); }