thtp_t thtp_new(switch_t s, xht_t index) { thtp_t t; t = malloc(sizeof (struct thtp_struct)); memset(t,0,sizeof (struct thtp_struct)); if(!index) index = xht_new(13); t->index = index; xht_set(s->index,"thtp",t); return t; }
link_t link_new(mesh_t mesh, hashname_t id) { link_t link; if(!mesh || !id) return LOG("invalid args"); LOG("adding link %s",id->hashname); if(!(link = malloc(sizeof (struct link_struct)))) return (link_t)hashname_free(id); memset(link,0,sizeof (struct link_struct)); link->id = id; link->mesh = mesh; xht_set(mesh->index,id->hashname,link); // to size larger, app can xht_free(); link->channels = xht_new(BIGGER) at start itself link->channels = xht_new(5); // index of all channels link->index = xht_new(5); // index for active channels and extensions return link; }
mesh_t mesh_new(uint32_t prime) { mesh_t mesh; // make sure we've initialized if(e3x_init(NULL)) return LOG_ERROR("e3x init failed"); if(!(mesh = malloc(sizeof (struct mesh_struct)))) return NULL; memset(mesh, 0, sizeof(struct mesh_struct)); mesh->index = xht_new(prime?prime:MAXPRIME); if(!mesh->index) return mesh_free(mesh); LOG_INFO("mesh created version %d.%d.%d",TELEHASH_VERSION_MAJOR,TELEHASH_VERSION_MINOR,TELEHASH_VERSION_PATCH); return mesh; }
net_serial_t net_serial_new(mesh_t mesh, lob_t options) { net_serial_t net; unsigned int pipes; if(!(net = malloc(sizeof (struct net_serial_struct)))) return LOG("OOM"); memset(net,0,sizeof (struct net_serial_struct)); pipes = lob_get_uint(options,"pipes"); if(!pipes) pipes = 3; // hashtable for active pipes net->pipes = xht_new(pipes); // connect us to this mesh net->mesh = mesh; xht_set(mesh->index, "net_serial", net); return net; }
net_tcp4_t net_tcp4_new(mesh_t mesh, lob_t options) { int port, sock, pipes, opt = 1; net_tcp4_t net; struct sockaddr_in sa; socklen_t size = sizeof(struct sockaddr_in); port = lob_get_int(options,"port"); pipes = lob_get_int(options,"pipes"); if(!pipes) pipes = 11; // hashtable for active pipes // create a udp socket if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) ) < 0 ) return LOG("failed to create socket %s",strerror(errno)); memset(&sa,0,sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(port); sa.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sock, (struct sockaddr*)&sa, size) < 0) return LOG("bind failed %s",strerror(errno)); getsockname(sock, (struct sockaddr*)&sa, &size); if(listen(sock, 10) < 0) return LOG("listen failed %s",strerror(errno)); setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt , sizeof(int)); fcntl(sock, F_SETFL, O_NONBLOCK); if(!(net = malloc(sizeof (struct net_tcp4_struct)))) return LOG("OOM"); memset(net,0,sizeof (struct net_tcp4_struct)); net->server = sock; net->port = ntohs(sa.sin_port); net->pipes = xht_new(pipes); // connect us to this mesh net->mesh = mesh; xht_set(mesh->index, MUID, net); mesh_on_path(mesh, MUID, tcp4_path); // convenience net->path = lob_new(); lob_set(net->path,"type","tcp4"); lob_set(net->path,"ip","127.0.0.1"); lob_set_int(net->path,"port",net->port); return net; }
static int server(int count, const char *ip) { TestInstanceServer *inc; MDDIface_t iface; if (ip == NULL) { printf("IP is not specific\n"); return -1; } inc = malloc(sizeof(TestInstanceServer)); memset(inc, 0, sizeof(TestInstanceServer)); inc->mdnsd = mdnsd_new(1, 1000); inc->socket_ipv4 = msock_ipv4(); inc->publish_host_name = strdup("qazxswedc"); inc->publish_service_name = strdup("stest"); inc->publish_max_rinterval = 2; inc->publish_service_port = 9999; inc->publish_txt_xht = xht_new(23); inc->max_sn = count; inc->sn = 1; xht_store(inc->publish_txt_xht, KEY_NAME, strlen(KEY_NAME), (void *)VALUE_STABILITY_TEST, strlen(VALUE_STABILITY_TEST)); if (MDDNet_Inet_pton(MDDNet_INET, ip, &iface.addr) < 0) { printf("Failed to convert ip, please make sure ip address is correct\n"); return -1; } if (getIfindex(iface.addr.ip.ipv4, &iface.ifindex) < 0) { printf("Failed to find ifindex\n"); return -1; } if (add_to_multicast(inc, iface) < 0) { printf("Failed to add to multicast group\n"); return -1; } test_publish_on_interface(inc, iface); server_loop(inc); return 0; }
int main(int argc, char *argv[]) { mdnsd d; mdnsdr r; struct message m; unsigned long int ip; unsigned short int port; struct timeval *tv; int bsize, ssize = sizeof(struct sockaddr_in); unsigned char buf[MAX_PACKET_LEN]; struct sockaddr_in from, to; fd_set fds; int s; unsigned char *packet, hlocal[256], nlocal[256]; int len = 0; xht h; if(argc < 4) { printf("usage: mhttp 'unique name' 12.34.56.78 80 '/optionalpath'\n"); return; } ip = inet_addr(argv[2]); port = atoi(argv[3]); printf("Announcing .local site named '%s' to %s:%d and extra path '%s'\n",argv[1],inet_ntoa(ip),port,argv[4]); signal(SIGINT,done); signal(SIGHUP,done); signal(SIGQUIT,done); signal(SIGTERM,done); pipe(_zzz); _d = d = mdnsd_new(1,1000); if((s = msock()) == 0) { printf("can't create socket: %s\n",strerror(errno)); return 1; } sprintf(hlocal,"%s._http._tcp.local.",argv[1]); sprintf(nlocal,"http-%s.local.",argv[1]); r = mdnsd_shared(d,"_http._tcp.local.",QTYPE_PTR,120); mdnsd_set_host(d,r,hlocal); r = mdnsd_unique(d,hlocal,QTYPE_SRV,600,con,0); mdnsd_set_srv(d,r,0,0,port,nlocal); r = mdnsd_unique(d,nlocal,QTYPE_A,600,con,0); mdnsd_set_raw(d,r,(unsigned char *)&ip,4); r = mdnsd_unique(d,hlocal,16,600,con,0); h = xht_new(11); if(argc == 5 && argv[4] && strlen(argv[4]) > 0) xht_set(h,"path",argv[4]); packet = sd2txt(h, &len); xht_free(h); mdnsd_set_raw(d,r,packet,len); free(packet); while(1) { tv = mdnsd_sleep(d); FD_ZERO(&fds); FD_SET(_zzz[0],&fds); FD_SET(s,&fds); select(s+1,&fds,0,0,tv); // only used when we wake-up from a signal, shutting down if(FD_ISSET(_zzz[0],&fds)) read(_zzz[0],buf,MAX_PACKET_LEN); if(FD_ISSET(s,&fds)) { while((bsize = recvfrom(s,buf,MAX_PACKET_LEN,0,(struct sockaddr*)&from,&ssize)) > 0) { bzero(&m,sizeof(struct message)); message_parse(&m,buf); mdnsd_in(d,&m,(unsigned long int)from.sin_addr.s_addr,from.sin_port); } if(bsize < 0 && errno != EAGAIN) { printf("can't read from socket %d: %s\n",errno,strerror(errno)); return 1; } } while(mdnsd_out(d,&m,&ip,&port)) { bzero(&to, sizeof(to)); to.sin_family = AF_INET; to.sin_port = port; to.sin_addr.s_addr = ip; if(sendto(s,message_packet(&m),message_packet_len(&m),0,(struct sockaddr *)&to,sizeof(struct sockaddr_in)) != message_packet_len(&m)) { printf("can't write to socket: %s\n",strerror(errno)); return 1; } } if(_shutdown) break; } mdnsd_shutdown(d); mdnsd_free(d); return 0; }