SCTP server has an abnormal behaviour when connecting with a client - linux

I have this code on a small server that is getting requests from a client using SCTP connection, I keep getting this error every now and then.
BlockingIOError: [Errno 11] Resource temporarily unavailable
I know that I can avoid it by using Try-except but I wanna have a deep understanding of the issue. any help?
my code is here. this is the server
server = ('', 29168)
sk = sctpsocket_tcp(socket.AF_INET)
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sk.bindx([server])
sk.listen(5)
connection, addr = sk.accept()
while c:
a,b,c,d = connection.sctp_recv(1024)
print(c)

After going again through the SCTP library, I found a closed issue on Github with the solution

Related

Pyzmq swallows error when connecting to blocked port (firewall)

I'm trying to connect to a server using python's pyzmq package.
In my case I'm expecting an error, because the server the client connects to, blocks the designated port by a firewall.
However, my code runs through until I terminate the context and then blocks infinitely.
I tried several things to catch the error condition beforehand, but none of them succeeded.
My base example looks like this:
import zmq
endpoint = "tcp://{IP}:{PORT}"
zmq_ctx = zmq.Context()
sock = zmq_ctx.socket(zmq.PAIR)
sock.connect(endpoint) # <--- I would expect an exception thrown here, however this is not the case
sock.disconnect(endpoint)
sock.close()
zmq_ctx.term() # <--- This blocks infinetely
I extended the sample by sock.poll(1000, zmq.POLLOUT | zmq.POLLIN), hoping that the poll command would fail if the connection could not be established due to the firewall.
Then, I tried to solve the issue by setting some sock options, before the sock = zmq_ctx.socket(zmq.PAIR):
zmq_ctx.setsockopt(zmq.IMMEDIATE, 1) # Hoping, this would lead to an error on the first `send`
zmq_ctx.setsockopt(zmq.HEARTBEAT_IVL, 100) # Hoping, the heartbeat would not be successful if the connection could not be established
zmq_ctx.setsockopt(zmq.HEARTBEAT_TTL, 500)
zmq_ctx.setsockopt(zmq.LINGER, 500) # Hoping, the `zmq_ctx.term()` would throw an exception when the linger period is over
I also added temporarily a sock.send_string("bla"), but it just enqueues the msg without returning me some error and did not provide any new insights.
The only thing I can imagine to solve the problem would be using the telnet package and attempting a connection to the endpoint.
However, adding a dependency just for the purpose of testing a connection is not really satisfactory.
Do you have any idea, how to determine a blocked port from the client side still using pyzmq? I'm not very happy that the code always runs into the blocking zmq_ctx.term() in that case.

Pyads connection refused with Beckhoff running Twincat 3

I am trying to make a connection from a server running Ubuntu to a Beckhoff PLC with TwinCAT 3. With Windows everything works fine but with the same server on Linux I can't get a connection.
The Linux server has a static IP and in the route manager in the PLC I can find the route and see the server. I have tried adding the route by the route manager in the PLC and with "add_route_to_plc" but both ways my connection is refused. I have already turned off all firewalls. Any of you guys any idea what goes wrong here? In the attachment I have added some picture to see my settings and code that I try to run.
Python error: "connection closed by remote"
Python code:
import pyads
SENDER_AMS = '192.168.1.180.1.1'
PLC_IP = '192.168.1.100'
PLC_USERNAME = 'Administrator'
PLC_PASSWORD = '1'
ROUTE_NAME = 'GID_TEST_ROUTE'
HOSTNAME = 'Grid-stabilizer'
pyads.open_port()
pyads.set_local_address(SENDER_AMS)
pyads.add_route_to_plc(SENDER_AMS, HOSTNAME, PLC_IP, PLC_USERNAME, PLC_PASSWORD, route_name=ROUTE_NAME)
pyads.close_port()
plc=pyads.Connection('192.168.1.100.1.1', pyads.PORT_TC3PLC1)
plc.open()
plc.read_state()
If you are running python on linux and the plc on windows try
plc=pyads.Connection('192.168.1.100.1.1', pyads.PORT_TC3PLC1, PLC_IP)
This will create a route on the linux system. In your code the ip is missing to create a proper route.
Check the port of your plc. It should be 851.

Trouble retrieving information from a web page using HTTP protocol

This is my first question on this forum. So here we go.
In an assignment on coursera python for everybody, I modified the url in a .py file as told to retrieve the document from the link provided. But after doing all and running in cmd, I get ‘socket.gaierror: [Errno 11001] getaddrinfo failed’ error. I also can’t get to work this with the other alternative methods i.e., browser developer console and telnet. Telnet throws a ‘Could not open connection to the host, on port 80: Connect failed’ error.
I looked into google but not getting a clear answer. It would really help if someone solved this issue for me.
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('http://data.pr4e.org/intro-short.txt', 80))
cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)
while True:
data = mysock.recv(512)
if len(data) < 1:
break
print(data.decode(),end='')
mysock.close()
I expect to get the metadata and contents of the url.
Sockets don't understand urls, only hostnames. You need to change
mysock.connect(('http://data.pr4e.org/intro-short.txt', 80))
to
mysock.connect((data.pr4e.org', 80))

nodename nor servname provided, or not known on tcp connection in Python

I am tring to understand the socket connections in python and everytime i am tring to connect to a url it's giving me this error:
nodename nor servname provided, or not known
which i have no idea why? And sometimes it's only showing 301 and never a 200 status!
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "Whatever url i am giving"
server_address = (host, 80)
request_header = request_header = 'GET / HTTP/1.1\r\nHost: '+host+'\r\n\r\n'
try:
s.connect(server_address)
s.send(request_header.encode('utf-8'))
result = s.recv(4096).decode('utf-8')
while (len(result) > 0):
print(result)
result = s.recv(4096)
except Exception as ex:
print("Unexpected error:", ex)
s.close()
I know there are other questions but that doesn't satisfy my query. Can someone point me out what's happening here??
You don't connect to a URL. You connect to a host. When I assign host = stackoverflow.com, for example, your code works fine.
The socket layer itself knows nothing about URLs. A URL includes the path you supply to the host's HTTP server after you've connected. So, if you wish to retrieve, say, the URL "http://stackoverflow.com/questions", you connect to the host "stackoverflow.com", then provide this as the first line in the HTTP request:
GET /questions HTTP/1.1\r\n
This request (to stackoverflow.com) will in fact deliver a 301 response. 301 is a redirect response, telling you that the document you seek is available from a different host or service. This is an increasingly common response as most "http" sites now redirect the client to their corresponding "https" service.
If the host name you provide is not a valid hostname (for example, if you attempt to connect to "szackoverflow.com"), the hostname lookup that's being done automatically on your behalf will fail, resulting in a socket.gaierror exception ("gai" => getaddrinfo). On my linux system, that looks like this:
Unexpected error: [Errno -2] Name or service not known
On a different operating system, the text provided with that error might be worded differently.

'No Ident response" and "Notice -- You need to identify via SASL to use this server" when attempting to connect to IRC with Roll Your Own Haskell Bot

I am using Roll Your Own IRC Bot on the Haskell wiki and I receive those two errors when running the code provided by the guide.
What is wrong? Why can't I connect?
I am using Ubuntu and I've tried compiling it with ghc and running it with ghci.
import Network
import System.IO
import Text.Printf
server = "irc.freenode.org"
port = 6667
chan = "#tutbot-testing"
nick = "tutbot"
main = do
h <- connectTo server (PortNumber (fromIntegral port))
hSetBuffering h NoBuffering
write h "NICK" nick
write h "USER" (nick++" 0 * :tutorial bot")
write h "JOIN" chan
listen h
write :: Handle -> String -> String -> IO ()
write h s t = do
hPrintf h "%s %s\r\n" s t
printf "> %s %s\n" s t
listen :: Handle -> IO ()
listen h = forever $ do
s <- hGetLine h
putStrLn s
where
forever a = do a; forever a
The No Ident Response notice refers to an hilariously antiqued protocol from the depths of Internet history. In the olden days, when multi-user Unix machines were the rule rather than the exception, sometimes a server wanted to get some information on the specific user on the connecting client who had initiated the connection. The Ident protocol was created for this purpose. I guess IRC servers still contain code to try to query Ident servers, but they probably receive vanishingly few responses, so you can probably ignore this error.
The SASL error is more serious. It suggests that the specific IRC server you're connecting to has been configured to require user identification and authentication via the SASL protocol before allowing a connection. You probably don't want the added complexity of adding SASL support to your bot!
However, I'm not able to duplicate your problem. When I run the code you've posted above, I connect to host kornbluth.freenode.net [2001:1bc0:c1::6667/6667], see the notice about No Ident response, but my connection is accepted, as are the commands to set my nickname and join the #tutbot-testing channel, and I see the message of the day. I also see messages from some poor soul (maybe you?) asking for help with his or her bot, but I can't respond because the tutbot code above can't send any commands!
Is it possible you were testing it using a difference server or nick than the one in the code above, and that's caused the problem? If not, it's possible that there's a specific "irc.freenode.org" server that's configured to require SASL while the rest are working fine. Try switching the server name to:
server = "kornbluth.freenode.net"
That seemed to work for me.
Update: It looks like freenode requires SASL authentication from Amazon Web Services IP addresses (as noted in this bug report for an IRC client), so that would explain the problem you're having.

Resources