int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id) { #ifdef LIBC_HAS_IP6 struct sockaddr_in6 sa; if (noipv6) { #endif if (ip6_isv4mapped(ip)) return socket_connect4(s,ip+12,port); if (byte_equal(ip,16,V6loopback)) return socket_connect4(s,ip4loopback,port); #ifdef LIBC_HAS_IP6 } byte_zero(&sa,sizeof sa); sa.sin6_family = PF_INET6; uint16_pack_big((char *) &sa.sin6_port,port); sa.sin6_flowinfo = 0; sa.sin6_scope_id = scope_id; byte_copy((char *) &sa.sin6_addr,16,ip); return connect(s,(struct sockaddr *) &sa,sizeof sa); #else errno=EPROTONOSUPPORT; return -1; #endif }
static int make_connection(char* ip,uint16 port,uint32 scope_id,int s) { int v6=byte_diff(ip,12,V4mappedprefix); if (v6) { if (s==-1) { s=socket_tcp6(); if (s==-1) return -1; } if (socket_connect6(s,ip,port,scope_id)==-1) { if (errno==EAGAIN || errno==EINPROGRESS || errno==EISCONN) return s; ++kaputt; if (errno!=ECONNREFUSED && errno!=ECONNRESET) carpsys("socket_connect6"); close(s); return -1; } } else { if (s==-1) { s=socket_tcp4(); if (s==-1) return -1; } if (socket_connect4(s,ip+12,port)==-1) { if (errno==EAGAIN || errno==EINPROGRESS || errno==EISCONN) return s; ++kaputt; if (errno!=ECONNREFUSED && errno!=ECONNRESET) carpsys("socket_connect6"); close(s); return -1; } } return s; }
int timeoutconn(int s,char ip[4],uint16 port,unsigned int timeout) { struct taia now; struct taia deadline; iopause_fd x; if (socket_connect4(s,ip,port) == -1) { if ((errno != error_wouldblock) && (errno != error_inprogress)) return -1; x.fd = s; x.events = IOPAUSE_WRITE; taia_now(&now); taia_uint(&deadline,timeout); taia_add(&deadline,&now,&deadline); for (;;) { taia_now(&now); iopause(&x,1,&deadline,&now); if (x.revents) break; if (taia_less(&deadline,&now)) { errno = error_timeout; /* note that connect attempt is continuing */ return -1; } } if (!socket_connected(s)) return -1; } if (ndelay_off(s) == -1) return -1; return 0; }
static int thistcp (struct dns_transmit *d) { struct taia now; const char *ip = NULL; socketfree (d); packetfree (d); for (; d->curserver < 16; ++d->curserver) { ip = d->servers + 4 * d->curserver; if (byte_diff (ip, 4, "\0\0\0\0")) { d->query[2] = dns_random (256); d->query[3] = dns_random (256); d->s1 = 1 + socket_tcp (); if (!d->s1) { dns_transmit_free (d); return -1; } if (randombind (d) == -1) { dns_transmit_free (d); return -1; } taia_now (&now); taia_uint (&d->deadline, 10); taia_add (&d->deadline, &d->deadline, &now); if (socket_connect4 (d->s1 - 1, ip, 53) == 0) { d->pos = 0; d->tcpstate = 2; return 0; } if (errno == error_inprogress || errno == error_wouldblock) { d->tcpstate = 1; return 0; } socketfree(d); } } dns_transmit_free(d); return -1; }
ssize_t socket_fastopen_connect4(int s,const char* ip,uint16 port,const char* buf,size_t len) { #ifndef MSG_FASTOPEN int r; { #else int r=socket_send4_flag(s,buf,len,ip,port,MSG_FASTOPEN); if (r==-1 && errno==ENOTCONN) { #endif /* apparently the kernel does not support TCP fast open */ r=socket_connect4(s,ip,port); if (r==0) return write(s,buf,len); } return r; }
static int make_connection(char* ip,uint16 port,uint32 scope_id) { int v6=byte_diff(ip,12,V4mappedprefix); int s; if (v6) { s=socket_tcp6(); if (s==-1) panic("socket_tcp6()"); ndelay_off(s); if (bindport) { for (;;) { int r=socket_bind6_reuse(s,V6any,bindport,0); if (++bindport<1024) bindport=1024; if (r==0) break; if (errno!=EADDRINUSE) panic("socket_bind6"); } } if (socket_connect6(s,ip,port,scope_id)==-1) { carp("socket_connect6"); close(s); return -1; } } else { s=socket_tcp4(); if (s==-1) panic("socket_tcp4()"); ndelay_off(s); if (bindport) { for (;;) { int r=socket_bind4_reuse(s,V6any,bindport); if (++bindport<1024) bindport=1024; if (r==0) break; if (errno!=EADDRINUSE) panic("socket_bind4"); } } if (socket_connect4(s,ip+12,port)==-1) { carp("socket_connect4"); close(s); return -1; } } return s; }
static int thisudp(struct dns_transmit *d) { const char *ip; socketfree(d); while (d->udploop < 4) { for (;d->curserver < 16;++d->curserver) { ip = d->servers + 4 * d->curserver; if (byte_diff(ip,4,"\0\0\0\0")) { d->query[2] = dns_random(256); d->query[3] = dns_random(256); d->s1 = 1 + socket_udp(); if (!d->s1) { dns_transmit_free(d); return -1; } if (randombind(d) == -1) { dns_transmit_free(d); return -1; } if (socket_connect4(d->s1 - 1,ip,53) == 0) if (send(d->s1 - 1,d->query + 2,d->querylen - 2,0) == d->querylen - 2) { struct taia now; taia_now(&now); taia_uint(&d->deadline,timeouts[d->udploop]); taia_add(&d->deadline,&d->deadline,&now); d->tcpstate = 0; return 0; } socketfree(d); } } ++d->udploop; d->curserver = 0; } dns_transmit_free(d); return -1; }
/** Connect a socket to an IPv4 address, honouring a timeout. * * \note The socket will have nonblocking mode turned on on return from * this function. */ int socket_connect4_timeout(int sock, const ipv4addr* ip, ipv4port port, int timeout) { iopoll_fd pf; if (!nonblock_on(sock)) return 0; if (socket_connect4(sock, ip, port)) return 1; if (errno != EINPROGRESS && errno != EWOULDBLOCK) return 0; pf.fd = sock; pf.events = IOPOLL_WRITE; switch (iopoll_restart(&pf, 1, timeout)) { case 0: errno = ETIMEDOUT; return 0; case 1: if (socket_connected(sock)) return 1; /* No break, fall through */ default: return 0; } }
int main(int argc,char* argv[]) { static size_t x; x=23; atomic_add(&x,3); printf("%u\n",x); printf("%u\n",atomic_add_return(&x,-3)); printf("%u\n",compare_and_swap(&x,26,17)); printf("%u\n",compare_and_swap(&x,23,17)); #if 0 atomic_add(&x,3); printf("%u\n",x); x=23; atomic_add(&x,3); assert(x==26); atomic_or(&x,1); assert(x==27); atomic_and(&x,-2); assert(x==26); #endif #if 0 iarray a; char* c; iarray_init(&a,sizeof(io_entry)); printf("15 -> %p\n",c=iarray_allocate(&a,15)); printf("23 -> %p\n",c=iarray_allocate(&a,23)); printf("1234567 -> %p\n",c=iarray_allocate(&a,1234567)); printf("23 -> %p\n",iarray_get(&a,23)); #endif #if 0 io_batch* b=iob_new(1234); int64 fd=open("t.c",0); iob_addbuf(b,"fnord",5); iob_addfile_close(b,fd,0,7365); iob_write(1,b,writecb); #endif #if 0 char dest[1024]; unsigned long len; scan_urlencoded2("libstdc++.tar.gz",dest,&len); buffer_putmflush(buffer_1,dest,"\n"); #endif #if 0 static stralloc sa; stralloc_copym(&sa,"foo ","bar ","baz.\n"); write(1,sa.s,sa.len); #endif #if 0 buffer_putmflush(buffer_1,"foo ","bar ","baz.\n"); #endif #if 0 char* c="fnord"; int fd=open_read(c); errmsg_iam(argv[0]); carp("could not open file `",c,"'"); diesys(23,"could not open file `",c,"'"); #endif #if 0 errmsg_warn("could not open file `",c,"'",0); errmsg_warnsys("could not open file `",c,"'",0); #endif #if 0 char buf[100]="/usr/bin/sh"; int len=str_len(buf); assert(byte_rchr(buf,len,'/')==8); assert(byte_rchr(buf,len,'@')==len); assert(byte_rchr(buf,len,'h')==len-1); printf("%d\n",byte_rchr("x",1,'x')); #endif #if 0 char buf[IP6_FMT+100]; int i; char ip[16]; uint32 scope_id; char* s="fec0::1:220:e0ff:fe69:ad92%eth0/64"; char blubip[16]="\0\0\0\0\0\0\0\0\0\0\xff\xff\x7f\0\0\001"; i=scan_ip6if(s,ip,&scope_id); assert(s[i]=='/'); buffer_put(buffer_1,buf,fmt_ip6if(buf,ip,scope_id)); buffer_putnlflush(buffer_1); buffer_put(buffer_1,buf,fmt_ip6ifc(buf,blubip,scope_id)); buffer_putnlflush(buffer_1); scan_ip6("2001:7d0:0:f015:0:0:0:1",ip); buffer_put(buffer_1,buf,fmt_ip6(buf,ip)); buffer_putnlflush(buffer_1); #endif #if 0 char buf[100]; int i; printf("%d\n",i=fmt_pad(buf,"fnord",5,7,10)); buf[i]=0; puts(buf); #endif #if 0 char ip[16]; char buf[32]; printf("%d (expect 2)\n",scan_ip6("::",ip)); printf("%d (expect 3)\n",scan_ip6("::1",ip)); printf("%d (expect 16)\n",scan_ip6("fec0:0:0:ffff::1/0",ip)); printf("%.*s\n",fmt_ip6(buf,ip),buf); #endif #if 0 static stralloc s,t; stralloc_copys(&s,"fnord"); stralloc_copys(&t,"abc"); printf("%d\n",stralloc_diff(&s,&t)); stralloc_copys(&t,"fnor"); printf("%d\n",stralloc_diff(&s,&t)); stralloc_copys(&t,"fnord"); printf("%d\n",stralloc_diff(&s,&t)); stralloc_copys(&t,"fnordh"); printf("%d\n",stralloc_diff(&s,&t)); stralloc_copys(&t,"hausen"); printf("%d\n",stralloc_diff(&s,&t)); #endif #if 0 static stralloc s; stralloc_copys(&s,"fnord"); printf("%d\n",stralloc_diffs(&s,"abc")); printf("%d\n",stralloc_diffs(&s,"fnor")); printf("%d\n",stralloc_diffs(&s,"fnord")); printf("%d\n",stralloc_diffs(&s,"fnordh")); printf("%d\n",stralloc_diffs(&s,"hausen")); #endif #if 0 printf("%d\n",case_starts("fnordhausen","FnOrD")); printf("%d\n",case_starts("fnordhausen","blah")); #endif #if 0 char buf[]="FnOrD"; case_lowers(buf); puts(buf); #endif #if 0 char buf[100]="foo bar baz"; printf("%d (expect 7)\n",byte_rchr(buf,11,' ')); #endif #if 0 unsigned long size; char* buf=mmap_read(argv[1],&size); if (buf) { unsigned int x=fmt_yenc(0,buf,size); unsigned int y; char* tmp=malloc(x+1); y=fmt_yenc(tmp,buf,size); write(1,tmp,x); } #endif #if 0 char buf[100]; char buf2[100]; unsigned int len,len2; buf[fmt_yenc(buf,"http://localhost/~fefe",22)]=0; buffer_puts(buffer_1,buf); buffer_putsflush(buffer_1,"\n"); if ((buf[len2=scan_yenc(buf,buf2,&len)])!='\n') { buffer_putsflush(buffer_2,"parse error!\n"); return 1; } buffer_put(buffer_1,buf2,len2); buffer_putsflush(buffer_1,"\n"); return 0; #endif #if 0 char buf[100]; char buf2[100]; unsigned int len,len2; buf[fmt_base64(buf,"foo:bar",7)]=0; buffer_puts(buffer_1,buf); buffer_putsflush(buffer_1,"\n"); if ((buf[len2=scan_base64(buf,buf2,&len)])!=0) { buffer_putsflush(buffer_2,"parse error!\n"); return 1; } buffer_put(buffer_1,buf2,len2); buffer_putsflush(buffer_1,"\n"); return 0; #endif #if 0 unsigned long size; char* buf=mmap_read(argv[1],&size); if (buf) { unsigned int x=fmt_uuencoded(0,buf,size); unsigned int y; char* tmp=malloc(x+1); y=fmt_uuencoded(tmp,buf,size); write(1,tmp,x); } #endif #if 0 char buf[]="00000000000000000000000000000001"; char ip[16]; if (scan_ip6_flat(buf,ip) != str_len(buf)) buffer_putsflush(buffer_2,"parse error!\n"); #endif #if 0 int fd=open_read("t.c"); buffer b; char buf[1024]; char line[20]; int i; buffer_init(&b,read,fd,buf,1024); i=buffer_getline(&b,line,19); buffer_puts(buffer_1,"getline returned "); buffer_putulong(buffer_1,i); buffer_puts(buffer_1,"\n"); buffer_puts(buffer_1,line); buffer_flush(buffer_1); #endif #if 0 buffer_putulong(buffer_1,23); // buffer_putspace(buffer_1); buffer_putsflush(buffer_1,"\n"); // buffer_flush(buffer_1); #endif #if 0 long a,b,c; char buf[4096]; char buf2[4096]; memcpy(buf,buf2,4096); byte_copy(buf,4096,buf2); rdtscl(a); memcpy(buf,buf2,4096); rdtscl(b); byte_copy(buf,4096,buf2); rdtscl(c); printf("memcpy: %d - byte_copy: %d\n",b-a,c-b); #endif #if 0 char ip[16]; int i; if ((i=scan_ip6(argv[1],ip))) { char buf[128]; buf[fmt_ip6(buf,ip)]=0; puts(buf); } #endif #if 0 char buf[100]; strcpy(buf,"foobarbaz"); buf[fmt_fill(buf,3,5,100)]=0; printf("\"%s\"\n",buf); #endif #if 0 unsigned long len; char *c=mmap_read("/etc/passwd",&len); printf("got map %p of len %lu\n",c,len); #endif #if 0 char c; printf("%d\n",buffer_getc(buffer_0,&c)); printf("%c\n",c); #endif #if 0 char buf[100]="01234567890123456789012345678901234567890123456789"; long a,b,c; #endif #if 0 buf[ip4_fmt(buf,ip4loopback)]=0; buffer_puts(buffer_1small,buf); buffer_flush(buffer_1small); #endif #if 0 buf[0]=0; buf[fmt_8long(buf,0)]=0; puts(buf); rdtscl(a); c=str_len(buf); rdtscl(b); /*byte_zero_djb(buf,j); */ // printf("\n%lu %d\n",b-a,c); #endif #if 0 buffer_puts(buffer_1small,"hello, world\n"); buffer_flush(buffer_1small); #endif #if 0 int s=socket_tcp4(); char ip[4]={127,0,0,1}; int t=socket_connect4(s,ip,80); #endif #if 0 char buf[100]="foo bar baz fnord "; char buf2[100]="foo braz fnord"; long a,b,c; long i=0,j=0,k=0; double d; uint32 l,m,n; stralloc sa={0}; stralloc_copys(&sa,"fnord"); stralloc_catlong0(&sa,-23,5); stralloc_append(&sa,"\n"); printf("%d %d\n",str_equal("fnord","fnord1"),str_equal("fnord1","fnord")); write(1,sa.s,sa.len); printf("%d %d\n",stralloc_starts(&sa,"fnord"),stralloc_starts(&sa,"fnord\na")); l=0xdeadbeef; uint32_pack_big((char*)&m,l); uint32_unpack_big((char*)&m,&n); printf("%x %x %x\n",l,m,n); rdtscl(a); /* i=scan_double("3.1415",&d); */ rdtscl(b); /*byte_zero_djb(buf,j); */ rdtscl(c); printf("%lu %lu\n",b-a,c-b); #endif #if 0 size_t size; char* buf=mmap_read(argv[1],&size); if (buf) { unsigned int x=fmt_urlencoded2(0,buf,size,"x"); unsigned int y; char* tmp=malloc(x+1); y=fmt_urlencoded2(tmp,buf,size,"x"); write(1,tmp,x); } #endif #if 0 printf("%d %d\n",strcmp("foo","bar"),str_diff("foo","bar")); printf("%d %d\n",strcmp("foo","üar"),str_diff("foo","üar")); #endif #if 0 { int16 a; int32 b; int64 c; assert(imult16(4,10000,&a)==0); assert(imult16(-4,10000,&a)==0); assert(imult16(5,10,&a)==1 && a==50); assert(imult16(-3,10000,&a)==1 && a==-30000); assert(imult32(0x40000000,2,&b)==0); assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe); assert(imult64(0x4000000000000000ll,2,&c)==0); assert(imult64(0x3fffffffffffffffll,2,&c)==1 && c==0x7ffffffffffffffell); } #endif #if 0 stralloc a; printf("%d\n",stralloc_copym(&a,"fnord",", ","foo")); #endif return 0; }
static int thisudp (struct dns_transmit *d) { const char *ip = NULL; socketfree (d); mergefree (d); while (d->udploop < 4) { for (; d->curserver < 16; ++d->curserver) { ip = d->servers + 4 * d->curserver; if (byte_diff (ip, 4, "\0\0\0\0")) { if (merge_enable && try_merge (d)) { if (merge_logger) merge_logger (ip, d->qtype, d->query + 14); return 0; } d->query[2] = dns_random (256); d->query[3] = dns_random (256); d->s1 = 1 + socket_udp (); if (!d->s1) { dns_transmit_free (d); return -1; } if (randombind (d) == -1) { dns_transmit_free (d); return -1; } if (socket_connect4 (d->s1 - 1, ip, 53) == 0) { if (send (d->s1 - 1, d->query + 2, d->querylen - 2, 0) == d->querylen - 2) { struct taia now; taia_now (&now); taia_uint (&d->deadline, timeouts[d->udploop]); taia_add (&d->deadline, &d->deadline, &now); d->tcpstate = 0; if (merge_enable) register_inprogress (d); return 0; } } socketfree (d); } } ++d->udploop; d->curserver = 0; } dns_transmit_free (d); return -1; }
int main(int argc, char* argv[]) { int s; uint16 port = 2342; uint32 scope_id = 0; char ip[16]; char buf[8192]; int i; struct pollfd p[2]; if(argc < 3) { usage: __write2("usage: client ip port\n\nexample: dump -0 -f - | client 10.0.0.149 8000\n"); return 0; } if(argv[1][i = scan_ip6if(argv[1], ip, &scope_id)]) { __write2("client: error: invalid ip address!\n"); goto usage; } if(argv[2][scan_ushort(argv[2], &port)]) { __write2("client: error: invalid port number!\n"); goto usage; } if(byte_equal(ip, 12, V4mappedprefix)) { s = socket_tcp4b(); if(s == -1) panic("client: error: socket() failed"); if(socket_connect4(s, ip + 12, port) == -1) panic("client: error: connect() failed"); } else { s = socket_tcp6b(); if(s == -1) panic("client: error: socket() failed"); if(socket_connect6(s, ip, port, scope_id) == -1) panic("client: error: connect() failed"); } ndelay_off(s); p[0].fd = 0; p[0].events = POLLIN; p[1].fd = s; p[1].events = POLLIN; while(poll(p, 2, 5000)) { if(p[0].revents) { int j; if(p[0].revents & POLLERR) panic("client: error: poll() signals POLLERR\n"); i = read(0, buf, sizeof(buf)); if(i == 0) { shutdown(s, SHUT_WR); blockingcopy(s, 1); } for(j = 0; j < i;) { int k = write(s, buf + j, i - j); if(k == -1) panic("client: error: write() failed"); if(k == 0) panic("client: error: short write!\n"); j += k; } } if(p[1].revents) { int j; if(p[1].revents & POLLERR) panic("client: error: poll() signals POLLERR\n"); i = read(s, buf, sizeof(buf)); if(i == 0) { shutdown(1, SHUT_WR); blockingcopy(0, s); } for(j = 0; j < i;) { int k = write(1, buf + j, i - j); if(k == -1) panic("client: error: write() failed"); if(k == 0) panic("client: error: short write!\n"); j += k; } } } return 0; }
int cli_main(int argc, char* argv[]) { const char* server_name = 0; const char* env; msg_debug_init(); encr_start(); prep_sender(); service = argv[0]; if (argc > 1 && argv[1][0] != '-' && argv[1][0] != '+') { server_name = argv[1]; ++argv; --argc; } if (argc > 1) load_patterns(argv + 1); if (server_name == 0 && (server_name = getenv("SERVER")) == 0) die1(1, "Server address not named on command line nor in $SERVER"); if (!resolve_ipv4name(server_name, &ip)) die3(1, "Could not resolve '", server_name, "'"); brandom_init(); if ((env = getenv("KEYDIR")) != 0) keydir = env; load_keys(server_name); if ((env = getenv("PORT")) != 0) port = strtoul(env, 0, 10); if (port == 0) port = 11014; if ((sock = socket_udp()) == -1) die1sys(1, "Could not create UDP socket"); if (!socket_connect4(sock, &ip, port)) die1sys(1, "Could not bind socket"); if (!str_ready(&packet, 65535) || !str_ready(&rpacket, 4+4+8+256*5)) die1(1, "Out of memory"); getenvu("ACK_TIMEOUT", &ack_timeout); getenvu("CID_TIMEOUT", &cid_timeout); getenvu("RETRANSMITS", &retransmits); getenvu("READWAIT", &readwait); getenvu("STARTLINES", &startlines); if (getenv("EXITONEOF") != 0) exitoneof = 1; if (getenv("NOFILES") == 0 && getenv("NOFILE") == 0) buffer = buffer_file_init(); else buffer = buffer_nofile_init(); sig_all_catch(sigfn); exitasap = 0; mainloop(); return 0; }