void conf_ifmetrics(FILE *output, int ifs, struct if_data if_data, char *ifname) { char tmpa[IPSIZ], tmpb[IPSIZ], tmpc[TMPSIZ]; /* * Various metrics valid for non-bridge interfaces */ if (phys_status(ifs, ifname, tmpa, tmpb, IPSIZ, IPSIZ) > 0) /* future os may use this for more than tunnel? */ fprintf(output, " tunnel %s %s\n", tmpa, tmpb); /* * print interface mtu, metric * * ignore interfaces named "pfsync" since their mtu * is dynamic and controlled by the kernel */ if (!MIN_ARG(ifname, "pfsync") && (if_mtu != default_mtu(ifname) && default_mtu(ifname) != MTU_IGNORE)) fprintf(output, " mtu %u\n", if_mtu); if (if_metric) fprintf(output, " metric %u\n", if_metric); if (get_nwinfo(ifname, tmpc, TMPSIZ, NWID) != 0) { fprintf(output, " nwid %s\n", tmpc); if (get_nwinfo(ifname, tmpc, TMPSIZ, NWKEY) != 0) fprintf(output, " nwkey %s\n", tmpc); if (get_nwinfo(ifname, tmpc, TMPSIZ, TXPOWER) != 0) fprintf(output, " txpower %s\n", tmpc); if (get_nwinfo(ifname, tmpc, TMPSIZ, POWERSAVE) != 0) fprintf(output, " powersave %s\n", tmpc); } }
void conf_ifmetrics(FILE *output, int ifs, struct if_data if_data, char *ifname) { int vnetid; int dstport; char tmpa[IPSIZ], tmpb[IPSIZ], tmpc[TMPSIZ]; struct ifreq ifrpriority; /* * Various metrics for non-bridge interfaces */ if ((dstport= phys_status(ifs, ifname, tmpa, tmpb, IPSIZ, IPSIZ)) >= 0) { int physrt, physttl; fprintf(output, " tunnel %s %s", tmpa, tmpb); if (dstport > 0) fprintf(output, ":%i", dstport); if (((physrt = conf_physrtable(ifs, ifname)) != 0)) fprintf(output, " rdomain %i", physrt); if (((physttl = conf_physttl(ifs, ifname)) != 0)) fprintf(output, " ttl %i", physttl); fprintf(output, "\n"); } if (((vnetid = conf_vnetid(ifs, ifname)) != 0)) fprintf(output, " vnetid %i\n", vnetid); /* * print interface mtu, metric * * ignore interfaces named "pfsync" since their mtu * is dynamic and controlled by the kernel */ if (!MIN_ARG(ifname, "pfsync") && (if_data.ifi_mtu != default_mtu(ifname) && default_mtu(ifname) != MTU_IGNORE) && if_data.ifi_mtu != 0) fprintf(output, " mtu %u\n", if_data.ifi_mtu); if (if_data.ifi_metric) fprintf(output, " metric %u\n", if_data.ifi_metric); strlcpy(ifrpriority.ifr_name, ifname, IFNAMSIZ); if (ioctl(ifs, SIOCGIFPRIORITY, (caddr_t)&ifrpriority) == 0) if(ifrpriority.ifr_metric) fprintf(output, " priority %u\n", ifrpriority.ifr_metric); if (get_nwinfo(ifname, tmpc, TMPSIZ, NWID) != 0) { fprintf(output, " nwid %s\n", tmpc); if (get_nwinfo(ifname, tmpc, TMPSIZ, NWKEY) != 0) fprintf(output, " nwkey %s\n", tmpc); if (get_nwinfo(ifname, tmpc, TMPSIZ, TXPOWER) != 0) fprintf(output, " txpower %s\n", tmpc); if (get_nwinfo(ifname, tmpc, TMPSIZ, POWERSAVE) != 0) fprintf(output, " powersave %s\n", tmpc); } }
int priceSide(struct order * side,unsigned int target){ unsigned int total=0; unsigned int inc; float price=0.0; while(side->next){ inc = MIN_ARG(target-total,side->size); price += inc*(side->price); total += inc; if(total>=target) return price; side = side->next; } return 0.0; }
int resizeSide(struct order ** side,struct order * newOrder){ //fprintf(stdout,"In resize mode resizing order %s\n",newOrder->id); struct order * curr = side[0]; if(!curr) return 1; //fprintf(stdout,"There is at least one order on this side of the book\n"); //fprintf(stdout,"looking for a match to %s\n",newOrder->id); //fprintf(stdout,"comparing %s\n",curr->id); if(!strcmp(curr->id,newOrder->id)){ //fprintf(stdout,"Found a match!\n"); curr->size -= MIN_ARG(newOrder->size,curr->size); //fprintf(stdout,"New size of %s is %u units.\n",curr->id,curr->size); if(!(curr->size)){ //fprintf(stdout,"Freeing %s.\n",curr->id); side[0] = curr->next; freeOrder(curr); } return 0; } //fprintf(stdout,"%s did not match with %s\n",curr->id,newOrder->id); struct order * prev = curr; curr = curr->next; while(curr){ //fprintf(stdout,"New iteration, comparing to %s\n",curr->id); if(!strcmp(curr->id,newOrder->id)){ //fprintf(stdout,"Found a match!\n"); curr->size -= MIN_ARG(newOrder->size,curr->size); //fprintf(stdout,"New size of %s is %u units.\n",curr->id,curr->size); if(!(curr->size)){ //fprintf(stdout,"Freeing %s.\n",curr->id); prev->next = curr->next; freeOrder(curr); } return 0; } prev = curr; curr = curr->next; } return 1; }
int updatePricesSide(struct order ** side,float *old,unsigned int * target,unsigned int * tstamp,char c){ unsigned int volume=0,ivol,count=0; float ep = 0.0; struct order *curr = side[0]; while(curr){ if(volume<target[0]){ ivol = MIN_ARG(target[0]-volume,curr->size); volume += ivol; ep += ivol*(curr->price); } curr = curr->next; count++; } if(volume<target[0]){ volume = 0.0; ep = 0.0; } if(ep!=old[0]){ (ep!=0)?fprintf(stdout,"%u %c %.2f\n",tstamp[0],c,ep):fprintf(stdout,"%u %c NA\n",tstamp[0],c); old[0] = ep; } return count; }
int intsyncpeer(char *ifname, int ifs, int argc, char **argv) { struct ifreq ifr; struct pfsyncreq preq; struct addrinfo hints, *peerres; int set, ecode; if (NO_ARG(argv[0])) { set = 0; argc--; argv++; } else set = 1; argc--; argv++; if (!MIN_ARG(ifname, "pfsync")) { printf("%% syncpeer is only for pfsync devices\n"); return 0; } if ((!set && argc > 1) || (set && argc != 1)) { printf("%% syncpeer <IPv4 peer address>\n"); printf("%% no syncpeer [IPv4 peer address]\n"); return (0); } memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; if ((ecode = getaddrinfo(argv[0], NULL, &hints, &peerres)) != 0) { printf("%% error in parsing address string: %s\n", gai_strerror(ecode)); return 0; } if (peerres->ai_addr->sa_family != AF_INET) { printf("%% only IPv4 addresses supported for syncpeer\n"); freeaddrinfo(peerres); return 0; } if (set) preq.pfsyncr_syncpeer.s_addr = ((struct sockaddr_in *) peerres->ai_addr)->sin_addr.s_addr; else preq.pfsyncr_syncpeer.s_addr = 0; if (ioctl(ifs, SIOCSETPFSYNC, (caddr_t)&ifr) == -1) { if (errno == ENXIO) printf("%% peer device (syncdev) not yet configured\n"); else printf("%% intsyncpeer: SIOCSETPFSYNC: %s\n", strerror(errno)); } freeaddrinfo(peerres); return 0; }
int intsyncdev(char *ifname, int ifs, int argc, char **argv) { struct ifreq ifr; struct pfsyncreq preq; int set; if (NO_ARG(argv[0])) { set = 0; argc--; argv++; } else set = 1; argc--; argv++; if ((!set && argc > 1) || (set && argc != 1)) { printf("%% syncdev <if>\n"); printf("%% no syncdev [if]\n"); return (0); } if (!MIN_ARG(ifname, "pfsync")) { printf("%% syncdev is only for pfsync devices\n"); return 0; } bzero((char *) &preq, sizeof(struct pfsyncreq)); ifr.ifr_data = (caddr_t) & preq; strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); if (ioctl(ifs, SIOCGETPFSYNC, (caddr_t) & ifr) == -1) { printf("%% intsyncdev: SIOCGETPFSYNC: %s\n", strerror(errno)); return (0); } if (argv[0]) if (!is_valid_ifname(argv[0])) { printf("%% Interface not found: %s\n", argv[0]); return (0); } if (set) { strlcpy(preq.pfsyncr_syncdev, argv[0], sizeof(preq.pfsyncr_syncdev)); set_ifflag(ifs, ifname, IFF_UP); } else bzero((char *) &preq.pfsyncr_syncdev, sizeof(preq.pfsyncr_syncdev)); if (ioctl(ifs, SIOCSETPFSYNC, (caddr_t) & ifr) == -1) { if (errno == ENOBUFS) printf("%% Invalid synchronization interface: %s\n", argv[0]); else printf("%% intsyncdev: SIOCSETPFSYNC: %s\n", strerror(errno)); } return (0); }
/* * mangled ieee80211_status() */ int get_nwinfo(char *ifname, char *str, int str_len, int type) { char tmp[128]; int ifs, len, i, nwkey_verbose; ifs = socket(AF_INET, SOCK_DGRAM, 0); if (ifs < 0) { printf("%% get_nwinfo: socket: %s\n", strerror(errno)); return(NULL); } memset(str, 0, str_len); switch(type) { case NWID: { struct ieee80211_nwid nwid; struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); ifr.ifr_data = (caddr_t)&nwid; (void) strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); if (ioctl(ifs, SIOCG80211NWID, (caddr_t) & ifr) == 0) { /* nwid.i_nwid is not NUL terminated. */ len = nwid.i_len; if (len > IEEE80211_NWID_LEN) len = IEEE80211_NWID_LEN; make_string(str, str_len, nwid.i_nwid, len); } } break; case TXPOWER: { struct ieee80211_txpower txpower; memset(&txpower, 0, sizeof(txpower)); (void) strlcpy(txpower.i_name, ifname, sizeof(txpower.i_name)); if (ioctl(ifs, SIOCG80211TXPOWER, (caddr_t) &txpower) == 0) { /* XXX FIXED is always set? For now, check for == 100, txpower is totally broken in the kernel anyways */ if (txpower.i_mode == IEEE80211_TXPOWER_MODE_FIXED && txpower.i_val != 100) snprintf(str, str_len, "%d", txpower.i_val); } else { if (errno != EINVAL) { printf("%% get_nwinfo: SIOCG80211TXPOWER: %s\n", strerror(errno)); } } } break; case POWERSAVE: { struct ieee80211_power power; memset(&power, 0, sizeof(power)); strlcpy(power.i_name, ifname, sizeof(power.i_name)); if (ioctl(ifs, SIOCG80211POWER, &power) == 0) { if (power.i_enabled && power.i_maxsleep != DEFAULT_POWERSAVE) snprintf(str, str_len, "%d", power.i_maxsleep); else if (power.i_enabled) memset(&str, '\0', str_len); /* "powersave " */ } else { printf("%% get_nwinfo: SIOCG80211POWER: %s\n", strerror(errno)); } } break; case BSSID: { struct ieee80211_bssid bssid; struct ether_addr ea; u_int8_t zero_bssid[IEEE80211_ADDR_LEN]; memset(&zero_bssid, 0, sizeof(zero_bssid)); strlcpy(bssid.i_name, ifname, sizeof(bssid.i_name)); if (ioctl(ifs, SIOCG80211BSSID, &bssid) == 0) { if (memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) { memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet)); snprintf(str, str_len, "%s", ether_ntoa(&ea)); } } else { printf("%% get_nwinfo: SIOCG80211BSSID: %s\n", strerror(errno)); } } break; case NWKEY: { struct ieee80211_nwkey nwkey; u_int8_t keybuf[IEEE80211_WEP_NKID][16]; memset(&nwkey, 0, sizeof(nwkey)); (void) strlcpy(nwkey.i_name, ifname, sizeof(nwkey.i_name)); if (ioctl(ifs, SIOCG80211NWKEY, (caddr_t) & nwkey) == 0 && nwkey.i_wepon > 0) { /* try to retrieve WEP keys */ for (i = 0; i < IEEE80211_WEP_NKID; i++) { nwkey.i_key[i].i_keydat = keybuf[i]; nwkey.i_key[i].i_keylen = sizeof(keybuf[i]); } if (ioctl(ifs, SIOCG80211NWKEY, (caddr_t) & nwkey) == -1) { strlcat(str, "*****", str_len); } else { nwkey_verbose = 0; /* * check to see non default key or multiple keys * defined */ if (nwkey.i_defkid != 1) { nwkey_verbose = 1; } else { for (i = 1; i < IEEE80211_WEP_NKID; i++) { if (nwkey.i_key[i].i_keylen != 0) { nwkey_verbose = 1; break; } } } /* check extra ambiguity with keywords */ if (!nwkey_verbose) { if (nwkey.i_key[0].i_keylen >= 2 && isdigit(nwkey.i_key[0].i_keydat[0]) && nwkey.i_key[0].i_keydat[1] == ':') nwkey_verbose = 1; else if (nwkey.i_key[0].i_keylen >= 7 && MIN_ARG( nwkey.i_key[0].i_keydat, "persist")) nwkey_verbose = 1; } if (nwkey_verbose) { snprintf(tmp, sizeof(tmp), "%d:", nwkey.i_defkid); strlcat(str, tmp, str_len); } for (i = 0; i < IEEE80211_WEP_NKID; i++) { if (i > 0) strlcat(str, ",", str_len); if (nwkey.i_key[i].i_keylen < 0) { strlcat(str, "persist", str_len); } else { /* * XXX - sanity check * nwkey.i_key[i].i_keylen */ make_string(str, str_len, nwkey.i_key[i].i_keydat, nwkey.i_key[i].i_keylen); } if (!nwkey_verbose) break; } } } } break; } /* switch {} */ close(ifs); return(strlen(str)); }
int addNewOrder(struct book * orderBook, struct order * newOrder){ orderBook->oldclock = orderBook->clock; orderBook->clock = newOrder->tstamp; if(newOrder->type=='R'){ resizeSide(&(orderBook->asks),newOrder); resizeSide(&(orderBook->bids),newOrder); freeOrder(newOrder); //fprintf(stdout,"Pointer to new order freed.\n"); return 0; } struct order ** side = ((newOrder->type)=='S')?(&(orderBook->asks)):(&(orderBook->bids)); struct order ** opposingSide = (!((newOrder->type)=='S'))?(&(orderBook->asks)):(&(orderBook->bids)); int coef = ((newOrder->type)=='S')?-1:1; while(opposingSide[0] && (coef*((opposingSide[0])->price)<=coef*(newOrder->price))){ // The new order can be at least partially filled //fprintf(stdout,"There is open interest in the opposing side of the book. Matching.\n"); //fprintf(stdout,"Order %s is favourable to match %s. Price is %f.\n",(opposingSide[0])->id,newOrder->id,(opposingSide[0])->price); unsigned int tradeSize; tradeSize = MIN_ARG((newOrder->size),((opposingSide[0])->size)); //tradeSize = (newOrder->size < (opposingSide[0])->size)?(newOrder->size):(opposingSide[0]->size); //fprintf(stdout,"Matching offers %s and %s. Reducing both by %u units.\n",opposingSide[0]->id,newOrder->id,tradeSize); //printOrder(stdout,opposingSide[0]); //printOrder(stdout,newOrder); orderBook->nTrades += tradeSize; newOrder->size -= tradeSize; opposingSide[0]->size -= tradeSize; //printOrder(stdout,opposingSide[0]); //printOrder(stdout,newOrder); if(!((opposingSide[0])->size)){ //fprintf(stdout,"Order %s filled.\n",(opposingSide[0])->id); newOrder->next = (opposingSide[0])->next; opposingSide[0] = newOrder->next; newOrder->next=0; } if(!(newOrder->size)){ //fprintf(stdout,"Order %s filled.\n",newOrder->id); // the order was filled and will be forgotten freeOrder(newOrder); return 0; } } // The new order was not filled if(!(side[0])){ side[0] = newOrder; newOrder->next=0; return 0; } if(coef*(side[0]->price)<coef*(newOrder->price)){ newOrder->next=side[0]; side[0]=newOrder; return 0; } // Find the best offer in the order book that is still // worse than the new order struct order * worstBetter = side[0]; //fprintf(stdout,"Adding the non-filled part of the order into the order book.\n"); while(worstBetter){ if (!(worstBetter->next)){ worstBetter->next = newOrder; return 0; } if(coef*(worstBetter->next->price)<coef*(newOrder->price)){ newOrder->next = worstBetter->next; worstBetter->next = newOrder; return 0; } worstBetter = worstBetter->next; } return 1; }