//----------------------------------------------------------------- // udpserver.s // // This is the code we wrote during class to reimplement // our 'udpserver.cpp' application, but which didn't yet // work and which was not yet completed anyway. Can you // find the 'bug'? // // written on: 15 APR 2009 //----------------------------------------------------------------- .equ STDOUT, 1 .equ sys_WRITE, 1 .equ sys_SOCKET, 41 .equ sys_BIND, 49 .equ sys_SENDTO, 44 .equ sys_RECVFROM, 45 .equ sys_GETSOCKNAME, 51 .equ sys_EXIT, 60 .equ BUFSIZ, 1500 .equ AF_INET, 2 # .equ SOCK_DGRAM, 1 # .equ IPPROTO_UDP, 6 .equ SOCK_DGRAM, 2 # bug correction by Lihui Shen .equ IPPROTO_UDP, 17 # bug correction by Lihui Shen .section .data buf: .space BUFSIZ # place to store any incoming datagram sock: .quad -1 # place to store our socket-handle sa: .short AF_INET port: .short 0 # big-endian order needed here ipv4: .byte 0, 0, 0, 0 # big-endian interger for in_addr .zero 8 # padd with 8 zero-bytes nlen: .quad 16 # for holding the socket_address length pa: .space 16 plen: .quad 16 mlen: .quad 0 portmsg: .ascii "Server is listening to UDP port " portbuf: .ascii " \n" portlen: .quad . - portmsg .section .text _start: # create a socket for receiving and sending datagrams mov $sys_SOCKET, %rax mov $AF_INET, %rdi mov $SOCK_DGRAM, %rsi mov $IPPROTO_UDP, %rdx syscall mov %rax, sock # bind our 'sa' to our socket mov $sys_BIND, %rax mov sock, %rdi lea sa, %rsi mov $16, %rdx syscall # get port and ip for display mov $sys_GETSOCKNAME, %rax mov sock, %rdi lea sa, %rsi lea nlen, %rdx syscall # show the port-number this server is listening to mov sa+2, %ax rol $8, %ax movzx %ax, %rax lea portbuf, %rdi call rax2hex mov $sys_WRITE, %rax mov $STDOUT, %rdi lea portmsg, %rsi mov portlen, %rdx syscall # receive a datagram from a clienty mov $sys_RECVFROM, %rax mov sock, %rdi lea buf, %rsi mov $BUFSIZ, %rdx xor %rbx, %rbx lea pa, %r8 lea plen, %r9 syscall mov %rax, mlen # show the received message mov $sys_WRITE, %rax mov $STDOUT, %rdi lea buf, %rsi mov mlen, %rdx syscall # send the received message back to the client mov $sys_SENDTO, %rax mov sock, %rdi lea buf, %rsi mov mlen, %rdx xor %rbx, %rbx lea pa, %r8 mov plen, %r9 syscall # terminate (for now) mov $sys_EXIT, %rax xor %rdi, %rdi syscall hex: .ascii "0123456789ABCDEF" rax2hex: push %rax push %rbx push %rcx push %rdx push %rdi mov $10, %rbx xor %rcx, %rcx nxdiv: xor %rdx, %rdx div %rbx push %rdx inc %rcx or %rax, %rax jnz nxdiv nxdgt: pop %rdx add $'0', %dl mov %dl, (%rdi) inc %rdi loop nxdgt pop %rdi pop %rdx pop %rcx pop %rbx pop %rax ret .global _start .end