By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

A starter needs help with struct.pack() and "".join()

P: 7
Basically i'm trying to form a mac packet.

1. a simple mac header is generated using struct.pack('!HH', desti_mac, src_mac)

2. the packet is created using ''.join(header, .....)

Could anyone explain what pack and join really do, and suggest a way of retrieving header part(for retrieving desti_mac and src_mac purpose) from received packet(demodulated, let's assume it's same as the string created by join)

Also, with a bit of luck, does anyone know where the ip interface/socket of GNU radio is? :)

Another one. I've tolde from all the place that list.delete_head() removes the first element. but no one would tell me what this function returns, does it return the removed first element?

Thanks a lot
May 23 '07 #1
Share this Question
Share on Google+
9 Replies


Motoma
Expert 2.5K+
P: 3,235
Basically i'm trying to form a mac packet.

1. a simple mac header is generated using struct.pack('!HH', desti_mac, src_mac)

2. the packet is created using ''.join(header, .....)

Could anyone explain what pack and join really do, and suggest a way of retrieving header part(for retrieving desti_mac and src_mac purpose) from received packet(demodulated, let's assume it's same as the string created by join)

Also, with a bit of luck, does anyone know where the ip interface/socket of GNU radio is? :)

Another one. I've tolde from all the place that list.delete_head() removes the first element. but no one would tell me what this function returns, does it return the removed first element?

Thanks a lot
There are a lot of very specific questions. Care to elaborate on what you are trying to do/accomplish?
May 23 '07 #2

P: 7
There are a lot of very specific questions. Care to elaborate on what you are trying to do/accomplish?


I'm just wandering after several 'struct.pack'ed objects are 'join'ed together, how can i retrieve the first 'pack' from the string generated by 'join'

I'm currently working on modifying GNU radio. the default packet format doesn't have mac header, so i'm trying to add a simple mac header(just include desti and src mac addresses) to it, it's seems fine to add mac addresses when sending packet, but i can't find a way to retrieve those mac addresses on receiving a packet

Also, in order to involve ip address when assigning mac address, i need to find a ip interface in GNU radio so i can retrieve ip address(which is assigned by e.g. 'ifconfig gr0 192.168.200.1'). at least i should know where GNU radio stores this ip address
May 23 '07 #3

Motoma
Expert 2.5K+
P: 3,235
A quick synopsis on pack and unpack.

Try using unpack("!HHs", packetstring)

Also, with a bit of luck, does anyone know where the ip interface/socket of GNU radio is? :)
I'm not quite sure what you are asking...Could you rephrase the question?

delete_head returns nothing.
May 23 '07 #4

Motoma
Expert 2.5K+
P: 3,235
I'm just wandering after several 'struct.pack'ed objects are 'join'ed together, how can i retrieve the first 'pack' from the string generated by 'join'

I'm currently working on modifying GNU radio. the default packet format doesn't have mac header, so i'm trying to add a simple mac header(just include desti and src mac addresses) to it, it's seems fine to add mac addresses when sending packet, but i can't find a way to retrieve those mac addresses on receiving a packet

Also, in order to involve ip address when assigning mac address, i need to find a ip interface in GNU radio so i can retrieve ip address(which is assigned by e.g. 'ifconfig gr0 192.168.200.1'). at least i should know where GNU radio stores this ip address

What part of GNU Radio are you modifying? What part of it uses IP? I am almost entirely unfamiliar with GNU Radio, as you can see. Have you tried posting to the GNU Radio boards? I am sure they would love to help with your modification/port/addon/plugin/mess.
May 23 '07 #5

P: 7
Thanks for your reply guys.

Let's not worry about the IP address for now.

There are two lines of code in GNU radio as below:

msg = self.rcvd_pktq.delete_head()
ok, payload = packet_utils.unmake_packet(msg.to_string())

rcvd_pktq seems like a msg queue held by physical layer. I'm wandering what 'msg' could be. is it the first packet of the entire queue?
May 23 '07 #6

P: 7
And for the pack problem. I constructed a mac header using the function below:

def make_mac_header(desti_mac, src_mac):
# Upper 12 bits is destination mac, lower 12 bits is source mac
val = ((desti_mac & 0xf) << 12) | (src_mac & 0x0fff)
#print "desti_mac =", desti_mac, " src_mac =", src_mac, " val=", val
return struct.pack('!HH', (desti_mac & 0xf) << 12, src_mac & 0xfff)

And later I generate a packet using:

pkt = ''.join((make_mac_header(desti_mac, src_mac), packed_preamble, packed_access_code, make_header(L, whitener_offset), (payload_with_crc), '\x55'))

As I understand, join will produce a string using whatever char in front of '.join()' as delimiter. Now suppose we received a pkt in string, it's sure that first part is gona be "make_mac_header()", but how can i split it out from the packet and retrieve desti_mac and src_mac?
May 23 '07 #7

Motoma
Expert 2.5K+
P: 3,235
Is rcvd_pktq a list, or is it another class? This is critical, as there may be a redefinition of the delete_head function. When in doubt RTFM.

It seems from this last question you did not look at my first response at all. Did my unpack solution not work?
May 24 '07 #8

P: 7
Is rcvd_pktq a list, or is it another class? This is critical, as there may be a redefinition of the delete_head function. When in doubt RTFM.

It seems from this last question you did not look at my first response at all. Did my unpack solution not work?
Sorry that i haven't say, unpack did work well if i do struct.pack('!HH', desti_mac, src_mac) in before.

I guess my problem is that i need to find the place where rcvd_pktq is defined, and where delete_head is defined. lots of code to look through :)

'case closed'

Thanks you guys, for ur patience
May 24 '07 #9

Motoma
Expert 2.5K+
P: 3,235
Sorry that i haven't say, unpack did work well if i do struct.pack('!HH', desti_mac, src_mac) in before.

I guess my problem is that i need to find the place where rcvd_pktq is defined, and where delete_head is defined. lots of code to look through :)

'case closed'

Thanks you guys, for ur patience
No problem. Perhaps you could post back the information that you find so we know how things turn out.

Come back anytime you have a question.
May 24 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.