/* **************************************************************** * Inicia um cliente * **************************************************************** */ int tcp_connect (int port, const char *name) { int fd; T_BIND bind; T_CALL call; INADDR bind_addr, addr; IPADDR remote_ip_addr; if ((fd = t_open (tcpdevname, O_RDWR, (T_INFO *)NULL)) < 0) return (-1); /* * Associa um endereço local. */ FILL_INADDR (bind_addr, 0, 0); FILL_NETBUF (bind.addr, &bind_addr, sizeof (bind_addr)); bind.qlen = 0; /* Não vamos dar "t_listen" nesta conexão */ if (t_bind (fd, &bind, (T_BIND *)NULL) < 0) goto bad; if (set_param (fd) < 0) goto bad; /* * Converte o nome da estação remota em um endereço IP. */ if (name != NOSTR && name[0] != '\0') { remote_ip_addr = t_node_to_addr (fd, name, NULL); if (remote_ip_addr == -1) goto bad; } else { remote_ip_addr = LOCAL_IP_ADDR; } /* * Preenche a estrutura T_CALL: só o membro addr é relevante. */ FILL_INADDR (addr, remote_ip_addr, port); FILL_NETBUF (call.addr, &addr, sizeof (addr)); FILL_NETBUF (call.opt, NULL, 0); FILL_NETBUF (call.udata, NULL, 0); /* * Tenta estabeler a conexão com a estação remota. */ if (t_connect (fd, &call, (T_CALL *)NULL) < 0) goto bad; return (fd); bad: t_close (fd); return (-1); } /* end tcp_connect */
static int TRANS(TLIConnect)(XtransConnInfo ciptr, struct t_call *sndcall ) { prmsg(2, "TLIConnect(%x->%d,%x)\n", ciptr, ciptr->fd, sndcall); if( t_connect(ciptr->fd,sndcall,NULL) < 0 ) { extern char *t_errlist[]; extern int t_errno; prmsg(1, "TLIConnect() t_connect() failed\n"); prmsg(1, "TLIConnect: %s\n", t_errlist[t_errno]); t_free((char *)sndcall,T_CALL); if (t_errno == TLOOK && t_look(ciptr->fd) == T_DISCONNECT) { t_rcvdis(ciptr->fd,NULL); return TRANS_TRY_CONNECT_AGAIN; } else return TRANS_CONNECT_FAILED; } t_free((char *)sndcall,T_CALL); /* * Sync up the address fields of ciptr. */ if( TRANS(TLIGetAddr)(ciptr) < 0 ) { prmsg(1, "TLIConnect: ...TLIGetAddr() failed: %d\n", errno); return TRANS_CONNECT_FAILED; } if( TRANS(TLIGetPeerAddr)(ciptr) < 0 ) { prmsg(1, "TLIConnect: ...TLIGetPeerAddr() failed: %d\n", errno); return TRANS_CONNECT_FAILED; } if( ioctl(ciptr->fd, I_POP,"timod") < 0 ) { prmsg(1, "TLIConnect() ioctl(I_POP,\"timod\") failed %d\n", errno); return TRANS_CONNECT_FAILED; } if( ioctl(ciptr->fd, I_PUSH,"tirdwr") < 0 ) { prmsg(1, "TLIConnect() ioctl(I_PUSH,\"tirdwr\") failed %d\n", errno); return TRANS_CONNECT_FAILED; } return 0; }
value xti_cots_connect (value device, value addr) { CAMLparam2(device,addr); int fd; char *dev; struct t_call sndcall; char abuf[ABUFLEN]; int k; dev = String_val(device); if ((fd = t_open(dev, O_RDWR, (struct t_info *) NULL)) == -1) { xti_error(-1, "t_open"); } if (t_bind(fd, (struct t_bind *) NULL, (struct t_bind *) NULL) == -1) { xti_error(fd, "t_bind"); } sndcall.opt.buf = NULL; sndcall.opt.len = 0; sndcall.opt.maxlen = 0; sndcall.udata.buf = NULL; sndcall.udata.len = 0; sndcall.udata.maxlen = 0; sndcall.addr.buf = abuf; sndcall.addr.len = 0; sndcall.addr.maxlen = ABUFLEN; sndcall.sequence = 0; if (string_length(addr) > sndcall.addr.maxlen) { t_close(fd); invalid_argument("cots_connect: address too long"); }; sndcall.addr.len = string_length(addr); for (k=0; k<string_length(addr); k++) { sndcall.addr.buf[k] = Byte(addr,k); }; if (t_connect( fd, &sndcall, (struct t_call *) NULL) == -1 ) { xti_error(fd, "t_connect"); } if (ioctl(fd, I_PUSH, "tirdwr") == -1) { int e = errno; t_close(fd); unix_error(e, "ioctl(I_PUSH)", Nothing); } CAMLreturn(Val_int(fd)); }
void submit_score(char *score) { char message[512]; char msgbox_msg[512]; int s_ret; sprintf(message, "GET /tantrix/?%s HTTP/1.0\r\n\r\n", score); t_connect(HOST, "80"); s_ret = send(sockfd, message, strlen(message), 0); sprintf(msgbox_msg, "Score sent to server: %s, (%d)", message, s_ret); //MessageBox(NULL, msgbox_msg, "Info", MB_OK); closesocket(sockfd); WSACleanup(); }
/* include qlen */ void do_parent(void) { int qlen, j, k, junk, fd[MAXBACKLOG + 1]; struct t_call tcall; Close(cfd); Signal(SIGALRM, parent_alrm); for (qlen = 0; qlen <= 14; qlen++) { printf("qlen = %d: ", qlen); Write(pfd, &qlen, sizeof(int)); /* tell child value */ Read(pfd, &junk, sizeof(int)); /* wait for child */ for (j = 0; j <= MAXBACKLOG; j++) { fd[j] = T_open(XTI_TCP, O_RDWR, NULL); T_bind(fd[j], NULL, NULL); tcall.addr.maxlen = sizeof(serv); tcall.addr.len = sizeof(serv); tcall.addr.buf = &serv; tcall.opt.len = 0; tcall.udata.len = 0; alarm(2); if (t_connect(fd[j], &tcall, NULL) < 0) { if (errno != EINTR) err_xti("t_connect error, j = %d", j); printf("timeout, %d connections completed\n", j-1); for (k = 1; k < j; k++) T_close(fd[k]); break; /* next value of qlen */ } alarm(0); } if (j > MAXBACKLOG) printf("%d connections?\n", MAXBACKLOG); } qlen = -1; /* tell child we're all done */ Write(pfd, &qlen, sizeof(int)); }
kii_socket_code_t socket_connect_cb( kii_socket_context_t* socket_context, const char* host, unsigned int port) { int ret; DNC_CFG_CMD dnsCfg; DNC_RESP_INFO dnsRespInfo; SOCKADDR_T hostAddr; A_UINT32 sock; SSL *ssl = NULL; SSL_CTX *ssl_ctx = NULL; context_t *ctx = (context_t*)socket_context->app_context; memset(&dnsRespInfo, 0, sizeof(dnsRespInfo)); /*Check if driver is loaded*/ if(IS_DRIVER_READY != A_OK){ return KII_SOCKETC_FAIL; } // resolve the IP address of the host if (0 == ath_inet_aton(host, &dnsRespInfo.ipaddrs_list[0])) { if (strlen(host) >= sizeof(dnsCfg.ahostname)) { printf("GetERROR: host name too long\n"); return KII_SOCKETC_FAIL; } strcpy((char*)dnsCfg.ahostname, host); dnsCfg.domain = ATH_AF_INET; dnsCfg.mode = RESOLVEHOSTNAME; if (A_OK != custom_ip_resolve_hostname(handle, &dnsCfg, &dnsRespInfo)) { printf("GetERROR: Unable to resolve host name\r\n"); return KII_SOCKETC_FAIL; } dnsRespInfo.ipaddrs_list[0] = A_BE2CPU32(dnsRespInfo.ipaddrs_list[0]); } #if CONNECT_SSL ssl_ctx = SSL_ctx_new(SSL_CLIENT, 4500, 2000, 0); if (ssl_ctx == NULL){ printf("failed to init ssl context.\n"); return KII_SOCKETC_FAIL; } #endif sock = t_socket((void *)handle, ATH_AF_INET, SOCK_STREAM_TYPE, 0); if (sock < 0) { printf("failed to init socket.\n"); return KII_SOCKETC_FAIL; } memset(&hostAddr, 0x00, sizeof(hostAddr)); hostAddr.sin_family = ATH_AF_INET; hostAddr.sin_addr = dnsRespInfo.ipaddrs_list[0]; hostAddr.sin_port = port; if (t_connect((void *)handle, sock, &hostAddr, sizeof(hostAddr)) == A_ERROR){ printf("failed to connect socket.\n"); return KII_SOCKETC_FAIL; } #if CONNECT_SSL ssl = SSL_new(ssl_ctx); if (ssl == NULL){ printf("failed to init ssl.\n"); return KII_SOCKETC_FAIL; } ret = SSL_set_fd(ssl, sock); if (ret < 0){ printf("failed to set fd: %d\n", ret); return KII_SOCKETC_FAIL; } ret = SSL_connect(ssl); if (ret < 0) { printf("failed to connect: %d\n", ret); return KII_SOCKETC_FAIL; } #endif ctx->sock = sock; ctx->ssl = ssl; ctx->ssl_ctx = ssl_ctx; return KII_SOCKETC_OK; }
enum error proto_open_connection(Var arglist, int *read_fd, int *write_fd, const char **local_name, const char **remote_name) { /* These are `static' rather than `volatile' because I can't cope with * getting all those nasty little parameter-passing rules right. This * function isn't recursive anyway, so it doesn't matter. */ struct sockaddr_in rec_addr; struct t_bind received; static const char *host_name; static int port; static Timer_ID id; int fd, result; int timeout = server_int_option("name_lookup_timeout", 5); static struct sockaddr_in addr; static Stream *st1 = 0, *st2 = 0; if (!st1) { st1 = new_stream(20); st2 = new_stream(50); } if (arglist.v.list[0].v.num != 2) return E_ARGS; else if (arglist.v.list[1].type != TYPE_STR || arglist.v.list[2].type != TYPE_INT) return E_TYPE; host_name = arglist.v.list[1].v.str; port = arglist.v.list[2].v.num; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = lookup_addr_from_name(host_name, timeout); if (addr.sin_addr.s_addr == 0) return E_INVARG; /* Cast to (void *) here to workaround const-less decls on some systems. */ fd = t_open((void *) "/dev/tcp", O_RDWR, 0); if (fd < 0) { if (t_errno != TSYSERR || errno != EMFILE) log_ti_error("Making endpoint in proto_open_connection"); return E_QUOTA; } received.addr.maxlen = sizeof(rec_addr); received.addr.len = sizeof(rec_addr); received.addr.buf = (void *) &rec_addr; if (t_bind(fd, 0, &received) < 0) { log_ti_error("Binding outbound endpoint"); t_close(fd); return E_QUOTA; } call->addr.maxlen = sizeof(addr); call->addr.len = sizeof(addr); call->addr.buf = (void *) &addr; TRY id = set_timer(server_int_option("outbound_connect_timeout", 5), timeout_proc, 0); result = t_connect(fd, call, 0); cancel_timer(id); EXCEPT(timeout_exception) result = -1; errno = ETIMEDOUT; t_errno = TSYSERR; reenable_timers(); ENDTRY if (result < 0) { t_close(fd); log_ti_error("Connecting in proto_open_connection"); return E_QUOTA; } if (!set_rw_able(fd)) { t_close(fd); return E_QUOTA; } *read_fd = *write_fd = fd; stream_printf(st1, "port %d", (int) ntohs(rec_addr.sin_port)); *local_name = reset_stream(st1); stream_printf(st2, "%s, port %d", host_name, port); *remote_name = reset_stream(st2); return E_NONE; }
bool c_Memcache::t_pconnect(const String& host, int port /*= 0*/, int timeout /*= 0*/, int timeoutms /*= 0*/) { return t_connect(host, port, timeout, timeoutms); }
int get_file_over_ftp (net_t *netpolicy) { char buffer [HUGE0], *ptr; struct hostent *phostent; struct sockaddr_in client, pasvclient; int rval; unsigned long filesz, received; slassert (netpolicy != NULL); fprintf (stdout, netpolicy->msg); fflush (stdout); if (!netpolicy->savepart) slassert (!netpolicy->checkstamp); if (!netpolicy->overwrite && file_exist (netpolicy->destpath, NULL, true)) /* regular file exist */ { return shutdown_net_t (netpolicy, FILE_DOWNLOADED, "Already downloaded"); } __act (phostent = gethostbyname (netpolicy->hostname), -1, "Cannot resolve host"); client.sin_family = phostent->h_addrtype; client.sin_port = htons ((unsigned short) netpolicy->port); memcpy (&client.sin_addr.s_addr, phostent->h_addr_list[0], phostent->h_length); __act ((netpolicy->sd = socket (client.sin_family, SOCK_STREAM, 0)) >= 0, -1, NULL); __act ((rval = t_connect (netpolicy->sd, (struct sockaddr *)&client, sizeof client, netpolicy->timeout)) >= 0, -1, NULL); /* starting FTP transaction */ fprintf (stdout, "\r%s [Starting ftp transaction]", netpolicy->msg); fflush (stdout); netpolicy->ftpquit = true; /* read server ready */ __act (t_recv (netpolicy->sd, buffer, HUGE) >= 0, FILE_NOT_DOWNLOADED, "Server not ready"); __act (strncmp (buffer, "220", 3) == 0, FILE_NOT_DOWNLOADED, "Server not ready"); /* anonymous login */ clrline (stdout, strlen (netpolicy->msg) + 30); fprintf (stdout, "\r%s [FTP anonymous login]", netpolicy->msg); fflush (stdout); /* sending username */ __act (!ftp_act (netpolicy, "USER anonymous\r\n", "331"), FILE_NOT_DOWNLOADED, "User failed"); /* sending pass */ __act (!ftp_act (netpolicy, "PASS [email protected]\r\n", "230"), FILE_NOT_DOWNLOADED, "Pass failed"); clrline (stdout, strlen (netpolicy->msg) + 25); fprintf (stdout, "\r%s [Retrieving file]", netpolicy->msg); fflush (stdout); __act (!ftp_act (netpolicy, "SYST\r\n", "215"), FILE_NOT_DOWNLOADED, "Syst error"); __act (!ftp_act (netpolicy, "TYPE I\r\n", "200"), FILE_NOT_DOWNLOADED, "Type I error"); __act (!ftp_act (netpolicy, "PASV\r\n", "227"), FILE_NOT_DOWNLOADED, "Pasv error"); /* obtain ip and port on passive mode */ __act (ptr = strchr (netpolicy->ftpreply, '('), FILE_NOT_DOWNLOADED, "Error on pasv mode"); sscanf (ptr, "(%hu,%hu,%hu,%hu,%hu,%hu)", &netpolicy->pasv[0], &netpolicy->pasv[1], /* IP */ &netpolicy->pasv[2], &netpolicy->pasv[3], /* IP */ &netpolicy->pasv[4], &netpolicy->pasv[5]); /* port: pasv[4] * 256 + pasv[5] */ snprintf (netpolicy->pasvip, sizeof netpolicy->pasvip, "%hu.%hu.%hu.%hu", netpolicy->pasv[0], netpolicy->pasv[1], netpolicy->pasv[2], netpolicy->pasv[3]); netpolicy->pasvport = netpolicy->pasv[4] * 256 + netpolicy->pasv[5]; netpolicy->pasvsd = socket (AF_INET, SOCK_STREAM, 0); pasvclient.sin_family = AF_INET; pasvclient.sin_port = htons((unsigned short) netpolicy->pasvport); __act (inet_pton (AF_INET, netpolicy->pasvip, &pasvclient.sin_addr) == 1 && !t_connect (netpolicy->pasvsd, (struct sockaddr *) &pasvclient, sizeof pasvclient, netpolicy->timeout), -1, "ftp pasv error"); snprintf (buffer, HUGE, "SIZE %s\r\n", netpolicy->srcpath); __act (!ftp_act (netpolicy, buffer, "213"), FILE_NOT_DOWNLOADED, (atoi (netpolicy->ftpreply) == 550) ? "File not found" : "Size error"); filesz = atoi (netpolicy->ftpreply + 4); /* 4 = strlen ("213 ") */ /* require file */ snprintf (buffer, HUGE, "RETR %s\r\n", netpolicy->srcpath); __act (!ftp_act (netpolicy, buffer, "150"), FILE_NOT_DOWNLOADED, "Retr error"); checkstamp (netpolicy, netpolicy->pasvsd); __act (netpolicy->oldstamp != 0, FILE_ALREADY_UPDATE, "Already update"); if (netpolicy->savepart) { slassert ((strlen (netpolicy->destpath) + strlen (".part")) < sizeof (netpolicy->destpath)); strcat (netpolicy->destpath, ".part"); } __act (netpolicy->fddest = fopen (netpolicy->destpath, "wb"), -1, netpolicy->destpath); if (netpolicy->oldstamp == 1) { fwrite (netpolicy->stamprecv, 1, STAMPSZ, netpolicy->fddest); } received = (netpolicy->checkstamp) ? STAMPSZ : 0; clrline (stdout, strlen (netpolicy->msg) + 25); netstat (NETSTAT_INIT, 0, 0, 0); /* init */ while ((rval = recv (netpolicy->pasvsd, buffer, HUGE, 0)) > 0) { received += (unsigned long) rval; fprintf (stdout, "\r%s [%s]", netpolicy->msg, netstat (0, 1, received, filesz)); fflush (stdout); fwrite (buffer, 1, rval, netpolicy->fddest); } fprintf (stdout, "\r%s [%s]\n", netpolicy->msg, netstat (NETSTAT_END, 1, received, filesz)); /* finish, flush 100% */ fflush (stdout); return shutdown_net_t (netpolicy, 0, NULL); }
int get_file_over_http (net_t *netpolicy) { char buffer[HUGE0]; unsigned long received, filesz; int rval; struct stat st; struct hostent *phostent; struct sockaddr_in client; slassert (netpolicy != NULL); fprintf (stdout, netpolicy->msg); fflush (stdout); /* sanity check */ if (!netpolicy->savepart) slassert (!netpolicy->checkstamp); if (!netpolicy->overwrite && file_exist (netpolicy->destpath, &st, true)) /* regular file exist */ { return shutdown_net_t (netpolicy, FILE_DOWNLOADED, "Already downloaded"); } __act (phostent = gethostbyname (netpolicy->hostname), -1, "Cannot resolve host"); client.sin_family = phostent->h_addrtype; client.sin_port = htons ((unsigned short) netpolicy->port); memcpy (&client.sin_addr.s_addr, phostent->h_addr_list[0], phostent->h_length); __act ((netpolicy->sd = socket (client.sin_family, SOCK_STREAM, 0)) >= 0, -1, NULL); __act (!t_connect (netpolicy->sd, (struct sockaddr *)&client, sizeof client, netpolicy->timeout), -1, NULL); snprintf (buffer, HUGE, HTTP_TEMPLATE, netpolicy->srcpath, netpolicy->hostname); __act (send (netpolicy->sd, buffer, strlen(buffer), 0) == (int) strlen(buffer), -1, NULL); /* receive http header and save to `filesz' length of file required */ __act ((rval = read_http_header (netpolicy->sd)) > 0, FILE_NOT_DOWNLOADED, "File Not Found"); filesz = (unsigned long) rval; checkstamp (netpolicy, netpolicy->sd); __act (netpolicy->oldstamp != 0, FILE_ALREADY_UPDATE, "Already update"); if (netpolicy->savepart) { slassert ((strlen (netpolicy->destpath) + strlen (".part")) < sizeof (netpolicy->destpath)); strcat (netpolicy->destpath, ".part"); } __act (netpolicy->fddest = fopen (netpolicy->destpath, "wb"), -1, netpolicy->destpath); if (netpolicy->oldstamp == 1) { fwrite (netpolicy->stamprecv, 1, STAMPSZ, netpolicy->fddest); } received = (netpolicy->checkstamp) ? STAMPSZ : 0; netstat (NETSTAT_INIT, 0, 0, 0); /* init */ while ((rval = recv (netpolicy->sd, buffer, HUGE, 0)) > 0) { received += rval; fprintf (stdout, "\r%s [%s]", netpolicy->msg, netstat (0, 1, received, filesz)); fflush (stdout); fwrite (buffer, 1, rval, netpolicy->fddest); } fprintf (stdout, "\r%s [%s]\n", netpolicy->msg, netstat (NETSTAT_END, 1, received, filesz)); /* finish, flush 100% */ fflush (stdout); return shutdown_net_t (netpolicy, 0, NULL); }
/* * Read the contents of the endbuffer, write each record to the * transport endpoint (to the server process), then read a ACKs back from * the transport endpoint and print it on the standard output. */ void process_client_service(int tfd, struct t_call *callptr, char *recbuffer, int length) { static SEQNO_dt current_seqnum, last_seqnum; int n, total_msg; int flags; struct gs_record_hdr hdr; char sendBuffer[4096]; int total_l, l, hl; l = length; hl = sizeof(struct gs_record_hdr); total_l = l + hl; /* make socket connection to server */ if (t_connect(tfd, callptr, (struct t_call *) 0) < 0) { printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf("!!! WORNING:Client can't connect to server... !!!\n"); printf("!!! Is the server running? or !!!\n"); printf("!!! Is the server host and port specified for client? !!!\n"); printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); return; } else { printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); printf("!!! Client: Connecting to server... !!!\n"); printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); if(t_rcv(tfd, (char*)&last_seqnum, sizeof(SEQNO_dt), &flags) > 0) { /* now ACKs completed.*/ current_seqnum = htonl(last_seqnum); current_seqnum++; printf( "**ACK: connected to server and rcvd seq nums %d\n", last_seqnum); } else { printf( "**ACK: server DID NOT sent back seq nums %d to %d\n", last_seqnum, current_seqnum); } } total_msg = 0; for(int i=0; i< MaxNumSend; i++) { /* build header with the seqno and size, and data buffer */ current_seqnum = htonl(last_seqnum)+1; hdr.length = htons(l); hdr.sequence_no = htonl(current_seqnum); memcpy(sendBuffer, &hdr, hl); memcpy(sendBuffer+hl, recbuffer, l); /* assume the connection is on, or we can check it if we want using t_look(fd) */ if( n = t_snd( tfd, sendBuffer, total_l, NULL) != total_l) { printf( "t_snd fails: record length is %d, but was sent %d\n", length, n); } else { printf( " ##Client sent data %d bytes for seqno = %d\n", length, hdr.sequence_no); if(t_rcv(tfd, (char*)&last_seqnum, sizeof(SEQNO_dt), &flags) > 0) { /* now ACKs completed.*/ printf( "**ACK: Server processed data SUCCESSFULLY for seqno = %d\n", last_seqnum); total_msg++; } else { continue; } } /* end if for send */ } /* end for-loop*/ printf("************************************************\n"); printf("* Result: # of msg sent and processed = %d\n", total_msg); printf("************************************************\n"); }
int tcp_connect(const char *host, const char *serv) { int tfd, i; void *handle; struct t_call tcall; struct t_discon tdiscon; struct netconfig *ncp; struct nd_hostserv hs; struct nd_addrlist *alp; struct netbuf *np; struct t_opthdr *topt; handle = Setnetpath(); hs.h_host = (char *) host; hs.h_serv = (char *) serv; while ( (ncp = getnetpath(handle)) != NULL) { if (strcmp(ncp->nc_proto, "tcp") != 0) continue; if (netdir_getbyname(ncp, &hs, &alp) != 0) continue; /* try each server address */ for (i = 0, np = alp->n_addrs; i < alp->n_cnt; i++, np++) { tfd = T_open(ncp->nc_device, O_RDWR, NULL); T_bind(tfd, NULL, NULL); tcall.addr.len = np->len; tcall.addr.buf = np->buf; /* pointer copy */ tcall.opt.len = 0; /* no options */ tcall.udata.len = 0; /* no user data with connect */ if (t_connect(tfd, &tcall, NULL) == 0) { endnetpath(handle); /* success, connected to server */ netdir_free(alp, ND_ADDRLIST); req = T_alloc(fd, T_OPTMGMT, T_ALL); ret = T_alloc(fd, T_OPTMGMT, T_ALL); topt = (struct t_opthdr *) req->opt.buf; topt->level = T_INET_TCP; topt->name = T_TCP_MAXSEG; topt->len = sizeof(struct t_opthdr) + sizeof(u_long); req->opt.len = topt->len; topt = OPT_NEXTHDR(req->opt.buf, req->opt.maxlen, topt); topt->level = XTI_GENERIC; topt->name = XTI_SNDBUF; topt->len = sizeof(struct t_opthdr) + sizeof(u_long); req->opt.len += topt->len; return(tfd); } if (t_errno == TLOOK && t_look(tfd) == T_DISCONNECT) { t_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; } t_close(tfd); } netdir_free(alp, ND_ADDRLIST); } endnetpath(handle); return(-1); }
bool c_Memcache::t_pconnect(CStrRef host, int port /*= 0*/, int timeout /*= 0*/, int timeoutms /*= 0*/) { INSTANCE_METHOD_INJECTION_BUILTIN(Memcache, Memcache::pconnect); return t_connect(host, port, timeout, timeoutms); }
int main(int argc, char **argv) { int opt; char *dir = NULL; char *puerto = NULL; int iPuerto = 0; int flagServidor=0; int resultado_primitiva; t_direccion tsap_destino, tsap_origen; memset(&tsap_origen, 0, sizeof (tsap_origen)); memset(&tsap_destino, 0, sizeof (tsap_destino)); while ((opt = getopt(argc, argv, "scp:r:")) != -1) { switch (opt) { case 's': fprintf(stderr,"\naplicacion servidor\n"); flagServidor = 1; break; case 'c': fprintf(stderr,"\naplicacion cliente\n"); flagServidor = 0; break; case 'p': puerto = optarg; iPuerto = atoi(puerto); fprintf(stderr,"\nEl puerto es: %d\n", iPuerto); //tsap_origen.puerto = iPuerto; break; case 'r': dir = optarg; inet_pton(AF_INET, dir, &tsap_destino.ip); fprintf(stderr,"\nla direccion es: %s\n", dir); break; } } //miramos si la aplicacion funciona como cliente o como servidor if(flagServidor == 0){ tsap_destino.puerto=iPuerto; resultado_primitiva = t_connect (&tsap_destino, &tsap_origen); }else{ tsap_origen.puerto = iPuerto; resultado_primitiva = t_listen (&tsap_origen, &tsap_destino); } //resultados de la primitiva switch(resultado_primitiva){ case 0: fprintf(stderr,"\nPrimitiva ejecutada con exito\n"); break; case -1: fprintf(stderr,"\nLa conexion referida no esta en la tabla\n"); break; case -2: fprintf(stderr,"\nColision con una conexion ya presente en la tabla\n"); break; case -3: fprintf(stderr,"\nError de (sub)red.Agotado el numero de retransmisiones de un paquete\n"); break; case -4: fprintf(stderr,"\nError al entrar en el kernel\n"); break; case -5: fprintf(stderr,"\nMensaje demasiado grande\n"); break; case -6: fprintf(stderr,"\nNo existe el TSAP especificado\n"); break; case -7: fprintf(stderr,"\nDatos no disponibles\n"); break; case -8: fprintf(stderr,"\nFlujo de datos ya cerrado\n"); break; case -9: fprintf(stderr,"\nArgumento invalido\n"); break; case -10: fprintf(stderr,"\nNumero maximo de conexiones alcanzado\n"); break; case -11: fprintf(stderr,"\nError indefinido\n"); break; case -12: fprintf(stderr,"\nRecibida desconexion\n"); break; } return 0; }
int qse_nwio_init ( qse_nwio_t* nwio, qse_mmgr_t* mmgr, const qse_nwad_t* nwad, int flags, const qse_nwio_tmout_t* tmout) { qse_skad_t addr; qse_sck_len_t addrlen; int family, type, tmp; QSE_MEMSET (nwio, 0, QSE_SIZEOF(*nwio)); nwio->mmgr = mmgr; nwio->flags = flags; nwio->errnum = QSE_NWIO_ENOERR; if (tmout) nwio->tmout = *tmout; else { nwio->tmout.r.sec = -1; nwio->tmout.w.sec = -1; nwio->tmout.c.sec = -1; nwio->tmout.a.sec = -1; } tmp = qse_nwadtoskad (nwad, &addr); if (tmp <= -1) { nwio->errnum = QSE_NWIO_EINVAL; return -1; } addrlen = tmp; #if defined(SOCK_STREAM) && defined(SOCK_DGRAM) if (flags & QSE_NWIO_TCP) type = SOCK_STREAM; else if (flags & QSE_NWIO_UDP) type = SOCK_DGRAM; else #endif { nwio->errnum = QSE_NWIO_EINVAL; return -1; } family = qse_skadfamily (&addr); #if defined(_WIN32) nwio->handle = socket (family, type, 0); if (nwio->handle == INVALID_SOCKET) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle == INVALID_SOCKET) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } closesocket (nwio->handle); nwio->handle = handle; } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { unsigned long cmd = 1; if (ioctlsocket(nwio->handle, FIONBIO, &cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { unsigned long cmd = 0; if ((xret == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) || ioctlsocket (nwio->handle, FIONBIO, &cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { int xlen; DWORD xerr; xlen = QSE_SIZEOF(xerr); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xerr, &xlen) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } else if (xerr != 0) { nwio->errnum = skerr_to_errnum (xerr); goto oops; } } } else { if (xret == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (WSAGetLastError()); goto oops; } } } #elif defined(__OS2__) nwio->handle = socket (family, type, 0); if (nwio->handle <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } soclose (nwio->handle); nwio->handle = handle; } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int noblk = 1; if (ioctl (nwio->handle, FIONBIO, (void*)&noblk, QSE_SIZEOF(noblk)) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int noblk = 0; if ((xret <= -1 && sock_errno() != SOCEINPROGRESS) || ioctl (nwio->handle, FIONBIO, (void*)&noblk, QSE_SIZEOF(noblk)) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { int xlen, xerr; xlen = QSE_SIZEOF(xerr); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xerr, &xlen) <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } else if (xerr != 0) { nwio->errnum = skerr_to_errnum (xerr); goto oops; } } } else { if (xret <= -1) { nwio->errnum = skerr_to_errnum (sock_errno()); goto oops; } } } #elif defined(__DOS__) nwio->handle = socket (family, type, 0); if (nwio->handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; #if defined(SO_REUSEADDR) if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } #endif if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } close_s (nwio->handle); nwio->handle = handle; } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int cmd = 1; if (ioctlsocket(nwio->handle, FIONBIO, (char*)&cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int cmd = 0; if ((xret == SOCKET_ERROR && errno != EWOULDBLOCK) || ioctlsocket (nwio->handle, FIONBIO, (char*)&cmd) == SOCKET_ERROR) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { int xlen, xerr; xlen = QSE_SIZEOF(xerr); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xerr, &xlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } else if (xerr != 0) { nwio->errnum = skerr_to_errnum (xerr); goto oops; } } } else { if (xret <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } } #elif defined(USE_TLI) { static const qse_mchar_t* dev_path[2][2] = { { "/dev/tcp", "/dev/inet/tcp" }, { "/dev/udp", "/dev/inet/tcp" } }; int dev_id; if (flags & QSE_NWIO_TCP) dev_id = 0; else { QSE_ASSERT (flags & QSE_NWIO_UDP); dev_id = 1; } nwio->handle = t_open (dev_path[dev_id][0], O_RDWR, QSE_NULL); if (nwio->handle <= -1) { nwio->handle = t_open (dev_path[dev_id][1], O_RDWR, QSE_NULL); if (nwio->handle <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } } if (flags & QSE_NWIO_PASSIVE) { /* TODO: */ nwio->errnum = QSE_NWIO_ENOIMPL; goto oops; } else { struct t_call call; /* for connecting */ struct t_bind req, ret; /* for binding */ qse_skad_t reqaddr, retaddr; qse_nwad_t reqnwad; /* call = t_alloc (nwio->handle, T_CALL, T_ADDR); if (!call) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; }*/ qse_clearnwad (&reqnwad, nwad->type); qse_nwadtoskad (&reqnwad, &reqaddr); QSE_MEMSET (&ret, 0, QSE_SIZEOF(req)); req.addr.maxlen = addrlen; req.addr.len = addrlen; req.addr.buf = &reqaddr; QSE_MEMSET (&ret, 0, QSE_SIZEOF(ret)); ret.addr.maxlen = addrlen; ret.addr.len = addrlen; ret.addr.buf = &retaddr; if (t_bind (nwio->handle, &req, &ret) <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } /* TODO: should i use t_alloc() and t_free for call, ret, req? */ QSE_MEMSET (&call, 0, QSE_SIZEOF(call)); call.addr.maxlen = addrlen; call.addr.len = addrlen; call.addr.buf = &addr; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int orgfl; orgfl = fcntl (nwio->handle, F_GETFL, 0); if (orgfl <= -1 || fcntl (nwio->handle, F_SETFL, orgfl | O_NONBLOCK) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (t_connect (nwio->handle, &call, 0) <= -1) { if (t_errno != TNODATA) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } /* TODO: this doesn't seem to work wel... REDO THE WORK */ if (wait_for_data (nwio, &nwio->tmout.c, 0) <= -1) goto oops; if (t_rcvconnect (nwio->handle, QSE_NULL) <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } } if (fcntl (nwio->handle, F_SETFL, orgfl) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } else { if (t_connect (nwio->handle, &call, 0) <= -1) { nwio->errnum = tlierr_to_errnum (t_errno, errno); goto oops; } } } } #else #if defined(SOCK_CLOEXEC) nwio->handle = socket (family, type | SOCK_CLOEXEC, 0); #else nwio->handle = socket (family, type, 0); #endif if (nwio->handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } #if !defined(SOCK_CLOEXEC) && defined(FD_CLOEXEC) { int tmp = fcntl (nwio->handle, F_GETFD); if (tmp >= 0) fcntl (nwio->handle, F_SETFD, tmp | FD_CLOEXEC); } #endif if ((flags & QSE_NWIO_TCP) && (flags & QSE_NWIO_KEEPALIVE)) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, QSE_SIZEOF(optval)); } if (flags & QSE_NWIO_PASSIVE) { qse_nwio_hnd_t handle; #if defined(SO_REUSEADDR) if (flags & QSE_NWIO_REUSEADDR) { int optval = 1; setsockopt (nwio->handle, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, QSE_SIZEOF(optval)); } #endif if (bind (nwio->handle, (struct sockaddr*)&addr, addrlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (flags & QSE_NWIO_TCP) { if (listen (nwio->handle, 10) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (TMOUT_ENABLED(nwio->tmout.a) && wait_for_data (nwio, &nwio->tmout.a, 0) <= -1) goto oops; handle = accept (nwio->handle, (struct sockaddr*)&addr, &addrlen); if (handle <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } qse_closesckhnd (nwio->handle); /* close the listening socket */ nwio->handle = handle; /* set the handle to the accepted socket */ } else if (flags & QSE_NWIO_UDP) { nwio->status |= STATUS_UDP_CONNECT; } } else { int xret; if (TMOUT_ENABLED(nwio->tmout.c) && (flags & QSE_NWIO_TCP)) { int orgfl; orgfl = fcntl (nwio->handle, F_GETFL, 0); if (orgfl <= -1 || fcntl (nwio->handle, F_SETFL, orgfl | O_NONBLOCK) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if ((xret <= -1 && errno != EINPROGRESS) || fcntl (nwio->handle, F_SETFL, orgfl) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } if (wait_for_data (nwio, &nwio->tmout.c, 1) <= -1) goto oops; else { qse_sck_len_t xlen; xlen = QSE_SIZEOF(xret); if (getsockopt (nwio->handle, SOL_SOCKET, SO_ERROR, (char*)&xret, &xlen) <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } else if (xret != 0) { nwio->errnum = skerr_to_errnum (xret); goto oops; } } } else { xret = connect (nwio->handle, (struct sockaddr*)&addr, addrlen); if (xret <= -1) { nwio->errnum = skerr_to_errnum (errno); goto oops; } } } #endif if (flags & QSE_NWIO_TEXT) { int topt = 0; if (flags & QSE_NWIO_IGNOREMBWCERR) topt |= QSE_TIO_IGNOREMBWCERR; if (flags & QSE_NWIO_NOAUTOFLUSH) topt |= QSE_TIO_NOAUTOFLUSH; nwio->tio = qse_tio_open (mmgr, QSE_SIZEOF(qse_nwio_t*), topt); if (nwio->tio == QSE_NULL) { nwio->errnum = QSE_NWIO_ENOMEM; goto oops; } /* store the back-reference to nwio in the extension area.*/ *(qse_nwio_t**)QSE_XTN(nwio->tio) = nwio; if (qse_tio_attachin (nwio->tio, socket_input, QSE_NULL, 4096) <= -1 || qse_tio_attachout (nwio->tio, socket_output, QSE_NULL, 4096) <= -1) { if (nwio->errnum == QSE_NWIO_ENOERR) nwio->errnum = tio_errnum_to_nwio_errnum (nwio->tio); goto oops; } }
static int get_tcp_socket( char *machine, /* remote host */ char *service, /* nttp/smtp etc. */ unsigned short port) /* tcp port number */ { int s = -1; int save_errno = 0; struct sockaddr_in sock_in; # ifdef TLI /* Transport Level Interface */ char device[20]; char *env_device; extern int t_errno; extern struct hostent *gethostbyname(); struct hostent *hp; struct t_call *callptr; /* * Create a TCP transport endpoint. */ if ((env_device = getenv("DEV_TCP")) != NULL) /* SCO uses DEV_TCP, most other OS use /dev/tcp */ STRCPY(device, env_device); else strcpy(device, "/dev/tcp"); if ((s = t_open(device, O_RDWR, (struct t_info *) 0)) < 0){ t_error(txt_error_topen); return -EPROTO; } if (t_bind(s, (struct t_bind *) 0, (struct t_bind *) 0) < 0) { t_error("t_bind"); t_close(s); return -EPROTO; } memset((char *) &sock_in, '\0', sizeof(sock_in)); sock_in.sin_family = AF_INET; sock_in.sin_port = htons(port); if (!isdigit((unsigned char)*machine) || # ifdef HAVE_INET_ATON !inet_aton(machine, &sock_in) # else # ifdef HAVE_INET_ADDR (long) (sock_in.sin_addr.s_addr = inet_addr(machine)) == INADDR_NONE) # endif /* HAVE_INET_ADDR */ # endif /* HAVE_INET_ATON */ { if ((hp = gethostbyname(machine)) == NULL) { my_fprintf(stderr, _(txt_gethostbyname), "gethostbyname() ", machine); t_close(s); return -EHOSTUNREACH; } memcpy((char *) &sock_in.sin_addr, hp->h_addr, hp->h_length); } /* * Allocate a t_call structure and initialize it. * Let t_alloc() initialize the addr structure of the t_call structure. */ if ((callptr = (struct t_call *) t_alloc(s, T_CALL, T_ADDR)) == NULL){ t_error("t_alloc"); t_close(s); return -EPROTO; } callptr->addr.maxlen = sizeof(sock_in); callptr->addr.len = sizeof(sock_in); callptr->addr.buf = (char *) &sock_in; callptr->opt.len = 0; /* no options */ callptr->udata.len = 0; /* no user data with connect */ /* * Connect to the server. */ if (t_connect(s, callptr, (struct t_call *) 0) < 0) { save_errno = t_errno; if (save_errno == TLOOK) fprintf(stderr, _(txt_error_server_unavailable)); else t_error("t_connect"); t_free((char *) callptr, T_CALL); t_close(s); return -save_errno; } /* * Now replace the timod module with the tirdwr module so that * standard read() and write() system calls can be used on the * descriptor. */ t_free((char *) callptr, T_CALL); if (ioctl(s, I_POP, (char *) 0) < 0) { perror("I_POP(timod)"); t_close(s); return -EPROTO; } if (ioctl(s, I_PUSH, "tirdwr") < 0) { perror("I_PUSH(tirdwr)"); t_close(s); return -EPROTO; } # else # ifndef EXCELAN struct servent *sp; struct hostent *hp; # ifdef h_addr int x = 0; char **cp; static char *alist[2] = {0, 0}; # endif /* h_addr */ static struct hostent def; static struct in_addr defaddr; static char namebuf[256]; # ifdef HAVE_GETSERVBYNAME if ((sp = (struct servent *) getservbyname(service, "tcp")) == NULL) { my_fprintf(stderr, _(txt_error_unknown_service), service); return -EHOSTUNREACH; } # else sp = my_malloc(sizeof(struct servent)); sp->s_port = htons(IPPORT_NNTP); # endif /* HAVE_GETSERVBYNAME */ /* If not a raw ip address, try nameserver */ if (!isdigit((unsigned char) *machine) || # ifdef HAVE_INET_ATON !inet_aton(machine, &defaddr) # else # ifdef HAVE_INET_ADDR (long) (defaddr.s_addr = (long) inet_addr(machine)) == -1 # endif /* HAVE_INET_ADDR */ # endif /* HAVE_INET_ATON */ ) { hp = gethostbyname(machine); } else { /* Raw ip address, fake */ STRCPY(namebuf, machine); def.h_name = (char *) namebuf; # ifdef h_addr def.h_addr_list = alist; # endif /* h_addr */ def.h_addr = (char *) &defaddr; def.h_length = sizeof(struct in_addr); def.h_addrtype = AF_INET; def.h_aliases = 0; hp = &def; } if (hp == NULL) { my_fprintf(stderr, _(txt_gethostbyname), "\n", machine); return -EHOSTUNREACH; } memset((char *) &sock_in, '\0', sizeof(sock_in)); sock_in.sin_family = hp->h_addrtype; sock_in.sin_port = htons(port); /* sock_in.sin_port = sp->s_port; */ # else memset((char *) &sock_in, '\0', sizeof(sock_in)); sock_in.sin_family = AF_INET; # endif /* !EXCELAN */ /* * The following is kinda gross. The name server under 4.3 * returns a list of addresses, each of which should be tried * in turn if the previous one fails. However, 4.2 hostent * structure doesn't have this list of addresses. * Under 4.3, h_addr is a #define to h_addr_list[0]. * We use this to figure out whether to include the NS specific * code... */ # ifdef h_addr /* * Get a socket and initiate connection -- use multiple addresses */ for (cp = hp->h_addr_list; cp && *cp; cp++) { # if defined(__hpux) && defined(SVR4) unsigned long socksize, socksizelen; # endif /* __hpux && SVR4 */ if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) { perror("socket"); return -errno; } memcpy((char *) &sock_in.sin_addr, *cp, hp->h_length); # ifdef HAVE_INET_NTOA if (x < 0) my_fprintf(stderr, _(txt_trying), (char *) inet_ntoa(sock_in.sin_addr)); # endif /* HAVE_INET_NTOA */ # if defined(__hpux) && defined(SVR4) /* recommended by [email protected] */ # define HPSOCKSIZE 0x8000 getsockopt(s, SOL_SOCKET, SO_SNDBUF, /* (caddr_t) */ &socksize, /* (caddr_t) */ &socksizelen); if (socksize < HPSOCKSIZE) { socksize = HPSOCKSIZE; setsockopt(s, SOL_SOCKET, SO_SNDBUF, /* (caddr_t) */ &socksize, sizeof(socksize)); } socksize = 0; socksizelen = sizeof(socksize); getsockopt(s, SOL_SOCKET, SO_RCVBUF, /* (caddr_t) */ &socksize, /* (caddr_t) */ &socksizelen); if (socksize < HPSOCKSIZE) { socksize = HPSOCKSIZE; setsockopt(s, SOL_SOCKET, SO_RCVBUF, /* (caddr_t) */ &socksize, sizeof(socksize)); } # endif /* __hpux && SVR4 */ if ((x = connect(s, (struct sockaddr *) &sock_in, sizeof(sock_in))) == 0) break; save_errno = errno; /* Keep for later */ # ifdef HAVE_INET_NTOA my_fprintf(stderr, _(txt_connection_to), (char *) inet_ntoa(sock_in.sin_addr)); perror(""); # endif /* HAVE_INET_NTOA */ (void) s_close(s); } if (x < 0) { my_fprintf(stderr, _(txt_giving_up)); return -save_errno; /* Return the last errno we got */ } # else # ifdef EXCELAN if ((s = socket(SOCK_STREAM, (struct sockproto *) NULL, &sock_in, SO_KEEPALIVE)) < 0) { perror("socket"); return -errno; } /* set up addr for the connect */ memset((char *) &sock_in, '\0', sizeof(sock_in)); sock_in.sin_family = AF_INET; sock_in.sin_port = htons(IPPORT_NNTP); if ((sock_in.sin_addr.s_addr = rhost(&machine)) == -1) { my_fprintf(stderr, _(txt_gethostbyname), "\n", machine); return -1; } /* And connect */ if (connect(s, (struct sockaddr *) &sock_in) < 0) { save_errno = errno; perror("connect"); (void) s_close(s); return -save_errno; } # else if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return -errno; } /* And then connect */ memcpy((char *) &sock_in.sin_addr, hp->h_addr, hp->h_length); if (connect(s, (struct sockaddr *) &sock_in, sizeof(sock_in)) < 0) { save_errno = errno; perror("connect"); (void) s_close(s); return -save_errno; } # endif /* !EXCELAN */ # endif /* !h_addr */ # endif /* !TLI */ return s; }
int main(int argc, char **argv) { int tfd, n, flags; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; struct t_call tcall; struct t_discon tdiscon; if (argc != 2) err_quit("usage: daytimecli01 <IPaddress>"); tfd = T_open(XTI_TCP, O_RDWR, NULL); T_bind(tfd, NULL, NULL); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); tcall.addr.maxlen = sizeof(servaddr); tcall.addr.len = sizeof(servaddr); tcall.addr.buf = &servaddr; tcall.opt.len = 0; /* no options with connect */ tcall.udata.len = 0; /* no user data with connect */ if (t_connect(tfd, &tcall, NULL) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(tfd)) == T_DISCONNECT) { tdiscon.udata.maxlen = 0; T_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; err_sys("t_connect error"); } else err_quit("unexpected event after t_connect: %d", n); } else err_xti("t_connect error"); } /* end daytimecli1 */ /* include daytimecli2 */ for ( ; ; ) { if ( (n = t_rcv(tfd, recvline, MAXLINE, &flags)) < 0) { if (t_errno == TLOOK) { if ( (n = T_look(tfd)) == T_ORDREL) { T_rcvrel(tfd); break; } else if (n == T_DISCONNECT) { tdiscon.udata.maxlen = 0; T_rcvdis(tfd, &tdiscon); errno = tdiscon.reason; /* probably ECONNRESET */ err_sys("server terminated prematurely"); } else err_quit("unexpected event after t_rcv: %d", n); } else err_xti("t_rcv error"); } recvline[n] = 0; /* null terminate */ fputs(recvline, stdout); } exit(0); }
bool c_Memcache::t_pconnect(CStrRef host, int port /*= 0*/, int timeout /*= 0*/, int timeoutms /*= 0*/) { return t_connect(host, port, timeout, timeoutms); }
int t_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen, int timeout) { int my_timeout = timeout; return t_connect(sockfd, serv_addr, addrlen, &my_timeout); }