static PyObject * ethernet_aton(PyObject *self, PyObject *args) { char *nstr; char *astr; int asize; eth_addr_t eth_a; int rval; struct intf_entry *entry = NULL; rval = PyArg_ParseTuple(args, "s#:eth_aton", &astr, &asize); if (!rval) return NULL; /* printf("Got addresses %s, %s\n", astr, bstr); */ rval = eth_aton(astr, ð_a); if (rval<0) myerror("Could not convert address"); return Py_BuildValue("s#", (char*) ð_a, ETH_ADDR_LEN); }
int stream_addr_aton(stream_addr_t* dst, const char* src, enum AddressType type, int flags){ char buf[48]; /* larger than max, just in case user provides large */ strncpy(buf, src, 48); /* input, will bail out later on bad data. */ memset(dst->buffer, 0, 26); dst->_type = htons(type); dst->_flags = htons(flags); switch( type ){ case STREAM_ADDR_GUESS: { char* delim = strstr(buf, "://"); /* check if prefix is set */ if ( delim ){ *delim = 0; const char* prefix = buf; const char* addr = delim + 3; if ( strcasecmp("tcp", prefix) == 0 ){ return stream_addr_aton(dst, addr, STREAM_ADDR_TCP, flags); } else if ( strcasecmp("udp", prefix) == 0 ){ return stream_addr_aton(dst, addr, STREAM_ADDR_UDP, flags); } else if ( strcasecmp("eth", prefix) == 0 ){ return stream_addr_aton(dst, addr, STREAM_ADDR_ETHERNET, flags); } else if ( strcasecmp("file", prefix) == 0 ){ return stream_addr_aton(dst, src+7, STREAM_ADDR_CAPFILE, flags | STREAM_ADDR_LOCAL); } return EINVAL; } /* try ethernet */ if ( stream_addr_aton(dst, src, STREAM_ADDR_ETHERNET, flags) == 0 ){ return 0; } /* last option: parse as local filename */ return stream_addr_aton(dst, src, STREAM_ADDR_CAPFILE, flags | STREAM_ADDR_LOCAL); } case STREAM_ADDR_TCP: // TCP case STREAM_ADDR_UDP: // UDP // DESTADDR is ipaddress:port { char* ip = buf; strncpy(buf, src, 48); dst->in_port = htons(0x0810); /* default port */ char* separator = strchr(buf, ':'); if( separator ) { *separator = 0; dst->in_port = htons(atoi(separator+1)); } dst->in_addr.s_addr = inet_addr(ip); if ( dst->in_addr.s_addr == INADDR_NONE ){ return EINVAL; } } break; case STREAM_ADDR_ETHERNET: // Ethernet homogenize_eth_addr(buf); if ( !eth_aton(&dst->ether_addr, buf) ){ return EINVAL; } break; case STREAM_ADDR_CAPFILE: // File if ( flags & STREAM_ADDR_LOCAL ){ dst->local_filename = src; } else { strncpy(dst->filename, src, 22); dst->filename[21] = 0; /* force null-terminator */ } break; } return 0; }