/* * Tries to connect site with ip 'site'. If the connection is already * established then is_open is set to '1'. New message management thread * is started. */ static int connect_site(char *site, int *is_open, pthread_t * hm_thrp) { int eid, ret; int s; msg_mng_loop_args *mma; if ((s = get_connected_socket(site, PORT, is_open, &eid)) < 0) return (CON_FAILED); if (*is_open) { return (0); } if ((mma = calloc(sizeof (msg_mng_loop_args), 1)) == NULL) { pax_log(LOG_ERR,"connect site: msg_mng_loop struct allocation: %s\n", strerror(errno)); ret = errno; goto err; } mma->fd = s; mma->eid = eid; mma->m_index = ip2index(site); if ((ret = pthread_create(hm_thrp, NULL, msg_mng_loop, (void *) mma)) != 0) { pax_log(LOG_ERR,"connect site:msg_mng_loop creation %s\n", strerror(errno)); goto err1; } return (0); err1: free(mma); err: return (ret); }
unsigned short int http_packet_send_and_check_status_code( const char *host, const char *packet, unsigned int packet_length, const char *need_status_code ) { int socket = get_connected_socket( host ); if( socket == -1 ) { return -1; } unsigned short int result = 0; if( socket_send( socket, packet, packet_length ) == 1 ) { char *receive_status_code = http_get_status_code( socket, 2000 ); if( receive_status_code != NULL ) { if( strcmp( receive_status_code, need_status_code ) == 0 ) { result = 1; } free( receive_status_code ); } } socket_close( socket ); return result; }
int main(int argc, char *argv[]) { struct addrinfo *result; int sfd; int status; const char *host; const char *port; const char *msg; if (argc < 3) { fprintf(stderr, "Usage: %s host port msg\n", argv[0]); exit(__LINE__); } host = argv[1]; port = argv[2]; msg = argv[3]; /* Get address information on specified host and port. */ status = get_server_addr_info(host, port, &result); if (status != 0) { exit(status); } /* Go through each addrinfo and try to open a socket an * connect to it. Either use the first one or exit. */ status = get_connected_socket(result, &sfd); if (status != 0) { exit(status); } freeaddrinfo(result); /* Send specified message as a separat datagram and read * the response from the server. */ status = echo_client(sfd, msg); if (status != 0) { exit(status); } return 0; }