static int relay_rcv_padi(struct session* ses, struct pppoe_packet *p_in, struct pppoe_packet **p_out){ char tag_buf[32]; struct pppoe_con *newpc = NULL; struct pppoe_tag *tag = (struct pppoe_tag *) tag_buf; tag->tag_type = PTT_RELAY_SID; tag->tag_len = htons(ETH_ALEN + sizeof(struct session *)); memcpy(tag->tag_data, p_in->addr.sll_addr, ETH_ALEN); memcpy(tag->tag_data + ETH_ALEN, &ses, sizeof(struct session *)); if(! p_in->tags[TAG_RELAY_SID] ){ copy_tag(p_in, tag); } poe_dbglog(ses, "Recv'd PADI: %P",p_in); poe_dbglog(ses, "Recv'd packet: %P",p_in); newpc = get_con( ntohs(tag->tag_len), tag->tag_data ); if(!newpc){ newpc = (struct pppoe_con *) malloc(sizeof(struct pppoe_con)); memset(newpc , 0, sizeof(struct pppoe_con)); newpc->id = pcid++; newpc->key_len = ntohs(p_in->tags[TAG_RELAY_SID]->tag_len); memcpy(newpc->key, p_in->tags[TAG_RELAY_SID]->tag_data, newpc->key_len); memcpy(newpc->client, p_in->addr.sll_addr, ETH_ALEN); memcpy(newpc->server, MAC_BCAST_ADDR, ETH_ALEN); store_con(newpc); } ++newpc->ref_count; memset(p_in->addr.sll_addr, 0xff, ETH_ALEN); p_in->addr.sll_ifindex = ses->remote.sll_ifindex; send_disc(ses, p_in); return 0; }
void server() { int confd; pthread_t tid; struct sockaddr_in servaddr, cliaddr; int len; char addrp[160]; char clientname[HOSTNAME_MAXLENGTH]; int clientname_len; char buf[1000]; if (pthread_create(&screen_read_tid, NULL, screen_read_thread, NULL) != 0) printf("thread!\n"); listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(APP_PORT); bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); if (listen(listenfd, CONNECT_MAXNUM) == -1) puts("listen failed"); puts("listening"); while (1) { confd = accept(listenfd, (struct sockaddr *)&cliaddr, &len); if (confd == -1) { puts("quit the application"); break; } inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, addrp, len); clientname_len = read(confd, clientname, HOSTNAME_MAXLENGTH); clientname[clientname_len] = '\0'; write(confd, gethostname(), gethostnamelen()); memset(buf, 0, sizeof(buf)); sprintf(buf, "%s joined the network\n", clientname); printf("%s", buf); sendout(buf, strlen(buf), -1); store_con(confd, clientname, clientname_len, addrp, strlen(addrp)); if (pthread_create(&tid, NULL, con_agent_server_thread, &confd) != 0) puts("thread creation failed\n"); } }
/************************************************************************* * * Register an ethernet address as a client of relaying services. * *************************************************************************/ int add_client(char *addr) { struct pppoe_con* pc = (struct pppoe_con*)malloc(sizeof(struct pppoe_con)); int ret; if(!pc) return -ENOMEM; memset(pc, 0 , sizeof(struct pppoe_con)); memcpy(pc->client,addr, ETH_ALEN); memcpy(pc->key, addr, ETH_ALEN); pc->key_len = ETH_ALEN; if( (ret=store_con(pc)) < 0 ){ free(pc); } return ret; }