How to connect to IRC server/ parse IRC MSGs / PING-PONG handling in C language (code provided)

你说的曾经没有我的故事 提交于 2019-12-04 22:41:29

So, a few issues I spot right away are:

  1. NICK is not properly terminated. It should be \r\n.
  2. USER is not terminated at all, it should end with \r\n.
  3. When you send your ping response, you have a hard coded size of 512. send() doesn't work on strings, it works on raw data. So, you'll be passing along lots of garbage here too. You need to pass the length based on what you received.
  4. printf(buf); is not safe. Any incoming string that happens to contain formatting specifiers will cause printf() to try and interpret them (this is known as a "format string vulnerability"). They should be replaced with printf("%s", buf); to achieve the same behavior in a safe way.
  5. In general your code assumes that the messages received from the IRC are nul-terminated. They are not. You should be using the return value of recv() to know how much data you received.
  6. You are using strlen() to determine the size of your buffer. This function calculates the length of a string, not the size of your buffer. You should be using the sizeof operator instead.
  7. I'm not sure what scanf(buf); is supposed to do, but it's almost certainly not what you wanted. Maybe you were looking for fgets()?
  8. Your recv() call for ping happens right after the one for buf. How do you know which will happen when and how long they will be? Seems like you should always be working with the same buffer.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!